diff --git a/main.go b/main.go index e5a600f..8f04247 100644 --- a/main.go +++ b/main.go @@ -56,20 +56,20 @@ func main() { }) m.Get("/:dashid", func(params martini.Params, res http.ResponseWriter) { - dash, err := LoadDashboard(params["dashid"]) + dash, err := loadDashboard(params["dashid"]) if err != nil { - dash = &Dashboard{APIKey: generateAPIKey(), Metrics: DashboardMetrics{}} + dash = &dashboard{APIKey: generateAPIKey(), Metrics: dashboardMetrics{}} } // Filter out expired metrics - metrics := DashboardMetrics{} + metrics := dashboardMetrics{} for _, m := range dash.Metrics { if m.Meta.LastUpdate.After(time.Now().Add(time.Duration(m.Expires*-1) * time.Second)) { metrics = append(metrics, m) } } - sort.Sort(sort.Reverse(DashboardMetrics(metrics))) + sort.Sort(sort.Reverse(dashboardMetrics(metrics))) renderTemplate("dashboard.html", pongo2.Context{ "dashid": params["dashid"], "metrics": metrics, @@ -79,7 +79,7 @@ func main() { }) m.Delete("/:dashid", func(params martini.Params, req *http.Request, res http.ResponseWriter) { - dash, err := LoadDashboard(params["dashid"]) + dash, err := loadDashboard(params["dashid"]) if err != nil { http.Error(res, "This dashboard does not exist.", http.StatusInternalServerError) return @@ -101,20 +101,20 @@ func main() { return } - metricUpdate := NewDashboardMetric() + metricUpdate := newDashboardMetric() err = json.Unmarshal(body, metricUpdate) if err != nil { http.Error(res, "Unable to unmarshal json", http.StatusInternalServerError) return } - dash, err := LoadDashboard(params["dashid"]) + dash, err := loadDashboard(params["dashid"]) if err != nil { if len(req.Header.Get("Authorization")) < 10 { http.Error(res, "APIKey is too insecure", http.StatusUnauthorized) return } - dash = &Dashboard{APIKey: req.Header.Get("Authorization"), Metrics: DashboardMetrics{}, DashboardID: params["dashid"]} + dash = &dashboard{APIKey: req.Header.Get("Authorization"), Metrics: dashboardMetrics{}, DashboardID: params["dashid"]} } if dash.APIKey != req.Header.Get("Authorization") { @@ -138,7 +138,7 @@ func main() { } if !updated { - tmp := NewDashboardMetric() + tmp := newDashboardMetric() tmp.MetricID = params["metricid"] tmp.Update(metricUpdate) dash.Metrics = append(dash.Metrics, tmp) @@ -150,9 +150,9 @@ func main() { }) m.Delete("/:dashid/:metricid", func(params martini.Params, req *http.Request, res http.ResponseWriter) { - dash, err := LoadDashboard(params["dashid"]) + dash, err := loadDashboard(params["dashid"]) if err != nil { - dash = &Dashboard{APIKey: req.Header.Get("Authorization"), Metrics: DashboardMetrics{}, DashboardID: params["dashid"]} + dash = &dashboard{APIKey: req.Header.Get("Authorization"), Metrics: dashboardMetrics{}, DashboardID: params["dashid"]} } if dash.APIKey != req.Header.Get("Authorization") { @@ -160,7 +160,7 @@ func main() { return } - tmp := DashboardMetrics{} + tmp := dashboardMetrics{} for _, m := range dash.Metrics { if m.MetricID != params["metricid"] { tmp = append(tmp, m) @@ -172,7 +172,7 @@ func main() { http.Error(res, "OK", http.StatusOK) }) - go RunWelcomePage() + go runWelcomePage() // GO! m.Run() diff --git a/structs.go b/structs.go index 9dd6602..8847d5f 100644 --- a/structs.go +++ b/structs.go @@ -9,25 +9,25 @@ import ( "launchpad.net/goamz/s3" ) -type Dashboard struct { +type dashboard struct { DashboardID string `json:"-"` APIKey string `json:"api_key"` - Metrics DashboardMetrics `json:"metrics"` + Metrics dashboardMetrics `json:"metrics"` } -func LoadDashboard(dashid string) (*Dashboard, error) { +func loadDashboard(dashid string) (*dashboard, error) { data, err := s3Storage.Get(dashid) if err != nil { - return &Dashboard{}, errors.New("Dashboard not found") + return &dashboard{}, errors.New("Dashboard not found") } - tmp := &Dashboard{DashboardID: dashid} + tmp := &dashboard{DashboardID: dashid} json.Unmarshal(data, tmp) return tmp, nil } -func (d *Dashboard) Save() { +func (d *dashboard) Save() { data, err := json.Marshal(d) if err != nil { log.Printf("Error while marshalling dashboard: %s", err) @@ -39,31 +39,31 @@ func (d *Dashboard) Save() { } } -type DashboardMetrics []*DashboardMetric +type dashboardMetrics []*dashboardMetric -func (a DashboardMetrics) Len() int { return len(a) } -func (a DashboardMetrics) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a DashboardMetrics) Less(i, j int) bool { +func (a dashboardMetrics) Len() int { return len(a) } +func (a dashboardMetrics) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a dashboardMetrics) Less(i, j int) bool { return a[i].HistoricalData[0].Time.Before(a[j].HistoricalData[0].Time) } -type DashboardMetric struct { +type dashboardMetric struct { MetricID string `json:"id"` Title string `json:"title"` Description string `json:"description"` Status string `json:"status"` Expires int64 `json:"expires,omitifempty"` Freshness int64 `json:"freshness,omitifempty"` - HistoricalData DashboardMetricHistory `json:"history,omitifempty"` - Meta DashboardMetricMeta `json:"meta,omitifempty"` + HistoricalData dashboardMetricHistory `json:"history,omitifempty"` + Meta dashboardMetricMeta `json:"meta,omitifempty"` } -type DashboardMetricStatus struct { +type dashboardMetricStatus struct { Time time.Time `json:"time"` Status string `json:"status"` } -type DashboardMetricMeta struct { +type dashboardMetricMeta struct { LastUpdate time.Time LastOK time.Time PercOK float64 @@ -71,19 +71,19 @@ type DashboardMetricMeta struct { PercCrit float64 } -type DashboardMetricHistory []DashboardMetricStatus +type dashboardMetricHistory []dashboardMetricStatus -func NewDashboardMetric() *DashboardMetric { - return &DashboardMetric{ +func newDashboardMetric() *dashboardMetric { + return &dashboardMetric{ Status: "Unknown", Expires: 604800, Freshness: 3600, - HistoricalData: DashboardMetricHistory{}, - Meta: DashboardMetricMeta{}, + HistoricalData: dashboardMetricHistory{}, + Meta: dashboardMetricMeta{}, } } -func (dm *DashboardMetric) Update(m *DashboardMetric) { +func (dm *dashboardMetric) Update(m *dashboardMetric) { dm.Title = m.Title dm.Description = m.Description dm.Status = m.Status @@ -93,7 +93,7 @@ func (dm *DashboardMetric) Update(m *DashboardMetric) { if m.Freshness != 0 { dm.Freshness = m.Freshness } - dm.HistoricalData = append(DashboardMetricHistory{DashboardMetricStatus{ + dm.HistoricalData = append(dashboardMetricHistory{dashboardMetricStatus{ Time: time.Now(), Status: m.Status, }}, dm.HistoricalData...) @@ -101,7 +101,7 @@ func (dm *DashboardMetric) Update(m *DashboardMetric) { countStatus := make(map[string]float64) expired := time.Now().Add(time.Duration(dm.Expires*-1) * time.Second) - tmp := DashboardMetricHistory{} + tmp := dashboardMetricHistory{} for _, s := range dm.HistoricalData { if s.Time.After(expired) { tmp = append(tmp, s) @@ -122,7 +122,7 @@ func (dm *DashboardMetric) Update(m *DashboardMetric) { } } -func (dm *DashboardMetric) IsValid() (bool, string) { +func (dm *dashboardMetric) IsValid() (bool, string) { if dm.Expires > 604800 || dm.Expires < 0 { return false, "Expires not in range 0 < x < 640800" } diff --git a/welcome_runner.go b/welcome_runner.go index fe6dd09..5563e40 100644 --- a/welcome_runner.go +++ b/welcome_runner.go @@ -11,7 +11,7 @@ import ( "time" ) -func RunWelcomePage() { +func runWelcomePage() { baseURL := os.Getenv("BASE_URL") welcomeAPIToken := os.Getenv("API_TOKEN") generateTicker := time.NewTicker(time.Minute) @@ -30,7 +30,7 @@ func RunWelcomePage() { break } - beer := DashboardMetric{ + beer := dashboardMetric{ Title: "Amount of beer in the fridge", Description: fmt.Sprintf("Currently there are %d bottles of beer in the fridge", beers), Status: status,