Skip to content

Commit 184cdab

Browse files
committed
Prepare data for downloads chart
1 parent db8039a commit 184cdab

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:_pub_shared/data/download_counts_data.dart';
6+
7+
Iterable<String> prepareRanges(List<VersionRangeCount> rangeDownloads) {
8+
return rangeDownloads.map((e) => e.versionRange);
9+
}
10+
11+
/// Returns an iterable containing data to be shown in a chart displaying the
12+
/// ranges in [rangeDownloads].
13+
///
14+
/// The 'i'th entry in the iterable is a list of the download values
15+
/// (y coordinates) for the 'i'th week (x coordinate).
16+
Iterable<List<int>> prepareWeekLists(
17+
List<int> totals,
18+
List<VersionRangeCount> rangeDownloads,
19+
int displayLength,
20+
) {
21+
final result = <List<int>>[];
22+
23+
final showOther =
24+
totals[0] > rangeDownloads.fold(0, (sum, d) => sum + d.counts[0]);
25+
26+
for (int week = 0; week < displayLength; week++) {
27+
final weekList = <int>[];
28+
if (showOther) {
29+
weekList.add(totals[week] -
30+
rangeDownloads.fold(0, (sum, d) => sum + d.counts[week]));
31+
}
32+
rangeDownloads.forEach((d) => weekList.add(d.counts[week]));
33+
result.add(weekList);
34+
}
35+
return result.reversed;
36+
}

pkg/web_app/lib/src/widget/downloads_chart/widget.dart

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'dart:convert';
77
import 'package:_pub_shared/data/download_counts_data.dart';
88
import 'package:web/web.dart';
99

10+
import 'computations.dart';
11+
1012
void create(HTMLElement element, Map<String, String> options) {
1113
final dataPoints = options['points'];
1214
if (dataPoints == null) {
@@ -18,7 +20,35 @@ void create(HTMLElement element, Map<String, String> options) {
1820
final data = WeeklyVersionDownloadCounts.fromJson((utf8.decoder
1921
.fuse(json.decoder)
2022
.convert(base64Decode(dataPoints)) as Map<String, dynamic>));
21-
drawChart(svg, data);
23+
24+
final weeksToDisplay = data.totalWeeklyDownloads.length > 28
25+
? 28
26+
: data.totalWeeklyDownloads.length;
27+
28+
final majorDisplayLists = prepareWeekLists(
29+
data.totalWeeklyDownloads,
30+
data.majorRangeWeeklyDownloads,
31+
weeksToDisplay,
32+
);
33+
final majorRanges = data.majorRangeWeeklyDownloads.map((e) => e.versionRange);
34+
35+
// final minorDisplayLists = prepareWeekLists(
36+
// data.totalWeeklyDownloads,
37+
// data.minorRangeWeeklyDownloads,
38+
// weeksToDisplay,
39+
// );
40+
// final minorRanges = data.minorRangeWeeklyDownloads.map((e) => e.versionRange);
41+
42+
// final patchDisplayLists = prepareWeekLists(
43+
// data.totalWeeklyDownloads,
44+
// data.patchRangeWeeklyDownloads,
45+
// weeksToDisplay,
46+
// );
47+
// final patchRanges = data.patchRangeWeeklyDownloads.map((e) => e.versionRange);
48+
49+
drawChart(svg, majorRanges, majorDisplayLists, data.newestDate);
2250
}
2351

24-
void drawChart(Element svg, WeeklyVersionDownloadCounts data) {}
52+
void drawChart(Element svg, Iterable<String> ranges, Iterable<List<int>> values,
53+
DateTime newestData,
54+
{bool stacked = true}) {}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test/test.dart';
6+
import 'package:web_app/src/widget/downloads_chart/computations.dart';
7+
// import 'package:web_app/src/widget/downloads_chart/widget.dart';
8+
9+
void main() {
10+
test('week lists - simpler', () {
11+
final List<int> l1 = List.from(List.filled(10, 10))
12+
..addAll(List.filled(10, 20))
13+
..add(2)
14+
..addAll(List.filled(31, 0));
15+
16+
final List<int> l2 = List.from(List.filled(10, 20))
17+
..addAll(List.filled(10, 40))
18+
..add(4)
19+
..addAll(List.filled(31, 0));
20+
21+
final List<int> l3 = List.from(List.filled(10, 70))
22+
..addAll(List.filled(10, 140))
23+
..add(14)
24+
..addAll(List.filled(31, 0));
25+
26+
final totals = List<int>.from(List.filled(10, 110))
27+
..addAll(List.filled(10, 220))
28+
..add(22)
29+
..addAll(List.filled(31, 0));
30+
31+
final patchRangeDownloads = [
32+
(counts: l1, versionRange: '>=6.2.0-0 <6.2.1'),
33+
(counts: l1, versionRange: '>=6.2.1-0 <6.2.2'),
34+
(counts: l1, versionRange: '>=6.3.1-0 <6.3.2'),
35+
(counts: l1, versionRange: '>=6.4.0-0 <6.4.1'),
36+
(counts: l1, versionRange: '>=6.6.1-0 <6.6.2'),
37+
];
38+
39+
final minorRangeDownloads = [
40+
(counts: l1, versionRange: '>=6.1.0-0 <6.2.0'),
41+
(counts: l2, versionRange: '>=6.2.0-0 <6.3.0'),
42+
(counts: l1, versionRange: '>=6.3.0-0 <6.4.0'),
43+
(counts: l1, versionRange: '>=6.4.0-0 <6.5.0'),
44+
(counts: l1, versionRange: '>=6.6.0-0 <6.7.0')
45+
];
46+
47+
final majorRangeDownloads = [
48+
(counts: l1, versionRange: '>=1.0.0-0 <2.0.0'),
49+
(counts: l1, versionRange: '>=2.0.0-0 <3.0.0'),
50+
(counts: l1, versionRange: '>=3.0.0-0 <4.0.0'),
51+
(counts: l1, versionRange: '>=4.0.0-0 <5.0.0'),
52+
(counts: l3, versionRange: '>=6.0.0-0 <7.0.0')
53+
];
54+
55+
final w1 = prepareWeekLists(totals, majorRangeDownloads, 52).toList();
56+
final w2 = prepareWeekLists(totals, minorRangeDownloads, 52).toList();
57+
final w3 = prepareWeekLists(totals, patchRangeDownloads, 52).toList();
58+
59+
for (int i = 42; i < 52; i++) {
60+
expect(w1[i], [10, 10, 10, 10, 70]);
61+
expect(w2[i], [50, 10, 20, 10, 10, 10]);
62+
expect(w3[i], [60, 10, 10, 10, 10, 10]);
63+
}
64+
65+
for (int i = 32; i < 42; i++) {
66+
expect(w1[i], [20, 20, 20, 20, 140]);
67+
expect(w2[i], [100, 20, 40, 20, 20, 20]);
68+
expect(w3[i], [120, 20, 20, 20, 20, 20]);
69+
}
70+
71+
expect(w1[31], [2, 2, 2, 2, 14]);
72+
expect(w2[31], [10, 2, 4, 2, 2, 2]);
73+
expect(w3[31], [12, 2, 2, 2, 2, 2]);
74+
75+
for (int i = 0; i < 31; i++) {
76+
expect(w1[i], [0, 0, 0, 0, 0]);
77+
expect(w2[i], [0, 0, 0, 0, 0, 0]);
78+
expect(w3[i], [0, 0, 0, 0, 0, 0]);
79+
}
80+
});
81+
}

0 commit comments

Comments
 (0)