Can backfill the previous year, although some API rate limiting but it works

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-19 23:55:08 +01:00
parent e6c16967f9
commit d1d1d9fb95
2 changed files with 107 additions and 5 deletions
+35 -4
View File
@@ -241,8 +241,20 @@ class RentController extends ChangeNotifier {
}
final year = DateTime.now().year - 1;
// Attempt a single timeseries call covering the whole year.
final batchRates = await _exchangeService.tryFetchYearRates(year);
if (kDebugMode) {
debugPrint(
batchRates != null
? '[RentController] backfill using batch timeseries (${batchRates.length} dates)'
: '[RentController] backfill falling back to individual daily requests',
);
}
var savedMonths = 0;
var pausedByRateLimit = false;
var isFirstIndividualFetch = true;
for (var month = 1; month <= 12; month++) {
final key = TimeService.monthKey(year, month);
@@ -256,10 +268,29 @@ class RentController extends ChangeNotifier {
try {
final usd = _settings.rentUsd;
final estDate = DateTime(year, month, 1);
final conversion = await _exchangeService.convertUsdToSek(
estDate: estDate,
amount: usd,
);
final ForexConversionResult conversion;
if (batchRates != null) {
final dateStr = DateFormat('yyyy-MM-dd').format(estDate);
final quote = batchRates[dateStr];
if (quote == null) {
throw StateError('Batch rates missing entry for $dateStr.');
}
conversion = ForexConversionResult(
quote: quote,
result: quote * usd,
);
} else {
// Pace individual requests to avoid triggering the rate limit.
if (!isFirstIndividualFetch) {
await Future.delayed(const Duration(milliseconds: 700));
}
isFirstIndividualFetch = false;
conversion = await _exchangeService.convertUsdToSek(
estDate: estDate,
amount: usd,
);
}
if (kDebugMode) {
debugPrint(