mirror of
https://github.com/Luzifer/mondash.git
synced 2024-12-22 20:11:18 +00:00
Added JSON version of dashboard
This commit is contained in:
parent
e98e4d715f
commit
46df546146
3 changed files with 54 additions and 0 deletions
2
main.go
2
main.go
|
@ -40,6 +40,8 @@ func main() {
|
|||
Methods("GET")
|
||||
r.HandleFunc("/create", handleCreateRandomDashboard).
|
||||
Methods("GET")
|
||||
r.HandleFunc("/{dashid}.json", handleDisplayDashboardJSON).
|
||||
Methods("GET")
|
||||
r.HandleFunc("/{dashid}", handleDisplayDashboard).
|
||||
Methods("GET")
|
||||
|
||||
|
|
|
@ -169,6 +169,10 @@ func (dm *dashboardMetric) StatisticalStatus() 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 {
|
||||
return dm.Status
|
||||
}
|
||||
|
|
|
@ -51,6 +51,54 @@ func handleDisplayDashboard(res http.ResponseWriter, req *http.Request) {
|
|||
}, 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) {
|
||||
params := mux.Vars(req)
|
||||
dash, err := loadDashboard(params["dashid"], store)
|
||||
|
|
Loading…
Reference in a new issue