Commit 7f16d770 authored by Administrator's avatar Administrator

storing events and laps as via upsertAll (but actually it seems not a lot faster at all)

parent 8a5fe475
......@@ -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:uuid/uuid.dart';
class Activity extends ChangeNotifier {
DbActivity db;
......@@ -44,7 +45,7 @@ class Activity extends ChangeNotifier {
setState("downloaded");
}
setState(String state) async{
setState(String state) async {
db.state = state;
await db.save();
notifyListeners();
......@@ -120,18 +121,17 @@ class Activity extends ChangeNotifier {
}
Future<int> get minPower async {
if (db.minPower == null){
if (db.minPower == null) {
List<Event> records = await this.records;
db.minPower = Lap.calculateMinPower(records: records);
await db.save();
notifyListeners();
}
return db.minPower;
}
Future<int> get maxPower async {
if (db.maxPower == null){
if (db.maxPower == null) {
List<Event> records = await this.records;
db.maxPower = Lap.calculateMaxPower(records: records);
await db.save();
......@@ -161,9 +161,11 @@ class Activity extends ChangeNotifier {
}
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 = Lap.calculateAverageVerticalOscillation(records: records);
db.avgVerticalOscillation =
Lap.calculateAverageVerticalOscillation(records: records);
await db.save();
notifyListeners();
}
......@@ -173,14 +175,14 @@ class Activity extends ChangeNotifier {
Future<double> get sdevVerticalOscillation async {
if (db.sdevVerticalOscillation == null) {
List<Event> records = await this.records;
db.sdevVerticalOscillation = Lap.calculateSdevVerticalOscillation(records: records);
db.sdevVerticalOscillation =
Lap.calculateSdevVerticalOscillation(records: records);
await db.save();
notifyListeners();
}
return db.sdevVerticalOscillation;
}
Future<double> get avgStrydCadence async {
if (db.avgStrydCadence == null) {
List<Event> records = await this.records;
......@@ -201,11 +203,11 @@ class Activity extends ChangeNotifier {
return db.sdevStrydCadence;
}
Future<double> get avgLegSpringStiffness async {
if (db.avgLegSpringStiffness == null) {
List<Event> records = await this.records;
db.avgLegSpringStiffness = Lap.calculateAverageLegSpringStiffness(records: records);
db.avgLegSpringStiffness =
Lap.calculateAverageLegSpringStiffness(records: records);
await db.save();
notifyListeners();
}
......@@ -215,7 +217,8 @@ class Activity extends ChangeNotifier {
Future<double> get sdevLegSpringStiffness async {
if (db.sdevLegSpringStiffness == null) {
List<Event> records = await this.records;
db.sdevLegSpringStiffness = Lap.calculateSdevLegSpringStiffness(records: records);
db.sdevLegSpringStiffness =
Lap.calculateSdevLegSpringStiffness(records: records);
await db.save();
notifyListeners();
}
......@@ -242,11 +245,13 @@ class Activity extends ChangeNotifier {
return db.sdevFormPower;
}
parse({@required Athlete athlete}) async {
var appDocDir = await getApplicationDocumentsDirectory();
var fitFile = FitFile(path: appDocDir.path + '/${db.stravaId}.fit').parse();
print("Parsing .fit-File ${db.stravaId} done.");
print("Parsing .fit-File for »${db.name}« done.");
List<Lap> laps = [];
List<Event> events = [];
// delete left overs from prior runs:
await db.getDbEvents().delete();
......@@ -301,17 +306,20 @@ class Activity extends ChangeNotifier {
break;
case "event":
Event(dataMessage: dataMessage, activity: this);
events.add(Event(dataMessage: dataMessage, activity: this));
break;
case "record":
Event.fromRecord(dataMessage: dataMessage, activity: this);
events.add(Event.fromRecord(dataMessage: dataMessage, activity: this));
break;
case "lap":
var event = Event.fromLap(dataMessage: dataMessage, activity: this);
var eventId = await event.db.save();
Lap(dataMessage: dataMessage, activity: this, eventId: eventId);
// a time based UUID to be able to reference this event from the lap
event.uuid = Uuid().v1();
events.add(event);
laps.add(Lap(dataMessage: dataMessage, activity: this, uuid: event.uuid));
break;
case "session":
......@@ -383,10 +391,17 @@ class Activity extends ChangeNotifier {
}
}
await DbEvent().upsertAll(events.map((event) => event.db).toList());
for (Lap lap in laps) {
lap.db.eventsId = events.firstWhere((event) => lap.uuid == event.uuid).db.id;
}
await DbLap().upsertAll(laps.map((lap) => lap.db).toList());
db.state = "persisted";
await db.save();
notifyListeners();
print("Persisted activity ${db.stravaId} to database.");
print("Activity data for »${db.name}« stored in database.");
}
delete() async {
......
......@@ -11,6 +11,7 @@ class Event {
DbEvent db;
Activity activity;
int index;
String uuid;
Event.fromDb(this.db);
......@@ -28,8 +29,7 @@ class Event {
..eventType = dataMessage.get('event_type')
..eventGroup = dataMessage.get('event_group')?.round()
..timerTrigger = dataMessage.get('timer_trigger')
..timeStamp = dateTimeFromStrava(dataMessage.get('timestamp'))
..save();
..timeStamp = dateTimeFromStrava(dataMessage.get('timestamp'));
} else if (dataMessage.values.any((value) =>
value.fieldName == 'event_type' && ['marker'].contains(value.value))) {
db = DbEvent()
......@@ -38,8 +38,7 @@ class Event {
..eventType = dataMessage.get('event_type')
..eventGroup = dataMessage.get('event_group')?.round()
..data = dataMessage.get('data')
..timeStamp = dateTimeFromStrava(dataMessage.get('timestamp'))
..save();
..timeStamp = dateTimeFromStrava(dataMessage.get('timestamp'));
} else {
debugger();
}
......@@ -63,8 +62,7 @@ class Event {
..groundTime = dataMessage.get('Ground Time')
..verticalOscillation = dataMessage.get('Vertical Oscillation')
..formPower = dataMessage.get('Form Power')?.round()
..legSpringStiffness = dataMessage.get('Leg Spring Stiffness')
..save();
..legSpringStiffness = dataMessage.get('Leg Spring Stiffness');
}
Event.fromLap({DataMessage dataMessage, this.activity}) {
......@@ -82,8 +80,7 @@ class Event {
..heartRate = dataMessage.get('avg_heart_rate')?.round()
..cadence = dataMessage.get('avg_running_cadence')
..timerTrigger = dataMessage.get('lap_trigger')
..distance = dataMessage.get('total_distance')
..save();
..distance = dataMessage.get('total_distance');
}
static Future<List<Event>> recordsByLap({Lap lap}) async {
......
......@@ -10,11 +10,11 @@ class Lap {
Activity activity;
int index;
List<Event> _records;
String uuid;
Lap({DataMessage dataMessage, this.activity, int eventId}) {
Lap({DataMessage dataMessage, this.activity, this.uuid}) {
db = DbLap()
..activitiesId = activity.db.id
..eventsId = eventId
..timeStamp = dateTimeFromStrava(dataMessage.get('timestamp'))
..startTime = dateTimeFromStrava(dataMessage.get('start_time'))
..startPositionLat = dataMessage.get('start_position_lat')
......@@ -49,9 +49,9 @@ class Lap {
..maxTemperature = dataMessage.get('max_temperature')?.round()
..avgFractionalCadence = dataMessage.get('avg_fractional_cadence')
..maxFractionalCadence = dataMessage.get('max_fractional_cadence')
..totalFractionalCycles = dataMessage.get('total_fractional_cycles')
..save();
..totalFractionalCycles = dataMessage.get('total_fractional_cycles');
}
Lap.fromDb(this.db);
Future<List<Event>> get records async {
......
......@@ -707,6 +707,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0+2"
uuid:
dependency: "direct main"
description:
name: uuid
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
vector_math:
dependency: transitive
description:
......
......@@ -22,6 +22,7 @@ dependencies:
fit_parser: ^0.1.6
charts_flutter: ^0.8.1
uuid: 2.0.4
dev_dependencies:
flutter_test:
......
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