cleanup and aesthetic changes
Costa Tsaousis (ktsaou) committed
May 2, 2014 at 02:02 UTC
cac3f14981e535336003bec6abe8943b7b71a49e
3 files changed
+82
-63
netdata.c
+72
-55
@@ -1730,10 +1730,13 @@ void rrd_stats_all_json(struct web_buffer *wb)
1730
1731
unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, size_t entries_to_show, size_t group, int group_method, time_t after, time_t before)
1732
{
1733
+ int c;
1734
pthread_mutex_lock(&st->mutex);
1735
1735
- unsigned long last_timestamp = 0;
1736
1737
+ // -------------------------------------------------------------------------
1738
+ // switch from JSON to google JSON
1739
+
1740
char kq[2] = "\"";
1741
char sq[2] = "\"";
1742
switch(type) {
@@ -1748,41 +1751,42 @@ unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, siz
1751
break;
1752
}
1753
1751
- // check the options
1754
+
1755
+ // -------------------------------------------------------------------------
1756
+ // validate the parameters
1757
+
1758
if(entries_to_show < 1) entries_to_show = 1;
1759
if(group < 1) group = 1;
1754
- // if(group > st->entries / 20) group = st->entries / 20;
1755
-
1756
- long printed = 0; // the lines of JSON data we have generated so far
1757
-
1758
- long stop_entry, current_entry = st->current_entry;
1760
+
1761
+ // make sure current_entry is within limits
1762
+ long current_entry = st->current_entry;
1763
+ if(current_entry < 0) current_entry = 0;
1764
+ else if(current_entry >= st->entries) current_entry = st->entries;
1765
+
1766
+ // find the oldest entry of the round-robin
1767
+ long stop_entry = current_entry + 1;
1768
+ if(stop_entry >= st->entries) stop_entry = 0;
1769
+
1770
+ if(before == 0) before = st->last_updated.tv_sec;
1771
+ if(after == 0) after = rrd_stats_first_entry_t(st);
1772
1760
- int c = 0; // counter for dimension loops
1761
- int dimensions = 0; // the total number of dimensions present
1773
1774
+ // -------------------------------------------------------------------------
1775
// find how many dimensions we have
1776
+
1777
+ int dimensions = 0;
1778
RRD_DIMENSION *rd;
1779
for( rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1766
-
1780
if(!dimensions) {
1781
pthread_mutex_unlock(&st->mutex);
1782
web_buffer_printf(wb, "No dimensions yet.");
1783
return 0;
1784
}
1785
1773
- int annotation_count = 0;
1774
-
1775
- // temp for the printable values
1776
- long long print_values[dimensions];
1777
- int print_hidden[dimensions];
1778
-
1779
- // temporary storage to keep track of group values and counts
1780
- long long group_values[dimensions];
1781
- int group_count = 0;
1782
- for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++)
1783
- group_values[c] = 0;
1784
-
1785
- // print the labels
1786
+
1787
+ // -------------------------------------------------------------------------
1788
+ // print the JSON header
1789
+
1790
web_buffer_printf(wb, "{\n %scols%s:\n [\n", kq, kq);
1791
web_buffer_printf(wb, " {%sid%s:%s%s,%slabel%s:%stime%s,%spattern%s:%s%s,%stype%s:%sdatetime%s},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq);
1792
web_buffer_printf(wb, " {%sid%s:%s%s,%slabel%s:%s%s,%spattern%s:%s%s,%stype%s:%sstring%s,%sp%s:{%srole%s:%sannotation%s}},\n", kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, sq, sq, kq, kq, kq, kq, sq, sq);
@@ -1791,44 +1795,29 @@ unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, siz
1795
// print the header for each dimension
1796
// and update the print_hidden array for the dimensions that should be hidden
1797
for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1794
- if(rd->hidden)
1795
- print_hidden[c] = 1;
1796
- else {
1797
- print_hidden[c] = 0;
1798
- web_buffer_printf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, rd->name, sq, kq, kq, sq, sq, kq, kq, sq, sq);
1799
- }
1798
+ if(!rd->hidden) web_buffer_printf(wb, ",\n {%sid%s:%s%s,%slabel%s:%s%s%s,%spattern%s:%s%s,%stype%s:%snumber%s}", kq, kq, sq, sq, kq, kq, sq, rd->name, sq, kq, kq, sq, sq, kq, kq, sq, sq);
1799
}
1800
1801
// print the begin of row data
1802
web_buffer_printf(wb, "\n ],\n %srows%s:\n [\n", kq, kq);
1803
1805
- // make sure current_entry is within limits
1806
- if(current_entry < 0 || current_entry >= st->entries) current_entry = 0;
1807
- if(before == 0) before = st->last_updated.tv_sec;
1808
-
1809
- // find the oldest entry of the round-robin
1810
- stop_entry = st->current_entry + 1;
1811
- if(stop_entry >= st->entries) stop_entry = st->entries - 1;
1812
-
1813
- // skip the oldest, to have incremental data
1814
- if(after == 0) after = rrd_stats_first_entry_t(st);
1815
-
1816
- // the minimum line length we expect
1817
- int line_size = 4096 + (dimensions * 200);
1804
1805
+ // -------------------------------------------------------------------------
1806
+ // prepare various strings, to speed up the loop
1807
+
1808
char overflow_annotation[201]; snprintf(overflow_annotation, 200, ",{%sv%s:%sRESET OR OVERFLOW%s},{%sv%s:%sThe counters have been wrapped.%s}", kq, kq, sq, sq, kq, kq, sq, sq);
1820
- char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1821
- char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1822
- char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1823
- char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1824
- char post_value[21]; snprintf(post_value, 20, "}");
1809
+ char normal_annotation[201]; snprintf(normal_annotation, 200, ",{%sv%s:null},{%sv%s:null}", kq, kq, kq, kq);
1810
+ char pre_date[51]; snprintf(pre_date, 50, " {%sc%s:[{%sv%s:%s", kq, kq, kq, kq, sq);
1811
+ char post_date[21]; snprintf(post_date, 20, "%s}", sq);
1812
+ char pre_value[21]; snprintf(pre_value, 20, ",{%sv%s:", kq, kq);
1813
+ char post_value[21]; snprintf(post_value, 20, "}");
1814
1826
- // to allow grouping on the same values, we need a pad
1827
- long pad = before % group;
1815
1816
+ // -------------------------------------------------------------------------
1817
// checks for debuging
1818
+
1819
if(st->debug) {
1831
- debug(D_RRD_STATS, "%s first_entry_t = %lu, last_entry_t = %lu, duration = %lu, after = %lu, before = %lu, duration = %lu, entries_to_show = %lu, group = %lu"
1820
+ debug(D_RRD_STATS, "%s first_entry_t = %lu, last_entry_t = %lu, duration = %lu, after = %lu, before = %lu, duration = %lu, entries_to_show = %lu, group = %lu, stop_entry = %ld"
1821
, st->id
1822
, rrd_stats_first_entry_t(st)
1823
, st->last_updated.tv_sec
@@ -1838,6 +1827,7 @@ unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, siz
1827
, before - after
1828
, entries_to_show
1829
, group
1830
+ , stop_entry
1831
);
1832
1833
if(before < after)
@@ -1847,11 +1837,40 @@ unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, siz
1837
debug(D_RRD_STATS, "WARNING: %s The time difference between the oldest and the newest entries (%lu) is higher than the capacity of the database (%lu)", st->name, before - after, st->entries * st->update_every);
1838
}
1839
1850
- // loop in dimension data
1840
+
1841
+ // -------------------------------------------------------------------------
1842
+ // temp arrays for keeping values per dimension
1843
+
1844
+ calculated_number group_values[dimensions]; // keep sums when grouping
1845
+ storage_number print_values[dimensions]; // keep the final value to be printed
1846
+ int print_hidden[dimensions]; // keep hidden flags
1847
+
1848
+ // initialize them
1849
+ for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1850
+ group_values[c] = print_values[c] = 0;
1851
+ print_hidden[c] = rd->hidden;
1852
+ }
1853
+
1854
+ // -------------------------------------------------------------------------
1855
+ // the main loop
1856
+
1857
+ // our return value (the last timestamp printed)
1858
+ // this is required to detect re-transmit in google JSONP
1859
+ time_t last_timestamp = 0;
1860
+
1861
int annotate_reset = 0;
1862
+ int annotation_count = 0;
1863
+
1864
+ // to allow grouping on the same values, we need a pad
1865
+ long pad = before % group;
1866
+
1867
+ // the minimum line length we expect
1868
+ int line_size = 4096 + (dimensions * 200);
1869
+
1870
unsigned long long time_usec = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
1853
- long t, count;
1854
- for (count = printed = 0, t = current_entry; t != stop_entry && st->timediff[t] ; time_usec -= st->timediff[t], t--) {
1871
+ long t, count, printed, group_count;
1872
+
1873
+ for (count = printed = group_count = 0, t = current_entry; t != stop_entry && st->timediff[t] ; time_usec -= st->timediff[t], t--) {
1874
if(t < 0) t = st->entries - 1;
1875
1876
int print_this = 0;
@@ -1943,8 +1962,6 @@ unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, siz
1962
if(!print_hidden[c]) {
1963
web_buffer_strcpy(wb, pre_value);
1964
web_buffer_rrd_value(wb, print_values[c]);
1946
- // long double x = (long double)print_values[c] / 10.0;
1947
- // web_buffer_printf(wb, "%0.1Lf", x);
1965
web_buffer_strcpy(wb, post_value);
1966
}
1967
}
web/index.js
+1
-1
@@ -154,7 +154,7 @@ function refreshHiddenChart(doNext) {
154
lineWidth: 1,
155
height: mainchart.hiddenchart.chartOptions.height,
156
chartArea: {'width': '98%'},
157
- hAxis: {'baselineColor': 'none'},
157
+ hAxis: {'baselineColor': 'none', viewWindowMode: 'maximized'},
158
vAxis: {'title': null},
159
});
160
web/netdata.js
+9
-7
@@ -240,8 +240,8 @@ function loadCharts(base_url, doNext) {
240
lineWidth: 1,
241
title: json.charts[i].title,
242
// hAxis: {title: "Time of Day", viewWindowMode: 'maximized', format:'HH:mm:ss'},
243
- hAxis: {viewWindowMode: 'maximized', format:'HH:mm:ss'},
244
- vAxis: {title: json.charts[i].units, viewWindowMode: 'pretty', minValue: 0, maxValue: 10},
243
+ hAxis: {viewWindowMode: 'maximized', slantedText: false, format:'HH:mm:ss'},
244
+ vAxis: {title: json.charts[i].units, viewWindowMode: 'pretty', minValue: -0.1, maxValue: 0.1},
245
chartArea : {width: '65%', height: '75%'},
246
focusTarget: 'category',
247
annotation: {'1': {style: 'line'}},
@@ -255,6 +255,10 @@ function loadCharts(base_url, doNext) {
255
json.charts[i].chartType = "AreaChart";
256
json.charts[i].chartOptions.isStacked = false;
257
json.charts[i].chartOptions.areaOpacity = 0.3;
258
+
259
+ json.charts[i].chartOptions.vAxis.viewWindowMode = 'maximized';
260
+ json.charts[i].chartOptions.vAxis.minValue = null;
261
+ json.charts[i].chartOptions.vAxis.maxValue = null;
262
break;
263
264
case "stacked":
@@ -264,8 +268,9 @@ function loadCharts(base_url, doNext) {
268
json.charts[i].chartOptions.lineWidth = 1;
269
json.charts[i].group_method = "average";
270
267
- if(json.charts[i].name == 'system.swap')
268
- json.charts[i].chartOptions.vAxis.viewWindowMode = 'maximized';
271
+ json.charts[i].chartOptions.vAxis.viewWindowMode = 'maximized';
272
+ json.charts[i].chartOptions.vAxis.minValue = null;
273
+ json.charts[i].chartOptions.vAxis.maxValue = null;
274
break;
275
276
default:
@@ -273,9 +278,6 @@ function loadCharts(base_url, doNext) {
278
json.charts[i].chartType = "LineChart";
279
json.charts[i].chartOptions.lineWidth = 2;
280
json.charts[i].chartOptions.curveType = 'function';
276
-
277
- json.charts[i].chartOptions.vAxis.minValue = -0.1;
278
- json.charts[i].chartOptions.vAxis.maxValue = 0.1;
281
break;
282
}
283