Skip to content

Commit 67d00c7

Browse files
committed
Sort packages and authors in nuget stats
This makes it easier to quickly check what changed over time
1 parent 0de56e9 commit 67d00c7

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

src/Commands/NuGetStatsCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ await Parallel.ForEachAsync(tasks, paralell, async (source, cancellation) =>
502502
});
503503
}
504504

505-
void PersistModel() => File.WriteAllText(fileName, JsonSerializer.Serialize(model, JsonOptions.Default));
505+
void PersistModel() => File.WriteAllText(fileName, JsonSerializer.Serialize(model.ToOrdered(), JsonOptions.Default));
506506

507507
if (settings.PackageId is { Length: > 0 } packageId)
508508
{

src/Core/Records.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,25 @@ public record OpenSource(ConcurrentDictionary<string, HashSet<string>> Authors,
4646
{
4747
}
4848

49+
public OrderedOpenSource ToOrdered() => new(
50+
ToSortedDictionary(Authors, static values => values.Order(StringComparer.Ordinal).ToList()),
51+
ToSortedDictionary(Repositories, static values => values.Order(StringComparer.Ordinal).ToList()),
52+
ToSortedDictionary(Packages, static values => ToSortedDictionary(values, static value => value)));
53+
4954
public OpenSourceSummary Summary => summary ??= new(Totals);
5055

5156
public OpenSourceTotals Totals => totals ??= new(this);
5257

58+
static SortedDictionary<string, TValue> ToSortedDictionary<TSource, TValue>(IEnumerable<KeyValuePair<string, TSource>> source, Func<TSource, TValue> valueSelector)
59+
{
60+
var ordered = new SortedDictionary<string, TValue>(StringComparer.Ordinal);
61+
62+
foreach (var (key, value) in source)
63+
ordered[key] = valueSelector(value);
64+
65+
return ordered;
66+
}
67+
5368
public class OpenSourceTotals(OpenSource source)
5469
{
5570
public double Authors => source.Authors.Count;
@@ -65,6 +80,32 @@ public class OpenSourceSummary(OpenSourceTotals totals)
6580
public string Packages => totals.Packages.ToMetric(decimals: 1);
6681
public string Downloads => totals.Downloads.ToMetric(decimals: 1);
6782
}
83+
84+
public record OrderedOpenSource(SortedDictionary<string, List<string>> Authors, SortedDictionary<string, List<string>> Repositories, SortedDictionary<string, SortedDictionary<string, long>> Packages)
85+
{
86+
OrderedOpenSourceSummary? summary;
87+
OrderedOpenSourceTotals? totals;
88+
89+
public OrderedOpenSourceSummary Summary => summary ??= new(Totals);
90+
91+
public OrderedOpenSourceTotals Totals => totals ??= new(this);
92+
93+
public class OrderedOpenSourceTotals(OrderedOpenSource source)
94+
{
95+
public double Authors => source.Authors.Count;
96+
public double Repositories => source.Repositories.Count;
97+
public double Packages => source.Packages.Sum(x => x.Value.Count);
98+
public double Downloads => source.Packages.Sum(x => x.Value.Sum(y => y.Value));
99+
}
100+
101+
public class OrderedOpenSourceSummary(OrderedOpenSourceTotals totals)
102+
{
103+
public string Authors => totals.Authors.ToMetric(decimals: 1);
104+
public string Repositories => totals.Repositories.ToMetric(decimals: 1);
105+
public string Packages => totals.Packages.ToMetric(decimals: 1);
106+
public string Downloads => totals.Downloads.ToMetric(decimals: 1);
107+
}
108+
}
68109
}
69110

70111
public record Rate(RateLimit General, RateLimit GraphQL);

src/Tests/NuGetStatsCommandTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,39 @@ public class NuGetStatsCommandTests
1414
{
1515
Config config = Config.Build().SetBoolean("sponsorlink", "tos", true);
1616

17+
[Fact]
18+
public void OrdersOpenSourceModelForPersistence()
19+
{
20+
var model = new OpenSource();
21+
22+
model.Authors.TryAdd("bob", new(["repo-z", "repo-a"], FnvHashComparer.Default));
23+
model.Authors.TryAdd("alice", new(["repo-c"], FnvHashComparer.Default));
24+
25+
model.Repositories.TryAdd("repo-b", new(["zoe", "amy"], FnvHashComparer.Default));
26+
model.Repositories.TryAdd("repo-a", new(["bob"], FnvHashComparer.Default));
27+
28+
model.Packages.TryAdd("repo-b", new(FnvHashComparer.Default)
29+
{
30+
["Z.Package"] = 2,
31+
["A.Package"] = 1,
32+
});
33+
model.Packages.TryAdd("", new(FnvHashComparer.Default)
34+
{
35+
["b"] = 2,
36+
["a"] = 1,
37+
});
38+
39+
var ordered = model.ToOrdered();
40+
41+
Assert.Equal(["alice", "bob"], ordered.Authors.Keys.ToArray());
42+
Assert.Equal(["repo-a", "repo-z"], ordered.Authors["bob"]);
43+
Assert.Equal(["repo-a", "repo-b"], ordered.Repositories.Keys.ToArray());
44+
Assert.Equal(["amy", "zoe"], ordered.Repositories["repo-b"]);
45+
Assert.Equal(["", "repo-b"], ordered.Packages.Keys.ToArray());
46+
Assert.Equal(["A.Package", "Z.Package"], ordered.Packages["repo-b"].Keys.ToArray());
47+
Assert.Equal(["a", "b"], ordered.Packages[""].Keys.ToArray());
48+
}
49+
1750
[LocalFact("GitHub:Token")]
1851
public async Task CollectsStatsForMerq()
1952
{

0 commit comments

Comments
 (0)