-
Notifications
You must be signed in to change notification settings - Fork 166
compute and upload 30 day total download counts #8091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/lib/service/download_counts/compute_30_days_total_counts.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
| import 'dart:async'; | ||
| import 'dart:math'; | ||
|
|
||
| import 'package:gcloud/storage.dart'; | ||
| import 'package:pub_dev/service/download_counts/backend.dart'; | ||
| import 'package:pub_dev/service/download_counts/models.dart'; | ||
| import 'package:pub_dev/shared/configuration.dart'; | ||
| import 'package:pub_dev/shared/storage.dart'; | ||
| import 'package:pub_dev/shared/utils.dart'; | ||
|
|
||
| Future<void> compute30DaysTotalTask() async { | ||
| final allDownloadCounts = await downloadCountsBackend.listAllDownloadCounts(); | ||
| final totals = await compute30DayTotals(allDownloadCounts); | ||
| await updload30DaysTotal(totals); | ||
| } | ||
|
|
||
| Future<Map<String, int>> compute30DayTotals( | ||
| Stream<DownloadCounts> downloadCounts) async { | ||
| final res = <String, int>{}; | ||
| await for (final dc in downloadCounts) { | ||
| res[dc.package] = compute30DayTotal(dc); | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| int compute30DayTotal(DownloadCounts downloadCounts) { | ||
| final totals = downloadCounts.countData.totalCounts; | ||
| return totals | ||
| .take(30) | ||
| .fold(0, (previousValue, element) => previousValue + max(0, element)); | ||
| } | ||
|
|
||
| final downloadCounts30DaysTotalsFileName = 'download-counts-30-days-total.json'; | ||
|
|
||
| Future<void> updload30DaysTotal(Map<String, int> counts) async { | ||
szakarias marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final reportsBucket = | ||
| storageService.bucket(activeConfiguration.reportsBucketName!); | ||
| await uploadBytesWithRetry(reportsBucket, downloadCounts30DaysTotalsFileName, | ||
| jsonUtf8Encoder.convert(counts)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
app/test/service/download_counts/compute_total_download_counts_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:basics/basics.dart'; | ||
| import 'package:gcloud/storage.dart'; | ||
| import 'package:pub_dev/service/download_counts/backend.dart'; | ||
| import 'package:pub_dev/service/download_counts/compute_30_days_total_counts.dart'; | ||
| import 'package:pub_dev/shared/configuration.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import '../../shared/test_services.dart'; | ||
|
|
||
| void main() { | ||
| group('', () { | ||
| testWithProfile('compute download counts 30 day totals', fn: () async { | ||
| final pkg = 'foo'; | ||
| final versionsCounts = { | ||
| '1.0.1': 2, | ||
| '2.0.0-alpha': 2, | ||
| '2.0.0': 2, | ||
| '2.1.0': 2, | ||
| '3.1.0': 2, | ||
| '4.0.0-0': 2, | ||
| '6.1.0': 2, | ||
| }; | ||
| final date = DateTime.parse('1986-02-16'); | ||
| var downloadCounts1 = await downloadCountsBackend.updateDownloadCounts( | ||
| pkg, versionsCounts, date); | ||
| for (var i = 1; i < 5; i++) { | ||
| downloadCounts1 = await downloadCountsBackend.updateDownloadCounts( | ||
| pkg, versionsCounts, date.addCalendarDays(i)); | ||
| } | ||
|
|
||
| expect(compute30DayTotal(downloadCounts1), 70); | ||
|
|
||
| final pkg2 = 'bar'; | ||
| final versionsCounts2 = { | ||
| '1.0.1': 3, | ||
| '2.0.0-alpha': 3, | ||
| '2.0.0': 3, | ||
| '2.1.0': 3, | ||
| '3.1.0': 3, | ||
| '4.0.0-0': 3, | ||
| '6.1.0': 3, | ||
| }; | ||
| var downloadCounts2 = await downloadCountsBackend.updateDownloadCounts( | ||
| pkg2, versionsCounts2, date); | ||
| for (var i = 1; i < 5; i++) { | ||
| downloadCounts2 = await downloadCountsBackend.updateDownloadCounts( | ||
| pkg2, versionsCounts2, date.addCalendarDays(i)); | ||
| } | ||
|
|
||
| expect(compute30DayTotal(downloadCounts2), 105); | ||
|
|
||
| final pkg3 = 'baz'; | ||
| final versionsCounts3 = { | ||
| '1.0.1': 4, | ||
| '2.0.0-alpha': 4, | ||
| '2.0.0': 4, | ||
| '2.1.0': 4, | ||
| '3.1.0': 4, | ||
| '4.0.0-0': 4, | ||
| '6.1.0': 4, | ||
| }; | ||
| var downloadCounts3 = await downloadCountsBackend.updateDownloadCounts( | ||
| pkg3, versionsCounts3, date); | ||
| for (var i = 1; i < 5; i++) { | ||
| downloadCounts3 = await downloadCountsBackend.updateDownloadCounts( | ||
| pkg3, versionsCounts3, date.addCalendarDays(i)); | ||
| } | ||
| expect(compute30DayTotal(downloadCounts3), 140); | ||
|
|
||
| final downloadCounts = [ | ||
| downloadCounts1, | ||
| downloadCounts2, | ||
| downloadCounts3 | ||
| ]; | ||
|
|
||
| final res = await compute30DayTotals(Stream.fromIterable(downloadCounts)); | ||
|
|
||
| expect( | ||
| res, | ||
| {'foo': 70, 'bar': 105, 'baz': 140}, | ||
| ); | ||
| }); | ||
|
|
||
| testWithProfile('succesful 30 day totals upload', fn: () async { | ||
| await updload30DaysTotal({'foo': 70, 'bar': 105, 'baz': 140}); | ||
|
|
||
| final data = await storageService | ||
| .bucket(activeConfiguration.reportsBucketName!) | ||
| .read(downloadCounts30DaysTotalsFileName) | ||
| .transform(utf8.decoder) | ||
| .transform(json.decoder) | ||
| .single; | ||
|
|
||
| expect(data, {'foo': 70, 'bar': 105, 'baz': 140}); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not merge these with
listAllDownloadCounts, and make a single method?Passing around streams is a bit scary, even if I think our stream will retry individual pages as it fetches the entire result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think logically this is the separation that makes sense....