Skip to content

Commit

Permalink
Handle diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Nov 21, 2023
1 parent 87433e3 commit c155a28
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 78 deletions.
42 changes: 11 additions & 31 deletions pub_stats_collector/lib/controller/score_fetch_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:async';

import 'package:pub_api_client/pub_api_client.dart' hide Credentials;
import 'package:pub_stats_collector/credential/credentials.dart';
import 'package:pub_stats_collector/model/diff.dart';
import 'package:pub_stats_collector/repo/database_repo.dart';
import 'package:pub_stats_collector/repo/discord_repo.dart';
import 'package:pub_stats_collector/repo/pub_repo.dart';
Expand Down Expand Up @@ -60,6 +59,9 @@ class ScoreFetchController {
if (publisher != null) ..._alertConfigs['publisher:$publisher'] ?? [],
];

final previousData = _data[package];
final diff = data.diffFrom(previousData);

// Don't alert for discontinued packages
if (!data.isDiscontinued) {
await Future.wait(
Expand All @@ -68,8 +70,7 @@ class ScoreFetchController {
package: package,
configs: configs,
score: metrics.score,
data: data,
previousData: _data[package],
diff: diff,
),
),
);
Expand All @@ -82,39 +83,18 @@ class ScoreFetchController {
);

await _database.writePackageData(package, data);

if (diff.isNotEmpty) {
await _database.writePackageDiff(package, diff);
}
}

Future<void> processAlert({
required String package,
required List<AlertConfig> configs,
required PackageScore score,
required PackageData data,
required PackageData? previousData,
required PackageDataDiff diff,
}) async {
final Map<PackageDataField, Diff> changes;
if (previousData != null) {
changes = {
PackageDataField.publisher:
StringDiff(previousData.publisher ?? '', data.publisher ?? ''),
PackageDataField.version:
StringDiff(previousData.version, data.version),
PackageDataField.likeCount:
StringDiff(previousData.likeCount, data.likeCount),
PackageDataField.popularityScore:
StringDiff(previousData.popularityScore, data.popularityScore),
PackageDataField.isDiscontinued:
StringDiff(previousData.isDiscontinued, data.isDiscontinued),
PackageDataField.isUnlisted:
StringDiff(previousData.isUnlisted, data.isUnlisted),
PackageDataField.isFlutterFavorite:
StringDiff(previousData.isFlutterFavorite, data.isFlutterFavorite),
PackageDataField.dependents:
SetDiff(previousData.dependents, data.dependents),
}..removeWhere((key, value) => !value.different);
} else {
changes = {};
}

final warnings = <PackageDataField, String>{};
final grantedPoints = score.grantedPoints;
final maxPoints = score.maxPoints;
Expand All @@ -125,11 +105,11 @@ class ScoreFetchController {
'Only $grantedPoints/$maxPoints pub points';
}

if (changes.isEmpty && warnings.isEmpty) return;
if (diff.isEmpty && warnings.isEmpty) return;

for (final config in configs) {
final filteredChanges = Map.fromEntries(
changes.entries.where((e) => !config.ignore.contains(e.key)),
diff.entries.where((e) => !config.ignore.contains(e.key)),
);
final filteredWarnings = Map.fromEntries(
warnings.entries.where((e) => !config.ignore.contains(e.key)),
Expand Down
39 changes: 0 additions & 39 deletions pub_stats_collector/lib/model/diff.dart

This file was deleted.

9 changes: 9 additions & 0 deletions pub_stats_collector/lib/repo/database_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class DatabaseRepo {
.set(score.toJson());
}

Future<void> writePackageDiff(String package, PackageDataDiff diff) {
return _database
.ref()
.child('diffs')
.child(package)
.child(DateTime.now().secondsSinceEpoch.toString())
.set(diff.toJson());
}

Future<void> writeGlobalStats(GlobalStats stats) async {
await _database.ref().child('global_stats').set(stats.toJson());

Expand Down
1 change: 0 additions & 1 deletion pub_stats_collector/lib/repo/discord_repo.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:discord_interactions/discord_interactions.dart';
import 'package:pub_stats_collector/credential/credentials.dart';
import 'package:pub_stats_collector/model/diff.dart';
import 'package:pub_stats_core/pub_stats_core.dart';
import 'package:recase/recase.dart';

Expand Down
3 changes: 2 additions & 1 deletion pub_stats_collector/lib/repo/pub_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class PubRepo {
Future<GlobalStats> fetchAllData(
Future<void> Function(PackageMetrics metrics, PackageData data) handleData,
) async {
final packages = await _client.packageNames();
// final packages = await _client.packageNames();
final packages = {'rexios_lints'};
print('Fetched ${packages.length} package names');

var mostLikedPackage = ('', 0);
Expand Down
2 changes: 1 addition & 1 deletion pub_stats_collector/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ packages:
source: hosted
version: "1.1.1"
collection:
dependency: "direct main"
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
Expand Down
1 change: 0 additions & 1 deletion pub_stats_collector/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dependencies:
discord_interactions: ^0.0.15
flutter_tools_task_queue: ^1.0.0
recase: ^4.1.0
collection: ^1.18.0

dev_dependencies:
rexios_lints: ^6.0.1
Expand Down
1 change: 1 addition & 0 deletions pub_stats_core/lib/pub_stats_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library pub_stats_core;

// model
export 'src/model/alert_config.dart';
export 'src/model/diff.dart';
export 'src/model/global_stats.dart';
export 'src/model/mini_package_score.dart';
export 'src/model/package_data.dart';
Expand Down
60 changes: 60 additions & 0 deletions pub_stats_core/lib/src/model/diff.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'diff.g.dart';

abstract class Diff {
bool get different;
String get text;

Map<String, dynamic> toJson();
}

@JsonSerializable()
class StringDiff extends Diff {
final Object before;
final Object after;

StringDiff(this.before, this.after);

@override
bool get different => before != after;

@override
String get text => '$before -> $after';

factory StringDiff.fromJson(Map<String, dynamic> json) =>
_$StringDiffFromJson(json);

@override
Map<String, dynamic> toJson() => _$StringDiffToJson(this);
}

@JsonSerializable(constructor: '_')
class SetDiff extends Diff {
final Set added;
final Set removed;

SetDiff._({
this.added = const {},
this.removed = const {},
});

SetDiff(Set before, Set after)
: added = after.difference(before),
removed = before.difference(after);

@override
bool get different => added.isNotEmpty || removed.isNotEmpty;

@override
String get text => [
if (added.isNotEmpty) 'Added: ${added.join(', ')}',
if (removed.isNotEmpty) 'Removed: ${removed.join(', ')}',
].join('\n');

factory SetDiff.fromJson(Map<String, dynamic> json) =>
_$SetDiffFromJson(json);

@override
Map<String, dynamic> toJson() => _$SetDiffToJson(this);
}
30 changes: 30 additions & 0 deletions pub_stats_core/lib/src/model/diff.g.dart

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

54 changes: 53 additions & 1 deletion pub_stats_core/lib/src/model/package_data.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// ignore_for_file: invalid_annotation_target

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:pub_stats_core/src/model/diff.dart';

part 'package_data.freezed.dart';
part 'package_data.g.dart';

@freezed
class PackageData with _$PackageData {
const PackageData._();

const factory PackageData({
@JsonKey(name: 'p') String? publisher,
@JsonKey(name: 'v') required String version,
Expand All @@ -20,6 +23,24 @@ class PackageData with _$PackageData {

factory PackageData.fromJson(Map<String, dynamic> json) =>
_$PackageDataFromJson(json);

PackageDataDiff diffFrom(PackageData? before) {
if (before == null) return {};
return {
PackageDataField.publisher:
StringDiff(before.publisher ?? '', publisher ?? ''),
PackageDataField.version: StringDiff(before.version, version),
PackageDataField.likeCount: StringDiff(before.likeCount, likeCount),
PackageDataField.popularityScore:
StringDiff(before.popularityScore, popularityScore),
PackageDataField.isDiscontinued:
StringDiff(before.isDiscontinued, isDiscontinued),
PackageDataField.isUnlisted: StringDiff(before.isUnlisted, isUnlisted),
PackageDataField.isFlutterFavorite:
StringDiff(before.isFlutterFavorite, isFlutterFavorite),
PackageDataField.dependents: SetDiff(before.dependents, dependents),
}..removeWhere((key, value) => !value.different);
}
}

enum PackageDataField {
Expand All @@ -33,5 +54,36 @@ enum PackageDataField {
dependents,

// Extra fields not actually on the PackageData model
pubPoints,
pubPoints;

Diff diffFromJson(Map<String, dynamic> json) {
switch (this) {
case PackageDataField.publisher:
case PackageDataField.version:
case PackageDataField.likeCount:
case PackageDataField.popularityScore:
case PackageDataField.isDiscontinued:
case PackageDataField.isUnlisted:
case PackageDataField.isFlutterFavorite:
return StringDiff.fromJson(json);
case PackageDataField.dependents:
return SetDiff.fromJson(json);
case PackageDataField.pubPoints:
throw UnimplementedError();
}
}
}

typedef PackageDataDiff = Map<PackageDataField, Diff>;

extension PackageDataDiffExtension on PackageDataDiff {
static Map<PackageDataField, Diff> fromJson(Map<String, dynamic> json) =>
json.map(
(k, v) {
final field = PackageDataField.values.byName(k);
return MapEntry(field, field.diffFromJson(v));
},
);

Map<String, dynamic> toJson() => map((k, v) => MapEntry(k.name, v.toJson()));
}
8 changes: 5 additions & 3 deletions pub_stats_core/lib/src/model/package_data.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class __$$PackageDataImplCopyWithImpl<$Res>

/// @nodoc
@JsonSerializable()
class _$PackageDataImpl implements _PackageData {
class _$PackageDataImpl extends _PackageData {
const _$PackageDataImpl(
{@JsonKey(name: 'p') this.publisher,
@JsonKey(name: 'v') required this.version,
Expand All @@ -207,7 +207,8 @@ class _$PackageDataImpl implements _PackageData {
@JsonKey(name: 'iu') required this.isUnlisted,
@JsonKey(name: 'iff') required this.isFlutterFavorite,
@JsonKey(name: 'd') final Set<String> dependents = const {}})
: _dependents = dependents;
: _dependents = dependents,
super._();

factory _$PackageDataImpl.fromJson(Map<String, dynamic> json) =>
_$$PackageDataImplFromJson(json);
Expand Down Expand Up @@ -296,7 +297,7 @@ class _$PackageDataImpl implements _PackageData {
}
}

abstract class _PackageData implements PackageData {
abstract class _PackageData extends PackageData {
const factory _PackageData(
{@JsonKey(name: 'p') final String? publisher,
@JsonKey(name: 'v') required final String version,
Expand All @@ -306,6 +307,7 @@ abstract class _PackageData implements PackageData {
@JsonKey(name: 'iu') required final bool isUnlisted,
@JsonKey(name: 'iff') required final bool isFlutterFavorite,
@JsonKey(name: 'd') final Set<String> dependents}) = _$PackageDataImpl;
const _PackageData._() : super._();

factory _PackageData.fromJson(Map<String, dynamic> json) =
_$PackageDataImpl.fromJson;
Expand Down

0 comments on commit c155a28

Please sign in to comment.