From 6009346180b86d06ca7ff550122d9ece5bfe0ef7 Mon Sep 17 00:00:00 2001 From: dangershony Date: Wed, 31 Jan 2024 22:35:01 +0000 Subject: [PATCH] Adding a stats endpoint to the angor project --- .../Controllers/ProjectQueryController.cs | 9 ++++++ .../Operations/Types/ProjectStats.cs | 9 ++++++ .../Properties/launchSettings.json | 24 ++++++++-------- .../Storage/IAngorStorage.cs | 4 +-- .../Storage/Mongo/AngorMongoData.cs | 28 +++++++++++++++++-- 5 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 src/Blockcore.Indexer.Angor/Operations/Types/ProjectStats.cs diff --git a/src/Blockcore.Indexer.Angor/Controllers/ProjectQueryController.cs b/src/Blockcore.Indexer.Angor/Controllers/ProjectQueryController.cs index e5c2f56..dd3ade8 100644 --- a/src/Blockcore.Indexer.Angor/Controllers/ProjectQueryController.cs +++ b/src/Blockcore.Indexer.Angor/Controllers/ProjectQueryController.cs @@ -37,6 +37,15 @@ public async Task GetProject([MinLength(2)][MaxLength(100)] strin return project == null ? NotFound() : Ok(project); } + [HttpGet] + [Route("projects/{projectId}/stats")] + public async Task GetProjectStats([MinLength(2)][MaxLength(100)] string projectId) + { + var project = await angorStorage.GetProjectStatsAsync(projectId); + + return project == null ? NotFound() : Ok(project); + } + [HttpGet] [Route("projects/{projectId}/investments")] public async Task GetInvestments([MinLength(2)][MaxLength(100)] string projectId,[Range(0, long.MaxValue)] int? offset = 0, [Range(1, 50)] int limit = 10) diff --git a/src/Blockcore.Indexer.Angor/Operations/Types/ProjectStats.cs b/src/Blockcore.Indexer.Angor/Operations/Types/ProjectStats.cs new file mode 100644 index 0000000..189c538 --- /dev/null +++ b/src/Blockcore.Indexer.Angor/Operations/Types/ProjectStats.cs @@ -0,0 +1,9 @@ +namespace Blockcore.Indexer.Angor.Operations.Types; + +public class ProjectStats +{ + public long InvestorCount { get; set; } + public long AmountInvested { get; set; } + public long AmountInPenalties { get; set; } + public long CountInPenalties { get; set; } +} diff --git a/src/Blockcore.Indexer.Angor/Properties/launchSettings.json b/src/Blockcore.Indexer.Angor/Properties/launchSettings.json index acedfab..17e333c 100644 --- a/src/Blockcore.Indexer.Angor/Properties/launchSettings.json +++ b/src/Blockcore.Indexer.Angor/Properties/launchSettings.json @@ -1,14 +1,14 @@ -{ - "profiles": { - "Blockcore.Indexer.Angor": { - "commandName": "Project", - "commandLineArgs": "--chain=TBTC", - "dotnetRunMessages": true, - "launchBrowser": false, - "applicationUrl": "http://localhost:9910/", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" +{ + "profiles": { + "TBTC-signet": { + "commandName": "Project", + "commandLineArgs": "--chain=TBTC --Network:RPCPort=38332", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:9910/", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } } - } - } + } } diff --git a/src/Blockcore.Indexer.Angor/Storage/IAngorStorage.cs b/src/Blockcore.Indexer.Angor/Storage/IAngorStorage.cs index bec531b..5c94a47 100644 --- a/src/Blockcore.Indexer.Angor/Storage/IAngorStorage.cs +++ b/src/Blockcore.Indexer.Angor/Storage/IAngorStorage.cs @@ -6,10 +6,8 @@ namespace Blockcore.Indexer.Angor.Storage; public interface IAngorStorage { Task GetProjectAsync(string projectId); - + Task GetProjectStatsAsync(string projectId); Task> GetProjectsAsync(int? offset, int limit); - Task> GetProjectInvestmentsAsync(string projectId, int? offset, int limit); - Task GetInvestmentsByInvestorPubKeyAsync(string projectId); } diff --git a/src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs b/src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs index 115d753..5dfb2f3 100644 --- a/src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs +++ b/src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs @@ -52,6 +52,30 @@ public AngorMongoData(ILogger dbLogger, SyncConnection connection, return null; } + public async Task GetProjectStatsAsync(string projectId) + { + var project = mongoDb.ProjectTable + .AsQueryable() + .FirstOrDefault(_ => _.AngorKey == projectId); + + if (project != null) + { + var total = await mongoDb.InvestmentTable.CountDocumentsAsync(Builders.Filter.Eq(_ => _.AngorKey, project.AngorKey)); + + var sum = mongoDb.InvestmentTable.AsQueryable() + .Where(_ => _.AngorKey == projectId) + .Sum(s => s.AmountSats); + + return new ProjectStats + { + InvestorCount = total, + AmountInvested = sum, + }; + } + + return null; + } + public async Task> GetProjectsAsync(int? offset, int limit) { long total = await mongoDb.ProjectTable.CountDocumentsAsync(FilterDefinition.Empty); @@ -101,8 +125,8 @@ public async Task> GetProjectInvestmentsAsync(str return new QueryResult { Items = investments, - Offset = 0, - Limit = 0, + Offset = itemsToSkip, + Limit = limit, Total = total }; }