mirror of
https://github.com/Luzifer/mondash.git
synced 2024-12-23 04:21:18 +00:00
GOLINT: Unexported data types
This commit is contained in:
parent
aa206743e2
commit
2cfd00b245
3 changed files with 39 additions and 39 deletions
26
main.go
26
main.go
|
@ -56,20 +56,20 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
m.Get("/:dashid", func(params martini.Params, res http.ResponseWriter) {
|
m.Get("/:dashid", func(params martini.Params, res http.ResponseWriter) {
|
||||||
dash, err := LoadDashboard(params["dashid"])
|
dash, err := loadDashboard(params["dashid"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dash = &Dashboard{APIKey: generateAPIKey(), Metrics: DashboardMetrics{}}
|
dash = &dashboard{APIKey: generateAPIKey(), Metrics: dashboardMetrics{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter out expired metrics
|
// Filter out expired metrics
|
||||||
metrics := DashboardMetrics{}
|
metrics := dashboardMetrics{}
|
||||||
for _, m := range dash.Metrics {
|
for _, m := range dash.Metrics {
|
||||||
if m.Meta.LastUpdate.After(time.Now().Add(time.Duration(m.Expires*-1) * time.Second)) {
|
if m.Meta.LastUpdate.After(time.Now().Add(time.Duration(m.Expires*-1) * time.Second)) {
|
||||||
metrics = append(metrics, m)
|
metrics = append(metrics, m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(sort.Reverse(DashboardMetrics(metrics)))
|
sort.Sort(sort.Reverse(dashboardMetrics(metrics)))
|
||||||
renderTemplate("dashboard.html", pongo2.Context{
|
renderTemplate("dashboard.html", pongo2.Context{
|
||||||
"dashid": params["dashid"],
|
"dashid": params["dashid"],
|
||||||
"metrics": metrics,
|
"metrics": metrics,
|
||||||
|
@ -79,7 +79,7 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
m.Delete("/:dashid", func(params martini.Params, req *http.Request, res http.ResponseWriter) {
|
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 {
|
if err != nil {
|
||||||
http.Error(res, "This dashboard does not exist.", http.StatusInternalServerError)
|
http.Error(res, "This dashboard does not exist.", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -101,20 +101,20 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
metricUpdate := NewDashboardMetric()
|
metricUpdate := newDashboardMetric()
|
||||||
err = json.Unmarshal(body, metricUpdate)
|
err = json.Unmarshal(body, metricUpdate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(res, "Unable to unmarshal json", http.StatusInternalServerError)
|
http.Error(res, "Unable to unmarshal json", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dash, err := LoadDashboard(params["dashid"])
|
dash, err := loadDashboard(params["dashid"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(req.Header.Get("Authorization")) < 10 {
|
if len(req.Header.Get("Authorization")) < 10 {
|
||||||
http.Error(res, "APIKey is too insecure", http.StatusUnauthorized)
|
http.Error(res, "APIKey is too insecure", http.StatusUnauthorized)
|
||||||
return
|
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") {
|
if dash.APIKey != req.Header.Get("Authorization") {
|
||||||
|
@ -138,7 +138,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !updated {
|
if !updated {
|
||||||
tmp := NewDashboardMetric()
|
tmp := newDashboardMetric()
|
||||||
tmp.MetricID = params["metricid"]
|
tmp.MetricID = params["metricid"]
|
||||||
tmp.Update(metricUpdate)
|
tmp.Update(metricUpdate)
|
||||||
dash.Metrics = append(dash.Metrics, tmp)
|
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) {
|
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 {
|
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") {
|
if dash.APIKey != req.Header.Get("Authorization") {
|
||||||
|
@ -160,7 +160,7 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp := DashboardMetrics{}
|
tmp := dashboardMetrics{}
|
||||||
for _, m := range dash.Metrics {
|
for _, m := range dash.Metrics {
|
||||||
if m.MetricID != params["metricid"] {
|
if m.MetricID != params["metricid"] {
|
||||||
tmp = append(tmp, m)
|
tmp = append(tmp, m)
|
||||||
|
@ -172,7 +172,7 @@ func main() {
|
||||||
http.Error(res, "OK", http.StatusOK)
|
http.Error(res, "OK", http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
go RunWelcomePage()
|
go runWelcomePage()
|
||||||
|
|
||||||
// GO!
|
// GO!
|
||||||
m.Run()
|
m.Run()
|
||||||
|
|
48
structs.go
48
structs.go
|
@ -9,25 +9,25 @@ import (
|
||||||
"launchpad.net/goamz/s3"
|
"launchpad.net/goamz/s3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Dashboard struct {
|
type dashboard struct {
|
||||||
DashboardID string `json:"-"`
|
DashboardID string `json:"-"`
|
||||||
APIKey string `json:"api_key"`
|
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)
|
data, err := s3Storage.Get(dashid)
|
||||||
if err != nil {
|
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)
|
json.Unmarshal(data, tmp)
|
||||||
|
|
||||||
return tmp, nil
|
return tmp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Dashboard) Save() {
|
func (d *dashboard) Save() {
|
||||||
data, err := json.Marshal(d)
|
data, err := json.Marshal(d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error while marshalling dashboard: %s", err)
|
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) Len() int { return len(a) }
|
||||||
func (a DashboardMetrics) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
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) Less(i, j int) bool {
|
||||||
return a[i].HistoricalData[0].Time.Before(a[j].HistoricalData[0].Time)
|
return a[i].HistoricalData[0].Time.Before(a[j].HistoricalData[0].Time)
|
||||||
}
|
}
|
||||||
|
|
||||||
type DashboardMetric struct {
|
type dashboardMetric struct {
|
||||||
MetricID string `json:"id"`
|
MetricID string `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Expires int64 `json:"expires,omitifempty"`
|
Expires int64 `json:"expires,omitifempty"`
|
||||||
Freshness int64 `json:"freshness,omitifempty"`
|
Freshness int64 `json:"freshness,omitifempty"`
|
||||||
HistoricalData DashboardMetricHistory `json:"history,omitifempty"`
|
HistoricalData dashboardMetricHistory `json:"history,omitifempty"`
|
||||||
Meta DashboardMetricMeta `json:"meta,omitifempty"`
|
Meta dashboardMetricMeta `json:"meta,omitifempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DashboardMetricStatus struct {
|
type dashboardMetricStatus struct {
|
||||||
Time time.Time `json:"time"`
|
Time time.Time `json:"time"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DashboardMetricMeta struct {
|
type dashboardMetricMeta struct {
|
||||||
LastUpdate time.Time
|
LastUpdate time.Time
|
||||||
LastOK time.Time
|
LastOK time.Time
|
||||||
PercOK float64
|
PercOK float64
|
||||||
|
@ -71,19 +71,19 @@ type DashboardMetricMeta struct {
|
||||||
PercCrit float64
|
PercCrit float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type DashboardMetricHistory []DashboardMetricStatus
|
type dashboardMetricHistory []dashboardMetricStatus
|
||||||
|
|
||||||
func NewDashboardMetric() *DashboardMetric {
|
func newDashboardMetric() *dashboardMetric {
|
||||||
return &DashboardMetric{
|
return &dashboardMetric{
|
||||||
Status: "Unknown",
|
Status: "Unknown",
|
||||||
Expires: 604800,
|
Expires: 604800,
|
||||||
Freshness: 3600,
|
Freshness: 3600,
|
||||||
HistoricalData: DashboardMetricHistory{},
|
HistoricalData: dashboardMetricHistory{},
|
||||||
Meta: DashboardMetricMeta{},
|
Meta: dashboardMetricMeta{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dm *DashboardMetric) Update(m *DashboardMetric) {
|
func (dm *dashboardMetric) Update(m *dashboardMetric) {
|
||||||
dm.Title = m.Title
|
dm.Title = m.Title
|
||||||
dm.Description = m.Description
|
dm.Description = m.Description
|
||||||
dm.Status = m.Status
|
dm.Status = m.Status
|
||||||
|
@ -93,7 +93,7 @@ func (dm *DashboardMetric) Update(m *DashboardMetric) {
|
||||||
if m.Freshness != 0 {
|
if m.Freshness != 0 {
|
||||||
dm.Freshness = m.Freshness
|
dm.Freshness = m.Freshness
|
||||||
}
|
}
|
||||||
dm.HistoricalData = append(DashboardMetricHistory{DashboardMetricStatus{
|
dm.HistoricalData = append(dashboardMetricHistory{dashboardMetricStatus{
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
Status: m.Status,
|
Status: m.Status,
|
||||||
}}, dm.HistoricalData...)
|
}}, dm.HistoricalData...)
|
||||||
|
@ -101,7 +101,7 @@ func (dm *DashboardMetric) Update(m *DashboardMetric) {
|
||||||
countStatus := make(map[string]float64)
|
countStatus := make(map[string]float64)
|
||||||
|
|
||||||
expired := time.Now().Add(time.Duration(dm.Expires*-1) * time.Second)
|
expired := time.Now().Add(time.Duration(dm.Expires*-1) * time.Second)
|
||||||
tmp := DashboardMetricHistory{}
|
tmp := dashboardMetricHistory{}
|
||||||
for _, s := range dm.HistoricalData {
|
for _, s := range dm.HistoricalData {
|
||||||
if s.Time.After(expired) {
|
if s.Time.After(expired) {
|
||||||
tmp = append(tmp, s)
|
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 {
|
if dm.Expires > 604800 || dm.Expires < 0 {
|
||||||
return false, "Expires not in range 0 < x < 640800"
|
return false, "Expires not in range 0 < x < 640800"
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunWelcomePage() {
|
func runWelcomePage() {
|
||||||
baseURL := os.Getenv("BASE_URL")
|
baseURL := os.Getenv("BASE_URL")
|
||||||
welcomeAPIToken := os.Getenv("API_TOKEN")
|
welcomeAPIToken := os.Getenv("API_TOKEN")
|
||||||
generateTicker := time.NewTicker(time.Minute)
|
generateTicker := time.NewTicker(time.Minute)
|
||||||
|
@ -30,7 +30,7 @@ func RunWelcomePage() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
beer := DashboardMetric{
|
beer := dashboardMetric{
|
||||||
Title: "Amount of beer in the fridge",
|
Title: "Amount of beer in the fridge",
|
||||||
Description: fmt.Sprintf("Currently there are %d bottles of beer in the fridge", beers),
|
Description: fmt.Sprintf("Currently there are %d bottles of beer in the fridge", beers),
|
||||||
Status: status,
|
Status: status,
|
||||||
|
|
Loading…
Reference in a new issue