Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Calculation of Prediction Quality History #348

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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