2015-02-20 18:58:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/flosch/pongo2"
|
2015-07-06 20:23:46 +00:00
|
|
|
"github.com/gorilla/mux"
|
2015-02-20 18:58:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func handleRedirectWelcome(res http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Redirect(res, req, "/welcome", 302)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleCreateRandomDashboard(res http.ResponseWriter, req *http.Request) {
|
2015-07-06 19:41:21 +00:00
|
|
|
var urlProposal string
|
|
|
|
for {
|
2015-02-20 18:58:16 +00:00
|
|
|
urlProposal = generateAPIKey()[0:20]
|
2015-07-06 19:41:21 +00:00
|
|
|
if exists, err := store.Exists(urlProposal); err == nil && !exists {
|
|
|
|
break
|
|
|
|
}
|
2015-02-20 18:58:16 +00:00
|
|
|
}
|
|
|
|
http.Redirect(res, req, fmt.Sprintf("/%s", urlProposal), http.StatusTemporaryRedirect)
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:23:46 +00:00
|
|
|
func handleDisplayDashboard(res http.ResponseWriter, req *http.Request) {
|
|
|
|
params := mux.Vars(req)
|
2015-07-06 19:41:21 +00:00
|
|
|
dash, err := loadDashboard(params["dashid"], store)
|
2015-02-20 18:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
dash = &dashboard{APIKey: generateAPIKey(), Metrics: dashboardMetrics{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out expired metrics
|
|
|
|
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)))
|
|
|
|
renderTemplate("dashboard.html", pongo2.Context{
|
|
|
|
"dashid": params["dashid"],
|
|
|
|
"metrics": metrics,
|
|
|
|
"apikey": dash.APIKey,
|
2015-07-06 20:24:56 +00:00
|
|
|
"baseurl": cfg.BaseURL,
|
2015-02-20 18:58:16 +00:00
|
|
|
}, res)
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:23:46 +00:00
|
|
|
func handleDeleteDashboard(res http.ResponseWriter, req *http.Request) {
|
|
|
|
params := mux.Vars(req)
|
2015-07-06 19:41:21 +00:00
|
|
|
dash, err := loadDashboard(params["dashid"], store)
|
2015-02-20 18:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(res, "This dashboard does not exist.", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if dash.APIKey != req.Header.Get("Authorization") {
|
|
|
|
http.Error(res, "APIKey did not match.", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-06 19:41:21 +00:00
|
|
|
store.Delete(params["dashid"])
|
2015-02-20 18:58:16 +00:00
|
|
|
http.Error(res, "OK", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:23:46 +00:00
|
|
|
func handlePutMetric(res http.ResponseWriter, req *http.Request) {
|
|
|
|
params := mux.Vars(req)
|
2015-02-20 18:58:16 +00:00
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(res, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
metricUpdate := newDashboardMetric()
|
|
|
|
err = json.Unmarshal(body, metricUpdate)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(res, "Unable to unmarshal json", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-06 19:41:21 +00:00
|
|
|
dash, err := loadDashboard(params["dashid"], store)
|
2015-02-20 18:58:16 +00:00
|
|
|
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"]}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dash.APIKey != req.Header.Get("Authorization") {
|
|
|
|
http.Error(res, "APIKey did not match.", http.StatusUnauthorized)
|
|
|
|
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"] {
|
|
|
|
m.Update(metricUpdate)
|
|
|
|
updated = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !updated {
|
|
|
|
tmp := newDashboardMetric()
|
|
|
|
tmp.MetricID = params["metricid"]
|
|
|
|
tmp.Update(metricUpdate)
|
|
|
|
dash.Metrics = append(dash.Metrics, tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
dash.Save()
|
|
|
|
|
|
|
|
http.Error(res, "OK", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:23:46 +00:00
|
|
|
func handleDeleteMetric(res http.ResponseWriter, req *http.Request) {
|
|
|
|
params := mux.Vars(req)
|
2015-07-06 19:41:21 +00:00
|
|
|
dash, err := loadDashboard(params["dashid"], store)
|
2015-02-20 18:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
dash = &dashboard{APIKey: req.Header.Get("Authorization"), Metrics: dashboardMetrics{}, DashboardID: params["dashid"]}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dash.APIKey != req.Header.Get("Authorization") {
|
|
|
|
http.Error(res, "APIKey did not match.", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp := dashboardMetrics{}
|
|
|
|
for _, m := range dash.Metrics {
|
|
|
|
if m.MetricID != params["metricid"] {
|
|
|
|
tmp = append(tmp, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dash.Metrics = tmp
|
|
|
|
dash.Save()
|
|
|
|
|
|
|
|
http.Error(res, "OK", http.StatusOK)
|
|
|
|
}
|