##// END OF EJS Templates
Fix server for noise data, correct codes
jespinoza -
r3:bb0aaf379008
parent child
Show More
@@ -1,52 +1,52
1 1 import os
2 2 import json
3 3
4 4 from datetime import datetime
5 5
6 6 from pymongo import MongoClient
7 7
8 8 from channels.handler import AsgiHandler
9 9 from channels.auth import channel_session_user
10 10 from channels import Group
11 11
12 12 host = os.environ.get('HOST_MONGO', 'localhost')
13 13 CLIENT = MongoClient('{}:27017'.format(host))
14 14 DB = CLIENT['dbplots']
15 15
16 16 # Connected to websocket.connect
17 17 def ws_connect(message, code, plot):
18 18 message.reply_channel.send({'accept': True})
19 19 pk = message.content['query_string'].split('=')[1]
20 20 Group('{}_{}'.format(pk, plot)).add(message.reply_channel)
21 print('New connection from: {}, creating Group: {}_{}'.format(message.content['client'][0], pk, plot))
21 print('New connection from: {}, Group: {}_{}'.format(message.content['client'][0], pk, plot))
22 22
23 23 def ws_message(message, code, plot):
24 24 # Accept the incoming connection
25 25 print 'incoming message'
26 26 dt = datetime.strptime(str(json.loads(message.content['text'])['date']), '%d/%m/%Y')
27 27 exp = DB.exp_meta.find_one({'code': int(code), 'date': dt})
28 print exp
29 if exp:
28
29 if exp and plot in exp['plots']:
30 30 if plot == 'spc':
31 31 datas = DB.exp_data.find({'expmeta': exp['_id']}, ['time', 'data']).sort('time', -1).limit(1)[0]
32 32 exp['time'] = [datas['time']]
33 33 exp['spc'] = datas['data']['spc']
34 34 exp['rti'] = datas['data']['rti']
35 35 exp['noise'] = datas['data']['noise']
36 36 else:
37 37 datas = DB.exp_data.find({'expmeta': exp['_id']}, ['time', 'data']).sort('time', 1)
38 38 print 'Counting...'
39 39 print datas.count()
40 40 dum = [(d['time'], d['data'][plot]) for d in datas]
41 41 exp['time'] = [d[0] for d in dum]
42 42 dum = [d[1] for d in dum]
43 43 exp[plot] = map(list, zip(*dum))
44 44 exp.pop('date', None)
45 45 exp.pop('_id', None)
46 46 message.reply_channel.send({'text': json.dumps(exp)})
47 47 else:
48 48 message.reply_channel.send({'text': json.dumps({'interval': 0})})
49 49
50 50 # Connected to websocket.disconnect
51 51 def ws_disconnect(message, code, plot):
52 52 Group('{}_{}'.format(code, plot)).discard(message.reply_channel)
@@ -1,454 +1,455
1 1
2 2
3 3 class PcolorBuffer{
4 4 constructor({div, data, key, props}){
5 5 this.div = document.getElementById(div);
6 6 this.n = 0
7 7 this.divs = [];
8 8 this.wait = false;
9 9 this.key = key;
10 10 this.timer = (props.throttle || 30)*1000;
11 11 this.lastRan = Date.now();
12 12 this.lastFunc = null;
13 13 this.zbuffer = [];
14 14 this.xbuffer = [];
15 15 this.empty = Array(data.yrange.length).fill(null);
16 16 this.props = props;
17 17 this.setup(data);
18 18 }
19 19
20 20 setup(data){
21 21 this.last = data.time.slice(-1);
22 22 if (data.time.length == 1){
23 23 var values = { 'time': data.time, 'data': data[this.key].map(function(x) {return [x]})};
24 24 }else{
25 25 var values = this.fill_gaps(data.time, data[this.key], data.interval, data[this.key].length);
26 26 }
27 27
28 28 var t = values.time.map(function(x) { return new Date(x*1000); });
29 29
30 30 for (var i = 0; i < data[this.key].length; i++){
31 31 var layout = {
32 32 xaxis: {
33 33 title: 'Time',
34 34 showgrid: false,
35 35 },
36 36 yaxis: {
37 37 title: 'km',
38 38 showgrid: false,
39 39 },
40 40 titlefont: {
41 41 size: 14,
42 42 }
43 43 };
44 44 var iDiv = document.createElement('div');
45 45 iDiv.id = 'plot-' + i;
46 46 this.zbuffer.push([]);
47 47 this.n = this.n + 1;
48 48 this.div.appendChild(iDiv);
49 49 this.divs.push(iDiv.id);
50 50 var trace = {
51 51 z: values.data[i],
52 52 x: t,
53 53 y: data.yrange,
54 54 colorscale: this.props.colormap || 'Viridis',
55 55 transpose: true,
56 56 type: 'heatmap'
57 57 };
58 58
59 59 if (this.props.zmin) {trace.zmin = this.props.zmin}
60 60 if (this.props.zmax) {trace.zmax = this.props.zmax}
61 61
62 62 layout.title = 'Channel ' + i + ' - ' + t.slice(-1).toLocaleString();
63 63 var conf = {
64 64 modeBarButtonsToRemove: ['sendDataToCloud', 'autoScale2d', 'hoverClosestCartesian', 'hoverCompareCartesian', 'lasso2d', 'select2d', 'zoomIn2d', 'zoomOut2d', 'toggleSpikelines'],
65 65 displaylogo: false,
66 66 showTips: true
67 67 };
68 68 Plotly.newPlot('plot-'+i, [trace], layout, conf);
69 69 }
70 70 }
71 71
72 72 getSize(){
73 73 var div = document.getElementById(this.divs[0]);
74 74 var t = this.xbuffer.slice(-1)[0];
75 75 var n = 0;
76 76 var timespan = (this.props.timespan || 8)*1000*60*60;
77 77
78 78 while ((t-div.data[0].x[n]) > timespan){
79 79 n += 1;
80 80 }
81 81 return n;
82 82 }
83 83
84 84 fill_gaps(xBuffer, zBuffer, interval, N){
85 85
86 86 var x = [xBuffer[0]];
87 87 var z = [];
88 88 var last;
89 89
90 90 for (var j = 0; j < N; j++){
91 91 z.push([zBuffer[j][0]]);
92 92 }
93 93
94 94 for (var i = 1; i < xBuffer.length; i++){
95 95 var cnt = 0;
96 96 last = x.slice(-1)[0];
97 97 while (Math.abs(parseFloat(xBuffer[i])-last-parseFloat(interval))>0.5){
98 98 cnt += 1;
99 99 last = last+interval;
100 100 x.push(last);
101 101 for (var j = 0; j < N; j++){
102 102 z[j].push(this.empty);
103 103 }
104 104 // Avoid infinite loop
105 105 if (cnt==100){break;}
106 106 }
107 107 x.push(xBuffer[i]);
108 108 for (var j = 0; j < N; j++){
109 109 z[j].push(zBuffer[j][i]);
110 110 }
111 111 }
112 112
113 113 return {'time':x, 'data':z};
114 114 }
115 115
116 116 plot(){
117 117 // add new data to plots and empty buffers
118 118 var N = this.getSize();
119 119 console.log('Plotting...');
120 120 for (var i = 0; i < this.n; i++){
121 121 var div = document.getElementById(this.divs[i]);
122 122 if (N > 0){
123 123 div.data[0].z = div.data[0].z.slice(N,)
124 124 div.data[0].x = div.data[0].x.slice(N,)
125 125 }
126 126 Plotly.extendTraces(div, {
127 127 z: [this.zbuffer[i]],
128 128 x: [this.xbuffer]
129 129 }, [0]);
130 130 this.zbuffer[i] = [];
131 131 }
132 132 this.xbuffer = [];
133 133 }
134 134
135 135 update(obj){
136 136
137 137 // fill data gaps
138 138 var cnt = 0;
139 139 while (Math.abs(parseFloat(obj.time[0])-this.last-parseFloat(obj.interval))>0.5){
140 140 cnt += 1;
141 141 this.last += obj.interval;
142 142 var newt = new Date((this.last)*1000);
143 143 this.xbuffer.push(newt);
144 144 for (var i = 0; i < obj[this.key].length; i++){
145 145 this.zbuffer[i].push(this.empty);
146 146 }
147 147 // Avoid infinite loop
148 148 if (cnt==100){break;}
149 149 }
150 150
151 151 // update buffers
152 152 this.last = parseFloat(obj.time[0]);
153 153 var t = new Date(obj.time[0]*1000);
154 154 this.xbuffer.push(t);
155 155 for (var i = 0; i < obj[this.key].length; i++){
156 156 this.zbuffer[i].push(obj[this.key][i]);
157 157 // update title
158 var title = this.props.title || ''
158 159 var div = document.getElementById(this.divs[i]);
159 160 Plotly.relayout(div, {
160 title: 'Channel ' + i + ' - ' + t.toLocaleString(),
161 title: title + ': Channel ' + i + ' - ' + t.toLocaleString(),
161 162 });
162 163 }
163 164
164 165 // plot when ready (every 10 secs)
165 166 if (!this.wait){
166 167 this.plot();
167 168 this.wait = true;
168 169 } else {
169 170 clearTimeout(this.lastFunc)
170 171 this.lastFunc = setTimeout(function(scope) {
171 172 if ((Date.now() - scope.lastRan) >= scope.timer) {
172 173 scope.plot()
173 174 scope.lastRan = Date.now()
174 175 }
175 176 }, this.timer - (Date.now() - this.lastRan), this)
176 177 }
177 178 }
178 179 }
179 180
180 181 class Pcolor{
181 182 constructor({div, data, props}){
182 183 this.div = document.getElementById(div);
183 184 this.n = 0
184 185 this.divs = [];
185 186 this.props = props;
186 187 this.setup(data);
187 188 }
188 189
189 190 setup(data){
190 191 console.log(data);
191 192 for (var i = 0; i < data.spc.length; i++){
192 193 var layout = {
193 194 height: 400,
194 195 xaxis: {
195 196 title: 'Velocity',
196 197 showgrid: false,
197 198 zeroline: false,
198 199 domain: [0, 0.7],
199 200 },
200 201 yaxis: {
201 202 title: 'km',
202 203 showgrid: false,
203 204 },
204 205 xaxis2: {
205 206 title: 'dB',
206 207 domain: [0.75, 1],
207 208 ticks: 'outside',
208 209 },
209 210 titlefont: {
210 211 size: 14,
211 212 }
212 213 };
213 214 var iDiv = document.createElement('div');
214 215 iDiv.id = 'plot-' + i;
215 216 iDiv.className += iDiv.className ? ' col-md-6' : 'col-md-6';
216 217 this.n = this.n + 1;
217 218 this.div.appendChild(iDiv);
218 219 this.divs.push(iDiv.id);
219 220
220 221 var trace1 = {
221 222 z: data.spc[i],
222 223 x: data.xrange,
223 224 y: data.yrange,
224 225 colorscale: this.props.colormap || 'Viridis',
225 226 transpose: true,
226 227 type: 'heatmap'
227 228 };
228 229
229 230 var trace2 = {
230 231 x: data.rti[i],
231 232 y: data.yrange,
232 233 xaxis: 'x2',
233 234 yaxis: 'y',
234 235 type: 'scatter',
235 236 };
236 237
237 238 if (this.props.zmin) {
238 239 trace1.zmin = this.props.zmin
239 240 }
240 241 if (this.props.zmax) {
241 242 trace1.zmax = this.props.zmax;
242 243 layout.xaxis2.range = [this.props.zmin, this.props.zmax]
243 244 }
244 245
245 246 var t = new Date(data.time*1000);
246 247 layout.title = 'Channel ' + i + ': ' + data.noise[i]+'dB - ' + t.toLocaleString();
247 248 var conf = {
248 249 modeBarButtonsToRemove: ['sendDataToCloud', 'autoScale2d', 'hoverClosestCartesian', 'hoverCompareCartesian', 'lasso2d', 'select2d', 'zoomIn2d', 'zoomOut2d', 'toggleSpikelines'],
249 250 displaylogo: false,
250 251 showTips: true
251 252 };
252 253 Plotly.newPlot('plot-'+i, [trace1, trace2], layout, conf);
253 254 }
254 255 }
255 256
256 257 plot(obj){
257 258
258 259 // add new data to plots and empty buffers
259 260 console.log('Plotting...');
260 261 var t = new Date(obj.time[0]*1000);
261 262 for (var i = 0; i < this.n; i++){
262 263 var div = document.getElementById(this.divs[i]);
263 264 Plotly.relayout(div, {
264 265 title: 'Channel ' + i + ': ' + obj.noise[i]+'dB - ' + t.toLocaleString(),
265 266 });
266 267 Plotly.restyle(div, {
267 268 z: [obj.spc[i], null],
268 269 x: [obj.xrange, obj.rti[i]]
269 270 }, [0, 1]);
270 271 }
271 272 }
272 273 }
273 274
274 275 class Scatter{
275 276 constructor({div, data, key, props}){
276 277 this.div = document.getElementById(div);
277 278 this.n = 0
278 279 this.key = key;
279 280 this.wait = false;
280 281 this.timer = (props.throttle || 30)*1000;
281 282 this.lastRan = Date.now();
282 283 this.lastFunc = null;
283 284 this.ybuffer = [];
284 285 this.xbuffer = [];
285 286 this.props = props;
286 287 this.setup(data);
287 288 }
288 289
289 290 setup(data){
290 291
291 292 var traces = [];
292 293 this.last = data.time.slice(-1);
293 294 if (data.time.length == 1){
294 295 var values = { 'time': data.time, 'data': data[this.key] };
295 296 }else{
296 297 var values = this.fill_gaps(data.time, data[this.key], data.interval, data[this.key].length);
297 298 }
298 299
299 300 var t = values.time.map(function(x) { return new Date(x*1000); });
300 301
301 302 for (var i = 0; i < data[this.key].length; i++){
302 303
303 304 this.n = this.n + 1;
304 305 this.ybuffer.push([]);
305 306 var trace = {
306 307 x: t,
307 308 y: data[this.key][i],
308 309 mode: 'lines',
309 310 type: 'scatter',
310 311 name: 'Channel ' + i
311 312 };
312 313
313 314 traces.push(trace);
314 315 }
315 316 var title = this.props.title || ''
316 317 var layout = {
317 318 // autosize: false,
318 319 // width: 800,
319 320 // height: 400,
320 321 title: title + ' - ' + t.slice(-1).toLocaleString(),
321 322 xaxis: {
322 323 title: 'Time',
323 324 linecolor: 'black',
324 325 },
325 326 yaxis: {
326 327 title: this.props.ylabel || 'dB',
327 328 linecolor: 'black',
328 329 },
329 330 titlefont: {
330 331 size: 14,
331 332 }
332 333 };
333 334
334 335 if (this.props.ymin) {layout.yaxis.range = [this.props.ymin, this.props.ymax] }
335 336
336 337 var conf = {
337 338 modeBarButtonsToRemove: ['sendDataToCloud', 'autoScale2d', 'hoverClosestCartesian', 'hoverCompareCartesian', 'lasso2d', 'select2d', 'zoomIn2d', 'zoomOut2d', 'toggleSpikelines'],
338 339 displaylogo: false,
339 340 showTips: true
340 341 };
341 342 Plotly.newPlot('plot', traces, layout, conf);
342 343 }
343 344
344 345 getSize(){
345 346 var t = this.xbuffer.slice(-1)[0];
346 347 var n = 0;
347 348 var timespan = (this.props.timespan || 12)*1000*60*60;
348 349
349 350 while ((t-this.div.data[0].x[n]) > timespan){
350 351 n += 1;
351 352 }
352 353 return n;
353 354 }
354 355
355 356 fill_gaps(xBuffer, yBuffer, interval, N){
356 357
357 358 var x = [xBuffer[0]];
358 359 var y = [];
359 360
360 361 for (var j = 0; j < N; j++){
361 362 y.push([yBuffer[j][0]]);
362 363 }
363 364
364 365 var last;
365 366
366 367 for (var i = 1; i < xBuffer.length; i++){
367 368 var cnt = 0;
368 369 last = x.slice(-1)[0];
369 370 while (Math.abs(parseFloat(xBuffer[i])-last-parseFloat(interval))>0.5){
370 371 cnt += 1;
371 372 last = last+interval;
372 373 x.push(last);
373 374 for (var j = 0; j < N; j++){
374 375 y[j].push(null);
375 376 }
376 377 // Avoid infinite loop
377 378 if (cnt==100){break;}
378 379 }
379 380 x.push(xBuffer[i]);
380 381
381 382 for (var j = 0; j < N; j++){
382 383 y[j].push(yBuffer[j][i]);
383 384 }
384 385 }
385 386 return {'time':x, 'data':y};
386 387 }
387 388
388 389 plot(){
389 390 // add new data to plots and empty buffers
390 391 var xvalues = [];
391 392 var yvalues = [];
392 393 var traces = [];
393 394 var N = this.getSize();
394 395 console.log('Plotting...');
395 396 for (var i = 0; i < this.n; i++){
396 397 if (N > 0){
397 398 this.div.data[i].y = this.div.data[i].y.slice(N,)
398 399 this.div.data[i].x = this.div.data[i].x.slice(N,)
399 400 }
400 401 yvalues.push(this.ybuffer[i]);
401 402 xvalues.push(this.xbuffer);
402 403 traces.push(i);
403 404 this.ybuffer[i] = [];
404 405 }
405 406 Plotly.extendTraces(this.div, {
406 407 y: yvalues,
407 408 x: xvalues
408 409 }, traces);
409 410 this.xbuffer = [];
410 411 }
411 412
412 413 update(obj){
413 414
414 415 // fill data gaps
415 416 var cnt = 0;
416 417 while (Math.abs(parseFloat(obj.time[0])-this.last-parseFloat(obj.interval))>0.5){
417 418 cnt += 1;
418 419 this.last += obj.interval;
419 420 var newt = new Date((this.last)*1000);
420 421 this.xbuffer.push(newt);
421 422 for (var i = 0; i < this.n; i++){
422 423 this.ybuffer[i].push(null);
423 424 }
424 425 // Avoid infinite loop
425 426 if (cnt==100){break;}
426 427 }
427 428
428 429 // update buffers
429 430 this.last = parseFloat(obj.time[0]);
430 431 var t = new Date(obj.time[0]*1000);
431 432 this.xbuffer.push(t);
432 433 for (var i = 0; i < this.n; i++){
433 434 this.ybuffer[i].push(obj[this.key][i][0]);
434 435 }
435 436 var title = this.props.title || ''
436 437 Plotly.relayout(this.div, {
437 438 title: title + ' - ' + t.toLocaleString(),
438 439 });
439 440
440 441 // plot when ready (every 10 secs)
441 442 if (!this.wait){
442 443 this.plot();
443 444 this.wait = true;
444 445 } else {
445 446 clearTimeout(this.lastFunc)
446 447 this.lastFunc = setTimeout(function(scope) {
447 448 if ((Date.now() - scope.lastRan) >= scope.timer) {
448 449 scope.plot()
449 450 scope.lastRan = Date.now()
450 451 }
451 452 }, this.timer - (Date.now() - this.lastRan), this)
452 453 }
453 454 }
454 455 }
@@ -1,118 +1,118
1 1 {% load static %} {% load bootstrap3 %}
2 2 <!doctype html>
3 3 <html lang="en">
4 4
5 5 <head>
6 6 <meta charset="utf-8">
7 7 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
8 8 <meta http-equiv="X-UA-Compatible" content="ie=edge">
9 9 <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
10 10 <link href="{% static 'css/bootstrap-datetimepicker.min.css' %}" media="screen" rel="stylesheet">
11 11 <script src="{% static 'js/jquery.min.js' %}"></script>
12 12 <style>
13 13 /* Paste this css to your style sheet file or under head tag */
14 14
15 15 /* This only works with JavaScript,
16 16 if it's not present, don't show loader */
17 17
18 18 .loader {
19 19 width: 100%;
20 20 height: 10px;
21 21 position: relative;
22 22 z-index: 9999;
23 23 background: url("{% static 'images/loader.gif' %}") center no-repeat #fff;
24 24 }
25 25
26 26 .no-data {
27 27 text-align: center;
28 28 background-color: #d58512;
29 29 padding: 8px 16px;
30 30 border-radius: 5px;
31 31 color: #FEFEFE;
32 32 font: 20px 'Helvetica';
33 33 text-transform: uppercase;
34 34 }
35 35 </style>
36 36 <title>Realtime plots at JRO</title>
37 37 </head>
38 38
39 39 <body>
40 40 <div class="container">
41 41 <div id="page" class="row" style="min-height:600px">
42 42 {% bootstrap_messages %}
43 43 <div class="page-header">
44 44 <h1>{% block content-title %}Realtime{% endblock %}
45 45 <small>{% block content-suptitle %}Plots{% endblock %}</small>
46 46 </h1>
47 47 </div>
48 48 <div class="col-md-3 hidden-xs hidden-sm" role="complementary">
49 49 <div id="sidebar">
50 50 {% if form.is_multipart %}
51 51 <form class="form" enctype="multipart/form-data" method="post" action="{{action}}">
52 52 {% else %}
53 53 <form class="form" method="post" action="{{action}}">
54 54 {% endif %} {% csrf_token %} {% bootstrap_form form layout='inline' size='medium' %}
55 55 <div style="clear: both;"></div>
56 56 <br>
57 57 <div class="pull-left">
58 58 <button type="button" id="bt_show" class="btn btn-primary">Show</button>
59 59 </div>
60 60 </form>
61 61 </div>
62 62 </div>
63 63 <div class="col-md-9 col-xs-12" role="main">
64 64 <div id="loader" class="loader"></div>
65 65 {% block content %} {% endblock %}
66 66 </div>
67 67 </div>
68 68 <!--/row-->
69 69 </div>
70 70 <script src="{% static 'js/plotly-latest.min.js' %}"></script>
71 71 <script src="{% static 'js/moment.min.js' %}"></script>
72 72 <script src="{% static 'js/bootstrap-datetimepicker.min.js' %}"></script>
73 73 <script src="{% static 'js/jroplots.js' %}"></script>
74 74 <script type="text/javascript">
75 75 $('#id_date').datetimepicker({
76 76 pickTime: false,
77 77 format: 'DD/MM/YYYY',
78 78 });
79 79 $("#bt_show").click(function () {
80 80 document.location = '/' + $("#id_experiment").val() + '/' + $("#id_plot").val() + '/?date=' + $("#id_date").val();
81 81 });
82 82
83 83 {% if date %}
84 84 $("#loader").css("display", "block");
85 85 var socket = new WebSocket('ws://' + window.location.host + '/{{code}}/{{plot}}/?' + 'pk={{id}}');
86 86 socket.onopen = function open() {
87 87 console.log('WebSockets connection created.');
88 88 {% if date %}
89 89 socket.send('{"date": "{{date}}" }');
90 90 {% else %}
91 91 socket.send('{"date": "{% now "d/m/Y" %}" }');
92 92 {% endif %}
93 93 };
94 94
95 95 socket.onmessage = function message(event) {
96 96 var data = JSON.parse(event.data);
97 console.log(data);
97 console.log(data.time);
98 98 if (data.interval == 0) {
99 99 $("#loader").removeClass("loader").addClass("no-data");
100 100 $("#loader").html("No data found");
101 101 } else {
102 102 var first = plot(data);
103 103 if (first == true) {
104 104 $("#loader").css("display", "none");
105 105 }
106 106 }
107 107 }
108 108
109 109 if (socket.readyState == WebSocket.OPEN) {
110 110 socket.onopen();
111 111 }
112 112 {% else %}
113 113 $("#loader").css("display", "none");
114 114 {% endif %}
115 115 </script> {% block script %}{% endblock script %}
116 116 </body>
117 117
118 118 </html> No newline at end of file
@@ -1,22 +1,22
1 1 {% extends 'base.html' %} {% block content %}
2 2 <div id="plot"></div>
3 3 {% endblock content %} {% block script %}
4 4 <script>
5 5 let flag = true;
6 6 function plot(data) {
7 7 if (flag === true) {
8 8 flag = false;
9 9 rti = new PcolorBuffer({
10 10 div: 'plot',
11 11 data: data,
12 12 key: 'rti',
13 props: { title: '{{title}}', zmin: 15, zmax: 30, throttle: 10, timespan: 12 },
13 props: { title: '{{title}}', zmin: 15, zmax: 35, throttle: 10, timespan: 12, colormap: 'Jet' },
14 14 });
15 15 return true;
16 16 } else {
17 17 rti.update(data);
18 18 return false;
19 19 }
20 20 }
21 21
22 22 </script> {% endblock script %} No newline at end of file
@@ -1,28 +1,28
1 1 {% extends 'base.html' %}
2 2
3 3 {% block content %}
4 4 <div id="plot"></div>
5 5 {% endblock content %}
6 6
7 7 {% block script %}
8 8 <script>
9 9 let flag = true;
10 10
11 11 function plot(data){
12 12 // console.log(data['time']);
13 13 if (flag === true){
14 14 flag = false;
15 15 plt = new Scatter({
16 16 div: 'plot',
17 17 data: data,
18 18 key: '{{plot}}',
19 props: {title: '{{title}}', ylabel:'Noise [dB]', ymin:10, ymax:20 },
19 props: {title: '{{title}}', ylabel:'Noise [dB]', ymin:18, ymax:22 },
20 20 });
21 21 return true;
22 22 } else {
23 23 plt.update(data);
24 24 return false;
25 25 }
26 26 }
27 27 </script>
28 28 {% endblock script %} No newline at end of file
@@ -1,84 +1,84
1 1 # -*- coding: utf-8 -*-
2 2 from __future__ import unicode_literals
3 3
4 4 import os
5 5 from datetime import datetime
6 6
7 7 from django import forms
8 8 from django.contrib import messages
9 9 from django.utils.safestring import mark_safe
10 10 from django.shortcuts import render
11 11
12 12 from bootstrap3_datetime.widgets import DateTimePicker
13 13
14 14 import mongoengine
15 15
16 16 from plotter.models import Experiment, ExpMeta
17 17
18 18 host = os.environ.get('HOST_MONGO', 'localhost')
19 19 mongoengine.connect('dbplots', host=host, port=27017)
20 20
21 21 # Forms
22 22 class SearchForm(forms.Form):
23 23
24 24 experiment = forms.ChoiceField()
25 25 plot = forms.ChoiceField(
26 26 choices = [(0, 'Select Plot'), ('rti', 'RTI'),('spc', 'Spectra'),('noise', 'Noise')]
27 27 )
28 28 date = forms.DateField(
29 29 widget=DateTimePicker(options={"format": "DD/MM/YYYY"})
30 30 )
31 31
32 32 def __init__(self, *args, **kwargs):
33 33
34 34 exp_choices = kwargs.pop('exp_choices', [])
35 35 super(SearchForm, self).__init__(*args, **kwargs)
36 36 self.fields['experiment'].choices = [(0, 'Select Experiment')] + exp_choices
37 37
38 38 # Create your views here.
39 39 def main(request, code=None, plot=None):
40 40
41 41 initial = {}
42 42 date = request.GET.get('date', datetime.now().strftime('%d/%m/%Y'))
43 43 initial['date'] = date
44 44
45 45 if code is not None:
46 46 initial['experiment'] = code
47 47 if plot is not None:
48 48 initial['plot'] = plot
49 49
50 50 exps = [(q['code'], q['name']) for q in Experiment.objects.all()]
51 51
52 52 form = SearchForm(
53 53 initial = initial,
54 54 exp_choices = [(e[0], e[1]) for e in exps]
55 55 )
56 56
57 57 try:
58 58 exp = ExpMeta.objects.get(code=int(code), date=datetime.strptime(date, '%d/%m/%Y'))
59 59 exp_id = exp.id
60 60 except:
61 61 exp_id = 0
62 62
63 63
64 64 kwargs = {
65 65 'code': code,
66 66 'plot': plot,
67 67 'date': date,
68 68 'form': form,
69 69 'id': exp_id
70 70 }
71 71
72 72 if code and exps:
73 kwargs['title'] = [t[1] for t in exps if t[0]==int(code)][0]
73 kwargs['title'] = [t[1] for t in exps if int(t[0])==int(code)][0]
74 74 else:
75 75 kwargs['title'] = 'JRO'
76 76
77 77 if plot == 'rti':
78 78 return render(request, 'rti.html', kwargs)
79 79 elif plot == 'spc':
80 80 return render(request, 'spectra.html', kwargs)
81 81 elif plot == 'noise':
82 82 return render(request, 'scatter.html', kwargs)
83 83 else:
84 84 return render(request, 'base.html', kwargs) No newline at end of file
@@ -1,110 +1,110
1 1 [
2 2 {
3 3 "code" : 100,
4 4 "name" : "HF JRO"
5 5 },
6 6 {
7 7 "code" : 101,
8 8 "name" : "HF Huancayo"
9 9 },
10 10 {
11 11 "code" : 102,
12 12 "name" : "HF Ica"
13 13 },
14 14 {
15 15 "code" : 110,
16 16 "name" : "ISR EW Drifts"
17 17 },
18 18 {
19 19 "code" : 111,
20 20 "name" : "ISR Faraday"
21 21 },
22 22 {
23 23 "code" : 112,
24 24 "name" : "ISR Imaging"
25 25 },
26 26 {
27 27 "code" : 120,
28 28 "name" : "Faraday"
29 29 },
30 30 {
31 31 "code" : 121,
32 32 "name" : "Faraday Long Pulse"
33 33 },
34 34 {
35 35 "code" : 122,
36 36 "name" : "Faraday Codded LP"
37 37 },
38 38 {
39 39 "code" : 123,
40 40 "name" : "Faraday Double Pulse"
41 41 },
42 42 {
43 43 "code" : 124,
44 44 "name" : "Faraday AC"
45 45 },
46 46 {
47 47 "code" : 125,
48 48 "name" : "Faraday Differential Phase"
49 49 },
50 50 {
51 51 "code" : 150,
52 52 "name" : "JASMET 50"
53 53 },
54 54 {
55 55 "code" : 151,
56 56 "name" : "JASMET 30"
57 57 },
58 58 {
59 59 "code" : 170,
60 60 "name" : "BLTR Huancayo"
61 61 },
62 62 {
63 63 "code" : 171,
64 64 "name" : "CIRI Huancayo"
65 65 },
66 66 {
67 67 "code" : 172,
68 68 "name" : "CLAIRE Huancayo"
69 69 },
70 70 {
71 71 "code" : 180,
72 72 "name" : "MST"
73 73 },
74 74 {
75 75 "code" : 181,
76 76 "name" : "ISR"
77 77 },
78 78 {
79 79 "code" : 182,
80 80 "name" : "EEJ"
81 81 },
82 82 {
83 83 "code" : 190,
84 84 "name" : "JULIA 150km (Natalia)"
85 85 },
86 86 {
87 87 "code" : 191,
88 88 "name" : "JULIA SpreadF (Natalia)"
89 89 },
90 90 {
91 91 "code" : 200,
92 92 "name" : "JULIA 150km"
93 93 },
94 94 {
95 95 "code" : 201,
96 96 "name" : "JULIA EEJ"
97 97 },
98 98 {
99 99 "code" : 202,
100 100 "name" : "JULIA SpreadF"
101 101 },
102 102 {
103 103 "code" : 203,
104 104 "name" : "JULIA Imaging"
105 105 },
106 106 {
107 "code" : 203,
107 "code" : 204,
108 108 "name" : "JULIA Bistatic"
109 109 }
110 110 ] No newline at end of file
@@ -1,84 +1,87
1 1 import os
2 2 import sys
3 3 import json
4 4 import simplejson
5 5 from datetime import datetime
6 6 import zmq
7 7 import redis
8 8 import asgi_redis
9 9 import mongoengine
10 10
11 11 sys.path.append('/app')
12 12 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "realtime.settings")
13 13
14 14 from plotter.models import Experiment, ExpMeta, ExpData
15 15
16 16 host_mongo = os.environ.get('HOST_MONGO', 'localhost')
17 17 mongoengine.connect('dbplots', host=host_mongo, port=27017)
18 18
19 19
20 20 host_redis = os.environ.get('HOST_REDIS', 'localhost')
21 21 channel = asgi_redis.RedisChannelLayer(hosts=[(host_redis, 6379)])
22 22
23 23 context = zmq.Context()
24 24 receiver = context.socket(zmq.SUB)
25 25
26 26 receiver.bind("tcp://0.0.0.0:4444")
27 27 receiver.setsockopt(zmq.SUBSCRIBE, '')
28 28
29 29 def loaddata():
30 30 print('Loading Experiments...')
31 31 for tup in json.load(open('scripts/experiments.json')):
32 32 print(tup['name'])
33 33 exp = Experiment.objects(code=tup['code']).modify(
34 34 upsert=True,
35 35 new=True,
36 36 set__code=tup['code'],
37 37 set__name=tup['name'],
38 38 )
39 39 exp.save()
40 40
41 41 def update(buffer):
42 42 dt = datetime.utcfromtimestamp(buffer['time'])
43 43 exp = ExpMeta.objects(code=buffer['exp_code'], date=dt.date()).modify(
44 44 upsert=True,
45 45 new=True,
46 46 set__code=buffer['exp_code'],
47 47 set__date=dt.date(),
48 48 set__yrange = buffer['yrange'],
49 49 set__xrange = buffer['xrange'],
50 50 set__interval = buffer['interval'],
51 51 set__localtime = buffer['localtime'],
52 52 set__plots = buffer['data'].keys()
53 53 )
54 54 exp.save()
55 55
56 56 data = ExpData.objects(expmeta=exp, time=buffer['time']).modify(
57 57 upsert=True,
58 58 new=True,
59 59 set__expmeta = exp,
60 60 set__time = buffer['time'],
61 61 set__data = buffer['data']
62 62 )
63 63
64 64 data.save()
65 65
66 66 return exp.id
67 67
68 68 def main():
69 69 print('Starting ZMQ server...')
70 70 while True:
71 71 buffer = receiver.recv_json()
72 72 code = update(buffer)
73 73 for plot in buffer['data']:
74 74 dum = buffer.copy()
75 75 dum['time'] = [buffer['time']]
76 dum[plot] = buffer['data'][plot]
76 if plot=='noise':
77 dum[plot] = [[x] for x in buffer['data'][plot]]
78 else:
79 dum[plot] = buffer['data'][plot]
77 80 dum.pop('data')
78 81 dum.pop('exp_code')
79 82 channel.send_group(u'{}_{}'.format(code, plot), {'text': simplejson.dumps(dum, ignore_nan=True)})
80 83 print('Sending...{} - {} bytes'.format(plot, len(str(dum))))
81 84
82 85 if __name__=='__main__':
83 86 loaddata()
84 87 main() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now