From 2e2ac9e783fe5799535ce31588623a5cf6039de4 Mon Sep 17 00:00:00 2001 From: Rexios Date: Mon, 20 Nov 2023 22:06:20 -0500 Subject: [PATCH] ... --- .../controller/score_fetch_controller.dart | 4 +-- pub_stats_collector/lib/repo/pub_repo.dart | 29 ++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pub_stats_collector/lib/controller/score_fetch_controller.dart b/pub_stats_collector/lib/controller/score_fetch_controller.dart index 2693a32..d4b7f2e 100644 --- a/pub_stats_collector/lib/controller/score_fetch_controller.dart +++ b/pub_stats_collector/lib/controller/score_fetch_controller.dart @@ -25,7 +25,7 @@ class ScoreFetchController { Future 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:'); @@ -46,7 +46,7 @@ class ScoreFetchController { print('Discord handling completed'); } - Future _handleScore(PackageMetrics metrics, PackageData data) async { + Future _handleData(PackageMetrics metrics, PackageData data) async { final package = metrics.scorecard.packageName; final miniScore = MiniPackageScore( likeCount: data.likeCount, diff --git a/pub_stats_collector/lib/repo/pub_repo.dart b/pub_stats_collector/lib/repo/pub_repo.dart index 14ed695..f5a6150 100644 --- a/pub_stats_collector/lib/repo/pub_repo.dart +++ b/pub_stats_collector/lib/repo/pub_repo.dart @@ -12,8 +12,8 @@ class PubRepo { PubRepo(Credentials credentials) : _client = PubClient(client: UserAgentClient(credentials.userAgent)); - Future fetchAllScores( - Future Function(PackageMetrics metrics, PackageData data) handleScore, + Future fetchAllData( + Future Function(PackageMetrics metrics, PackageData data) handleData, ) async { final packages = await _client.packageNames(); print('Fetched ${packages.length} package names'); @@ -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 handleWrapper(PackageDataWrapper wrapper) async { final metrics = wrapper.metrics; final package = metrics.scorecard.packageName; @@ -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++; @@ -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,