Split view_configs and modules into separate files

This commit is contained in:
Patrick McDonagh
2017-02-15 13:17:27 -06:00
parent 5c75bbeeab
commit 7713096eb1
5 changed files with 127 additions and 108 deletions

View File

@@ -87,5 +87,8 @@ def main(global_config, **settings):
config.add_route('values_time', '/values/time/{time}')
# JSON ROUTES
config.add_route('json_lastcard', "/json/lastcard")
config.scan()
return config.make_wsgi_app()

View File

@@ -0,0 +1,7 @@
from pyramid.view import view_config
# JSON
@view_config(route_name="json_lastcard", renderer="json")
def json_lastcard(request):
pass

View File

@@ -0,0 +1,36 @@
from math import ceil
class Pagination(object):
def __init__(self, page, per_page, total_count):
self.page = page
self.per_page = per_page
self.total_count = total_count
self.prev_num = page - 1
self.next_num = page + 1
@property
def pages(self):
return int(ceil(self.total_count / float(self.per_page)))
@property
def has_prev(self):
return self.page > 1
@property
def has_next(self):
return self.page < self.pages
def iter_pages(self, left_edge=2, left_current=2,
right_current=5, right_edge=2):
last = 0
for num in range(1, self.pages + 1):
if num <= left_edge or \
(num > self.page - left_current - 1 and
num < self.page + right_current) or \
num > self.pages - right_edge:
if last + 1 != num:
yield None
yield num
last = num

View File

@@ -0,0 +1,74 @@
from datetime import datetime, timedelta
from .pagination import Pagination
def get_lastest_tag_values(request):
latest_tag_values = []
latesttag_agg = request.db['wellData'].aggregate([
{
'$sort': {"tagname": 1, "timestamp": 1}
},
{
'$group': {
'_id': "$tagname",
'timestamp': {'$last': "$timestamp"},
'value': {'$last': "$currentValue"},
'max': {'$last': "$maxDailyValue"},
'min': {'$last': "$minDailyValue"},
'average': {'$last': "$dailyAverage"},
'total': {'$last': "$dailyTotal"}
}
}
])
for t in latesttag_agg:
latest_tag_values.append(t)
return latest_tag_values
def get_latest_card(request):
try:
return list(request.db['cards'].find().sort("timestamp", -1).limit(1))[0]
except IndexError:
return []
def card_page(request):
page_num = 1
try:
page_num = int(request.matchdict['page_num'])
except KeyError:
pass
cards_date_start = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
try:
cards_date_start = datetime.strptime(request.matchdict['cards_date'], "%Y-%m-%d")
except KeyError:
pass
cards_date_end = cards_date_start + timedelta(days=1)
num_per_page = 100
num_cards = request.db['cards'].find({'timestamp': {'$lt': cards_date_end, '$gte': cards_date_start}}).count()
pages = ceil(num_cards / num_per_page)
cards = request.db['cards'].find({'timestamp': {'$lt': cards_date_end, '$gte': cards_date_start}}).sort("timestamp", -1).skip(num_per_page * (page_num - 1)).limit(num_per_page)
return {'cards': list(cards), 'pagination': Pagination(page_num, num_per_page, num_cards), 'cards_date': cards_date_start.strftime("%Y-%m-%d"), 'navgroup': 'cards'}
def get_all_tags_between(request):
tag_data = []
grouped_tags = request.db['wellData'].aggregate([
{
'$sort': {"tagname": 1, "timestamp": 1}
},
{
'$group': {
'_id': "$tagname",
'timestamps': {'$push': "$timestamp"},
'currentValues': {'$push': "$currentValue"}
}
}
])
for t in grouped_tags:
tag_data.append({"tagname": t['_id'], "timestamps": list(map(lambda a: a.strftime("%Y-%m-%d %H:%M:%S.%fZ"), t['timestamps'])), "currentValues": t['currentValues']})
return tag_data

View File

@@ -1,69 +1,6 @@
from pyramid.view import view_config
from datetime import datetime, timedelta
from math import ceil
class Pagination(object):
def __init__(self, page, per_page, total_count):
self.page = page
self.per_page = per_page
self.total_count = total_count
self.prev_num = page - 1
self.next_num = page + 1
@property
def pages(self):
return int(ceil(self.total_count / float(self.per_page)))
@property
def has_prev(self):
return self.page > 1
@property
def has_next(self):
return self.page < self.pages
def iter_pages(self, left_edge=2, left_current=2,
right_current=5, right_edge=2):
last = 0
for num in range(1, self.pages + 1):
if num <= left_edge or \
(num > self.page - left_current - 1 and
num < self.page + right_current) or \
num > self.pages - right_edge:
if last + 1 != num:
yield None
yield num
last = num
def get_lastest_tag_values(request):
latest_tag_values = []
latesttag_agg = request.db['wellData'].aggregate([
{
'$sort': {"tagname": 1, "timestamp": 1}
},
{
'$group': {
'_id': "$tagname",
'timestamp': {'$last': "$timestamp"},
'value': {'$last': "$currentValue"},
'max': {'$last': "$maxDailyValue"},
'min': {'$last': "$minDailyValue"},
'average': {'$last': "$dailyAverage"},
'total': {'$last': "$dailyTotal"}
}
}
])
for t in latesttag_agg:
latest_tag_values.append(t)
return latest_tag_values
def get_latest_card(request):
return list(request.db['cards'].find().sort("timestamp", -1).limit(1))[0]
from .view_helpers import *
@view_config(route_name='home', renderer='templates/dashboard.jinja2')
@@ -71,29 +8,6 @@ def my_view(request):
return {'project': 'POC Web Interface', 'navgroup': 'dashboard', 'tag_values': get_lastest_tag_values(request), 'latest_card': get_latest_card(request)}
def card_page(request):
page_num = 1
try:
page_num = int(request.matchdict['page_num'])
except KeyError:
pass
cards_date_start = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
try:
cards_date_start = datetime.strptime(request.matchdict['cards_date'], "%Y-%m-%d")
except KeyError:
pass
cards_date_end = cards_date_start + timedelta(days=1)
num_per_page = 100
num_cards = request.db['cards'].find({'timestamp': {'$lt': cards_date_end, '$gte': cards_date_start}}).count()
pages = ceil(num_cards / num_per_page)
cards = request.db['cards'].find({'timestamp': {'$lt': cards_date_end, '$gte': cards_date_start}}).sort("timestamp", -1).skip(num_per_page * (page_num - 1)).limit(num_per_page)
return {'cards': list(cards), 'pagination': Pagination(page_num, num_per_page, num_cards), 'cards_date': cards_date_start.strftime("%Y-%m-%d"), 'navgroup': 'cards'}
@view_config(route_name='cards', renderer='templates/datelist.jinja2')
def cards(request):
datelist = []
@@ -123,28 +37,13 @@ def cards_page(request):
@view_config(route_name='card_single', renderer='templates/cardsingle.jinja2')
def card_single(request):
card = list(request.db['cards'].find({"strokeNumber": int(request.matchdict['stroke_number'])}))
return {"card": card[0], 'navgroup': 'cards'}
card = {}
try:
card = list(request.db['cards'].find({"strokeNumber": int(request.matchdict['stroke_number'])}))[0]
except IndexError:
pass
def get_all_tags_between(request):
tag_data = []
grouped_tags = request.db['wellData'].aggregate([
{
'$sort': {"tagname": 1, "timestamp": 1}
},
{
'$group': {
'_id': "$tagname",
'timestamps': {'$push': "$timestamp"},
'currentValues': {'$push': "$currentValue"}
}
}
])
for t in grouped_tags:
tag_data.append({"tagname": t['_id'], "timestamps": list(map(lambda a: a.strftime("%Y-%m-%d %H:%M:%S.%fZ"), t['timestamps'])), "currentValues": t['currentValues']})
return tag_data
return {"card": card, 'navgroup': 'cards'}
@view_config(route_name='values_all', renderer="templates/valuesall.jinja2")