Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Nov 21, 2023
1 parent f2b116a commit 2e2ac9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ScoreFetchController {

Future<void> fetchScores() async {
final startTime = DateTime.now();
final globalStats = await _pub.fetchAllScores(_handleScore);
final globalStats = await _pub.fetchAllData(_handleData);

await _database.writeGlobalStats(globalStats);
print('Global stats:');
Expand All @@ -46,7 +46,7 @@ class ScoreFetchController {
print('Discord handling completed');
}

Future<void> _handleScore(PackageMetrics metrics, PackageData data) async {
Future<void> _handleData(PackageMetrics metrics, PackageData data) async {
final package = metrics.scorecard.packageName;
final miniScore = MiniPackageScore(
likeCount: data.likeCount,
Expand Down
29 changes: 22 additions & 7 deletions pub_stats_collector/lib/repo/pub_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class PubRepo {
PubRepo(Credentials credentials)
: _client = PubClient(client: UserAgentClient(credentials.userAgent));

Future<GlobalStats> fetchAllScores(
Future<void> Function(PackageMetrics metrics, PackageData data) handleScore,
Future<GlobalStats> fetchAllData(
Future<void> Function(PackageMetrics metrics, PackageData data) handleData,
) async {
final packages = await _client.packageNames();
print('Fetched ${packages.length} package names');
Expand Down Expand Up @@ -100,16 +100,16 @@ class PubRepo {
try {
await fetchPackageData(package);
} catch (e) {
print('Error processing package $package: $e');
print('Error fetching data for $package: $e');
}
}),
);
}
await queue.tasksComplete;
print('Fetched all scores');
print('Fetched all data');

var handled = 0;
for (final wrapper in wrappers) {
Future<void> handleWrapper(PackageDataWrapper wrapper) async {
final metrics = wrapper.metrics;
final package = metrics.scorecard.packageName;

Expand All @@ -121,10 +121,10 @@ class PubRepo {
mostDependedPackage = (package, dependents.length);
}

await handleScore(metrics, data).timeout(
await handleData(metrics, data).timeout(
Duration(seconds: 30),
onTimeout: () =>
throw TimeoutException('Timeout handling score for $package'),
throw TimeoutException('Timeout handling wrapper for $package'),
);

handled++;
Expand All @@ -133,6 +133,21 @@ class PubRepo {
}
}

for (final wrapper in wrappers) {
unawaited(
queue.add(() async {
final package = wrapper.metrics.scorecard.packageName;
try {
await handleWrapper(wrapper);
} catch (e) {
print('Error handling wrapper $package: $e');
}
}),
);
}
await queue.tasksComplete;
print('Handled all wrappers');

return GlobalStats(
packageCount: packages.length,
mostLikedPackage: mostLikedPackage.$1,
Expand Down

0 comments on commit 2e2ac9e

Please sign in to comment.