Skip to content

Commit

Permalink
Adding a stats endpoint to the angor project
Browse files Browse the repository at this point in the history
  • Loading branch information
dangershony committed Jan 31, 2024
1 parent 0f0d012 commit 6009346
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public async Task<IActionResult> GetProject([MinLength(2)][MaxLength(100)] strin
return project == null ? NotFound() : Ok(project);
}

[HttpGet]
[Route("projects/{projectId}/stats")]
public async Task<IActionResult> 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<IActionResult> GetInvestments([MinLength(2)][MaxLength(100)] string projectId,[Range(0, long.MaxValue)] int? offset = 0, [Range(1, 50)] int limit = 10)
Expand Down
9 changes: 9 additions & 0 deletions src/Blockcore.Indexer.Angor/Operations/Types/ProjectStats.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
24 changes: 12 additions & 12 deletions src/Blockcore.Indexer.Angor/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
4 changes: 1 addition & 3 deletions src/Blockcore.Indexer.Angor/Storage/IAngorStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ namespace Blockcore.Indexer.Angor.Storage;
public interface IAngorStorage
{
Task<ProjectIndexerData?> GetProjectAsync(string projectId);

Task<ProjectStats?> GetProjectStatsAsync(string projectId);
Task<QueryResult<ProjectIndexerData>> GetProjectsAsync(int? offset, int limit);

Task<QueryResult<ProjectInvestment>> GetProjectInvestmentsAsync(string projectId, int? offset, int limit);

Task<ProjectInvestment> GetInvestmentsByInvestorPubKeyAsync(string projectId);
}
28 changes: 26 additions & 2 deletions src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ public AngorMongoData(ILogger<AngorMongoDb> dbLogger, SyncConnection connection,
return null;
}

public async Task<ProjectStats?> GetProjectStatsAsync(string projectId)
{
var project = mongoDb.ProjectTable
.AsQueryable()
.FirstOrDefault(_ => _.AngorKey == projectId);

if (project != null)
{
var total = await mongoDb.InvestmentTable.CountDocumentsAsync(Builders<Investment>.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<QueryResult<ProjectIndexerData>> GetProjectsAsync(int? offset, int limit)
{
long total = await mongoDb.ProjectTable.CountDocumentsAsync(FilterDefinition<Project>.Empty);
Expand Down Expand Up @@ -101,8 +125,8 @@ public async Task<QueryResult<ProjectInvestment>> GetProjectInvestmentsAsync(str
return new QueryResult<ProjectInvestment>
{
Items = investments,
Offset = 0,
Limit = 0,
Offset = itemsToSkip,
Limit = limit,
Total = total
};
}
Expand Down

0 comments on commit 6009346

Please sign in to comment.