Skip to content

Proposal: ScoreCard model #1398

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 3 commits into from
Jun 28, 2018
Merged
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
120 changes: 120 additions & 0 deletions app/lib/scorecard/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (c) 2018, 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 'dart:io';

import 'package:gcloud/db.dart' as db;
import 'package:meta/meta.dart';

import 'package:pub_dartlang_org/search/scoring.dart'
show calculateOverallScore;

import '../shared/model_properties.dart';

String _id(String packageName, String packageVersion) =>
'$packageName/$packageVersion';

final _gzipCodec = new GZipCodec();

/// Summary of various reports for a given PackageVersion.
@db.Kind(name: 'ScoreCard', idType: db.IdType.String)
class ScoreCard extends db.ExpandoModel {
@db.StringProperty(required: true)
String packageName;

@db.StringProperty(required: true)
String packageVersion;

@db.DateTimeProperty(required: true)
DateTime packageCreated;

@db.DateTimeProperty(required: true)
DateTime packageVersionCreated;

/// Whether the package has its discontinued flag set.
@db.BoolProperty()
bool isDiscontinued;

/// The platform tags (flutter, web, other) set by `pana` analysis.
@CompatibleStringListProperty()
List<String> panaPlatformTags;

/// Score for documentation coverage (0.0 - 1.0).
@db.DoubleProperty()
double documentationScore;

/// Score for code health (0.0 - 1.0).
@db.DoubleProperty()
double healthScore;

/// Score for package maintenance (0.0 - 1.0).
@db.DoubleProperty()
double maintenanceScore;

/// Score for package popularity (0.0 - 1.0).
@db.DoubleProperty()
double popularityScore;

ScoreCard();

ScoreCard.init({
@required this.packageName,
@required this.packageVersion,
@required this.packageCreated,
@required this.packageVersionCreated,
}) {
id = _id(packageName, packageVersion);
}

double get overallScore =>
// TODO: use documentationScore too
calculateOverallScore(
health: healthScore ?? 0.0,
maintenance: maintenanceScore ?? 0.0,
popularity: popularityScore ?? 0.0,
);
}

/// Detail of a specific report for a given PackageVersion.
@db.Kind(name: 'ScoreCardReport', idType: db.IdType.String)
class ScoreCardReport extends db.ExpandoModel {
@db.StringProperty(required: true)
String packageName;

@db.StringProperty(required: true)
String packageVersion;

@db.StringProperty(required: true)
String reportType;

@db.BlobProperty()
List<int> reportJsonGz;

ScoreCardReport();

ScoreCardReport.init({
@required this.packageName,
@required this.packageVersion,
@required this.reportType,
}) {
parentKey = db.dbService.emptyKey
.append(ScoreCard, id: _id(packageName, packageVersion));
id = reportType;
}

Map<String, dynamic> get reportJson {
if (reportJsonGz == null) return null;
return json.decode(utf8.decode(_gzipCodec.decode(reportJsonGz)))
as Map<String, dynamic>;
}

set reportJson(Map<String, dynamic> map) {
if (map == null) {
reportJsonGz = null;
} else {
reportJsonGz = _gzipCodec.encode(utf8.encode(json.encode(map)));
}
}
}