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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user