Skip to content

Commit

Permalink
Code cleanup: removing obsolete code. (#7248)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Dec 4, 2023
1 parent a6a52e0 commit 8b2e418
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 154 deletions.
5 changes: 0 additions & 5 deletions app/lib/admin/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import '../package/backend.dart'
show checkPackageVersionParams, packageBackend, purgePackageCache;
import '../package/models.dart';
import '../publisher/models.dart';
import '../scorecard/models.dart';
import '../shared/configuration.dart';
import '../shared/datastore.dart';
import '../shared/email.dart';
Expand Down Expand Up @@ -371,10 +370,6 @@ class AdminBackend {
await _db.deleteWithQuery(
_db.query<PackageVersionAsset>()..filter('package =', packageName));

_logger.info('Removing package from ScoreCard ...');
await _db.deleteWithQuery(
_db.query<ScoreCard>()..filter('packageName =', packageName));

_logger.info('Removing package from Like ...');
await _db.deleteWithQuery(
_db.query<Like>()..filter('packageName =', packageName));
Expand Down
2 changes: 0 additions & 2 deletions app/lib/admin/tools/delete_all_staging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import '../../account/models.dart';
import '../../audit/models.dart';
import '../../package/models.dart';
import '../../publisher/models.dart';
import '../../scorecard/models.dart';
import '../../service/secret/models.dart';
import '../../shared/datastore.dart';
import '../../shared/env_config.dart';
Expand Down Expand Up @@ -46,7 +45,6 @@ Future<String> executeDeleteAllStaging(List<String> args) async {
dbService.query<UserSession>(): 500,
dbService.query<User>(): 500,
dbService.query<Like>(): 500,
dbService.query<ScoreCard>(): 500,
dbService.query<PackageVersionInfo>(): 500,
dbService.query<PackageVersionAsset>(): 100,
dbService.query<PackageVersion>(): 500,
Expand Down
6 changes: 0 additions & 6 deletions app/lib/scorecard/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import '../package/overrides.dart';
import '../shared/datastore.dart' as db;
import '../shared/redis_cache.dart' show cache;
import '../shared/utils.dart';
import '../shared/versions.dart' as versions;

import 'models.dart';

Expand Down Expand Up @@ -160,11 +159,6 @@ class ScoreCardBackend {
return data;
}

/// Deletes the old entries that predate [versions.gcBeforeRuntimeVersion].
Future<void> deleteOldEntries() async {
await _db.deleteWithQuery(_db.query<ScoreCard>());
}

/// Returns the status of a package and version.
Future<PackageStatus> getPackageStatus(String package, String version) async {
final packageKey = _db.emptyKey.append(Package, id: package);
Expand Down
8 changes: 0 additions & 8 deletions app/lib/scorecard/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:pana/models.dart';
import 'package:pub_dev/task/models.dart';

import '../scorecard/backend.dart';
import '../shared/datastore.dart' as db;
import '../shared/popularity_storage.dart';
import '../shared/utils.dart' show jsonUtf8Encoder, utf8JsonDecoder;

Expand All @@ -24,13 +23,6 @@ abstract class ReportStatus {
static const String aborted = 'aborted';
}

/// Summary of various reports for a given PackageVersion.
///
/// The details are pulled in from various data sources, and the entry is
/// recalculated from scratch each time any of the sources change.
@db.Kind(name: 'ScoreCard', idType: db.IdType.String)
class ScoreCard extends db.ExpandoModel<String> {}

@JsonSerializable()
class ScoreCardData {
final String? packageName;
Expand Down
67 changes: 0 additions & 67 deletions app/lib/shared/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import 'package:logging/logging.dart';
import 'package:mime/src/default_extension_map.dart' as mime;
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart' as semver;
import 'package:stream_transform/stream_transform.dart';

export 'package:pana/pana.dart' show exampleFileCandidates;

Expand Down Expand Up @@ -120,36 +119,6 @@ extension VersionIterableExt on Iterable<semver.Version> {
}
}

List<List<T>> _sliceList<T>(List<T> list, int limit) {
if (list.length <= limit) return [list];
final int maxPageIndex = (list.length - 1) ~/ limit;
return List.generate(maxPageIndex + 1,
(p) => list.sublist(p * limit, min(list.length, (p + 1) * limit)));
}

/// Buffers for [duration] and then randomizes the order of the items in the
/// stream. For every single item, their final position would be in the range of
/// [maxPositionDiff] of its original position.
Stream<T> randomizeStream<T>(
Stream<T> stream, {
Duration duration = const Duration(minutes: 1),
int maxPositionDiff = 1000,
Random? random,
}) {
random ??= Random.secure();
final Stream trigger = Stream.periodic(duration);
return stream.buffer(trigger).transform(StreamTransformer.fromHandlers(
handleData: (List<T> items, Sink<T> sink) {
for (List<T> list in _sliceList(items, maxPositionDiff)) {
list.shuffle(random);
for (T task in list) {
sink.add(task);
}
}
},
));
}

class LastNTracker<T extends Comparable<T>> {
final Queue<T> _lastItems = Queue();
final int _n;
Expand All @@ -165,19 +134,6 @@ class LastNTracker<T extends Comparable<T>> {
T? get p90 => _getP(0.9);
T? get p99 => _getP(0.99);

Map<T, int> toCounts() {
return _lastItems.fold<Map<T, int>>({}, (Map<T, int> m, T item) {
m[item] = (m[item] ?? 0) + 1;
return m;
});
}

double get average {
if (_lastItems.isEmpty) return 0.0;
final double sum = _lastItems.whereType<num>().fold(0.0, (a, b) => a + b);
return sum / _lastItems.length;
}

T? _getP(double p) {
if (_lastItems.isEmpty) return null;
final List<T> list = List.from(_lastItems);
Expand All @@ -194,23 +150,6 @@ class DurationTracker extends LastNTracker<Duration> {
};
}

String formatDuration(Duration d) {
final List<String> parts = [];
int minutes = d.inMinutes;
if (minutes == 0) return '0 mins';

int hours = minutes ~/ 60;
minutes = minutes % 60;
final int days = hours ~/ 24;
hours = hours % 24;

if (days > 0) parts.add('$days days');
if (hours > 0) parts.add('$hours hours');
if (minutes > 0) parts.add('$minutes mins');

return parts.join(' ');
}

/// Returns the MIME content type based on the name of the file.
String contentType(String name) {
final ext = p.extension(name).replaceAll('.', '');
Expand Down Expand Up @@ -299,12 +238,6 @@ Map<String, String>? cloudTraceHeaders() {
}
}

extension LoggerExt on Logger {
/// Reports an error [message] with the current stacktrace.
void reportError(String message) =>
shout(message, Exception(message), StackTrace.current);
}

/// Statistics for delete + filter operations.
class DeleteCounts {
/// The number of items found by the query.
Expand Down
13 changes: 0 additions & 13 deletions app/lib/tool/neat_task/pub_dev_tasks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import '../../account/backend.dart';
import '../../account/consent_backend.dart';
import '../../audit/backend.dart';
import '../../package/backend.dart';
import '../../scorecard/backend.dart';
import '../../search/backend.dart';
import '../../service/email/backend.dart';
import '../../service/security_advisories/sync_security_advisories.dart';
Expand Down Expand Up @@ -172,7 +171,6 @@ void _setupGenericPeriodicTasks() {
/// Setup the tasks that we are running in the analyzer service.
void setupAnalyzerPeriodicTasks() {
_setupGenericPeriodicTasks();
_setupJobCleanupPeriodicTasks();

// Checks the Datastore integrity of the model objects.
_weekly(
Expand All @@ -194,17 +192,6 @@ void setupSearchPeriodicTasks() {
);
}

/// Setup the tasks that we are running in both analyzer and dartdoc services.
void _setupJobCleanupPeriodicTasks() {
// Deletes ScoreCard and ScoreCardReport entities that are older than the
// accepted runtime versions.
_weekly(
name: 'delete-old-scorecards',
isRuntimeVersioned: true,
task: () async => await scoreCardBackend.deleteOldEntries(),
);
}

// ignore: non_constant_identifier_names
void _15mins({
required String name,
Expand Down
53 changes: 0 additions & 53 deletions app/test/shared/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,11 @@
// 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:pub_dev/shared/utils.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';

void main() {
group('Randomize Stream', () {
test('Single batch', () async {
final input = List.generate(10, (i) => i);
final Stream<int> randomizedStream = randomizeStream(
Stream.fromIterable(input),
duration: Duration(milliseconds: 100),
random: Random(123),
);
final result = await randomizedStream.toList();
expect(input.every(result.contains), isTrue);
expect(result, isNot(input)); // checks that items are randomized
});

test('Two batches', () async {
final StreamController<int> controller = StreamController<int>();
final Stream<int> randomizedStream = randomizeStream(
controller.stream,
duration: Duration(milliseconds: 100),
random: Random(123),
);
final Future<List<int>> valuesFuture = randomizedStream.toList();
List.generate(8, (i) => i).forEach(controller.add);
await Future.delayed(Duration(milliseconds: 200));
List.generate(8, (i) => i + 10).forEach(controller.add);
await controller.close();
final result = await valuesFuture;
final input = [0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17];
expect(input.every(result.contains), isTrue);
expect(result, isNot(input)); // checks that items are randomized
});

test('Small slices', () async {
final StreamController<int> controller = StreamController<int>();
final Stream<int> randomizedStream = randomizeStream(
controller.stream,
duration: Duration(milliseconds: 100),
maxPositionDiff: 4,
random: Random(123),
);
final Future<List<int>> valuesFuture = randomizedStream.toList();
List.generate(8, (i) => i).forEach(controller.add);
List.generate(8, (i) => i + 10).forEach(controller.add);
await controller.close();
final result = await valuesFuture;
final input = [0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17];
expect(input.every(result.contains), isTrue);
expect(result, isNot(input)); // checks that items are randomized
});
});

group('boundedList', () {
final numbers10 = List.generate(10, (i) => i);

Expand Down

0 comments on commit 8b2e418

Please sign in to comment.