This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPackageVersionToCsvDriver.cs
More file actions
95 lines (81 loc) · 4.16 KB
/
PackageVersionToCsvDriver.cs
File metadata and controls
95 lines (81 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using NuGet.Insights.Worker.LoadPackageVersion;
namespace NuGet.Insights.Worker.PackageVersionToCsv
{
public class PackageVersionToCsvDriver : ICatalogLeafToCsvDriver<PackageVersionRecord>, ICsvResultStorage<PackageVersionRecord>
{
private readonly PackageVersionStorageService _storageService;
private readonly IOptions<NuGetInsightsWorkerSettings> _options;
public PackageVersionToCsvDriver(
PackageVersionStorageService storageService,
IOptions<NuGetInsightsWorkerSettings> options)
{
_storageService = storageService;
_options = options;
}
public string ResultContainerName => _options.Value.PackageVersionContainerName;
public bool SingleMessagePerId => true;
public async Task InitializeAsync()
{
await _storageService.InitializeAsync();
}
public Task DestroyAsync()
{
return Task.CompletedTask;
}
public async Task<DriverResult<IReadOnlyList<PackageVersionRecord>>> ProcessLeafAsync(CatalogLeafScan leafScan)
{
var records = await ProcessLeafInternalAsync(leafScan);
return DriverResult.Success(records);
}
private async Task<IReadOnlyList<PackageVersionRecord>> ProcessLeafInternalAsync(CatalogLeafScan leafScan)
{
var scanId = Guid.NewGuid();
var scanTimestamp = DateTimeOffset.UtcNow;
// Fetch all of the known versions for this package ID.
var entities = await _storageService.GetAsync(leafScan.PackageId);
// Parse the NuGet version for all entities
var entityToNuGetVersion = entities
.ToDictionary(
x => x,
x => NuGetVersion.Parse(x.PackageVersion),
ReferenceEqualityComparer<PackageVersionEntity>.Instance);
var entityToSemVerOrder = entityToNuGetVersion
.OrderBy(x => x.Value)
.Select((x, i) => (Entity: x.Key, SemVerOrder: i))
.ToDictionary(
x => x.Entity,
x => x.SemVerOrder,
entityToNuGetVersion.Comparer);
// Find the set of versions that can possibly the latest.
var listedVersions = entities
.Where(x => x.LeafType != CatalogLeafType.PackageDelete) // Deleted versions can't be the latest
.Where(x => x.IsListed.Value) // Only listed versions can be latest
.Select(x => (Entity: x, Version: entityToNuGetVersion[x], IsSemVer2: x.SemVerType.Value.IsSemVer2()))
.OrderByDescending(x => x.Version)
.ToList();
var semVer1Versions = listedVersions.Where(x => !x.IsSemVer2).ToList();
// Determine the four definitions of "latest". Reminds me of NuGet.org Azure Search implementation...
var latest = semVer1Versions.FirstOrDefault();
var latestStable = semVer1Versions.Where(x => !x.Version.IsPrerelease).FirstOrDefault();
var latestSemVer2 = listedVersions.FirstOrDefault();
var latestStableSemVer2 = listedVersions.Where(x => !x.Version.IsPrerelease).FirstOrDefault();
// Map all entities to CSV records.
var records = new List<PackageVersionRecord>();
for (var i = 0; i < entities.Count; i++)
{
var entity = entities[i];
records.Add(new PackageVersionRecord(scanId, scanTimestamp, entity)
{
SemVerOrder = entityToSemVerOrder[entity],
IsLatest = ReferenceEquals(entity, latest.Entity),
IsLatestStable = ReferenceEquals(entity, latestStable.Entity),
IsLatestSemVer2 = ReferenceEquals(entity, latestSemVer2.Entity),
IsLatestStableSemVer2 = ReferenceEquals(entity, latestStableSemVer2.Entity),
});
}
return records;
}
}
}