1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-12-23 04:21:18 +00:00

Added JSON version of dashboard

This commit is contained in:
Knut Ahlers 2016-03-27 22:18:13 +02:00
parent e98e4d715f
commit 46df546146
3 changed files with 54 additions and 0 deletions

View file

@ -40,6 +40,8 @@ func main() {
Methods("GET") Methods("GET")
r.HandleFunc("/create", handleCreateRandomDashboard). r.HandleFunc("/create", handleCreateRandomDashboard).
Methods("GET") Methods("GET")
r.HandleFunc("/{dashid}.json", handleDisplayDashboardJSON).
Methods("GET")
r.HandleFunc("/{dashid}", handleDisplayDashboard). r.HandleFunc("/{dashid}", handleDisplayDashboard).
Methods("GET") Methods("GET")

View file

@ -169,6 +169,10 @@ func (dm *dashboardMetric) StatisticalStatus() string {
} }
func (dm *dashboardMetric) PreferredStatus() string { func (dm *dashboardMetric) PreferredStatus() string {
if dm.Meta.LastUpdate.Before(time.Now().Add(-1 * time.Duration(dm.Freshness) * time.Second)) {
return "Unknown"
}
if dm.IgnoreMAD { if dm.IgnoreMAD {
return dm.Status return dm.Status
} }

View file

@ -51,6 +51,54 @@ func handleDisplayDashboard(res http.ResponseWriter, req *http.Request) {
}, res) }, res)
} }
func handleDisplayDashboardJSON(res http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
dash, err := loadDashboard(params["dashid"], store)
if err != nil {
dash = &dashboard{APIKey: generateAPIKey(), Metrics: dashboardMetrics{}}
}
response := struct {
APIKey string `json:"api_key,omitempty"`
Metrics []struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Status string `json:"status"`
Value float64 `json:"value"`
LastUpdate time.Time `json:"last_update"`
} `json:"metrics"`
}{}
// Filter out expired metrics
for _, m := range dash.Metrics {
if m.Meta.LastUpdate.After(time.Now().Add(time.Duration(m.Expires*-1) * time.Second)) {
response.Metrics = append(response.Metrics, struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Status string `json:"status"`
Value float64 `json:"value"`
LastUpdate time.Time `json:"last_update"`
}{
ID: m.MetricID,
Title: m.Title,
Description: m.Description,
Status: m.PreferredStatus(),
Value: m.Value,
LastUpdate: m.Meta.LastUpdate,
})
}
}
if len(response.Metrics) == 0 {
response.APIKey = dash.APIKey
}
res.Header().Set("Content-Type", "application/json")
json.NewEncoder(res).Encode(response)
}
func handleDeleteDashboard(res http.ResponseWriter, req *http.Request) { func handleDeleteDashboard(res http.ResponseWriter, req *http.Request) {
params := mux.Vars(req) params := mux.Vars(req)
dash, err := loadDashboard(params["dashid"], store) dash, err := loadDashboard(params["dashid"], store)