From 91b4e1a21cbd25bcc3c72ddc278ccfeb2943a0e1 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 10 Jul 2015 20:28:55 +0200 Subject: [PATCH] Display only 60 items in graph The decision is left to the template so template designers can easily change this behavior by removing the lastNItems filter in the template itself. Also the value can be changed easily. fixes #4 --- filters/filter.go | 16 ++++++++++++++++ main.go | 1 + structs.go | 26 ++++++++++---------------- templates/dashboard.html | 10 ++++------ 4 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 filters/filter.go diff --git a/filters/filter.go b/filters/filter.go new file mode 100644 index 0000000..5bd67bb --- /dev/null +++ b/filters/filter.go @@ -0,0 +1,16 @@ +package filters + +import "github.com/flosch/pongo2" + +func init() { + pongo2.RegisterFilter("lastNItems", filterLastNItems) +} + +func filterLastNItems(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { + if !in.CanSlice() { + return in, nil + } + + from := in.Len() - param.Integer() + return in.Slice(from, in.Len()), nil +} diff --git a/main.go b/main.go index f0da838..c2a301a 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/flosch/pongo2" "github.com/gorilla/mux" + _ "github.com/Luzifer/mondash/filters" _ "github.com/flosch/pongo2-addons" ) diff --git a/structs.go b/structs.go index 353873b..d9aa5a1 100644 --- a/structs.go +++ b/structs.go @@ -166,27 +166,21 @@ func (dm *dashboardMetric) StatisticalStatus() string { return "OK" } -func (dm *dashboardMetric) LabelHistory() string { - s := "[" - for i, v := range dm.HistoricalData { - if i != 0 { - s = s + ", " - } - s = s + "" + strconv.Itoa(int(v.Time.Unix())) + "" +func (dm *dashboardMetric) LabelHistory() []string { + s := []string{} + for _, v := range dm.HistoricalData[len(dm.HistoricalData)-60:] { + s = append(s, strconv.Itoa(int(v.Time.Unix()))) } - s = s + "]" + return s } -func (dm *dashboardMetric) DataHistory() string { - s := "[" - for i, v := range dm.HistoricalData { - if i != 0 { - s = s + ", " - } - s = s + strconv.FormatFloat(v.Value, 'g', 4, 64) +func (dm *dashboardMetric) DataHistory() []string { + s := []string{} + for _, v := range dm.HistoricalData[len(dm.HistoricalData)-60:] { + s = append(s, strconv.FormatFloat(v.Value, 'g', 4, 64)) } - s = s + "]" + return s } diff --git a/templates/dashboard.html b/templates/dashboard.html index 675e8c1..127a2eb 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -11,8 +11,8 @@ - - + +