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+ }
0 commit comments