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');
|
|
|
|
|
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();
|
|
|
|
|
final statsToDelete = allStats.where((stat) => !testIds.contains(stat.testId)).toList();
|
|
|
|
|
int count = 0;
|
|
|
|
|
for (final stat in statsToDelete) {
|
|
|
|
|
await (_db.delete(_db.testStatistics)..where((ts) => ts.id.equals(stat.id))).go();
|
|
|
|
|
count++;
|
|
|
|
|
}
|
2025-11-16 11:25:27 +00:00
|
|
|
print('deleted $count old test stats');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
int get intervalSeconds => 60 * 30;
|
|
|
|
|
}
|