Skip to content

Commit

Permalink
send current usage statistics upon websocket connection
Browse files Browse the repository at this point in the history
this ensures that users don´t need to run cecilifier to get the statistics
  • Loading branch information
adrianoc committed Feb 2, 2024
1 parent bc3fe99 commit 8bb32a8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
21 changes: 20 additions & 1 deletion Cecilifier.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using System.Threading.Tasks;
using System.Web;
using Cecilifier.Core;
using Cecilifier.Core.Extensions;
using Cecilifier.Core.Mappings;
using Cecilifier.Core.Naming;
using Cecilifier.Web.Pages;
Expand Down Expand Up @@ -119,6 +118,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// instead `context.Connection.RemoteIpAddress` which will always be the proxy's IP.
var forwardedClientIP = context.Request.Headers["X-Forwarded-For"];
var webSocket = await context.WebSockets.AcceptWebSocketAsync();

await SendStatisticsAsync(webSocket);
await CecilifyCodeAsync(webSocket, string.IsNullOrWhiteSpace(forwardedClientIP) ? context.Connection.RemoteIpAddress.GetHashCode() : forwardedClientIP.GetHashCode());
}
else
Expand All @@ -131,6 +132,24 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
await next();
}
});

async Task SendStatisticsAsync(WebSocket webSocket)
{
var cecilifiedWebResult = new CecilifiedWebResult
{
Status = 4,
CecilifiedCode = String.Empty,
Counter = CecilifierApplication.Count,
MaximumUnique = CecilifierApplication.MaximumUnique,
Clients = CecilifierApplication.UniqueClients,
Kind = 'F',
MainTypeName = String.Empty,
Mappings = Array.Empty<Mapping>(),
};

var dataToReturn = JsonSerializer.SerializeToUtf8Bytes(cecilifiedWebResult);
await webSocket.SendAsync(dataToReturn, WebSocketMessageType.Text, true, CancellationToken.None);
}

async Task CecilifyCodeAsync(WebSocket webSocket, int remoteIpAddressHashCode)
{
Expand Down
41 changes: 25 additions & 16 deletions Cecilifier.Web/wwwroot/js/cecilifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,22 +744,7 @@ function initializeWebSocket() {
// this is where we get the cecilified code back...
let response = JSON.parse(event.data);
if (response.status === 0) {
if (document.getElementById("cecilifier-stats")._tippy === undefined) {
tippy('#cecilifier-stats', {
content: "N/A",
placement: 'top',
interactive: true,
allowHTML: true,
theme: 'cecilifier-tooltip',
delay: [500, null]
});
}

document.getElementById("cecilifier-stats")._tippy.setContent(`
Total: ${response.counter}<br/>
Clients: ${response.clientsCounter}<br/>
Maximum: ${response.maximumUnique}<br/>
`);


if (response.kind === 'Z') {
setTimeout(function() {
Expand All @@ -769,10 +754,15 @@ function initializeWebSocket() {
}
else {
cecilifiedCode.setValue(response.cecilifiedCode);
updateUsageStatisticsToolTip(response);

// save the returned mappings used to map between code snippet <-> Cecilified Code.
blockMappings = response.mappings;
}
} else if (response.status === 4) {
let response = JSON.parse(event.data);
updateUsageStatisticsToolTip(response);

} else if (response.status === 1) {
let compilerErrors = response.errors;
for(let i = 0; i < compilerErrors.length; i++) {
Expand Down Expand Up @@ -805,6 +795,25 @@ function initializeWebSocket() {
};
}

function updateUsageStatisticsToolTip(response) {
if (document.getElementById("cecilifier-stats")._tippy === undefined) {
tippy('#cecilifier-stats', {
content: "N/A",
placement: 'top',
interactive: true,
allowHTML: true,
theme: 'cecilifier-tooltip',
delay: [500, null]
});
}

document.getElementById("cecilifier-stats")._tippy.setContent(`
Total: ${response.counter}<br/>
Clients: ${response.clientsCounter}<br/>
Maximum: ${response.maximumUnique}<br/>
`);
}

function sendMissingAssemblyReferences(missingAssemblyHashes, continuation) {
getAssemblyReferencesContents(missingAssemblyHashes, function (missingAssemblies) {
const xhttp = new XMLHttpRequest();
Expand Down

0 comments on commit 8bb32a8

Please sign in to comment.