]> git.sesse.net Git - nms/blob - web/ext/flowutil.cpp
Added Chillout.
[nms] / web / ext / flowutil.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 "graph.h"
8 #include "flowutil.h"
9
10 std::vector<flow_element> sum_flows(const std::vector<flow_element> &a, const std::vector<flow_element> &b)
11 {
12         std::vector<flow_element> ret;
13         std::vector<flow_element>::const_iterator ia = a.begin(), ib = b.begin();
14
15         double last_xa = 0.0, last_y1a = 0.0, last_y2a = 0.0;
16         double last_xb = 0.0, last_y1b = 0.0, last_y2b = 0.0;
17         
18         while (ia != a.end() && ib != b.end()) {
19                 if (ia->x == ib->x) {
20                         flow_element fea = *ia, feb = *ib, fe;
21                         
22                         last_xa = fea.x;
23                         last_y1a = fea.y1;
24                         last_y2a = fea.y2;
25                         
26                         last_xb = feb.x;
27                         last_y1b = feb.y1;
28                         last_y2b = feb.y2;
29                 
30                         fe.x = fea.x;
31                         fe.y1 = fea.y1 + feb.y1;
32                         fe.y2 = fea.y2 + feb.y2;
33
34                         ret.push_back(fe);
35
36                         ++ia, ++ib;
37                 } else if (ia->x < ib->x) {
38                         flow_element fe = *ia;
39                         
40                         last_xa = fe.x;
41                         last_y1a = fe.y1;
42                         last_y2a = fe.y2;
43                         
44                         double t = (fe.x - last_xb) / (ib->x - last_xb);
45                         fe.y1 += last_y1b + t * (ib->y1 - last_y1b);
46                         fe.y2 += last_y2b + t * (ib->y2 - last_y2b);
47
48                         ret.push_back(fe);
49
50                         ++ia;
51                 } else {
52                         flow_element fe = *ib;
53                         
54                         last_xb = fe.x;
55                         last_y1b = fe.y1;
56                         last_y2b = fe.y2;
57                         
58                         double t = (fe.x - last_xa) / (ia->x - last_xa);
59                         fe.y1 += last_y1a + t * (ia->y1 - last_y1a);
60                         fe.y2 += last_y2a + t * (ia->y2 - last_y2a);
61
62                         ret.push_back(fe);
63                         ++ib;
64                 }
65         }
66
67         while (ia != a.end()) {
68                 ret.push_back(*ia);
69                 ++ia;
70         }
71         while (ib != b.end()) {
72                 ret.push_back(*ib);
73                 ++ib;
74         }
75
76         return ret;
77 }
78
79 std::vector<flow_element> filter_flow(const std::vector<flow_element> &flow)
80 {
81         unsigned long long last_y1 = 0, last_y2 = 0;
82         std::vector<flow_element> ret;
83         
84         for (std::vector<flow_element>::const_iterator i = flow.begin(); i != flow.end(); ++i) {
85 /*              if ((i->y1 > 100000 && last_y1 < 10000 && i->y2 > 100000 && last_y2 < 1000) ||
86                     (last_y1 > 10000 && last_y2 > 10000 && i->y1 / last_y1 > 10 && i->y2 / last_y2 > 10)) {
87                         printf("yoyo: %llu %llu (%llu %llu)\n", i->y1, i->y2, last_y1, last_y2);
88                 } else {
89                         ret.push_back(*i);
90                         last_y1 = i->y1;
91                         last_y2 = i->y2;
92                 } */
93                 if (!(i->x >= 1145056800 && i->x <= 1145070800)) {
94                         ret.push_back(*i);
95                 }
96         }
97
98         return ret;
99 }
100
101 void make_graph(int port, unsigned width, unsigned height, unsigned min_x, unsigned max_x, unsigned long long min_y, unsigned long long max_y, std::vector<flow_element> &flow)
102 {
103         char filename[256];
104         graph *g = mygraph_new(width, height);
105         g = mygraph_make_graph(g, min_x, max_x, min_y, max_y, 5);
106
107         int *x = new int[flow.size()];
108         unsigned long long *y1 = new unsigned long long[flow.size()];
109         unsigned long long *y2 = new unsigned long long[flow.size()];
110
111         // de-interleave
112         for (unsigned i = 0; i < flow.size(); ++i) {
113                 x[i] = flow[i].x;
114                 y1[i] = flow[i].y1;
115                 y2[i] = flow[i].y2;
116         }
117
118         mygraph_plot_series(g, x, y1, flow.size(), 1.0f, 0.0f, 0.0f);
119         mygraph_plot_series(g, x, y2, flow.size(), 0.0f, 0.0f, 1.0f);
120         sprintf(filename, "port-%u-500-250.png", port);
121         mygraph_to_file(g, filename);
122         mygraph_cleanup(g);
123
124         delete[] x;
125         delete[] y1;
126         delete[] y2;
127 }
128