Skip to content

Commit e543342

Browse files
Project files
1 parent 0400dd7 commit e543342

24 files changed

+512
-0
lines changed

Diff for: .gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.env
2+
.idea
3+
*.pyc
4+
*.db
5+
pgdata/
6+
data/

Diff for: Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:3.6
2+
ENV PYTHONUNBUFFERED 1
3+
ADD requirements.txt /tmp/requirements.txt
4+
RUN apt-get update && \
5+
apt-get install -y python3-gdal postgis && \
6+
pip install -r /tmp/requirements.txt

Diff for: docker-compose.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '2'
2+
services:
3+
4+
web:
5+
build: .
6+
restart: always
7+
ports:
8+
- "0.0.0.0:8030:8000"
9+
container_name: graphic-web
10+
env_file: .env
11+
volumes:
12+
- ./:/opt/app
13+
working_dir: /opt/app/graphictest
14+
#entrypoint: ['tail', '-f', '/dev/null']
15+
entrypoint: ['/bin/bash', '../init.sh']
16+
links:
17+
- db
18+
19+
db:
20+
image: mdillon/postgis:9.4
21+
volumes:
22+
- ./pgdata:/var/lib/postgresql/data
23+
env_file: .env
24+
container_name: graphic-db

Diff for: env_default

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
POSTGRES_USER=graphic
2+
POSTGRES_DB=graphic
3+
POSTGRES_PASSWORD=P9Jvf6vk
4+
POSTGRES_HOST=db
5+
POSTGRES_PORT=5432
6+
7+
DATA_DIR='../data'

Diff for: graphictest/graphictest/__init__.py

Whitespace-only changes.

Diff for: graphictest/graphictest/settings.py

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
"""
2+
Django settings for graphictest project.
3+
4+
Generated by 'django-admin startproject' using Django 2.0.5.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.0/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = 'ls%3+($v+zrfu)f%137!95rpiznd76(ihf_!03i!*2-)(7w1*i'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
'django.contrib.gis',
41+
'django_extensions',
42+
43+
'locations',
44+
]
45+
46+
MIDDLEWARE = [
47+
'django.middleware.security.SecurityMiddleware',
48+
'django.contrib.sessions.middleware.SessionMiddleware',
49+
'django.middleware.common.CommonMiddleware',
50+
'django.middleware.csrf.CsrfViewMiddleware',
51+
'django.contrib.auth.middleware.AuthenticationMiddleware',
52+
'django.contrib.messages.middleware.MessageMiddleware',
53+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
54+
]
55+
56+
ROOT_URLCONF = 'graphictest.urls'
57+
58+
TEMPLATES = [
59+
{
60+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
61+
'DIRS': [],
62+
'APP_DIRS': True,
63+
'OPTIONS': {
64+
'context_processors': [
65+
'django.template.context_processors.debug',
66+
'django.template.context_processors.request',
67+
'django.contrib.auth.context_processors.auth',
68+
'django.contrib.messages.context_processors.messages',
69+
],
70+
},
71+
},
72+
]
73+
74+
WSGI_APPLICATION = 'graphictest.wsgi.application'
75+
76+
77+
# Database
78+
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
79+
80+
DATABASES = {
81+
'default': {
82+
'ENGINE':"django.contrib.gis.db.backends.postgis",
83+
'NAME': os.environ.get('POSTGRES_DB'),
84+
'USER': os.environ.get('POSTGRES_USER'),
85+
'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
86+
'HOST': os.environ.get('POSTGRES_HOST', 'db'),
87+
'PORT': os.environ.get('POSTGRES_PORT', '5432'),
88+
}
89+
}
90+
91+
# Password validation
92+
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
93+
94+
AUTH_PASSWORD_VALIDATORS = [
95+
{
96+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
97+
},
98+
{
99+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
100+
},
101+
{
102+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
103+
},
104+
{
105+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
106+
},
107+
]
108+
109+
110+
# Internationalization
111+
# https://docs.djangoproject.com/en/2.0/topics/i18n/
112+
113+
LANGUAGE_CODE = 'en-us'
114+
115+
TIME_ZONE = 'UTC'
116+
117+
USE_I18N = True
118+
119+
USE_L10N = True
120+
121+
USE_TZ = True
122+
123+
124+
# Static files (CSS, JavaScript, Images)
125+
# https://docs.djangoproject.com/en/2.0/howto/static-files/
126+
127+
STATIC_URL = '/static/'
128+
DATA_DIR_ = os.environ.get('DATA_DIR', '../data')
129+
DATA_DIR = os.path.join(BASE_DIR, DATA_DIR_)
130+
131+
132+
GDAL_LIBRARY_PATH = '/usr/lib/libgdal.so.1.17.1'

Diff for: graphictest/graphictest/urls.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""graphictest URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/2.0/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
19+
urlpatterns = [
20+
path('admin/', admin.site.urls),
21+
]

Diff for: graphictest/graphictest/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for graphictest project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "graphictest.settings")
15+
16+
application = get_wsgi_application()

Diff for: graphictest/locations/__init__.py

Whitespace-only changes.

Diff for: graphictest/locations/admin.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
кfrom django.contrib import admin
2+
from locations.models import Location, Measurement
3+
4+
5+
@admin.register(Location)
6+
class LocationAdmin(admin.ModelAdmin):
7+
pass
8+
9+
10+
@admin.register(Measurement)
11+
class MeasurementAdmin(admin.ModelAdmin):
12+
pass

Diff for: graphictest/locations/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class LocationsConfig(AppConfig):
5+
name = 'locations'

Diff for: graphictest/locations/helper.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import os
2+
import matplotlib
3+
4+
matplotlib.use('Agg')
5+
6+
from django.conf import settings
7+
import matplotlib.pyplot as plt
8+
import matplotlib.dates as mdates
9+
from matplotlib.dates import date2num
10+
from locations.models import Location, Measurement
11+
12+
FILE_FORMAT = '{location}-{proxy_name}-{type}.{ext}'
13+
14+
15+
class LocationsGraph(object):
16+
def __init__(self, **kwargs):
17+
self.from_date = kwargs.get('from_date')
18+
self.to_date = kwargs.get('to_date')
19+
self.from_date_p = date2num(kwargs.get('from_date'))
20+
self.to_date_p = date2num(kwargs.get('to_date'))
21+
22+
def get_graphs(self):
23+
# Get all locations
24+
for location in Location.objects.all():
25+
city = location.city.replace(' ', '_')
26+
# Get all proxies in location
27+
for proxy in Measurement.objects.filter(
28+
location=location).values_list('proxy',
29+
flat=True).distinct():
30+
img_meta = {
31+
'location': city,
32+
'proxy_name': proxy,
33+
'ext': 'png'
34+
}
35+
36+
y1 = []
37+
y2 = []
38+
x = []
39+
for data in Measurement.objects.filter(
40+
location=location,
41+
proxy=proxy,
42+
date__gte=self.from_date,
43+
date__lte=self.to_date):
44+
y1.append(float(data.avg_resp_proxy))
45+
y2.append(float(data.avg_resp_direct))
46+
x.append(date2num(data.date))
47+
48+
# If none data is available - just skip creating data
49+
if y1:
50+
img_meta['type'] = 'proxy'
51+
filename_proxy = os.path.join(settings.DATA_DIR,
52+
FILE_FORMAT.format(
53+
**img_meta))
54+
print('Create file {}'.format(filename_proxy))
55+
plt.gcf().clear()
56+
plt.gca().xaxis.set_major_formatter(
57+
mdates.DateFormatter('%m/%d/%y'))
58+
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
59+
# If data consists of one measurement - we draw only one dot
60+
if len(y1) == 1:
61+
plt.plot(x, y1, 'bo')
62+
else:
63+
plt.plot(x, y1, 'b-')
64+
plt.title('avg_resp_proxy')
65+
plt.xlabel('dates')
66+
plt.ylabel('milliseconds')
67+
plt.gcf().autofmt_xdate()
68+
plt.savefig(filename_proxy)
69+
70+
# If none data is available - just skip creating data
71+
if y2:
72+
img_meta['type'] = 'direct'
73+
filename_direct = os.path.join(settings.DATA_DIR,
74+
FILE_FORMAT.format(
75+
**img_meta))
76+
print('Create file {}'.format(filename_direct))
77+
plt.gcf().clear()
78+
plt.gca().xaxis.set_major_formatter(
79+
mdates.DateFormatter('%m/%d/%y'))
80+
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
81+
# If data consists of one measurement - we draw only one dot
82+
if len(y1) == 1:
83+
plt.plot(x, y2, 'bo')
84+
else:
85+
plt.plot(x, y2, 'b-')
86+
plt.title('avg_resp_direct')
87+
plt.xlabel('dates')
88+
plt.ylabel('milliseconds')
89+
plt.gcf().autofmt_xdate()
90+
plt.savefig(filename_direct)

Diff for: graphictest/locations/management/__init__.py

Whitespace-only changes.

Diff for: graphictest/locations/management/commands/__init__.py

Whitespace-only changes.
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import datetime
2+
from optparse import make_option
3+
from django.core.management import BaseCommand, CommandError
4+
from locations.helper import LocationsGraph
5+
6+
7+
class Command(BaseCommand):
8+
9+
help = """Command for images build"""
10+
11+
12+
def add_arguments(selfself, parser):
13+
parser.add_argument(
14+
'--today',
15+
action='store_true',
16+
dest='today',
17+
help='Get data till today'
18+
)
19+
20+
parser.add_argument(
21+
'--date',
22+
action='store',
23+
dest='date',
24+
help='Get data till given date'
25+
)
26+
27+
parser.add_argument(
28+
'--days',
29+
action='store',
30+
dest='days',
31+
help='Get data for given days amount'
32+
)
33+
34+
def handle(self, *args, **options):
35+
if not options['today'] and not options['date']:
36+
return 'You need to set --today or --date=<dd/mm/yyyy> option for target date'
37+
38+
if not options['days']:
39+
return 'You need to set days amount with option --days=<amount>'
40+
41+
try:
42+
days_amount = int(options['days'])
43+
except ValueError:
44+
return 'Please use number as days amount'
45+
46+
if options['today']:
47+
to_date = datetime.datetime.utcnow()
48+
49+
if options['date']:
50+
try:
51+
to_date = datetime.datetime.strptime(options['date'], '%d/%m/%Y')
52+
except ValueError:
53+
return 'Please use date in format dd/mm/yyyy'
54+
55+
from_date = to_date - datetime.timedelta(days=days_amount)
56+
print('Create graph data from {} to {}'.format(from_date.strftime('%d/%m/%Y'), to_date.strftime('%d/%m/%Y')))
57+
58+
# Get graphs for given date range
59+
my_graph = LocationsGraph(from_date=from_date, to_date=to_date)
60+
my_graph.get_graphs()

0 commit comments

Comments
 (0)