-
-
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.
Allow retrieving a shields.io badge with nuget stats
Examples: - https://img.shields.io/endpoint?url=https%3A%2F%2Fsponsorlink.devlooped.com%2Fnuget%2Fall%3Fkzu - https://img.shields.io/endpoint?url=https%3A%2F%2Fsponsorlink.devlooped.com%2Fnuget%2Fdl%3Fkzu
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
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,62 @@ | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using Microsoft.Azure.Functions.Worker; | ||
using System.Net; | ||
using Humanizer; | ||
|
||
namespace Devlooped.Sponsors; | ||
|
||
public class Stats(AsyncLazy<OpenSource> oss, SponsorsManager manager) | ||
{ | ||
readonly TimeSpan expiration = TimeSpan.FromDays(1); | ||
|
||
[Function("nuget-count")] | ||
public async Task<HttpResponseData> NuGetCountAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "nuget/all")] HttpRequestData req) | ||
{ | ||
var stats = await oss; | ||
var manifest = await manager.GetManifestAsync(); | ||
var owner = req.Query.Count == 1 ? req.Query.ToString() : manifest.Sponsorable; | ||
|
||
var count = stats.Packages | ||
.Where(x => x.Key.StartsWith(owner + "/")) | ||
.Sum(x => x.Value.Count); | ||
|
||
var output = req.CreateResponse(HttpStatusCode.OK); | ||
|
||
// Also cache downstream (specifically shields.io) | ||
output.Headers.Add("Cache-Control", "public,max-age=" + expiration.TotalSeconds); | ||
await output.WriteAsJsonAsync(new | ||
{ | ||
schemaVersion = 1, | ||
label = "nugets", | ||
message = ((double)count).ToMetric(decimals: 1) | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
[Function("nuget-downloads")] | ||
public async Task<HttpResponseData> NuGetDownloadsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "nuget/dl")] HttpRequestData req) | ||
{ | ||
var stats = await oss; | ||
var manifest = await manager.GetManifestAsync(); | ||
var owner = req.Query.Count == 1 ? req.Query.ToString() : manifest.Sponsorable; | ||
|
||
var count = stats.Packages | ||
.Where(x => x.Key.StartsWith(owner + "/")) | ||
.Sum(x => x.Value.Sum(y => y.Value)); | ||
|
||
var output = req.CreateResponse(HttpStatusCode.OK); | ||
|
||
// Also cache downstream (specifically shields.io) | ||
output.Headers.Add("Cache-Control", "public,max-age=" + expiration.TotalSeconds); | ||
await output.WriteAsJsonAsync(new | ||
{ | ||
schemaVersion = 1, | ||
label = "dl/day", | ||
message = ((double)count).ToMetric(decimals: 1) | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
} |
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