##// END OF EJS Templates
New template, clean code, workig for realtime
Juan C. Espinoza -
r21:dc5f8680b6e1
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,56 +1,81
1 1 #!/usr/bin/python
2 2 # -*- coding: UTF-8 -*-
3 3 import os
4 4 import json
5 5
6 from datetime import datetime
6 from datetime import datetime, timedelta
7 7
8 8 from pymongo import MongoClient
9 9
10 10 from channels.handler import AsgiHandler
11 11 from channels.auth import channel_session_user
12 12 from channels import Group
13 13 # Here we create the db named "dbplots"
14 14 host = os.environ.get('HOST_MONGO', 'localhost')
15 15 CLIENT = MongoClient('{}:27017'.format(host))
16 16 DB = CLIENT['dbplots']
17 17
18 18 # Connected to websocket.connect
19 def ws_connect(message, code=None, plot=None):
20 message.reply_channel.send({'accept': True})
21 if plot == 'main':
19 def ws_connect(message, id, code, plot):
20
21 if id == 'main':
22 22 Group('main').add(message.reply_channel)
23 23 print('New main connection')
24 elif id == 'realtime':
25 Group('{}_{}'.format(code, plot)).add(message.reply_channel)
26 print('New connection from: {}, Group: {}_{}'.format(message.content['client'][0], code, plot))
24 27 else:
25 pk = message.content['query_string'].decode().split('=')[1]
26 Group('{}_{}'.format(pk, plot)).add(message.reply_channel)
27 print('New connection from: {}, Group: {}_{}'.format(message.content['client'][0], pk, plot))
28 print('New connection from: {}, history, id: {}'.format(message.content['client'][0], id))
29 message.reply_channel.send({
30 'accept': True
31 })
28 32
29 def ws_message(message, code, plot):
33 def ws_message(message, id, code, plot):
30 34 # Accept the incoming connection
31 35 dt = datetime.strptime(str(json.loads(message.content['text'])['date']), '%d/%m/%Y')
36 exp0 = DB.exp_meta.find_one({'code': int(code), 'date': dt-timedelta(days=1)})
32 37 exp = DB.exp_meta.find_one({'code': int(code), 'date': dt})
33
38 print('New request for id={}'.format(id))
34 39 if exp and plot in exp['plots']:
35 40 if plot == 'spc':
36 41 datas = DB.exp_data.find({'expmeta': exp['_id']}, ['time', 'data']).sort('time', -1).limit(1)[0]
37 42 exp['time'] = [datas['time']]
38 43 exp['spc'] = datas['data']['spc']
39 44 exp['rti'] = datas['data']['rti']
40 45 exp['noise'] = datas['data']['noise']
41 46 else:
42 datas = DB.exp_data.find({'expmeta': exp['_id']}, ['time', 'data']).sort('time', 1)
47 last = DB.exp_data.find_one({'expmeta': exp['_id']}, ['time'], sort=[('time', -1)])
48 if exp0:
49 datas = DB.exp_data.find(
50 {
51 'expmeta': {'$in': [exp0['_id'], exp['_id']]},
52 'time': {'$gt': last['time']-12*60*60}
53 },
54 ['time', 'data'],
55 sort=[('time', 1)],
56 limit=720
57 )
58 else:
59 datas = DB.exp_data.find(
60 {
61 'expmeta': exp['_id'],
62 'time': {'$gt': last['time']-12*60*60}
63 },
64 ['time', 'data'],
65 sort=[('time', 1)],
66 limit=720
67 )
43 68 dum = [(d['time'], d['data'][plot]) for d in datas]
44 69 exp['time'] = [d[0] for d in dum]
45 70 dum = [d[1] for d in dum]
46 71 exp[plot] = [t for t in map(list, zip(*dum))]
47 print([datetime.fromtimestamp(t) for t in exp['time']])
72
48 73 exp.pop('date', None)
49 74 exp.pop('_id', None)
50 75 message.reply_channel.send({'text': json.dumps(exp)})
51 76 else:
52 77 message.reply_channel.send({'text': json.dumps({'interval': 0})})
53 78
54 79 # Connected to websocket.disconnect
55 def ws_disconnect(message, code, plot):
80 def ws_disconnect(message, id, code, plot):
56 81 Group('{}_{}'.format(code, plot)).discard(message.reply_channel)
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (2050 lines changed) Show them Hide them
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now