API & Client: fix

This commit is contained in:
Sam 2018-06-07 22:44:52 +02:00
parent 76b2d5aad2
commit e7e0a20f89
10 changed files with 56 additions and 30 deletions

View File

@ -1,7 +1,7 @@
import json import json
import os import os
import shutil import shutil
from datetime import datetime from datetime import datetime, timedelta
from fittrackee_api import appLog, db from fittrackee_api import appLog, db
from flask import Blueprint, current_app, jsonify, request, send_file from flask import Blueprint, current_app, jsonify, request, send_file
@ -41,8 +41,9 @@ def get_activities(auth_user_id):
Activity.sport_id == sport_id if sport_id else True, Activity.sport_id == sport_id if sport_id else True,
Activity.activity_date >= datetime.strptime(date_from, '%Y-%m-%d') Activity.activity_date >= datetime.strptime(date_from, '%Y-%m-%d')
if date_from else True, if date_from else True,
Activity.activity_date <= datetime.strptime(date_to, '%Y-%m-%d') Activity.activity_date < (
if date_to else True, datetime.strptime(date_to, '%Y-%m-%d') + timedelta(days=1)
) if date_to else True,
Activity.distance >= int(distance_from) if distance_from else True, Activity.distance >= int(distance_from) if distance_from else True,
Activity.distance <= int(distance_to) if distance_to else True, Activity.distance <= int(distance_to) if distance_to else True,
Activity.duration >= convert_in_duration(duration_from) Activity.duration >= convert_in_duration(duration_from)

View File

@ -43,8 +43,9 @@ def get_activities(user_id, type):
Activity.user_id == user_id, Activity.user_id == user_id,
Activity.activity_date >= datetime.strptime(date_from, '%Y-%m-%d') Activity.activity_date >= datetime.strptime(date_from, '%Y-%m-%d')
if date_from else True, if date_from else True,
Activity.activity_date <= datetime.strptime(date_to, '%Y-%m-%d') Activity.activity_date < (
if date_to else True, datetime.strptime(date_to, '%Y-%m-%d') + timedelta(days=1)
) if date_to else True,
Activity.sport_id == sport_id if sport_id else True, Activity.sport_id == sport_id if sport_id else True,
).order_by( ).order_by(
Activity.activity_date.asc() Activity.activity_date.asc()

View File

@ -87,15 +87,18 @@ class User(db.Model):
nb_activity = Activity.query.filter( nb_activity = Activity.query.filter(
Activity.user_id == self.id Activity.user_id == self.id
).count() ).count()
sports = db.session.query( sports = []
func.count(Activity.sport_id) total = (None, None)
).group_by( if nb_activity > 0:
Activity.sport_id sports = db.session.query(
).all() func.count(Activity.sport_id)
total = db.session.query( ).group_by(
func.sum(Activity.distance), Activity.sport_id
func.sum(Activity.duration) ).all()
).first() total = db.session.query(
func.sum(Activity.distance),
func.sum(Activity.duration)
).first()
return { return {
'id': self.id, 'id': self.id,
'username': self.username, 'username': self.username,

View File

@ -135,9 +135,9 @@ export const getMoreActivities = params => dispatch => FitTrackeeGenericApi
}) })
.catch(error => dispatch(setError(`activities: ${error}`))) .catch(error => dispatch(setError(`activities: ${error}`)))
export const getMonthActivities = (start, end) => dispatch => export const getMonthActivities = (from, to) => dispatch =>
FitTrackeeGenericApi FitTrackeeGenericApi
.getData('activities', { start, end, order: 'asc', per_page: 100 }) .getData('activities', { from, to, order: 'asc', per_page: 100 })
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
if (ret.data.activities.length > 0) { if (ret.data.activities.length > 0) {
@ -146,8 +146,8 @@ export const getMonthActivities = (start, end) => dispatch =>
ret.data.activities[i].activity_date ret.data.activities[i].activity_date
) )
} }
dispatch(updateCalendar(ret.data.activities))
} }
dispatch(updateCalendar(ret.data.activities))
} else { } else {
dispatch(setError(`activities: ${ret.message}`)) dispatch(setError(`activities: ${ret.message}`))
} }

View File

@ -11,7 +11,10 @@ function FormWithoutGpx (props) {
const { activity, onAddOrEdit, sports } = props const { activity, onAddOrEdit, sports } = props
let activityDate, activityTime, sportId = '' let activityDate, activityTime, sportId = ''
if (activity) { if (activity) {
const activityDateTime = formatActivityDate(activity.activity_date) const activityDateTime = formatActivityDate(
activity.activity_date,
'YYYY-MM-DD'
)
activityDate = activityDateTime.activity_date activityDate = activityDateTime.activity_date
activityTime = activityDateTime.activity_time activityTime = activityDateTime.activity_time
sportId = activity.sport_id sportId = activity.sport_id

View File

@ -302,7 +302,6 @@ input, textarea {
height: 3em; height: 3em;
border-right: 1px solid var(--border-color); border-right: 1px solid var(--border-color);
overflow: hidden; overflow: hidden;
cursor: pointer;
background: var(--neutral-color); background: var(--neutral-color);
} }

View File

@ -128,8 +128,8 @@ export default connect(
loadMonthActivities: (userId, start, end) => { loadMonthActivities: (userId, start, end) => {
const dateFormat = 'YYYY-MM-DD' const dateFormat = 'YYYY-MM-DD'
const params = { const params = {
start: format(start, dateFormat), from: format(start, dateFormat),
end: format(end, dateFormat), to: format(end, dateFormat),
time: 'week' time: 'week'
} }
dispatch(getStats(userId, 'by_time', params)) dispatch(getStats(userId, 'by_time', params))

View File

@ -3,6 +3,13 @@ import React from 'react'
export default function UserStatistics (props) { export default function UserStatistics (props) {
const { user } = props const { user } = props
const days = user.totalDuration.match(/day/g)
? `${user.totalDuration.split(',')[0]},`
: '0 days,'
let duration = user.totalDuration.match(/day/g)
? user.totalDuration.split(', ')[1]
: user.totalDuration
duration = `${duration.split(':')[0]}h ${duration.split(':')[1]}min`
return ( return (
<div className="row"> <div className="row">
<div className="col"> <div className="col">
@ -26,7 +33,7 @@ export default function UserStatistics (props) {
</div> </div>
<div className="col-9 text-right"> <div className="col-9 text-right">
<div className="huge"> <div className="huge">
{Math.round(user.totalDistance * 100) / 100} {Number(user.totalDistance).toFixed(2)}
</div> </div>
<div>km</div> <div>km</div>
</div> </div>
@ -40,8 +47,8 @@ export default function UserStatistics (props) {
<i className="fa fa-clock-o fa-3x fa-color" /> <i className="fa fa-clock-o fa-3x fa-color" />
</div> </div>
<div className="col-9 text-right"> <div className="col-9 text-right">
<div className="huge">{user.totalDuration}</div> <div className="huge">{days}</div>
<div>total duration</div> <div>{duration}</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -10,10 +10,19 @@ class Logout extends React.Component {
} }
render() { render() {
return ( return (
<div> <div className="container dashboard">
<p className="App-center"> <div className="row">
You are now logged out. <div className="col-2" />
Click <Link to="/login">here</Link> to log back in.</p> <div className="card col-8">
<div className="card-body">
<div className="text-center">
You are now logged out.
Click <Link to="/login">here</Link> to log back in.
</div>
</div>
</div>
<div className="col-2" />
</div>
</div> </div>
) )
} }

View File

@ -63,11 +63,14 @@ export const getGeoJson = gpxContent => {
return { jsonData } return { jsonData }
} }
export const formatActivityDate = activityDateTime => { export const formatActivityDate = (activityDateTime, dateFormat = null) => {
if (activityDateTime) { if (activityDateTime) {
const dateTime = parse(activityDateTime) const dateTime = parse(activityDateTime)
return { return {
activity_date: format(dateTime, 'DD/MM/YYYY'), activity_date: format(
dateTime,
dateFormat ? dateFormat : 'DD/MM/YYYY'
),
activity_time: activityDateTime.match(/[0-2][0-9]:[0-5][0-9]/)[0] activity_time: activityDateTime.match(/[0-2][0-9]:[0-5][0-9]/)[0]
} }
} }