calculate total distance traveled

This commit is contained in:
sessionm21 2020-05-13 01:29:40 +01:00
parent 09cbae3233
commit 7a991f697a
9 changed files with 24 additions and 0 deletions

BIN
fgs/.model.py.swp Normal file

Binary file not shown.

BIN
fgs/.views.py.swo Normal file

Binary file not shown.

BIN
fgs/.views.py.swp Normal file

Binary file not shown.

BIN
fgs/__init__.pyc Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -3,6 +3,7 @@ from .jwt import jwt_required
from .model import * from .model import *
from flask import g, jsonify, request, abort from flask import g, jsonify, request, abort
from sqlalchemy import func from sqlalchemy import func
import geopy.distance
@app.route('/') @app.route('/')
def index(): def index():
@ -37,6 +38,29 @@ def collars():
return jsonify(active_collars) return jsonify(active_collars)
@app.route('/collars/stats/distance')
@jwt_required
def collars_distance():
active_collars = []
for collar in Collar.query.filter_by(active=True).all():
coords = DataPoint.query.filter_by(collar_id=collar.id).\
order_by(DataPoint.datetime.desc())
distance_kilometers = 0
coords_pairs = list(map(lambda e : (e.longitude, e.latitude), coords))
coord_last = coords_pairs.pop()
for coord in coords_pairs:
distance_kilometers += geopy.distance.VincentyDistance(coord_last,coord).km
coord_last = coord
active_collars.append(
{'id': collar.id, 'name': collar.name,
'distance': distance_kilometers }
)
return jsonify(active_collars)
@app.route('/collars/<int:id>/history') @app.route('/collars/<int:id>/history')
@jwt_required @jwt_required
def collar_history(id): def collar_history(id):