1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-12-23 04:21:18 +00:00

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
This commit is contained in:
Knut Ahlers 2015-07-10 20:28:55 +02:00
parent ee8770b9d8
commit 91b4e1a21c
4 changed files with 31 additions and 22 deletions

16
filters/filter.go Normal file
View file

@ -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
}

View file

@ -13,6 +13,7 @@ import (
"github.com/flosch/pongo2"
"github.com/gorilla/mux"
_ "github.com/Luzifer/mondash/filters"
_ "github.com/flosch/pongo2-addons"
)

View file

@ -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 + ", "
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 + "" + 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 + ", "
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 + strconv.FormatFloat(v.Value, 'g', 4, 64)
}
s = s + "]"
return s
}

View file

@ -110,11 +110,9 @@
<script>
var data = {
// A labels array that can contain any sort of values
labels: {{ metric.LabelHistory }},
labels: [{{ metric.LabelHistory|lastNItems:"60"|join:"," }}],
// Our series array that contains series objects or in this case series data arrays
series: [
{{ metric.DataHistory }}
]
series: [[{{ metric.DataHistory|lastNItems:"60"|join:"," }}]]
};
for (var i = 0; i < data.labels.length; i++) {