-
Notifications
You must be signed in to change notification settings - Fork 88
/
dashboard.go
107 lines (92 loc) · 2.74 KB
/
dashboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package handlers
import (
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/pkg/errors"
appstatetypes "github.com/replicatedhq/kots/pkg/appstate/types"
"github.com/replicatedhq/kots/pkg/helm"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
"github.com/replicatedhq/kots/pkg/version"
)
type GetAppDashboardResponse struct {
AppStatus *appstatetypes.AppStatus `json:"appStatus"`
Metrics []version.MetricChart `json:"metrics"`
PrometheusAddress string `json:"prometheusAddress"`
EmbeddedClusterState string `json:"embeddedClusterState"`
}
func (h *Handler) GetAppDashboard(w http.ResponseWriter, r *http.Request) {
appSlug := mux.Vars(r)["appSlug"]
clusterID := mux.Vars(r)["clusterId"]
appStatus := new(appstatetypes.AppStatus)
if util.IsHelmManaged() {
release := helm.GetHelmApp(appSlug)
if release == nil {
logger.Errorf("release for app: %s does not exist\n", appSlug)
w.WriteHeader(http.StatusNotFound)
return
}
// we have received informer data
if len(release.Status.ResourceStates) > 0 {
getAppDashboardResponse := GetAppDashboardResponse{
AppStatus: &release.Status,
Metrics: nil,
PrometheusAddress: "",
}
JSON(w, 200, getAppDashboardResponse)
return
}
w.WriteHeader(http.StatusNotFound)
return
}
a, err := store.GetStore().GetAppFromSlug(appSlug)
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
appStatus, err = store.GetStore().GetAppStatus(a.ID)
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
ecState, err := store.GetStore().GetEmbeddedClusterState()
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
parentSequence, err := store.GetStore().GetCurrentParentSequence(a.ID, clusterID)
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
prometheusAddress, err := store.GetStore().GetPrometheusAddress()
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}
if prometheusAddress == "" {
prometheusAddress = os.Getenv("PROMETHEUS_ADDRESS")
}
metrics := []version.MetricChart{}
if prometheusAddress != "" {
graphs, err := version.GetGraphs(a, parentSequence, store.GetStore())
if err != nil {
logger.Error(errors.Wrapf(err, "failed to get graphs for app %s sequence %d. falling back to default graphs", a.Slug, parentSequence))
}
metrics = version.GetMetricCharts(graphs, prometheusAddress)
}
getAppDashboardResponse := GetAppDashboardResponse{
AppStatus: appStatus,
Metrics: metrics,
PrometheusAddress: prometheusAddress,
EmbeddedClusterState: ecState,
}
JSON(w, 200, getAppDashboardResponse)
}