Skip to content

Commit

Permalink
fix(api,ui): return empty app stats and timeline if no elastic service (
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Jun 12, 2020
1 parent 69fff58 commit 1b83551
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
10 changes: 10 additions & 0 deletions engine/api/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package api

import (
"context"
"encoding/json"
"net/http"

"github.com/ovh/cds/engine/api/event"
"github.com/ovh/cds/engine/api/project"
"github.com/ovh/cds/engine/api/services"
"github.com/ovh/cds/engine/api/user"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/engine/service"
Expand All @@ -14,6 +16,14 @@ import (

func (api *API) getTimelineHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
srvs, err := services.LoadAllByType(ctx, api.mustDB(), services.TypeElasticsearch)
if err != nil {
return err
}
if len(srvs) == 0 {
return service.WriteJSON(w, []json.RawMessage{}, http.StatusOK)
}

consumer := getAPIConsumer(ctx)

// Get index of the first element to return
Expand Down
65 changes: 34 additions & 31 deletions engine/api/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/ovh/cds/engine/api/application"
"github.com/ovh/cds/engine/api/metrics"
"github.com/ovh/cds/engine/api/navbar"
"github.com/ovh/cds/engine/api/project"
"github.com/ovh/cds/engine/api/repositoriesmanager"
"github.com/ovh/cds/engine/api/services"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/sdk"
Expand All @@ -29,24 +29,27 @@ func (api *API) getNavbarHandler() service.Handler {

func (api *API) getApplicationOverviewHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
srvs, err := services.LoadAllByType(ctx, api.mustDB(), services.TypeElasticsearch)
if err != nil {
return err
}
if len(srvs) == 0 {
return service.WriteJSON(w, sdk.ApplicationOverview{}, http.StatusOK)
}

vars := mux.Vars(r)
key := vars[permProjectKey]
projectKey := vars[permProjectKey]
appName := vars["applicationName"]
db := api.mustDB()

p, errP := project.Load(db, key)
if errP != nil {
return sdk.WrapError(errP, "getApplicationOverviewHandler> unable to load project")
}

app, errA := application.LoadByName(db, key, appName)
if errA != nil {
return sdk.WrapError(errA, "getApplicationOverviewHandler> unable to load application")
app, err := application.LoadByName(db, projectKey, appName)
if err != nil {
return sdk.WrapError(err, "unable to load application")
}

usage, errU := loadApplicationUsage(ctx, db, key, appName)
if errU != nil {
return sdk.WrapError(errU, "getApplicationOverviewHandler> Cannot load application usage")
usage, err := loadApplicationUsage(ctx, db, projectKey, appName)
if err != nil {
return sdk.WrapError(err, "cannot load application usage")
}
app.Usage = &usage

Expand All @@ -55,39 +58,39 @@ func (api *API) getApplicationOverviewHandler() service.Handler {
History: make(map[string][]sdk.WorkflowRun, len(app.Usage.Workflows)),
}

// GET METRICS
m1, errMV := metrics.GetMetrics(ctx, db, key, app.ID, sdk.MetricKeyVulnerability)
if errMV != nil {
return sdk.WrapError(errMV, "getApplicationOverviewHandler> Cannot list vulnerability metrics")
// Get metrics
mVulnerability, err := metrics.GetMetrics(ctx, db, projectKey, app.ID, sdk.MetricKeyVulnerability)
if err != nil {
return sdk.WrapError(err, "cannot list vulnerability metrics")
}
appOverview.Graphs = append(appOverview.Graphs, sdk.ApplicationOverviewGraph{
Type: sdk.MetricKeyVulnerability,
Datas: m1,
Datas: mVulnerability,
})

m2, errUT := metrics.GetMetrics(ctx, db, key, app.ID, sdk.MetricKeyUnitTest)
if errUT != nil {
return sdk.WrapError(errUT, "getApplicationOverviewHandler> Cannot list Unit test metrics")
mTest, err := metrics.GetMetrics(ctx, db, projectKey, app.ID, sdk.MetricKeyUnitTest)
if err != nil {
return sdk.WrapError(err, "cannot list Unit test metrics")
}
appOverview.Graphs = append(appOverview.Graphs, sdk.ApplicationOverviewGraph{
Type: sdk.MetricKeyUnitTest,
Datas: m2,
Datas: mTest,
})

mCov, errCov := metrics.GetMetrics(ctx, db, key, app.ID, sdk.MetricKeyCoverage)
if errCov != nil {
return sdk.WrapError(errCov, "cannot list coverage metrics")
mCoverage, err := metrics.GetMetrics(ctx, db, projectKey, app.ID, sdk.MetricKeyCoverage)
if err != nil {
return sdk.WrapError(err, "cannot list coverage metrics")
}
appOverview.Graphs = append(appOverview.Graphs, sdk.ApplicationOverviewGraph{
Type: sdk.MetricKeyCoverage,
Datas: mCov,
Datas: mCoverage,
})

// GET VCS URL
// Get vcs info to known if we are on the default branch or not
projectVCSServer, err := repositoriesmanager.LoadProjectVCSServerLinkByProjectKeyAndVCSServerName(ctx, api.mustDB(), p.Key, app.VCSServer)
projectVCSServer, err := repositoriesmanager.LoadProjectVCSServerLinkByProjectKeyAndVCSServerName(ctx, api.mustDB(), projectKey, app.VCSServer)
if err == nil {
client, err := repositoriesmanager.AuthorizedClient(ctx, db, api.Cache, p.Key, projectVCSServer)
client, err := repositoriesmanager.AuthorizedClient(ctx, db, api.Cache, projectKey, projectVCSServer)
if err != nil {
return sdk.NewErrorWithStack(err, sdk.NewErrorFrom(sdk.ErrNoReposManagerClientAuth,
"cannot get repo client %s", app.VCSServer))
Expand All @@ -106,9 +109,9 @@ func (api *API) getApplicationOverviewHandler() service.Handler {
tagFilter := make(map[string]string, 1)
tagFilter["git.branch"] = defaultBranch.DisplayID
for _, w := range app.Usage.Workflows {
runs, _, _, _, errR := workflow.LoadRuns(db, key, w.Name, 0, 5, tagFilter)
if errR != nil {
return sdk.WrapError(errR, "getApplicationOverviewHandler> Unable to load runs")
runs, _, _, _, err := workflow.LoadRuns(db, projectKey, w.Name, 0, 5, tagFilter)
if err != nil {
return sdk.WrapError(err, "unable to load runs")
}
appOverview.History[w.Name] = runs
}
Expand Down
6 changes: 3 additions & 3 deletions sdk/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import "encoding/json"

// ApplicationOverview represents the overview of an application
type ApplicationOverview struct {
Graphs []ApplicationOverviewGraph `json:"graphs"`
GitURL string `json:"git_url"`
History map[string][]WorkflowRun `json:"history"`
Graphs []ApplicationOverviewGraph `json:"graphs,omitempty"`
GitURL string `json:"git_url,omitempty"`
History map[string][]WorkflowRun `json:"history,omitempty"`
}

// ApplicationOverviewGraph represents data pushed by CDS for metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ApplicationHomeComponent implements OnInit {

renderGraph(): void {
this.dashboards = new Array<GraphConfiguration>();
if (this.overview && this.overview.graphs.length > 0) {
if (this.overview?.graphs?.length > 0) {
this.overview.graphs.forEach(g => {
if (g.datas && g.datas.length) {
switch (g.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ <h4>{{ 'common_usage' | translate}}</h4>
</div>

<!-- HISTORY -->
<div class="history" *ngIf="overview">
<div class="history" *ngIf="overview?.history">
<h4>{{ 'application_home_history' | translate }}</h4>
<div class="ui stackable three cards">
<ng-container *ngFor="let k of overview.history | keys">
Expand Down

0 comments on commit 1b83551

Please sign in to comment.