Skip to content

Commit 1d3583c

Browse files

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Web/Stats.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.Azure.Functions.Worker.Http;
2+
using Microsoft.Azure.Functions.Worker;
3+
using System.Net;
4+
using Humanizer;
5+
6+
namespace Devlooped.Sponsors;
7+
8+
public class Stats(AsyncLazy<OpenSource> oss, SponsorsManager manager)
9+
{
10+
readonly TimeSpan expiration = TimeSpan.FromDays(1);
11+
12+
[Function("nuget-count")]
13+
public async Task<HttpResponseData> NuGetCountAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "nuget/all")] HttpRequestData req)
14+
{
15+
var stats = await oss;
16+
var manifest = await manager.GetManifestAsync();
17+
var owner = req.Query.Count == 1 ? req.Query.ToString() : manifest.Sponsorable;
18+
19+
var count = stats.Packages
20+
.Where(x => x.Key.StartsWith(owner + "/"))
21+
.Sum(x => x.Value.Count);
22+
23+
var output = req.CreateResponse(HttpStatusCode.OK);
24+
25+
// Also cache downstream (specifically shields.io)
26+
output.Headers.Add("Cache-Control", "public,max-age=" + expiration.TotalSeconds);
27+
await output.WriteAsJsonAsync(new
28+
{
29+
schemaVersion = 1,
30+
label = "nugets",
31+
message = ((double)count).ToMetric(decimals: 1)
32+
});
33+
34+
return output;
35+
}
36+
37+
[Function("nuget-downloads")]
38+
public async Task<HttpResponseData> NuGetDownloadsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "nuget/dl")] HttpRequestData req)
39+
{
40+
var stats = await oss;
41+
var manifest = await manager.GetManifestAsync();
42+
var owner = req.Query.Count == 1 ? req.Query.ToString() : manifest.Sponsorable;
43+
44+
var count = stats.Packages
45+
.Where(x => x.Key.StartsWith(owner + "/"))
46+
.Sum(x => x.Value.Sum(y => y.Value));
47+
48+
var output = req.CreateResponse(HttpStatusCode.OK);
49+
50+
// Also cache downstream (specifically shields.io)
51+
output.Headers.Add("Cache-Control", "public,max-age=" + expiration.TotalSeconds);
52+
await output.WriteAsJsonAsync(new
53+
{
54+
schemaVersion = 1,
55+
label = "dl/day",
56+
message = ((double)count).ToMetric(decimals: 1)
57+
});
58+
59+
return output;
60+
}
61+
62+
}

src/Web/Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="Azure.Identity" Version="1.12.1" />
1111
<PackageReference Include="Devlooped.CredentialManager" Version="2.5.0.1" />
1212
<PackageReference Include="DotNetConfig.Configuration" Version="1.2.0" />
13+
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
1314
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
1415
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
1516
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.3.2" />

0 commit comments

Comments
 (0)