mnemo_cards/mnemo_cards_backend/lib/cron/test_generator.dart

97 lines
3.1 KiB
Dart
Raw Normal View History

2025-12-13 14:48:00 +00:00
import 'package:mnemo_cards_backend/cron/task.dart' as cron_task;
import 'package:mnemo_cards_backend/database/database.dart';
2025-11-16 11:25:27 +00:00
import 'package:mnemo_cards_backend/tests/test_manager.dart';
2025-12-13 14:48:00 +00:00
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart';
2025-11-16 11:25:27 +00:00
2025-12-13 14:48:00 +00:00
class TestGeneratorTask with cron_task.Task {
2025-11-16 11:25:27 +00:00
final TestManager _testManager;
2025-12-13 14:48:00 +00:00
final AppDatabase _db;
2025-11-16 11:25:27 +00:00
2025-12-13 14:48:00 +00:00
TestGeneratorTask(this._testManager, this._db);
2025-11-16 11:25:27 +00:00
@override
String get name => 'test_gen';
@override
Future<void> task() async {
print('Refreshing custom test data...');
try {
await _testManager.refreshCustomCreationTestsData();
} catch (e) {
print(e);
}
print('Refreshed custom test data');
2025-12-13 14:48:00 +00:00
final packs = await _db.packDao.getAllPacks();
print('Will generate tests for ${packs.length} packs');
2025-11-16 11:25:27 +00:00
int ok = 0;
2025-12-13 14:48:00 +00:00
for (final pack in packs) {
2025-11-16 11:25:27 +00:00
try {
2025-12-13 14:48:00 +00:00
// Convert CardPack to CardPackModel for updateGeneratedTests
final packModel = CardPackModel(
id: pack.id,
title: pack.title,
subtitle: pack.subtitle,
description: pack.description,
color: pack.color,
cover: pack.cover,
size: pack.size,
version: pack.version,
order: pack.order,
enabled: pack.enabled,
cardsOrder: pack.cardsOrder,
googlePlayId: pack.googlePlayId,
rustoreId: pack.rustoreId,
appStoreId: pack.appStoreId,
price: pack.price,
currency: pack.currency,
);
await _testManager.updateGeneratedTests(packModel);
2025-11-16 11:25:27 +00:00
ok++;
} catch (e) {
print(e);
}
}
print('Generated tests for $ok packs');
2025-12-18 20:40:48 +00:00
print('Purging old generated tests (hard delete TTL)...');
try {
final orphanDeleted = await _db.testDao.hardDeleteOrphanGeneratedTests(
olderThan: const Duration(hours: 1),
);
print('purged $orphanDeleted orphan generated tests');
final deleted = await _db.testDao.hardDeleteOldSoftDeletedGeneratedTests(
olderThan: const Duration(days: 7),
);
print('purged $deleted old generated tests');
} catch (e) {
print('failed to purge old generated tests: $e');
}
2025-11-16 11:25:27 +00:00
print('Deleting old test stats');
2025-12-13 14:48:00 +00:00
// Delete test statistics where test doesn't exist (test was deleted)
final allTests = await _db.testDao.getAllTests();
final testIds = allTests.map((t) => t.id).toSet();
if (testIds.isEmpty) {
// If no tests exist, delete all statistics
final count = await (_db.delete(_db.testStatistics)).go();
print('deleted $count old test stats');
return;
}
// Get all statistics and filter in memory (more efficient for small datasets)
final allStats = await (_db.select(_db.testStatistics)).get();
2025-12-20 18:26:15 +00:00
final statsToDelete = allStats
.where((stat) => !testIds.contains(stat.testId))
.toList();
2025-12-13 14:48:00 +00:00
int count = 0;
for (final stat in statsToDelete) {
2025-12-20 18:26:15 +00:00
await (_db.delete(
_db.testStatistics,
)..where((ts) => ts.id.equals(stat.id))).go();
2025-12-13 14:48:00 +00:00
count++;
}
2025-11-16 11:25:27 +00:00
print('deleted $count old test stats');
}
@override
int get intervalSeconds => 60 * 30;
}