Commit c8c06429 authored by Administrator's avatar Administrator

storing records lap wise in batches, proper relation from lap to record

parent 12a8d98c
......@@ -136,6 +136,12 @@ const tableEvent = SqfEntityTable(
parentTable: tableActivity,
deleteRule: DeleteRule.CASCADE,
defaultValue: 0),
SqfEntityFieldRelationship(
parentTable: tableLap,
deleteRule: DeleteRule.CASCADE,
defaultValue: 0),
],
);
......@@ -195,10 +201,6 @@ const tableLap = SqfEntityTable(
SqfEntityField('sdevStrydCadence', DbType.real),
SqfEntityField('sdevVerticalOscillation', DbType.real),
SqfEntityFieldRelationship(
parentTable: tableEvent,
deleteRule: DeleteRule.CASCADE,
defaultValue: 0),
SqfEntityFieldRelationship(
parentTable: tableActivity,
deleteRule: DeleteRule.CASCADE,
......
This diff is collapsed.
......@@ -18,6 +18,10 @@ class Activity extends ChangeNotifier {
List<Event> _records;
List<Lap> _laps;
// intermediate data structures used for parsing
Lap currentLap;
List<Event> eventsForCurrentLap;
Activity();
Activity.fromDb(this.db);
......@@ -272,10 +276,11 @@ class Activity extends ChangeNotifier {
await db.save();
int numberOfMessages = fitFile.dataMessages.length;
await resetCurrentLap();
for (DataMessage dataMessage in fitFile.dataMessages) {
counter++;
await handleDataMessage(dataMessage: dataMessage);
percentage = await handleDataMessage(dataMessage: dataMessage);
if (counter % 100 == 0) {
percentage = (counter / numberOfMessages * 100).round();
......@@ -290,7 +295,9 @@ class Activity extends ChangeNotifier {
yield 100;
}
handleDataMessage({DataMessage dataMessage}) async {
handleDataMessage({
DataMessage dataMessage,
}) async {
if (dataMessage.definitionMessage.globalMessageName == null) {
switch (dataMessage.definitionMessage.globalMessageNumber) {
case 13:
......@@ -340,30 +347,36 @@ class Activity extends ChangeNotifier {
dataMessage: dataMessage,
activity: this,
);
await event.db.save();
eventsForCurrentLap.add(event);
break;
case "record":
var event = Event.fromRecord(
dataMessage: dataMessage,
activity: this,
lapsId: currentLap.db.id,
);
await event.db.save();
eventsForCurrentLap.add(event);
break;
case "lap":
var event = Event.fromLap(
dataMessage: dataMessage,
activity: this,
lapsId: currentLap.db.id,
);
var eventsId = await event.db.save();
eventsForCurrentLap.add(event);
var lap = Lap(
var lap = Lap.fromLap(
dataMessage: dataMessage,
activity: this,
eventsId: eventsId,
lap: currentLap,
);
await lap.db.save();
await DbEvent()
.upsertAll(eventsForCurrentLap.map((event) => event.db).toList());
await resetCurrentLap();
break;
case "session":
......@@ -431,6 +444,12 @@ class Activity extends ChangeNotifier {
}
}
resetCurrentLap() async{
currentLap = Lap();
await currentLap.db.save();
eventsForCurrentLap = [];
}
delete() async {
await this.db.delete();
}
......
......@@ -10,11 +10,15 @@ import 'package:encrateia/models/plot_point.dart';
class Event {
DbEvent db;
Activity activity;
Lap lap;
int index;
Event.fromDb(this.db);
Event({DataMessage dataMessage, this.activity}) {
Event({
@required DataMessage dataMessage,
@required this.activity,
}) {
if (dataMessage.any('max_heart_rate')) {
activity.db
..maxHeartRate = dataMessage.get('max_heart_rate')?.round()
......@@ -43,9 +47,14 @@ class Event {
}
}
Event.fromRecord({DataMessage dataMessage, this.activity}) {
Event.fromRecord({
@required DataMessage dataMessage,
@required this.activity,
@required int lapsId,
}) {
db = DbEvent()
..activitiesId = activity.db.id
..lapsId = lapsId
..event = "record"
..timeStamp = dateTimeFromStrava(dataMessage.get('timestamp'))
..positionLat = dataMessage.get('position_lat')
......@@ -64,9 +73,14 @@ class Event {
..legSpringStiffness = dataMessage.get('Leg Spring Stiffness');
}
Event.fromLap({DataMessage dataMessage, this.activity}) {
Event.fromLap({
@required DataMessage dataMessage,
@required this.activity,
@required int lapsId,
}) {
db = DbEvent()
..activitiesId = activity.db.id
..lapsId = lapsId
..positionLat = dataMessage.get('end_position_lat')
..positionLong = dataMessage.get('end_position_long')
..timeStamp = dateTimeFromStrava(dataMessage.get('timestamp'))
......@@ -83,16 +97,8 @@ class Event {
}
static Future<List<Event>> recordsByLap({Lap lap}) async {
int firstRecordId = await lap.firstEventId();
DbEvent lastRecord = await lap.db.getDbEvent();
int lastRecordId = lastRecord.id;
var events = await by(activity: lap.activity);
var records = events.where((event) =>
event.db.id < lastRecordId &&
event.db.id > firstRecordId &&
event.db.event == "record");
var events = await byLap(lap: lap);
var records = events.where((event) => event.db.event == "record");
return records.toList();
}
......@@ -102,11 +108,26 @@ class Event {
return records.toList();
}
static Future<List<Event>> byLap({Lap lap}) async {
int counter = 1;
List<DbEvent> dbEventList = await lap.db.getDbEvents().toList();
var eventList = dbEventList.map((dbEvent) => Event.fromDb(dbEvent)).toList();
for (Event event in eventList) {
event.lap = lap;
event.index = counter;
counter = counter + 1;
}
return eventList;
}
static Future<List<Event>> by({Activity activity}) async {
int counter = 1;
List<DbEvent> dbEventList = await activity.db.getDbEvents().toList();
var eventList = dbEventList.map((dbLap) => Event.fromDb(dbLap)).toList();
var eventList = dbEventList.map((dbEvent) => Event.fromDb(dbEvent)).toList();
for (Event event in eventList) {
event.activity = activity;
......
......@@ -11,10 +11,19 @@ class Lap {
int index;
List<Event> _records;
Lap({DataMessage dataMessage, this.activity, int eventsId}) {
db = DbLap()
Lap() {
db = DbLap();
}
Lap.fromDb(this.db);
static Lap fromLap({
DataMessage dataMessage,
Activity activity,
Lap lap,
}) {
lap.db
..activitiesId = activity.db.id
..eventsId = eventsId
..timeStamp = dateTimeFromStrava(dataMessage.get('timestamp'))
..startTime = dateTimeFromStrava(dataMessage.get('start_time'))
..startPositionLat = dataMessage.get('start_position_lat')
......@@ -50,10 +59,9 @@ class Lap {
..avgFractionalCadence = dataMessage.get('avg_fractional_cadence')
..maxFractionalCadence = dataMessage.get('max_fractional_cadence')
..totalFractionalCycles = dataMessage.get('total_fractional_cycles');
return lap;
}
Lap.fromDb(this.db);
Future<List<Event>> get records async {
if (_records == null) {
_records = await Event.recordsByLap(lap: this);
......@@ -116,9 +124,11 @@ class Lap {
}
Future<double> get avgVerticalOscillation async {
if (db.avgVerticalOscillation == null || db.avgVerticalOscillation == 6553.5) {
if (db.avgVerticalOscillation == null ||
db.avgVerticalOscillation == 6553.5) {
List<Event> records = await this.records;
db.avgVerticalOscillation = calculateAverageVerticalOscillation(records: records);
db.avgVerticalOscillation =
calculateAverageVerticalOscillation(records: records);
await db.save();
}
return db.avgVerticalOscillation;
......@@ -127,13 +137,13 @@ class Lap {
Future<double> get sdevVerticalOscillation async {
if (db.sdevVerticalOscillation == null) {
List<Event> records = await this.records;
db.sdevVerticalOscillation = calculateSdevVerticalOscillation(records: records);
db.sdevVerticalOscillation =
calculateSdevVerticalOscillation(records: records);
await db.save();
}
return db.sdevVerticalOscillation;
}
Future<double> get avgStrydCadence async {
if (db.avgStrydCadence == null) {
List<Event> records = await this.records;
......@@ -155,7 +165,8 @@ class Lap {
Future<double> get avgLegSpringStiffness async {
if (db.avgLegSpringStiffness == null) {
List<Event> records = await this.records;
db.avgLegSpringStiffness = calculateAverageLegSpringStiffness(records: records);
db.avgLegSpringStiffness =
calculateAverageLegSpringStiffness(records: records);
await db.save();
}
return db.avgLegSpringStiffness;
......@@ -164,7 +175,8 @@ class Lap {
Future<double> get sdevLegSpringStiffness async {
if (db.sdevLegSpringStiffness == null) {
List<Event> records = await this.records;
db.sdevLegSpringStiffness = calculateSdevLegSpringStiffness(records: records);
db.sdevLegSpringStiffness =
calculateSdevLegSpringStiffness(records: records);
await db.save();
}
return db.sdevLegSpringStiffness;
......@@ -188,20 +200,6 @@ class Lap {
return db.sdevFormPower;
}
Future<int> firstEventId() async {
if (index > 1) {
var lapList = await activity.laps;
DbEvent firstEvent = await lapList
.firstWhere((lap) => lap.index == index - 1)
.db
.getDbEvent();
return firstEvent.id;
} else {
return 0;
}
}
static Future<List<Lap>> by({Activity activity}) async {
int counter = 1;
......@@ -267,7 +265,7 @@ class Lap {
static double calculateAverageStrydCadence({List<Event> records}) {
var strydCadences =
records.map((record) => 2 * record.db.strydCadence).nonZeroDoubles();
records.map((record) => 2 * record.db.strydCadence).nonZeroDoubles();
if (strydCadences.length > 0) {
return strydCadences.mean();
} else
......@@ -276,14 +274,13 @@ class Lap {
static double calculateSdevStrydCadence({List<Event> records}) {
var strydCadences =
records.map((record) => 2 * record.db.strydCadence).nonZeroDoubles();
records.map((record) => 2 * record.db.strydCadence).nonZeroDoubles();
return strydCadences.sdev();
}
static double calculateAverageLegSpringStiffness({List<Event> records}) {
var legSpringStiffnesses =
records.map((record) => record.db.legSpringStiffness).nonZeroDoubles();
records.map((record) => record.db.legSpringStiffness).nonZeroDoubles();
if (legSpringStiffnesses.length > 0) {
return legSpringStiffnesses.mean();
} else
......@@ -292,13 +289,13 @@ class Lap {
static double calculateSdevLegSpringStiffness({List<Event> records}) {
var legSpringStiffnesses =
records.map((record) => record.db.legSpringStiffness).nonZeroDoubles();
records.map((record) => record.db.legSpringStiffness).nonZeroDoubles();
return legSpringStiffnesses.sdev();
}
static double calculateAverageVerticalOscillation({List<Event> records}) {
var verticalOscillation =
records.map((record) => record.db.verticalOscillation).nonZeroDoubles();
records.map((record) => record.db.verticalOscillation).nonZeroDoubles();
if (verticalOscillation.length > 0) {
return verticalOscillation.mean();
} else
......@@ -307,14 +304,13 @@ class Lap {
static double calculateSdevVerticalOscillation({List<Event> records}) {
var verticalOscillation =
records.map((record) => record.db.verticalOscillation).nonZeroDoubles();
records.map((record) => record.db.verticalOscillation).nonZeroDoubles();
return verticalOscillation.sdev();
}
static double calculateAverageFormPower({List<Event> records}) {
var legSpringStiffnesses =
records.map((record) => record.db.formPower).nonZeroInts();
records.map((record) => record.db.formPower).nonZeroInts();
if (legSpringStiffnesses.length > 0) {
return legSpringStiffnesses.mean();
} else
......@@ -323,7 +319,7 @@ class Lap {
static double calculateSdevFormPower({List<Event> records}) {
var legSpringStiffnesses =
records.map((record) => record.db.formPower).nonZeroInts();
records.map((record) => record.db.formPower).nonZeroInts();
return legSpringStiffnesses.sdev();
}
}
......@@ -75,7 +75,7 @@ class _ListActivitiesScreenState extends State<ListActivitiesScreen> {
Future queryStrava() async {
flushbar = Flushbar(
message: "Downloading new activities",
duration: Duration(seconds: 30),
duration: Duration(seconds: 10),
icon: MyIcon.stravaDownloadWhite,
)..show(context);
await Activity.queryStrava(athlete: widget.athlete);
......@@ -113,7 +113,7 @@ class _ListActivitiesScreenState extends State<ListActivitiesScreen> {
flushbar.dismiss();
flushbar = Flushbar(
message: "0% of storing »${activity.db.name}«",
duration: Duration(seconds: 3),
duration: Duration(seconds: 10),
animationDuration: Duration(milliseconds: 1),
titleText: LinearProgressIndicator(value: 0),
)..show(context);
......
......@@ -114,7 +114,7 @@ class _ActivitiesListWidgetState extends State<ActivitiesListWidget> {
Future download({Activity activity}) async {
var flushbar = Flushbar(
message: "Download .fit-File for »${activity.db.name}«",
duration: Duration(seconds: 30),
duration: Duration(seconds: 10),
icon: MyIcon.stravaDownloadWhite,
)..show(context);
......@@ -133,7 +133,7 @@ class _ActivitiesListWidgetState extends State<ActivitiesListWidget> {
Future parse({Activity activity}) async {
var flushbar = Flushbar(
message: "0% of storing »${activity.db.name}«",
duration: Duration(seconds: 3),
duration: Duration(seconds: 10),
animationDuration: Duration(milliseconds: 1),
titleText: LinearProgressIndicator(value: 0),
)..show(context);
......
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