##// END OF EJS Templates
Docker working
Juan C. Espinoza -
r1:f2324aa7e45d
parent child
Show More
@@ -0,0 +1,2
1 HOST_MONGO=mongo
2 HOST_REDIS=redis No newline at end of file
@@ -1,7 +1,12
1 1 FROM python:2.7-slim
2 2 RUN mkdir /app
3 3 WORKDIR /app
4 4 ADD requirements.txt ./requirements.txt
5 RUN pip install -r requirements.txt
5 RUN apt-get clean && apt-get update && apt-get install -y --no-install-recommends \
6 g++ \
7 gcc \
8 && pip install -r requirements.txt \
9 && apt-get purge -y --auto-remove gcc g++\
10 && rm -rf /var/lib/apt/lists/*
6 11 COPY . /app/
7 12 No newline at end of file
@@ -1,47 +1,55
1 1 version: '2'
2 2
3 3 services:
4 4 web:
5 container_name: 'realtime_web'
5 6 build: .
6 7 image: realtime
7 8 command: python manage.py runserver 0.0.0.0:8000
9 env_file: .env
8 10 ports:
9 11 - "8000:8000"
10 12 links:
11 13 - redis
12 14 - mongo
15 volumes:
16 - './:/app'
13 17 depends_on:
14 18 - redis
15 19 - mongo
16 20
17 21 zmq_server:
22 container_name: 'zmq_server'
18 23 image: 'realtime'
19 24 ports:
20 25 - '127.0.0.1:4444:4444'
21 command: 'python scripts/server.py'
26 command: 'python -u scripts/server.py'
27 env_file: .env
22 28 links:
23 29 - redis
24 30 - mongo
31 volumes:
32 - './:/app'
25 33 depends_on:
26 34 - web
27 35
28 36 redis:
29 37 container_name: 'redis'
30 38 image: 'redis:3.2-alpine'
31 39 ports:
32 40 - '127.0.0.1:6379:6379'
33 41 volumes:
34 42 - 'redisdata:/data'
35 43
36 44 mongo:
37 45 container_name: 'mongo'
38 46 image: 'mongo:3.3'
39 47 command: '--storageEngine wiredTiger'
40 48 ports:
41 49 - '127.0.0.1:27017:27017'
42 50 volumes:
43 51 - 'mongodata:/data/db'
44 52
45 53 volumes:
46 54 redisdata:
47 55 mongodata: No newline at end of file
@@ -1,53 +1,51
1
1 import os
2 2 import json
3 3 import numpy as np
4 4 from datetime import datetime
5 import mongoengine
5
6 6 from pymongo import MongoClient
7 7
8 8 from models import Experiment, Data
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
14 from profilehooks import profile
15
16 # mongoengine.connect('dbplots')
17 CLIENT = MongoClient('mongo:27017')
14 host = os.environ.get('HOST_MONGO', 'localhost')
15 CLIENT = MongoClient('{}:27017'.format(host))
18 16 DB = CLIENT['dbplots']
19 17
20 18 # Connected to websocket.connect
21 19 def ws_connect(message, code, plot):
22 20 # Accept the incoming connection
23 21 message.reply_channel.send({'accept': True})
24 22 # Add them to the chat group
25 23 Group('{}_{}'.format(code, plot)).add(message.reply_channel)
26 24
27 25 def ws_message(message, code, plot):
28 26 # Accept the incoming connection
29 27
30 28 dt = datetime.strptime(str(json.loads(message.content['text'])['date']), '%d/%m/%Y')
31 29 e = DB.experiment.find_one({'date': dt})
32 30 if e:
33 31 if plot == 'spc':
34 32 datas = DB.data.find({'experiment': e['_id']}, ['time', 'data']).sort('time', -1).limit(1)[0]
35 33 e['time'] = [datas['time']]
36 34 e['spc'] = datas['data']['spc']
37 35 e['rti'] = datas['data']['rti']
38 36 e['noise'] = datas['data']['noise']
39 37 else:
40 38 datas = DB.data.find({'experiment': e['_id']}, ['time', 'data']).sort('time', 1)
41 39 dum = [(d['time'], d['data'][plot]) for d in datas]
42 40 e['time'] = [d[0] for d in dum]
43 dum = np.array([d[1] for d in dum])
44 e[plot] = np.swapaxes(dum, 0, 1).tolist()
41 dum = [d[1] for d in dum]
42 e[plot] = map(list, zip(*dum))
45 43 e.pop('date', None)
46 44 e.pop('_id', None)
47 45 message.reply_channel.send({'text': json.dumps(e)})
48 46 else:
49 47 message.reply_channel.send({'text': json.dumps({'interval': 0})})
50 48
51 49 # Connected to websocket.disconnect
52 50 def ws_disconnect(message, code, plot):
53 51 Group('{}_{}'.format(code, plot)).discard(message.reply_channel)
@@ -1,72 +1,81
1 1 # -*- coding: utf-8 -*-
2 2 from __future__ import unicode_literals
3 3
4 import os
4 5 from datetime import datetime
5 6
6 7 from django import forms
7 8 from django.contrib import messages
8 9 from django.utils.safestring import mark_safe
9 10 from django.shortcuts import render
10 11
11 12 from bootstrap3_datetime.widgets import DateTimePicker
12 13
13 14 from pymongo import MongoClient
14 15
15 CLIENT = MongoClient('mongo:27017')
16 host = os.environ.get('HOST_MONGO', 'localhost')
17
18 CLIENT = MongoClient('{}:27017'.format(host))
16 19 DB = CLIENT['dbplots']
17 20
18 21 # Forms
19 22 class SearchForm(forms.Form):
20 23
21 24 experiment = forms.ChoiceField()
22 25 plot = forms.ChoiceField(
23 26 choices = [(0, 'Select Plot'), ('rti', 'RTI'),('spc', 'Spectra'),('noise', 'Noise')]
24 27 )
25 28 date = forms.DateField(
26 29 widget=DateTimePicker(options={"format": "DD/MM/YYYY"})
27 30 )
28 31
29 32 def __init__(self, *args, **kwargs):
30 33
31 34 exp_choices = kwargs.pop('exp_choices', [])
32 35 super(SearchForm, self).__init__(*args, **kwargs)
33 36 self.fields['experiment'].choices = [(0, 'Select Experiment')] + exp_choices
34 37
35 38 # Create your views here.
36 39 def main(request, code=None, plot=None):
37 40
38 41 initial = {}
39 42 date = request.GET.get('date', datetime.now().strftime('%d/%m/%Y'))
40 43 initial['date'] = date
41 44
42 45 if code is not None:
43 46 initial['experiment'] = code
44 47 if plot is not None:
45 48 initial['plot'] = plot
46 49
50 print 'hola'
47 51 codes = DB.experiment.find().distinct('code')
52 print codes
48 53 exps = [DB.experiment.find_one({'code': c}, ['name']) for c in codes]
54 print exps
49 55 names = [q['name'] for q in exps]
56 print names
50 57 form = SearchForm(
51 58 initial = initial,
52 59 exp_choices = [(e[0], e[1]) for e in zip(codes, names)]
53 60 )
54 61
55 62 kwargs = {
56 63 'code': code,
57 64 'plot': plot,
58 65 'date': date,
59 66 'form': form,
60 67 }
61 68
62 if codes:
69 if code and codes:
63 70 kwargs['title'] = [t[1] for t in zip(codes, names) if t[0]==int(code)][0]
71 else:
72 kwargs['title'] = 'JRO'
64 73
65 74 if plot == 'rti':
66 75 return render(request, 'rti.html', kwargs)
67 76 elif plot == 'spc':
68 77 return render(request, 'spectra.html', kwargs)
69 78 elif plot == 'noise':
70 79 return render(request, 'scatter.html', kwargs)
71 80 else:
72 81 return render(request, 'base.html', kwargs) No newline at end of file
@@ -1,133 +1,135
1 1 """
2 2 Django settings for realtime project.
3 3
4 4 Generated by 'django-admin startproject' using Django 1.11.7.
5 5
6 6 For more information on this file, see
7 7 https://docs.djangoproject.com/en/1.11/topics/settings/
8 8
9 9 For the full list of settings and their values, see
10 10 https://docs.djangoproject.com/en/1.11/ref/settings/
11 11 """
12 12
13 13 import os
14 14
15 15 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16 16 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 17
18 18
19 19 # Quick-start development settings - unsuitable for production
20 20 # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
21 21
22 22 # SECURITY WARNING: keep the secret key used in production secret!
23 23 SECRET_KEY = '(htjox+v27sda)$61vjhuveeh_q$gw5+z^cn71!h2m-!!s!7ra'
24 24
25 25 # SECURITY WARNING: don't run with debug turned on in production!
26 26 DEBUG = True
27 27
28 28 ALLOWED_HOSTS = ['*']
29 29
30 30
31 31 # Application definition
32 32
33 33 INSTALLED_APPS = [
34 34 'django.contrib.admin',
35 35 'django.contrib.auth',
36 36 'django.contrib.contenttypes',
37 37 'django.contrib.sessions',
38 38 'django.contrib.messages',
39 39 'django.contrib.staticfiles',
40 40 'bootstrap3',
41 41 'channels',
42 42 'plotter',
43 43 ]
44 44
45 45 MIDDLEWARE = [
46 46 'django.middleware.security.SecurityMiddleware',
47 47 'django.contrib.sessions.middleware.SessionMiddleware',
48 48 'django.middleware.common.CommonMiddleware',
49 49 'django.middleware.csrf.CsrfViewMiddleware',
50 50 'django.contrib.auth.middleware.AuthenticationMiddleware',
51 51 'django.contrib.messages.middleware.MessageMiddleware',
52 52 'django.middleware.clickjacking.XFrameOptionsMiddleware',
53 53 ]
54 54
55 55 ROOT_URLCONF = 'realtime.urls'
56 56
57 57 TEMPLATES = [
58 58 {
59 59 'BACKEND': 'django.template.backends.django.DjangoTemplates',
60 60 'DIRS': [],
61 61 'APP_DIRS': True,
62 62 'OPTIONS': {
63 63 'context_processors': [
64 64 'django.template.context_processors.debug',
65 65 'django.template.context_processors.request',
66 66 'django.contrib.auth.context_processors.auth',
67 67 'django.contrib.messages.context_processors.messages',
68 68 ],
69 69 },
70 70 },
71 71 ]
72 72
73 73 WSGI_APPLICATION = 'realtime.wsgi.application'
74 74
75 75
76 76 # Database
77 77 # https://docs.djangoproject.com/en/1.11/ref/settings/#databases
78 78
79 79 DATABASES = {
80 80 'default': {
81 81 'ENGINE': 'django.db.backends.sqlite3',
82 82 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
83 83 }
84 84 }
85 85
86 86
87 87 # Password validation
88 88 # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
89 89
90 90 AUTH_PASSWORD_VALIDATORS = [
91 91 {
92 92 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
93 93 },
94 94 {
95 95 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
96 96 },
97 97 {
98 98 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
99 99 },
100 100 {
101 101 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
102 102 },
103 103 ]
104 104
105 105
106 106 # Internationalization
107 107 # https://docs.djangoproject.com/en/1.11/topics/i18n/
108 108
109 109 LANGUAGE_CODE = 'en-us'
110 110
111 111 TIME_ZONE = 'UTC'
112 112
113 113 USE_I18N = True
114 114
115 115 USE_L10N = True
116 116
117 117 USE_TZ = True
118 118
119 119
120 120 # Static files (CSS, JavaScript, Images)
121 121 # https://docs.djangoproject.com/en/1.11/howto/static-files/
122 122
123 123 STATIC_URL = '/static/'
124 124
125 host = os.environ.get('HOST_REDIS', 'localhost')
126
125 127 CHANNEL_LAYERS = {
126 128 "default": {
127 129 "BACKEND": "asgi_redis.RedisChannelLayer",
128 130 "CONFIG": {
129 "hosts": [("redis", 6379)],
131 "hosts": [(host, 6379)],
130 132 },
131 133 "ROUTING": "realtime.routing.channel_routing",
132 134 },
133 135 } No newline at end of file
@@ -1,12 +1,12
1 1 asgi-redis==1.4.3
2 2 Django==1.11.7
3 3 django-bootstrap3==9.1.0
4 4 django-bootstrap3-datetimepicker-2==2.5.0
5 django-channels==0.7.0
5 channels==1.1.8
6 6 mongoengine==0.15.0
7 7 numpy==1.13.3
8 8 pymongo==3.5.1
9 9 pyzmq==16.0.3
10 10 redis==2.10.6
11 11 requests==2.18.4
12 12 simplejson==3.12.0
@@ -1,88 +1,94
1 1 import os
2 2 import sys
3 3 import simplejson
4 4 from datetime import datetime
5 5 import zmq
6 6 import redis
7 7 import asgi_redis
8 8 import mongoengine
9 9
10 10 sys.path.append('/app')
11 11 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "realtime.settings")
12 12
13 13 from plotter.models import Experiment, Data
14 14
15 mongoengine.connect('dbplots', host='mongo', port=27017)
15
16 host_mongo = os.environ.get('HOST_MONGO', 'localhost')
17 mongoengine.connect('dbplots', host=host_mongo, port=27017)
18
19
20 host_redis = os.environ.get('HOST_REDIS', 'localhost')
21 channel = asgi_redis.RedisChannelLayer(hosts=[(host_redis, 6379)])
16 22
17 23 context = zmq.Context()
18 24 receiver = context.socket(zmq.SUB)
19 25
20 receiver.bind("tcp://127.0.0.1:4444")
26 receiver.bind("tcp://0.0.0.0:4444")
21 27 receiver.setsockopt(zmq.SUBSCRIBE, '')
22 28
23 channel = asgi_redis.RedisChannelLayer(hosts=[('redis', 6379)])
29
24 30
25 31 def update_db(buffer):
26 32 dt = datetime.utcfromtimestamp(buffer['time'])
27 33 exp = Experiment.objects(code=buffer['exp_code'], date=dt.date()).first()
28 34 if exp is None:
29 35 exp = Experiment(
30 36 code=buffer['exp_code'],
31 37 date=dt.date(),
32 38 yrange = buffer['yrange'],
33 39 xrange = buffer['xrange'],
34 40 interval = buffer['interval'],
35 41 localtime = buffer['localtime'],
36 42 name = buffer['name'],
37 43 )
38 44 exp.save()
39 45
40 46 data = Data.objects(experiment=exp, time=buffer['time']).first()
41 47
42 48 if data is None:
43 49 data = Data(
44 50 experiment = exp,
45 51 time = buffer['time'],
46 52 data = buffer['data']
47 53 ).save()
48 54 new = True
49 55 else:
50 56 data.data = buffer['data']
51 57 data.save()
52 58 new = False
53 59
54 60 return new
55 print 'Waiting for messages...'
61 print 'Starting...'
56 62 while True:
57 63 buffer = receiver.recv_json()
58 64 if 'xrange' not in buffer:
59 65 buffer['xrange'] = []
60 66 if 'name' not in buffer:
61 67 buffer['name'] = 'Experiment'
62 68 if update_db(buffer):
63 69 dum = buffer.copy()
64 70 dum['time'] = [buffer['time']]
65 71 dum['rti'] = buffer['data']['rti']
66 72 # dum['noise'] = buffer['data']['noise']
67 73 dum.pop('data')
68 74 code = dum.pop('exp_code')
69 75 channel.send_group(u'{}_rti'.format(code), {'text': simplejson.dumps(dum, ignore_nan=True)})
70 76 print 'Sending...{} - {} bytes'.format('rti', len(str(dum)))
71 77
72 78 # dum = buffer.copy()
73 79 # dum['time'] = [buffer['time']]
74 80 # dum['rti'] = buffer['data']['rti']
75 81 # dum['spc'] = buffer['data']['spc']
76 82 # dum['noise'] = buffer['data']['noise']
77 83 # dum.pop('data')
78 84 # code = dum.pop('exp_code')
79 85 # channel.send_group(u'{}_spc'.format(code), {'text': simplejson.dumps(dum, ignore_nan=True)})
80 86 # print 'Sending...{} - {} bytes'.format('spc', len(str(dum)))
81 87
82 88 # dum = buffer.copy()
83 89 # dum['time'] = [buffer['time']]
84 90 # dum['noise'] = [[x] for x in buffer['data']['noise']]
85 91 # dum.pop('data')
86 92 # code = dum.pop('exp_code')
87 93 # channel.send_group(u'{}_noise'.format(code), {'text': simplejson.dumps(dum, ignore_nan=True)})
88 94 # print 'Sending...{} - {} bytes'.format('noise', len(str(dum)))
General Comments 0
You need to be logged in to leave comments. Login now