Skip to content

Commit

Permalink
Resolve #56
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewyazura committed Jan 3, 2021
1 parent 28513cf commit 91db3e5
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions resources/Map.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
from datetime import datetime, timedelta
from os import environ

from common.convert_types_to_names import convert_types_to_names
from flask_restful import Resource
from models import AreaModel
from models import AreaModel, SensorModel, SensorDataModel
from sqlalchemy import desc


class Map(Resource):
def get(self):
maxdate = datetime.now() - timedelta(hours=int(environ.get('MAX_RECORD_HOURS')))
latest_records = self._get_latest_records()
aqi_records = []

areas = AreaModel.query.order_by(desc(AreaModel.created)).filter(
AreaModel.created >= maxdate).all()
for record in latest_records:
aqi_record = AreaModel.query \
.order_by(
AreaModel.created.desc()
).filter_by(
latitude=record.latitude
).filter_by(
longitude=record.longitude
).first()

areas_coords = []
areas_list = []
if aqi_record:
aqi_records.append(aqi_record.as_dict())

for area in areas:
if (area.latitude, area.longitude) in areas_coords:
return aqi_records

def _get_latest_records(self):
sensors = SensorModel.query.all()

latest_records = []
for sensor in sensors:
record = SensorDataModel.query.order_by(SensorDataModel.recorded.desc()).filter_by(
internal_id=sensor.external_id).first()

if not record:
continue

areas_coords.append((area.latitude, area.longitude))
areas_list.append(convert_types_to_names(area))
timedelta = datetime.now() - record.recorded
if (timedelta.seconds // 3600) <= int(environ.get('MAX_RECORD_HOURS')):
latest_records.append(record)

return areas_list
return latest_records

0 comments on commit 91db3e5

Please sign in to comment.