1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-11-10 00:20:02 +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 + ", "
}
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
}

View file

@ -11,8 +11,8 @@
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<!-- Chartist Library -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css">
<script src="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css">
<script src="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
<script>
function timeConverter(UNIX_timestamp){
var a = new Date(UNIX_timestamp*1000);
@ -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++) {