From 6b826d8e0fa5edfc22d4418eb1e44766d9d2b4ad Mon Sep 17 00:00:00 2001 From: sessionm21 Date: Wed, 13 May 2020 02:40:44 +0100 Subject: [PATCH 1/2] add gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..08cf93c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.swp +*.sqlite +__pycache__ From f2cad65626abf5d9e7cc0ef6689e4e7198ef8f9a Mon Sep 17 00:00:00 2001 From: sessionm21 Date: Wed, 13 May 2020 02:40:59 +0100 Subject: [PATCH 2/2] add stimulus activation report --- fgs/model.py | 2 ++ fgs/views.py | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/fgs/model.py b/fgs/model.py index 34b9e1a..b47bc90 100644 --- a/fgs/model.py +++ b/fgs/model.py @@ -26,10 +26,12 @@ class DataPoint(db.Model): class StimulusActivation(db.Model): id = db.Column(db.Integer, primary_key=True) + collar_id = db.Column(db.Integer, db.ForeignKey('collar.id')) longitude = db.Column(db.Float(precision=10)) latitude = db.Column(db.Float(precision=10)) volume_level = db.Column(db.Integer) voltage_level = db.Column(db.Integer) + datetime = db.Column(db.DateTime) class User(db.Model): id = db.Column(db.Integer, primary_key=True) diff --git a/fgs/views.py b/fgs/views.py index 162ad26..e35ccaf 100644 --- a/fgs/views.py +++ b/fgs/views.py @@ -4,6 +4,7 @@ from .model import * from flask import g, jsonify, request, abort from sqlalchemy import func import geopy.distance +from datetime import datetime, timedelta @app.route('/') def index(): @@ -49,7 +50,7 @@ def collars_distance(): distance_kilometers = 0 coords_pairs = list(map(lambda e : (e.longitude, e.latitude), coords)) - coord_last = coords_pairs.pop() + coord_last = coords_pairs.pop(0) for coord in coords_pairs: distance_kilometers += geopy.distance.VincentyDistance(coord_last,coord).km coord_last = coord @@ -79,4 +80,12 @@ def collar_detail(id): collar = Collar.query.filter_by(id=id).first() if collar is None: abort(404) - return jsonify({'id': collar.id, 'name': collar.name }) + # get stimulus activation reports from within last 24 hours + since = datetime.now() - timedelta(hours=24) + stimulus_points = StimulusActivation.query.\ + filter_by(collar_id=id).\ + filter(StimulusActivation.datetime < since).\ + order_by(StimulusActivation.datetime.desc()) + n_stimulus = stimulus_points.count() + + return jsonify({'id': collar.id, 'name': collar.name, 'stimilus': n_stimulus })