-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add downbload statistics, hash validation on package query, and downl…
…oad tracking
- Loading branch information
Showing
13 changed files
with
496 additions
and
8 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/PackageRegistryService/API/Endpoints/StatisticsEndpointsV1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using PackageRegistryService.API.Handlers; | ||
using PackageRegistryService.Authentication; | ||
|
||
namespace PackageRegistryService.API.Endpoints | ||
{ | ||
public static class StatisticsEndpointsV1 | ||
{ | ||
public static RouteGroupBuilder MapStatisticsApiV1(this RouteGroupBuilder group) | ||
{ | ||
|
||
// packages endpoints | ||
group.MapGet("/downloads", DownloadsHandlers.GetAllDownloads) | ||
.WithOpenApi() | ||
.WithName("GetAllDownloads"); | ||
|
||
group.MapGet("/downloads/{name}", DownloadsHandlers.GetAllDownloadsByName) | ||
.WithOpenApi() | ||
.WithName("GetAllDownloadsByName"); | ||
|
||
group.MapGet("/downloads/{name}/{version}", DownloadsHandlers.GetDownloadsByNameAndVersion) | ||
.WithOpenApi() | ||
.WithName("GetDownloadsByNameAndVersion"); | ||
|
||
return group.WithTags("Statistics"); ; | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/PackageRegistryService/API/Handlers/DownloadsHandlers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.EntityFrameworkCore; | ||
using PackageRegistryService.Models; | ||
|
||
namespace PackageRegistryService.API.Handlers | ||
{ | ||
public class DownloadsHandlers | ||
{ | ||
// get all download stats | ||
public static async Task<Ok<PackageDownloads[]>> GetAllDownloads(ValidationPackageDb database) | ||
{ | ||
var downloads = await database.Downloads.ToArrayAsync(); | ||
return TypedResults.Ok(downloads); | ||
} | ||
|
||
public static async Task<Results<Ok<PackageDownloads[]>, NotFound<string>>> GetAllDownloadsByName(string name, ValidationPackageDb database) | ||
{ | ||
var downloads = | ||
await database.Downloads | ||
.Where(p => p.PackageName == name) | ||
.ToArrayAsync(); | ||
|
||
return downloads is null || downloads.Length == 0 | ||
? TypedResults.NotFound($"No download stats for package '{name}' available.") | ||
: TypedResults.Ok(downloads); | ||
} | ||
|
||
public static async Task<Results<BadRequest<string>, NotFound<string>, Ok<PackageDownloads>>> GetDownloadsByNameAndVersion(string name, string version, ValidationPackageDb database) | ||
{ | ||
var splt = version.Split('.'); | ||
if (splt.Length != 3) | ||
{ | ||
return TypedResults.BadRequest("version was not a of valid format MAJOR.MINOR.REVISION"); | ||
} | ||
|
||
int major; int minor; int revision; | ||
|
||
if ( | ||
!int.TryParse(splt[0], out major) | ||
|| !int.TryParse(splt[1], out minor) | ||
|| !int.TryParse(splt[2], out revision) | ||
) | ||
{ | ||
return TypedResults.BadRequest("version was not a of valid format MAJOR.MINOR.REVISION"); | ||
} | ||
|
||
var downloads = await database.Downloads.FindAsync(name, major, minor, revision); | ||
|
||
return downloads is null | ||
? TypedResults.NotFound($"No download stats for package '{name}' version '{version}' available.") | ||
: TypedResults.Ok(downloads); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
src/PackageRegistryService/Migrations/20240305110638_AddDownloadStatistics.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.