]> git.sesse.net Git - nms/blob - web/ext/totalgraph.cpp
Add lots of TG06 graph stuff.
[nms] / web / ext / totalgraph.cpp
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/wait.h>
4 #include <malloc.h>
5 #include <vector>
6 #include <algorithm>
7 #include <pqxx/pqxx>
8 #include "flowpusher.h"
9 #include "flowutil.h"
10
11 namespace pqxx {
12         template<>
13         void from_string<long long>(const char *from, long long &to)
14         {
15                 to = atoll(from);
16         }
17 }
18                                                 
19
20 int main(int argc, char **argv)
21 {
22         int width = 1000, height = 500;
23         pqxx::connection conn("dbname=nms host=localhost user=nms password=seesahS4");
24
25         std::vector<flow_element> flow, total_flow;
26         FlowPusher fp(flow);
27         int last_port = -1;
28         
29         mallopt(M_TRIM_THRESHOLD, -1);
30         
31         int num_total = 0;
32         pqxx::work t(conn, "fetch_all");
33         pqxx::icursorstream::icursorstream cstream(t, "select port,extract(epoch from time) as time,bytes_in,bytes_out from polls natural join switches where (switchtype='es3024' and port < 25) or (switchtype='summit400' and port > 1) order by switch,port,time", "fetch_all", 500);
34         
35         for ( ;; ) {
36                 pqxx::result res;
37                 
38                 cstream >> res;
39                 if (res.empty()) 
40                         break;
41
42                 for (unsigned i = 0; i < res.size(); ++i) {
43                         int port = res[i][0].as<int>();
44                         double x = res[i][1].as<double>();
45                         unsigned long long y1 = res[i][2].as<long long>(), y2 = res[i][3].as<long long>();
46
47                         if (port != last_port) {
48                                 if (last_port != -1) {
49                                         total_flow = sum_flows(total_flow, flow);
50                                         fprintf(stderr, "%u (%u)\n", last_port, ++num_total);
51                                 }
52
53                                 // reset
54                                 last_port = port;
55                                 fp.reset(x, y1, y2);
56                                 continue;
57                         }
58
59                         fp.push(x, y1, y2);
60                 }
61         }
62
63         unsigned long long min_y = 0;
64         unsigned long long max_y = 10000000;
65
66         for (unsigned i = 0; i < total_flow.size(); ++i) {
67                 flow_element fe = total_flow[i];
68                 
69                 min_y = std::min(min_y, fe.y1);
70                 max_y = std::max(max_y, fe.y1);
71                 
72                 min_y = std::min(min_y, fe.y2);
73                 max_y = std::max(max_y, fe.y2);
74         }
75         
76         make_graph(65535, width, height, total_flow[0].x, total_flow[total_flow.size() - 1].x, min_y, max_y, total_flow);
77 }