Skip to content

Commit

Permalink
Fix Calculation of Prediction Quality History (#348)
Browse files Browse the repository at this point in the history
* change key for prediction monitor

* use new key for good prediction quality

* change names to reflect good predictions
  • Loading branch information
kruegercharles authored Nov 3, 2023
1 parent 491ab76 commit 8937e62
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/status/services/status_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:priobike/settings/services/settings.dart';

class StatusHistory with ChangeNotifier {
/// The count of predictions in the last week.
Map<double, double> weekPredictions = {};
Map<double, double> weekGoodPredictions = {};

/// The count of subscriptions in the last week.
Map<double, double> weekSubscriptions = {};
Expand All @@ -18,7 +18,7 @@ class StatusHistory with ChangeNotifier {
Map<double, double> weekPercentages = {};

/// The count of predictions in the last day.
Map<double, double> dayPredictions = {};
Map<double, double> dayGoodPredictions = {};

/// The count of subscriptions in the last day.
Map<double, double> daySubscriptions = {};
Expand All @@ -39,28 +39,28 @@ class StatusHistory with ChangeNotifier {

/// Parses the data from the json response.
List parseData(dynamic json) {
final Map<double, double> predictions = {};
final Map<double, double> goodPredictions = {};
final Map<double, double> subscriptions = {};
final Map<double, double> percentages = {};
double maxSubscriptions = 0;

const predictionsKey = "average_prediction_service_predictions_count_total";
const predictionsKey = "prediction_service_good_prediction_total";
const subscriptionsKey = "prediction_service_subscription_count_total";

// Parse into maps and get max number of subscriptions.
for (final entry in json[predictionsKey].entries) {
final predictionValue = entry.value.toDouble();
final double timestamp = double.parse(entry.key);

predictions[timestamp] = predictionValue;
goodPredictions[timestamp] = predictionValue;
subscriptions[timestamp] = json[subscriptionsKey][entry.key].toDouble();
if (subscriptions[timestamp]! > maxSubscriptions) {
maxSubscriptions = subscriptions[timestamp]!;
}
}

// Calculate percentages.
for (final entry in predictions.entries) {
for (final entry in goodPredictions.entries) {
if (maxSubscriptions == 0) {
percentages[entry.key] = 0;
continue;
Expand All @@ -69,7 +69,7 @@ class StatusHistory with ChangeNotifier {
percentages[entry.key] = percentage > 1 ? 1 : percentage;
}

return [predictions, subscriptions, percentages];
return [goodPredictions, subscriptions, percentages];
}

/// Fetches the status history data from priobike-prediction-monitor.
Expand Down Expand Up @@ -109,11 +109,11 @@ class StatusHistory with ChangeNotifier {
final jsonWeek = jsonDecode(responseWeek.body);

final dayData = parseData(jsonDay);
dayPredictions = dayData[0];
dayGoodPredictions = dayData[0];
daySubscriptions = dayData[1];
dayPercentages = dayData[2];
final weekData = parseData(jsonWeek);
weekPredictions = weekData[0];
weekGoodPredictions = weekData[0];
weekSubscriptions = weekData[1];
weekPercentages = weekData[2];

Expand All @@ -131,10 +131,10 @@ class StatusHistory with ChangeNotifier {

/// Reset the status.
Future<void> reset() async {
weekPredictions = {};
weekGoodPredictions = {};
weekSubscriptions = {};
weekPercentages = {};
dayPredictions = {};
dayGoodPredictions = {};
daySubscriptions = {};
dayPercentages = {};
isLoading = false;
Expand Down

0 comments on commit 8937e62

Please sign in to comment.