diff --git a/main.go b/main.go index dbed29b..fdc2200 100644 --- a/main.go +++ b/main.go @@ -113,6 +113,12 @@ func main() { return } + valid, reason := metricUpdate.IsValid() + if !valid { + http.Error(res, fmt.Sprintf("Invalid data: %s", reason), http.StatusInternalServerError) + return + } + updated := false for _, m := range dash.Metrics { if m.MetricID == params["metricid"] { @@ -123,7 +129,7 @@ func main() { } if !updated { - tmp := &DashboardMetric{MetricID: params["metricid"]} + tmp := NewDashboardMetric() tmp.Update(metricUpdate) dash.Metrics = append(dash.Metrics, tmp) } diff --git a/structs.go b/structs.go index bb9650e..9dd6602 100644 --- a/structs.go +++ b/structs.go @@ -30,11 +30,12 @@ func LoadDashboard(dashid string) (*Dashboard, error) { func (d *Dashboard) Save() { data, err := json.Marshal(d) if err != nil { - log.Println(err) + log.Printf("Error while marshalling dashboard: %s", err) + return } err = s3Storage.Put(d.DashboardID, data, "application/json", s3.Private) if err != nil { - log.Println(err) + log.Printf("Error while storing dashboard: %s", err) } } @@ -114,9 +115,11 @@ func (dm *DashboardMetric) Update(m *DashboardMetric) { dm.HistoricalData = tmp dm.Meta.LastUpdate = time.Now() - dm.Meta.PercCrit = countStatus["Critical"] / countStatus["Total"] * 100 - dm.Meta.PercWarn = countStatus["Warning"] / countStatus["Total"] * 100 - dm.Meta.PercOK = countStatus["OK"] / countStatus["Total"] * 100 + if countStatus["Total"] > 0 { + dm.Meta.PercCrit = countStatus["Critical"] / countStatus["Total"] * 100 + dm.Meta.PercWarn = countStatus["Warning"] / countStatus["Total"] * 100 + dm.Meta.PercOK = countStatus["OK"] / countStatus["Total"] * 100 + } } func (dm *DashboardMetric) IsValid() (bool, string) {