This repository has been archived on 2024-03-27. You can view files and clone it, but cannot push or open issues/pull-requests.
encrateia/lib/models/minimum_power_duration.dart

54 lines
1.6 KiB
Dart

import 'dart:math';
import 'package:encrateia/models/event.dart';
import 'package:encrateia/models/plot_point.dart';
class MinimumPowerDuration {
MinimumPowerDuration({List<Event> records}) {
for (int index = 0; index < records.length - 1; index++) {
final int power = records[index].power;
final DateTime nextLower = records
.sublist(index + 1, records.length)
.firstWhere((Event record) => record.power < power,
orElse: () => records.last)
.timeStamp;
final DateTime recentLower = records
.sublist(0, index)
.lastWhere((Event record) => record.power < power,
orElse: () => records.first)
.timeStamp;
final int persistedFor = nextLower.difference(recentLower).inSeconds;
if (power > (powerMap[persistedFor] ?? 0)) {
for (int durationIndex = persistedFor;
durationIndex > 0;
durationIndex--) {
if (power <= (powerMap[durationIndex] ?? 0))
break;
powerMap[durationIndex] = power;
}
}
}
}
Map<int, int> powerMap = <int, int>{};
List<IntPlotPoint> asList() {
final List<IntPlotPoint> plotPoints = <IntPlotPoint>[];
powerMap.forEach((int duration, int power) {
plotPoints.add(IntPlotPoint(
domain: scaled(seconds: duration),
measure: power,
));
});
plotPoints
.sort((IntPlotPoint a, IntPlotPoint b) => a.domain.compareTo(b.domain));
return plotPoints;
}
static int scaled({int seconds}) => (200 * log(seconds)).round();
}