Skip to content

Commit

Permalink
feat: serve nodes and rounds datasets from cache (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Aug 29, 2024
1 parent d9716e4 commit 4992e40
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 232 deletions.
2 changes: 1 addition & 1 deletion dkgNodeLibrary/dkgNodeLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="Solana.Wallet" Version="8.0.2" />
</ItemGroup>
Expand Down
16 changes: 11 additions & 5 deletions dkgNodesTests/dkgNodesTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="Solana.Wallet" Version="8.0.2" />
</ItemGroup>
Expand Down
323 changes: 154 additions & 169 deletions dkgServiceNode/Controllers/NodesController.cs

Large diffs are not rendered by default.

53 changes: 26 additions & 27 deletions dkgServiceNode/Controllers/RoundsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,23 @@ public RoundsController(IHttpContextAccessor httpContextAccessor, UserContext uC
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<Round>))]
public async Task<ActionResult<IEnumerable<Round>>> GetRounds()
{
var rounds = await dkgContext.Rounds.OrderByDescending(r => r.Id).ToListAsync();
var rounds = dkgContext.GetAllRoundsSortedByIdDescending();

var nodeCounts = await dkgContext.Nodes
.Where(n => dkgContext.Rounds.Any(r => r.Id == n.RoundId))
var roundIds = rounds.Select(r => r.Id).ToArray();

var nodeCounts = dkgContext.GetAllNodes()
.Where(n => roundIds.Contains(n.RoundId ?? 0))
.GroupBy(n => new { n.RoundId, n.StatusValue })
.Select(g => new NodeCountResult
{
RoundId = g.Key.RoundId ?? 0,
Status = (NStatus)g.Key.StatusValue,
Count = g.Count()
})
.ToListAsync();
.ToList();

var nodeCountsH = await dkgContext.NodesRoundHistory
.Where(n => dkgContext.Rounds.Any(r => r.Id == n.RoundId))
List<NodeCountResult> nodeCountsH = await dkgContext.NodesRoundHistory
.Where(n => roundIds.Contains(n.RoundId))
.GroupBy(n => new { n.RoundId, n.NodeFinalStatusValue })
.Select(g => new NodeCountResult
{
Expand All @@ -85,7 +87,7 @@ public async Task<ActionResult<IEnumerable<Round>>> GetRounds()

foreach (var round in rounds)
{
round.NodeCount = NodeCountResult.GetCount(nodeCounts, round.Id, null) +
round.NodeCount = NodeCountResult.GetCount(nodeCounts, round.Id, null) +
NodeCountResult.GetCount(nodeCountsH, round.Id, null) -
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.NotRegistered);

Expand All @@ -103,12 +105,12 @@ public async Task<ActionResult<IEnumerable<Round>>> GetRounds()
round.NodeCountTimedOut = NodeCountResult.GetCount(nodeCounts, round.Id, NStatus.TimedOut) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.TimedOut);

int eaCount = NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.RunningStepOne) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.WaitingStepTwo) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.RunningStepTwo) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.WaitingStepThree) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.RunningStepThree) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.RunningStepFour);
int eaCount = NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.RunningStepOne) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.WaitingStepTwo) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.RunningStepTwo) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.WaitingStepThree) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.RunningStepThree) +
NodeCountResult.GetCount(nodeCountsH, round.Id, NStatus.RunningStepFour);
if (round.Status == RStatus.Cancelled) round.NodeCountFailed += eaCount;
else round.NodeCountTimedOut += eaCount;
}
Expand All @@ -120,11 +122,11 @@ public async Task<ActionResult<IEnumerable<Round>>> GetRounds()
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Round))]
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrMessage))]
public async Task<ActionResult<Round>> GetRound(int id)
public ActionResult<Round> GetRound(int id)
{
var round = await dkgContext.Rounds.FindAsync(id);
var round = dkgContext.GetRoundById(id);
if (round == null) return _404Round(id);
round.NodeCount = await dkgContext.Nodes.CountAsync(n => n.RoundId == round.Id);
round.NodeCount = dkgContext.GetAllNodes().Count(n => n.RoundId == round.Id);

return round;
}
Expand All @@ -146,8 +148,7 @@ public async Task<ActionResult<Reference>> AddRound(RoundSettings roundSettings)
TimeoutR = roundSettings.TimeoutR
};

dkgContext.Rounds.Add(round);
await dkgContext.SaveChangesAsync();
await dkgContext.AddRoundAsync(round);

var reference = new Reference(round.Id);
return CreatedAtAction(nameof(AddRound), new { id = round.Id }, reference);
Expand All @@ -164,7 +165,7 @@ public async Task<ActionResult<Round>> NextRoundStep(int id)
var ch = await userContext.CheckAdmin(curUserId);
if (ch == null || !ch.Value) return _403();

Round? round = await dkgContext.Rounds.FindAsync(id);
Round? round = dkgContext.GetRoundById(id);
if (round == null) return _404Round(id);

round.ModifiedOn = DateTime.Now.ToUniversalTime();
Expand Down Expand Up @@ -213,7 +214,7 @@ public async Task<ActionResult<Round>> CancelRound(int id)
var ch = await userContext.CheckAdmin(curUserId);
if (ch == null || !ch.Value) return _403();

Round? round = await dkgContext.Rounds.FindAsync(id);
Round? round = dkgContext.GetRoundById(id);
if (round == null) return _404Round(id);

round.ModifiedOn = DateTime.Now.ToUniversalTime();
Expand All @@ -233,10 +234,9 @@ internal static List<Node> GetNodesWithRandomForRound(List<Node> nodes, int roun

internal async Task TryRunRound(Round round)
{
List<Node> rNodes = await dkgContext.Nodes
.Include(n => n.NodesRoundHistory)
List<Node> rNodes = dkgContext.GetAllNodes()
.Where(n => n.RoundId == round.Id || n.RoundId == round.Id - 1)
.ToListAsync();
.ToList();

List<Node> fiNodes = rNodes
.Where(node => node.NodesRoundHistory.Any(nrh => nrh.RoundId == round.Id - 1 && nrh.NodeRandom != null))
Expand All @@ -258,7 +258,7 @@ internal async Task TryRunRound(Round round)

if (round.MaxNodes < fiNodes.Count)
{
int lastRR = await dkgContext.LastRoundResult() ?? new Random().Next();
int lastRR = dkgContext.LastRoundResult() ?? new Random().Next();
fiNodes.Sort(new NodeComparer(lastRR, round.Id - 1));
fi2Nodes = fiNodes.Take(round.MaxNodes).ToList();
reNodes = fiNodes.Skip(round.MaxNodes).ToList();
Expand All @@ -269,14 +269,13 @@ internal async Task TryRunRound(Round round)
}
internal async Task<ActionResult<Round>> UpdateRoundState(DkgContext dkgContext, Round round)
{
dkgContext.Entry(round).State = EntityState.Modified;
try
{
await dkgContext.SaveChangesAsync();
await dkgContext.UpdateRoundAsync(round);
}
catch (DbUpdateConcurrencyException)
{
if (!await dkgContext.RoundExistsAsync(round.Id))
if (!dkgContext.RoundExists(round.Id))
{
return _404Round(round.Id);
}
Expand Down
11 changes: 3 additions & 8 deletions dkgServiceNode/Data/DbEnsure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,10 @@ public static void Ensure(string connectionString)
}

Ensure_0_8_0(connection);
PuVersionUpdate("0.9.1", connection);
PuVersionUpdate("0.9.2", connection);
PuVersionUpdate("0.9.3", connection);
PuVersionUpdate("0.9.4", connection);
PuVersionUpdate("0.9.5", connection);
PuVersionUpdate("0.9.6", connection);
PuVersionUpdate("0.10.0", connection);
PuVersionUpdate("0.10.1", connection);
PuVersionUpdate("0.10.2", connection); }
PuVersionUpdate("0.10.2", connection);
PuVersionUpdate("0.11.0", connection);
}
}


Expand Down
Loading

0 comments on commit 4992e40

Please sign in to comment.