Commit 7e54dab4 authored by Administrator's avatar Administrator

extract gliding enrichment

parent ebdda842
......@@ -12,6 +12,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:encrateia/utils/date_time_utils.dart';
import 'dart:developer';
import 'package:intl/intl.dart';
import 'package:encrateia/utils/enums.dart';
class Activity extends ChangeNotifier {
DbActivity db;
......@@ -44,6 +45,20 @@ class Activity extends ChangeNotifier {
String toString() => '$db.name $db.startTime';
Duration movingDuration() => Duration(seconds: db.movingTime ?? 0);
get({ActivityAttr quantity}) {
switch (quantity) {
case ActivityAttr.avgPower:
return db.avgPower;
}
}
setGliding({ActivityAttr quantity, double value}) {
switch (quantity) {
case ActivityAttr.avgPower:
glidingAvgPower = value;
}
}
download({@required Athlete athlete}) async {
await StravaFitDownload.byId(id: db.stravaId.toString(), athlete: athlete);
setState("downloaded");
......@@ -472,8 +487,8 @@ class Activity extends ChangeNotifier {
"${dataMessage.definitionMessage.globalMessageName} "
"are not implemented yet.");
print(dataMessage.values.map((v) => v.fieldName).toList());
// Use this debugger to implement additional message types!
// debugger();
// Use this debugger to implement additional message types!
// debugger();
}
}
}
......
import 'package:encrateia/models/activity.dart';
import 'package:flutter/material.dart';
import 'package:encrateia/utils/enums.dart';
class ActivityList {
List<Activity> activities = [];
ActivityList({this.activities});
enrichGlidingAverage({
@required int fullDecay,
@required ActivityAttr quantity,
}) {
activities.asMap().forEach((index, activity) {
double sumOfAvg = activity.get(quantity: quantity) * fullDecay;
double sumOfWeightings = fullDecay * 1.0;
for (var olderIndex = index + 1;
olderIndex < activities.length;
olderIndex++) {
double daysAgo = activity.db.timeCreated
.difference(activities[olderIndex].db.timeCreated)
.inHours /
24;
if (daysAgo > fullDecay) break;
sumOfAvg += (fullDecay - daysAgo) *
activities[olderIndex].get(quantity: quantity);
sumOfWeightings += (fullDecay - daysAgo);
}
activity.setGliding(
quantity: quantity,
value: sumOfAvg / sumOfWeightings,
);
});
}
}
......@@ -144,7 +144,7 @@ class Event {
static toIntDataPoints({
Iterable<Event> records,
int amount,
@required IntQuantity attribute,
@required LapIntAttr attribute,
}) {
int index = 0;
List<IntPlotPoint> plotPoints = [];
......@@ -152,13 +152,13 @@ class Event {
for (var record in records) {
switch (attribute) {
case IntQuantity.power:
case LapIntAttr.power:
sum = sum + record.db.power;
break;
case IntQuantity.formPower:
case LapIntAttr.formPower:
sum = sum + record.db.formPower;
break;
case IntQuantity.heartRate:
case LapIntAttr.heartRate:
sum = sum + record.db.heartRate;
}
......@@ -177,7 +177,7 @@ class Event {
static toDoubleDataPoints({
Iterable<Event> records,
int amount,
@required DoubleQuantity attribute,
@required LapDoubleAttr attribute,
}) {
int index = 0;
List<DoublePlotPoint> plotPoints = [];
......@@ -185,25 +185,25 @@ class Event {
for (var record in records) {
switch (attribute) {
case DoubleQuantity.powerPerHeartRate:
case LapDoubleAttr.powerPerHeartRate:
sum = sum + (record.db.power / record.db.heartRate);
break;
case DoubleQuantity.speedPerHeartRate:
case LapDoubleAttr.speedPerHeartRate:
sum = sum + (record.db.speed / record.db.heartRate);
break;
case DoubleQuantity.groundTime:
case LapDoubleAttr.groundTime:
sum = sum + record.db.groundTime;
break;
case DoubleQuantity.strydCadence:
case LapDoubleAttr.strydCadence:
sum = sum + 2 * record.db.strydCadence;
break;
case DoubleQuantity.verticalOscillation:
case LapDoubleAttr.verticalOscillation:
sum = sum + record.db.verticalOscillation;
break;
case DoubleQuantity.legSpringStiffness:
case LapDoubleAttr.legSpringStiffness:
sum = sum + record.db.legSpringStiffness;
break;
case DoubleQuantity.powerRatio:
case LapDoubleAttr.powerRatio:
sum = sum +
((record.db.power - record.db.formPower) / record.db.power * 100);
}
......
enum IntQuantity {
enum LapIntAttr {
power,
formPower,
heartRate,
}
enum DoubleQuantity {
enum LapDoubleAttr {
powerPerHeartRate,
speedPerHeartRate,
groundTime,
......@@ -20,3 +20,7 @@ enum ActivityAction {
delete,
state,
}
enum ActivityAttr {
avgPower
}
......@@ -18,7 +18,7 @@ class ActivityFormPowerChart extends StatelessWidget {
Widget build(BuildContext context) {
var nonZero = records.where((value) => value.db.formPower > 0);
var smoothedRecords = Event.toIntDataPoints(
attribute: IntQuantity.formPower,
attribute: LapIntAttr.formPower,
records: nonZero,
amount: 30,
);
......
......@@ -18,7 +18,7 @@ class ActivityGroundTimeChart extends StatelessWidget {
Widget build(BuildContext context) {
var nonZero = records.where((value) => value.db.groundTime > 0);
var smoothedRecords = Event.toDoubleDataPoints(
attribute: DoubleQuantity.groundTime,
attribute: LapDoubleAttr.groundTime,
records: nonZero,
amount: 30,
);
......
......@@ -19,7 +19,7 @@ class ActivityHeartRateChart extends StatelessWidget {
var nonZero = records.where(
(value) => value.db.heartRate != null && value.db.heartRate > 10);
var smoothedRecords = Event.toIntDataPoints(
attribute: IntQuantity.heartRate,
attribute: LapIntAttr.heartRate,
records: nonZero,
amount: 30,
);
......
......@@ -18,7 +18,7 @@ class ActivityLegSpringStiffnessChart extends StatelessWidget {
Widget build(BuildContext context) {
var nonZero = records.where((value) => value.db.groundTime > 0);
var smoothedRecords = Event.toDoubleDataPoints(
attribute: DoubleQuantity.legSpringStiffness,
attribute: LapDoubleAttr.legSpringStiffness,
records: nonZero,
amount: 30,
);
......
......@@ -18,7 +18,7 @@ class ActivityPowerChart extends StatelessWidget {
Widget build(BuildContext context) {
var nonZero = records.where((value) => value.db.power > 100);
var smoothedRecords = Event.toIntDataPoints(
attribute: IntQuantity.power,
attribute: LapIntAttr.power,
records: nonZero,
amount: 30,
);
......
......@@ -19,7 +19,7 @@ class ActivityPowerPerHeartRateChart extends StatelessWidget {
var nonZero = records
.where((value) => value.db.power > 100 && value.db.heartRate > 0);
var smoothedRecords = Event.toDoubleDataPoints(
attribute: DoubleQuantity.powerPerHeartRate,
attribute: LapDoubleAttr.powerPerHeartRate,
records: nonZero,
amount: 30,
);
......
......@@ -18,7 +18,7 @@ class ActivityPowerRatioChart extends StatelessWidget {
Widget build(BuildContext context) {
var nonZero = records.where((value) => value.db.power > 0);
var smoothedRecords = Event.toDoubleDataPoints(
attribute: DoubleQuantity.powerRatio,
attribute: LapDoubleAttr.powerRatio,
records: nonZero,
amount: 30,
);
......
......@@ -19,7 +19,7 @@ class ActivitySpeedPerHeartRateChart extends StatelessWidget {
var nonZero = records
.where((value) => value.db.power > 100 && value.db.heartRate > 0);
var smoothedRecords = Event.toDoubleDataPoints(
attribute: DoubleQuantity.speedPerHeartRate,
attribute: LapDoubleAttr.speedPerHeartRate,
records: nonZero,
amount: 30,
);
......
......@@ -18,7 +18,7 @@ class ActivityStrydCadenceChart extends StatelessWidget {
Widget build(BuildContext context) {
var nonZero = records.where((value) => value.db.strydCadence > 0);
var smoothedRecords = Event.toDoubleDataPoints(
attribute: DoubleQuantity.strydCadence,
attribute: LapDoubleAttr.strydCadence,
records: nonZero,
amount: 30,
);
......
......@@ -18,7 +18,7 @@ class ActivityVerticalOscillationChart extends StatelessWidget {
Widget build(BuildContext context) {
var nonZero = records.where((value) => value.db.power > 100);
var smoothedRecords = Event.toDoubleDataPoints(
attribute: DoubleQuantity.verticalOscillation,
attribute: LapDoubleAttr.verticalOscillation,
records: nonZero,
amount: 30,
);
......
import 'package:charts_flutter/flutter.dart';
import 'package:encrateia/models/activity_list.dart';
import 'package:flutter/material.dart';
import 'package:encrateia/models/activity.dart';
import 'package:encrateia/utils/enums.dart';
class AthletePowerChart extends StatelessWidget {
final List<Activity> activities;
......@@ -9,7 +11,6 @@ class AthletePowerChart extends StatelessWidget {
@override
Widget build(BuildContext context) {
int fullDecay = 30;
int xAxesDays = 60;
List<Activity> nonZeroActivities = activities
......@@ -17,24 +18,10 @@ class AthletePowerChart extends StatelessWidget {
activity.db.avgPower != null && activity.db.avgPower > 0)
.toList();
nonZeroActivities.asMap().forEach((index, activity) {
double sumOfAvgPower = activity.db.avgPower * fullDecay;
double sumOfWeightings = fullDecay * 1.0;
for (var olderIndex = index + 1;
olderIndex < nonZeroActivities.length;
olderIndex++) {
double daysAgo = activity.db.timeCreated
.difference(nonZeroActivities[olderIndex].db.timeCreated)
.inHours /
24;
if (daysAgo > fullDecay) break;
sumOfAvgPower +=
(fullDecay - daysAgo) * nonZeroActivities[olderIndex].db.avgPower;
sumOfWeightings += (fullDecay - daysAgo);
}
activity.glidingAvgPower = sumOfAvgPower / sumOfWeightings;
});
ActivityList(activities: nonZeroActivities).enrichGlidingAverage(
quantity: ActivityAttr.avgPower,
fullDecay: 30,
);
var nonZeroDateLimited = nonZeroActivities
.where((activity) =>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment