the API now also support "min" group method
Costa Tsaousis committed
Aug 16, 2016 at 13:36 UTC
7e6200e42ff1c44261bbb9567ce32e1c1ea53458
4 files changed
+56
-27
src/rrd2json.c
+39
-15
@@ -1370,7 +1370,7 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
1370
long c;
1371
for( rd = st->dimensions, c = 0 ; rd && c < dimensions ; rd = rd->next, c++) {
1372
last_values[c] = 0;
1373
- group_values[c] = 0;
1373
+ group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1374
group_counts[c] = 0;
1375
group_options[c] = 0;
1376
found_non_zero[c] = 0;
@@ -1449,14 +1449,22 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
1449
group_options[c] |= RRDR_RESET;
1450
1451
switch(group_method) {
1452
+ case GROUP_MIN:
1453
+ if(unlikely(isnan(group_values[c])) ||
1454
+ fabsl(value) < fabsl(group_values[c]))
1455
+ group_values[c] = value;
1456
+ break;
1457
+
1458
case GROUP_MAX:
1453
- if(unlikely(fabsl(value) > fabsl(group_values[c])))
1459
+ if(unlikely(isnan(group_values[c])) ||
1460
+ fabsl(value) > fabsl(group_values[c]))
1461
group_values[c] = value;
1462
break;
1463
1464
default:
1465
case GROUP_SUM:
1466
case GROUP_AVERAGE:
1467
+ case GROUP_UNDEFINED:
1468
group_values[c] += value;
1469
break;
1470
@@ -1491,23 +1499,39 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
1499
if(unlikely(group_counts[c] == 0)) {
1500
cn[c] = 0.0;
1501
co[c] |= RRDR_EMPTY;
1494
- }
1495
- else if(unlikely(group_method == GROUP_AVERAGE)) {
1496
- // GROUP_AVERAGE
1497
- cn[c] = group_values[c] / group_counts[c];
1502
+ group_values[c] = (group_method == GROUP_MAX || group_method == GROUP_MIN)?NAN:0;
1503
}
1504
else {
1500
- // GROUP_SUM
1501
- // GROUP_MAX
1502
- // GROUP_INCREMENTAL_SUM
1503
- cn[c] = group_values[c];
1504
- }
1505
+ switch(group_method) {
1506
+ case GROUP_MIN:
1507
+ case GROUP_MAX:
1508
+ if(unlikely(isnan(group_values[c])))
1509
+ cn[c] = 0;
1510
+ else {
1511
+ cn[c] = group_values[c];
1512
+ group_values[c] = NAN;
1513
+ }
1514
+ break;
1515
+
1516
+ case GROUP_SUM:
1517
+ case GROUP_INCREMENTAL_SUM:
1518
+ cn[c] = group_values[c];
1519
+ group_values[c] = 0;
1520
+ break;
1521
+
1522
+ default:
1523
+ case GROUP_AVERAGE:
1524
+ case GROUP_UNDEFINED:
1525
+ cn[c] = group_values[c] / group_counts[c];
1526
+ group_values[c] = 0;
1527
+ break;
1528
+ }
1529
1506
- if(cn[c] < r->min) r->min = cn[c];
1507
- if(cn[c] > r->max) r->max = cn[c];
1530
+ if(cn[c] < r->min) r->min = cn[c];
1531
+ if(cn[c] > r->max) r->max = cn[c];
1532
+ }
1533
1509
- // reset them for the next loop
1510
- group_values[c] = 0;
1534
+ // reset for the next loop
1535
group_counts[c] = 0;
1536
group_options[c] = 0;
1537
}
src/rrd2json.h
+6
-4
@@ -30,10 +30,12 @@ extern char *hostname;
30
#define DATASOURCE_FORMAT_SSV_COMMA "ssvcomma"
31
#define DATASOURCE_FORMAT_CSV_JSON_ARRAY "csvjsonarray"
32
33
-#define GROUP_AVERAGE 0
34
-#define GROUP_MAX 1
35
-#define GROUP_SUM 2
36
-#define GROUP_INCREMENTAL_SUM 3
33
+#define GROUP_UNDEFINED 0
34
+#define GROUP_AVERAGE 1
35
+#define GROUP_MIN 2
36
+#define GROUP_MAX 3
37
+#define GROUP_SUM 4
38
+#define GROUP_INCREMENTAL_SUM 5
39
40
#define RRDR_OPTION_NONZERO 0x00000001 // don't output dimensions will just zero values
41
#define RRDR_OPTION_REVERSED 0x00000002 // output the rows in reverse order (oldest to newest)
src/web_client.c
+7
-4
@@ -553,12 +553,15 @@ uint32_t web_client_api_request_v1_data_google_format(char *name)
553
554
int web_client_api_request_v1_data_group(char *name, int def)
555
{
556
- if(!strcmp(name, "max"))
557
- return GROUP_MAX;
558
-
559
- else if(!strcmp(name, "average"))
556
+ if(!strcmp(name, "average"))
557
return GROUP_AVERAGE;
558
559
+ else if(!strcmp(name, "min"))
560
+ return GROUP_MIN;
561
+
562
+ else if(!strcmp(name, "max"))
563
+ return GROUP_MAX;
564
+
565
else if(!strcmp(name, "sum"))
566
return GROUP_SUM;
567
web/netdata-swagger.yaml
+4
-4
@@ -89,10 +89,10 @@ paths:
89
default: 20
90
- name: group
91
in: query
92
- description: 'The grouping method. If multiple collected values are to be grouped in order to return fewer points, this parameters defines the method of grouping. 4 methods are supported, "max", "average", "sum", "incremental-sum". "max" is actually calculated on the absolute value collected (so it works for both positive and negative dimesions to return the most extreme value in either direction).'
92
+ description: 'The grouping method. If multiple collected values are to be grouped in order to return fewer points, this parameters defines the method of grouping. methods supported "min", "max", "average", "sum", "incremental-sum". "max" is actually calculated on the absolute value collected (so it works for both positive and negative dimesions to return the most extreme value in either direction).'
93
required: true
94
type: string
95
- enum: [ 'max', 'average', 'sum', 'incremental-sum' ]
95
+ enum: [ 'min', 'max', 'average', 'sum', 'incremental-sum' ]
96
default: 'average'
97
allowEmptyValue: false
98
- name: format
@@ -184,10 +184,10 @@ paths:
184
default: 0
185
- name: group
186
in: query
187
- description: 'The grouping method. If multiple collected values are to be grouped in order to return fewer points, this parameters defines the method of grouping. 4 methods are supported, "max", "average", "sum", "incremental-sum". "max" is actually calculated on the absolute value collected (so it works for both positive and negative dimesions to return the most extreme value in either direction).'
187
+ description: 'The grouping method. If multiple collected values are to be grouped in order to return fewer points, this parameters defines the method of grouping. methods are supported "min", "max", "average", "sum", "incremental-sum". "max" is actually calculated on the absolute value collected (so it works for both positive and negative dimesions to return the most extreme value in either direction).'
188
required: true
189
type: string
190
- enum: [ 'max', 'average', 'sum', 'incremental-sum' ]
190
+ enum: [ 'min', 'max', 'average', 'sum', 'incremental-sum' ]
191
default: 'average'
192
allowEmptyValue: false
193
- name: options