Skip to content

Commit

Permalink
Merge pull request #67 from SimCorp/fix-develop-main-merge
Browse files Browse the repository at this point in the history
Release 0.4.0
  • Loading branch information
TKShade authored May 6, 2021
2 parents d241221 + 150677f commit c6ce882
Show file tree
Hide file tree
Showing 42 changed files with 21,701 additions and 3,923 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 2
fetch-depth: 0
- name: Set current commit sha as env var
run: echo COMMIT_SHA=$(git rev-parse HEAD) >> $GITHUB_ENV
- name: Test code
run: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
- name: Upload code
run:
bash <(curl -s https://codecov.io/bash) -t ${{ secrets.CODECOV_TOKEN }} -B ${{ github.event.inputs.branch }}
bash <(curl -s https://codecov.io/bash) -t ${{ secrets.CODECOV_TOKEN }}
24 changes: 24 additions & 0 deletions .github/workflows/codecov-pr-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Generate Codecov PR report

on:
issue_comment:
types: [created, edited]

jobs:
codecov-comment:
if: contains(github.event.comment.html_url, '/pull/') && contains(github.event.comment.body, 'run codecov')
name: CodeCov comment
runs-on: ubuntu-latest
strategy:
matrix:
dotnet: ['5.0']
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Test code
run: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
- name: Upload code
run:
bash <(curl -s https://codecov.io/bash) -t ${{ secrets.CODECOV_TOKEN }}
5 changes: 5 additions & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"CORS": {
"AllowedHosts": [ "http://localhost:8080" ]
}
}
179 changes: 54 additions & 125 deletions backend/Controller.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.AspNetCore.Http.StatusCodes;
using backend.models;
using System.Linq;
using backend.drawables;
using System.Collections.Concurrent;
using System.Threading.Tasks;

namespace backend
{
Expand All @@ -15,39 +14,79 @@ namespace backend
public class Controller : ControllerBase
{

private static ConcurrentBag<Link> links = new ConcurrentBag<Link>();
private static ConcurrentBag<ServerNode> processedServers = new ConcurrentBag<ServerNode>();
private static ConcurrentBag<ClusterNode> processedClusters = new ConcurrentBag<ClusterNode>();
private static HashSet<string> missingServerIds = new HashSet<string>();
private readonly DataStorage _dataStorage;

private static String timeOfRequest;

public Controller(DataStorage dataStorage)
{
_dataStorage = dataStorage;
}

[HttpGet("/nodes")]
[ProducesResponseType(Status200OK)]
public ActionResult<IEnumerable<ServerNode>> GetNodes()
{
return processedServers;
return _dataStorage.processedServers;
}

[HttpGet("/gatewayerrors")]
[ProducesResponseType(Status200OK)]
public ActionResult<Dictionary<string, List<string>>> GetGatewayErrors()
{
return _dataStorage.clusterConnectionErrors;
}

[HttpGet("/clusters")]
[ProducesResponseType(Status200OK)]
public ActionResult<IEnumerable<ClusterNode>> GetClusters()
{
return processedClusters;
return _dataStorage.processedClusters;
}

[HttpGet("/timeOfRequest")]
[ProducesResponseType(Status200OK)]
public ActionResult<String> GetTimeOfRequest()
{
timeOfRequest = Program.dateOfNatsRequest.ToString("hh:mm tt - dd MMMM yyyy");
return timeOfRequest;
}

[HttpGet("/gatewayLinks")]
[ProducesResponseType(Status200OK)]
public ActionResult<IEnumerable<Link>> GetGatewayLinks()
{
var gatewayLinks = new List<Link>();
foreach (var cluster in _dataStorage.clusterConnectionErrors)
{
var split = cluster.Key.Split(" NAMESPLIT ");
var source = split[0];
var target = split[1];
var link = new Link (source, target, cluster.Value.Count > 0);
link.errors = cluster.Value;
foreach (var err in cluster.Value)
{
link.errorsAsString += "\n" + err;
}
gatewayLinks.Add(link);
}

return gatewayLinks;
}

[HttpGet("/varz")]
[ProducesResponseType(Status200OK)]
public ActionResult<IEnumerable<Server>> GetVarz()
{
return Program.servers;
return _dataStorage.servers;
}

// TODO use to get info about server, when it is clicked in UI
[HttpGet("/varz/{serverId}")]
[ProducesResponseType(Status200OK)]
public ActionResult<Server> GetVarz([FromRoute] string serverId)
{
var query = from server in Program.servers.AsParallel()
var query = from server in _dataStorage.servers.AsParallel()
where server.server_id == serverId
select server;
return query.FirstOrDefault();
Expand All @@ -57,145 +96,35 @@ public ActionResult<Server> GetVarz([FromRoute] string serverId)
[ProducesResponseType(Status200OK)]
public ActionResult<IEnumerable<Connection>> GetConnz()
{
return Program.connections;
return _dataStorage.connections;
}

[HttpGet("/links")]
[ProducesResponseType(Status200OK)]
public ActionResult<IEnumerable<Link>> GetLinks()
{
return links;
return _dataStorage.links;
}

[HttpGet("/routez")]
[ProducesResponseType(Status200OK)]
public ActionResult<IEnumerable<Route>> GetRoutez()
{
return Program.routes;
return _dataStorage.routes;
}

[HttpGet("/gatewayz")]
[ProducesResponseType(Status200OK)]
public ActionResult<IEnumerable<Gateway>> GetGatewayz()
{
return Program.gateways;
return _dataStorage.gateways;
}

[HttpGet("/leafz")]
[ProducesResponseType(Status200OK)]
public ActionResult<IEnumerable<Leaf>> GetLeafz()
{
return Program.leafs;
}

public static void ProcessData()
{

ProcessServers();
ProcessClusters();
ProcessLinks();

// Patch for a missing node from varz
// TODO dynamically handle these types of errors
foreach(var serverId in missingServerIds)
{
processedServers.Add(new ServerNode{server_id = serverId, ntv_error = true});
}


return _dataStorage.leafs;
}

public static void ProcessClusters()
{
// TODO crashed node is not in cluster, decide whether it should be.
var markedServers = new HashSet<string>();
var id = 0;
foreach (var server in Program.routes)
{
if (markedServers.Contains(server.server_id)) continue; // Probably only once for each cluster

var cluster = new ClusterNode {
name = "cluster nr:" + id,
servers = new ConcurrentBag<ServerNode>()
};
id++;

ServerNode processedServer = processedServers.Where(p => p.server_id == server.server_id).Select(p => p).FirstOrDefault();
if (processedServer is null) continue;
cluster.servers.Add(processedServer);
markedServers.Add(server.server_id);

foreach (var route in server.routes)
{
if (markedServers.Contains(route.remote_id)) continue;
markedServers.Add(route.remote_id);

processedServer = processedServers.Where(p => p.server_id == route.remote_id).Select(p => p).FirstOrDefault();
if (processedServer is null) continue;
cluster.servers.Add(processedServer);
markedServers.Add(server.server_id);
};

processedClusters.Add(cluster);
}

foreach (var gateway in Program.gateways)
{
foreach (var cluster in processedClusters)
{
if (!cluster.ContainsServer(gateway.server_id)) continue;
if (gateway.name is null) continue;
cluster.name = gateway.name;
}
}
}

public static void ProcessLinks()
{
// Information about routes are also on server, no request to routez necessary
// Maybe info on routes is more up-to-date?
Parallel.ForEach(Program.servers, server => {
var source = server.server_id;
Parallel.ForEach(server.route.routes, route => {
var target = route.remote_id;
links.Add(new Link(source, target));
});
});

foreach (var link in links)
{
if (!Program.idToServer.ContainsKey(link.target))
{
missingServerIds.Add(link.target);
link.ntv_error = true;
links.Add(link);
}
}
}

public static void ProcessServers()
{
// Add serverNodes to processedServers
Parallel.ForEach(Program.servers, server => {
processedServers.Add(new ServerNode {
server_id = server.server_id,
server_name = server.server_name,
ntv_error = false,
clients = new ConcurrentBag<ConnectionNode>()
});
});

// Add connections to servers
Parallel.ForEach(Program.connections, server => {
Parallel.ForEach(server.connections, connection => {
var processedServer = processedServers.Where(p => p.server_id == server.server_id).Select(p => p).FirstOrDefault();
if (processedServer != null)
{
processedServer.clients.Add(new ConnectionNode{ip = connection.ip, port = connection.port});
}
});
});
}

}
}
50 changes: 50 additions & 0 deletions backend/DataStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using backend.models;
using backend.drawables;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace backend
{
public class DataStorage
{

public ConcurrentDictionary<string, Server> idToServer;
public List<Server> servers;
public ConcurrentBag<backend.models.Connection> connections;
public ConcurrentBag<Route> routes;
public ConcurrentBag<Gateway> gateways;
public ConcurrentBag<Leaf> leafs;
public List<Link> links;
public ConcurrentBag<ServerNode> processedServers;
public ConcurrentBag<ClusterNode> processedClusters;
public Dictionary<string, List<string>> serverToMissingServer;

public HashSet<string> missingServerIds = new HashSet<string>();
public HashSet<string> foundServers = new HashSet<string>();
public Dictionary<string, string> serverToCluster = new Dictionary<string, string>();
public Dictionary<string, List<string>> clusterConnectionErrors = new Dictionary<string, List<string>>();
public List<ClusterNode> errorClusters = new List<ClusterNode>();


public DataStorage() {

idToServer = new ConcurrentDictionary<string, Server>();
servers = new List<Server>();
connections = new ConcurrentBag<backend.models.Connection>();
routes = new ConcurrentBag<Route>();
gateways = new ConcurrentBag<Gateway>();
leafs = new ConcurrentBag<Leaf>();

links = new List<Link>();
processedServers = new ConcurrentBag<ServerNode>();
processedClusters = new ConcurrentBag<ClusterNode>();
serverToMissingServer = new Dictionary<string, List<string>>();

missingServerIds = new HashSet<string>();
foundServers = new HashSet<string>();
serverToCluster = new Dictionary<string, string>();
clusterConnectionErrors = new Dictionary<string, List<string>>();
errorClusters = new List<ClusterNode>();
}
}
}
Loading

0 comments on commit c6ce882

Please sign in to comment.