"Fossies" - the Fresh Open Source Software Archive 
Member "vnstat-2.9/src/dbjson.c" (10 May 2019, 3348 Bytes) of package /linux/misc/vnstat-2.9.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "dbjson.c" see the
Fossies "Dox" file reference documentation.
1 #include "common.h"
2 #include "dbsql.h"
3 #include "dbjson.h"
4
5 void showjson(const char *interface, const int ifcount, const char mode, const char *databegin, const char *dataend)
6 {
7 interfaceinfo info;
8
9 timeused_debug(__func__, 1);
10
11 if (!db_getinterfacecountbyname(interface)) {
12 return;
13 }
14
15 if (!db_getinterfaceinfo(interface, &info)) {
16 return;
17 }
18
19 if (ifcount) {
20 printf(",");
21 }
22 printf("{");
23 printf("\"name\":\"%s\",", info.name);
24 printf("\"alias\":\"%s\",", info.alias);
25
26 printf("\"created\":{");
27 jsondate(&info.created, 1);
28 printf("},");
29 printf("\"updated\":{");
30 jsondate(&info.updated, 2);
31 printf("},");
32
33 printf("\"traffic\":");
34 printf("{\"total\":{\"rx\":%" PRIu64 ",\"tx\":%" PRIu64 "},", info.rxtotal, info.txtotal);
35
36 switch (mode) {
37 case 'd':
38 jsondump(&info, "day", 1, databegin, dataend);
39 break;
40 case 'm':
41 jsondump(&info, "month", 3, databegin, dataend);
42 break;
43 case 't':
44 jsondump(&info, "top", 1, databegin, dataend);
45 break;
46 case 'h':
47 jsondump(&info, "hour", 2, databegin, dataend);
48 break;
49 case 'y':
50 jsondump(&info, "year", 4, databegin, dataend);
51 break;
52 case 'f':
53 jsondump(&info, "fiveminute", 2, databegin, dataend);
54 break;
55 case 'a':
56 default:
57 jsondump(&info, "fiveminute", 2, databegin, dataend);
58 printf(",");
59 jsondump(&info, "hour", 2, databegin, dataend);
60 printf(",");
61 jsondump(&info, "day", 1, databegin, dataend);
62 printf(",");
63 jsondump(&info, "month", 3, databegin, dataend);
64 printf(",");
65 jsondump(&info, "year", 4, databegin, dataend);
66 printf(",");
67 jsondump(&info, "top", 1, databegin, dataend);
68 break;
69 }
70
71 printf("}}");
72
73 timeused_debug(__func__, 0);
74 }
75
76 void jsondump(const interfaceinfo *interface, const char *tablename, const int datetype, const char *databegin, const char *dataend)
77 {
78 int first = 1;
79 dbdatalist *datalist = NULL, *datalist_i = NULL;
80 dbdatalistinfo datainfo;
81
82 if (!db_getdata_range(&datalist, &datainfo, interface->name, tablename, (uint32_t)cfg.listjsonxml, databegin, dataend)) {
83 printf("Error: Failed to fetch %s data.\n", tablename);
84 return;
85 }
86
87 printf("\"%s\":[", tablename);
88 datalist_i = datalist;
89 while (datalist_i != NULL) {
90 if (!first) {
91 printf(",");
92 } else {
93 first = 0;
94 }
95 printf("{\"id\":%" PRId64 ",", datalist_i->rowid);
96 jsondate(&datalist_i->timestamp, datetype);
97 printf(",\"rx\":%" PRIu64 ",\"tx\":%" PRIu64 "}", datalist_i->rx, datalist_i->tx);
98 datalist_i = datalist_i->next;
99 }
100 dbdatalistfree(&datalist);
101 printf("]");
102 }
103
104 void jsondate(const time_t *date, const int type)
105 {
106 struct tm *d;
107
108 d = localtime(date);
109
110 switch (type) {
111 case 1:
112 printf("\"date\":{\"year\":%d,\"month\":%d,\"day\":%d}",
113 1900 + d->tm_year, 1 + d->tm_mon, d->tm_mday);
114 break;
115 case 2:
116 printf("\"date\":{\"year\":%d,\"month\":%d,\"day\":%d},\"time\":{\"hour\":%d,\"minute\":%d}",
117 1900 + d->tm_year, 1 + d->tm_mon, d->tm_mday, d->tm_hour, d->tm_min);
118 break;
119 case 3:
120 printf("\"date\":{\"year\":%d,\"month\":%d}",
121 1900 + d->tm_year, 1 + d->tm_mon);
122 break;
123 case 4:
124 printf("\"date\":{\"year\":%d}",
125 1900 + d->tm_year);
126 break;
127 default:
128 break;
129 }
130 }
131
132 void jsonheader(void)
133 {
134 printf("{\"vnstatversion\":\"%s\",\"jsonversion\":\"%d\",\"interfaces\":[", getversion(), JSONVERSION);
135 }
136
137 void jsonfooter(void)
138 {
139 printf("]}\n");
140 }