]> git.sesse.net Git - nms/blob - web/ext/comparegraph.cpp
Add lots of TG06 graph stuff.
[nms] / web / ext / comparegraph.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 "graph.h"
9 #include "flowpusher.h"
10 #include "flowutil.h"
11
12 namespace pqxx {
13         template<>
14         void from_string<long long>(const char *from, long long &to)
15         {
16                 to = atoll(from);
17         }
18 }
19                                                 
20
21 int main(int argc, char **argv)
22 {
23         int width = 1000, height = 500;
24         pqxx::connection conn05("dbname=nms05 host=localhost user=nms password=seesahS4");
25         pqxx::connection conn("dbname=nms host=localhost user=nms password=seesahS4");
26
27         std::vector<flow_element> flow, total_flow, total_flow05;
28         FlowPusher fp(flow);
29         int last_port = -1;
30         
31         mallopt(M_TRIM_THRESHOLD, -1);
32         
33         int num_total = 0;
34         pqxx::work t(conn, "fetch_all");
35         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);
36         
37         for ( ;; ) {
38                 pqxx::result res;
39                 
40                 cstream >> res;
41                 if (res.empty()) 
42                         break;
43
44                 for (unsigned i = 0; i < res.size(); ++i) {
45                         int port = res[i][0].as<int>();
46                         double x = res[i][1].as<double>();
47                         unsigned long long y1 = res[i][2].as<long long>(), y2 = res[i][3].as<long long>();
48
49                         if (port != last_port) {
50                                 if (last_port != -1) {
51                                         total_flow = sum_flows(total_flow, flow);
52                                         fprintf(stderr, "%u (%u)\n", last_port, ++num_total);
53                                 }
54
55                                 // reset
56                                 last_port = port;
57                                 fp.reset(x, y1, y2);
58                                 continue;
59                         }
60
61                         fp.push(x, y1, y2);
62                 }
63         }
64         total_flow = sum_flows(total_flow, flow);
65
66         unsigned long long min_y = 0;
67         unsigned long long max_y = 10000000;
68
69         for (unsigned i = 0; i < total_flow.size(); ++i) {
70                 flow_element fe = total_flow[i];
71                 
72                 min_y = std::min(min_y, fe.y1);
73                 max_y = std::max(max_y, fe.y1);
74                 
75                 min_y = std::min(min_y, fe.y2);
76                 max_y = std::max(max_y, fe.y2);
77         }
78
79         printf("06 done.\n");
80         
81         num_total = 0;
82         pqxx::work t05(conn05, "fetch_all");
83         pqxx::icursorstream::icursorstream cstream05(t05, "select port,extract(epoch from (time + interval '1 year 20 days')) as time,bytes_in,bytes_out from polls natural join switches where (switchtype='es3024' and port < 25) order by switch,port,time", "fetch_all", 500);
84
85         last_port = -1;
86         
87         for ( ;; ) {
88                 pqxx::result res;
89         
90                 cstream05 >> res;
91                 if (res.empty()) 
92                         break;
93         
94                 for (unsigned i = 0; i < res.size(); ++i) {
95                         int port = res[i][0].as<int>();
96                         double x = res[i][1].as<double>();
97                         unsigned long long y1 = res[i][2].as<long long>(), y2 = res[i][3].as<long long>();
98
99                         if (port != last_port) {
100                                 if (last_port != -1) {
101                                         total_flow05 = sum_flows(total_flow05, filter_flow(flow));
102                                         fprintf(stderr, "TG05: %u (%u)\n", last_port, ++num_total);
103                                 }
104
105                                 // reset
106                                 last_port = port;
107                                 fp.reset(x, y1, y2);
108                                 continue;
109                         }
110
111                         fp.push(x, y1, y2);
112                 }
113         }
114         total_flow05 = sum_flows(total_flow05, filter_flow(flow));
115
116         for (unsigned i = 0; i < total_flow05.size(); ++i) {
117                 flow_element fe = total_flow05[i];
118                 
119                 min_y = std::min(min_y, fe.y1);
120                 max_y = std::max(max_y, fe.y1);
121                 
122                 min_y = std::min(min_y, fe.y2);
123                 max_y = std::max(max_y, fe.y2);
124         }
125         
126         double min_x = std::min(total_flow[0].x, total_flow05[0].x);
127         double max_x = std::max(total_flow[total_flow.size() - 1].x, total_flow05[total_flow05.size() - 1].x);
128         
129         graph *g = mygraph_new(width, height);
130         g = mygraph_make_graph(g, min_x, max_x, min_y, max_y, 5);
131
132         // PLOT FIRST
133         int *x = new int[total_flow.size()];
134         unsigned long long *y1 = new unsigned long long[total_flow.size()];
135         unsigned long long *y2 = new unsigned long long[total_flow.size()];
136
137         // de-interleave
138         for (unsigned i = 0; i < total_flow.size(); ++i) {
139                 x[i] = total_flow[i].x;
140                 y1[i] = total_flow[i].y1;
141                 y2[i] = total_flow[i].y2;
142         }
143
144         mygraph_plot_series(g, x, y1, total_flow.size(), 1.0f, 0.0f, 0.0f);
145         mygraph_plot_series(g, x, y2, total_flow.size(), 0.0f, 0.0f, 1.0f);
146
147         delete[] x;
148         delete[] y1;
149         delete[] y2;
150         
151         // PLOT SECOND
152         x = new int[total_flow05.size()];
153         y1 = new unsigned long long[total_flow05.size()];
154         y2 = new unsigned long long[total_flow05.size()];
155
156         // de-interleave
157         for (unsigned i = 0; i < total_flow05.size(); ++i) {
158                 x[i] = total_flow05[i].x;
159                 y1[i] = total_flow05[i].y1;
160                 y2[i] = total_flow05[i].y2;
161         }
162
163         mygraph_plot_series(g, x, y1, total_flow05.size(), 0.0f, 1.0f, 0.0f);
164         mygraph_plot_series(g, x, y2, total_flow05.size(), 1.0f, 0.0f, 1.0f);
165
166         delete[] x;
167         delete[] y1;
168         delete[] y2;
169         
170         mygraph_to_file(g, "comparative.png");
171         mygraph_cleanup(g);
172 }