Skip to content

Refactor /score and /options endpoint #8774

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 1 commit into from
May 20, 2025
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
44 changes: 2 additions & 42 deletions app/lib/frontend/handlers/custom_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import '../../scorecard/backend.dart';
import '../../search/backend.dart';
import '../../search/search_client.dart';
import '../../search/search_service.dart';
import '../../service/download_counts/backend.dart';
import '../../service/topics/count_topics.dart';
import '../../shared/configuration.dart';
import '../../shared/exceptions.dart';
Expand Down Expand Up @@ -218,38 +217,7 @@ Future<VersionScore> packageVersionScoreHandler(
{String? version}) async {
checkPackageVersionParams(package, version);
return (await cache.versionScore(package, version).get(() async {
final pkg = await packageBackend.lookupPackage(package);
if (pkg == null) {
throw NotFoundException.resource('package "$package"');
}
final v =
(version == null || version == 'latest') ? pkg.latestVersion! : version;
final pv = await packageBackend.lookupPackageVersion(package, v);
if (pv == null) {
throw NotFoundException.resource('package "$package" version "$version"');
}

var updated = pkg.updated;
final card = await scoreCardBackend.getScoreCardData(package, v);
if (updated == null || card.updated?.isAfter(updated) == true) {
updated = card.updated;
}

final tags = <String>{
...pkg.getTags(),
...pv.getTags(),
...?card.derivedTags,
};

return VersionScore(
grantedPoints: card.grantedPubPoints,
maxPoints: card.maxPubPoints,
likeCount: pkg.likes,
downloadCount30Days:
downloadCountsBackend.lookup30DaysTotalCounts(package),
tags: tags.toList(),
lastUpdated: updated,
);
return await scoreCardBackend.getVersionScore(package, version: version);
}))!;
}

Expand Down Expand Up @@ -475,15 +443,7 @@ Future<PkgOptions> getPackageOptionsHandler(
shelf.Request request,
String package,
) async {
checkPackageVersionParams(package);
final p = await packageBackend.lookupPackage(package);
if (p == null) {
throw NotFoundException.resource(package);
}
return PkgOptions(
isDiscontinued: p.isDiscontinued,
isUnlisted: p.isUnlisted,
);
return await packageBackend.getPackageOptions(package);
}

/// Handles `PUT /api/packages/<package>/options`.
Expand Down
14 changes: 14 additions & 0 deletions app/lib/package/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,20 @@ class PackageBackend {
return count;
}

/// Returns the package options.
Future<api.PkgOptions> getPackageOptions(String package) async {
checkPackageVersionParams(package);
final p = await packageBackend.lookupPackage(package);
if (p == null) {
throw NotFoundException.resource(package);
}
return api.PkgOptions(
isDiscontinued: p.isDiscontinued,
replacedBy: p.replacedBy,
isUnlisted: p.isUnlisted,
);
}

/// Updates [options] on [package].
Future<void> updateOptions(String package, api.PkgOptions options) async {
final authenticatedUser = await requireAuthenticatedWebUser();
Expand Down
39 changes: 39 additions & 0 deletions app/lib/scorecard/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import 'dart:async';

import 'package:_pub_shared/data/package_api.dart';
import 'package:clock/clock.dart';
import 'package:gcloud/service_scope.dart' as ss;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:pool/pool.dart';
import 'package:pub_dev/service/download_counts/backend.dart';
import 'package:pub_dev/service/download_counts/computations.dart';
import 'package:pub_dev/shared/exceptions.dart';
import 'package:pub_dev/task/backend.dart';
Expand Down Expand Up @@ -186,6 +188,43 @@ class ScoreCardBackend {
final pv = list[1] as PackageVersion?;
return PackageStatus.fromModels(p, pv);
}

/// Return the version score object served in the API.
Future<VersionScore> getVersionScore(String package,
{String? version}) async {
final pkg = await packageBackend.lookupPackage(package);
if (pkg == null) {
throw NotFoundException.resource('package "$package"');
}
final v =
(version == null || version == 'latest') ? pkg.latestVersion! : version;
final pv = await packageBackend.lookupPackageVersion(package, v);
if (pv == null) {
throw NotFoundException.resource('package "$package" version "$version"');
}

var updated = pkg.updated;
final card = await scoreCardBackend.getScoreCardData(package, v);
if (updated == null || card.updated?.isAfter(updated) == true) {
updated = card.updated;
}

final tags = <String>{
...pkg.getTags(),
...pv.getTags(),
...?card.derivedTags,
};

return VersionScore(
grantedPoints: card.grantedPubPoints,
maxPoints: card.maxPubPoints,
likeCount: pkg.likes,
downloadCount30Days:
downloadCountsBackend.lookup30DaysTotalCounts(package),
tags: tags.toList(),
lastUpdated: updated,
);
}
}

Future<void> purgeScorecardData(
Expand Down