Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/lib/frontend/handlers/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:_pub_shared/data/advisories_api.dart'
import 'package:_pub_shared/utils/sdk_version_cache.dart';
import 'package:meta/meta.dart';
import 'package:neat_cache/neat_cache.dart';
import 'package:pub_dev/service/download_counts/computations.dart';
import 'package:pub_dev/service/security_advisories/backend.dart';
import 'package:pub_dev/shared/versions.dart';
import 'package:pub_dev/task/backend.dart';
Expand Down Expand Up @@ -453,6 +454,8 @@ Future<PackagePageData> loadPackagePageData(
final scoreCardFuture = scoreCardBackend
.getScoreCardData(packageName, versionName, package: package);

final weeklyDownloadCountsFuture = getWeeklyDownloads(package.name!);

await Future.wait([
latestReleasesFuture,
isLikedFuture,
Expand All @@ -461,6 +464,7 @@ Future<PackagePageData> loadPackagePageData(
assetFuture,
isAdminFuture,
scoreCardFuture,
weeklyDownloadCountsFuture,
]);

final selectedVersion = await selectedVersionFuture;
Expand All @@ -484,6 +488,7 @@ Future<PackagePageData> loadPackagePageData(
scoreCard: await scoreCardFuture,
isAdmin: await isAdminFuture,
isLiked: await isLikedFuture,
weeklyDownloadCounts: await weeklyDownloadCountsFuture,
);
}

Expand Down
21 changes: 21 additions & 0 deletions app/lib/frontend/templates/views/pkg/info_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// 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 'package:_pub_shared/format/encoding.dart';
import 'package:pana/pana.dart';
import 'package:pub_dev/service/download_counts/download_counts.dart';
import 'package:pubspec_parse/pubspec_parse.dart' as pubspek;

import '../../../../package/models.dart';
Expand Down Expand Up @@ -69,6 +71,8 @@ d.Node packageInfoBoxNode({
}
return d.fragment([
labeledScores,
if (data.weeklyDownloadCounts != null)
_downloadsChart(data.weeklyDownloadCounts!),
if (thumbnailUrl != null)
d.div(classes: [
'detail-screenshot-thumbnail'
Expand Down Expand Up @@ -105,6 +109,23 @@ d.Node packageInfoBoxNode({
]);
}

d.Node _downloadsChart(WeeklyDownloadCounts wdc) {
final container = d.div(
classes: ['weekly-downloads-sparkline'],
id: '-weekly-downloads-sparkline',
attributes: {
'data-widget': 'weekly-sparkline',
'data-weekly-sparkline-points':
_encodeForWeeklySparkline(wdc.weeklyDownloads, wdc.newestDate),
});
return container;
}

String _encodeForWeeklySparkline(List<int> downloads, DateTime newestDate) {
final date = newestDate.toUtc().millisecondsSinceEpoch ~/ 1000;
return encodeIntsAsLittleEndianBase64String([date, ...downloads]);
}

d.Node _publisher(String? publisherId) {
return _block(
'Publisher',
Expand Down
3 changes: 3 additions & 0 deletions app/lib/package/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:clock/clock.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:pana/models.dart';
import 'package:pub_dev/service/download_counts/backend.dart';
import 'package:pub_dev/service/download_counts/download_counts.dart';
import 'package:pub_dev/shared/markdown.dart';
import 'package:pub_semver/pub_semver.dart';

Expand Down Expand Up @@ -1132,6 +1133,7 @@ class PackagePageData {
final ScoreCardData scoreCard;
final bool isAdmin;
final bool isLiked;
final WeeklyDownloadCounts? weeklyDownloadCounts;
PackageView? _view;

PackagePageData({
Expand All @@ -1143,6 +1145,7 @@ class PackagePageData {
required this.scoreCard,
required this.isAdmin,
required this.isLiked,
required this.weeklyDownloadCounts,
}) : latestReleases = latestReleases ?? package.latestReleases;

bool get hasReadme => versionInfo.assets.contains(AssetKind.readme);
Expand Down
14 changes: 14 additions & 0 deletions app/lib/service/download_counts/computations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import 'dart:math';

import 'package:gcloud/storage.dart';
import 'package:pub_dev/service/download_counts/backend.dart';
import 'package:pub_dev/service/download_counts/download_counts.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';

import '../../shared/redis_cache.dart' show cache;

Future<void> compute30DaysTotalTask() async {
final allDownloadCounts = await downloadCountsBackend.listAllDownloadCounts();
final totals = await compute30DayTotals(allDownloadCounts);
Expand Down Expand Up @@ -43,6 +46,17 @@ Future<void> upload30DaysTotal(Map<String, int> counts) async {
jsonUtf8Encoder.convert(counts));
}

Future<WeeklyDownloadCounts?> getWeeklyDownloads(String package) async {
return (await cache.weeklyDownloadCounts(package).get(() async {
final wdc = await computeWeeklyDownloads(package);
if (wdc.newestDate == null) {
return null;
}
return WeeklyDownloadCounts(
weeklyDownloads: wdc.weeklyDownloads, newestDate: wdc.newestDate!);
}));
}

/// Computes `weeklyDownloads` starting from `newestDate` for [package].
///
/// Each number in `weeklyDownloads` is the total number of downloads for
Expand Down
15 changes: 15 additions & 0 deletions app/lib/service/download_counts/download_counts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,18 @@ class CountData {
_$CountDataFromJson(json);
Map<String, dynamic> toJson() => _$CountDataToJson(this);
}

@JsonSerializable(includeIfNull: false)
class WeeklyDownloadCounts {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not pretty, but you could just pass around the binary encoding 🤣 Or the base64 encoding.

This could also be used in the redis cache. Then you won't have to JSON decode, and reencode as binary for every request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is indeed an optimization. But as you say it's not pretty. Also the other things we store in redis are human readable strings which is nice for debugging.

final List<int> weeklyDownloads;
final DateTime newestDate;

WeeklyDownloadCounts({
required this.weeklyDownloads,
required this.newestDate,
});

factory WeeklyDownloadCounts.fromJson(Map<String, dynamic> json) =>
_$WeeklyDownloadCountsFromJson(json);
Map<String, dynamic> toJson() => _$WeeklyDownloadCountsToJson(this);
}
16 changes: 16 additions & 0 deletions app/lib/service/download_counts/download_counts.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions app/lib/shared/redis_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ class CachePatterns {
decode: (d) => CountData.fromJson(d as Map<String, dynamic>),
))[package];

Entry<WeeklyDownloadCounts> weeklyDownloadCounts(String package) => _cache
.withPrefix('weekly-download-counts/')
.withTTL(Duration(hours: 6))
.withCodec(utf8)
.withCodec(json)
.withCodec(wrapAsCodec(
encode: (WeeklyDownloadCounts wdc) => wdc.toJson(),
decode: (d) => WeeklyDownloadCounts.fromJson(d as Map<String, dynamic>),
Comment on lines +253 to +257
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default it's possible to store binary data in redis.

That's why we have .withCodec(utf8).

So why not just store:

base64.encode(Uint8List.sublistView(Uint32List.fromList(dataPoints)));

Or:

return Uint8List.sublistView(Uint32List.fromList(dataPoints));

))[package];

Entry<List<LikeData>> userPackageLikes(String userId) => _cache
.withPrefix('user-package-likes/')
.withTTL(Duration(minutes: 60))
Expand Down
Loading
Loading