Skip to content
Permalink
Browse files

Test stats page and function based on app insights telemetry

Refresh every minute, since the telemetry doesn't land on AppInsights faster than that anyway.

Try table format for values as well as simple header.
  • Loading branch information...
kzu committed May 9, 2019
1 parent fc31f4a commit 8a38001456d58ade2d05d3b2de19793df682cb6d
Showing with 107 additions and 0 deletions.
  1. +66 −0 functions/Stats.linq
  2. +41 −0 stats.html
@@ -0,0 +1,66 @@
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Configuration.dll</Reference>
<NuGetReference>Microsoft.ApplicationInsights</NuGetReference>
<NuGetReference>Microsoft.AspNetCore.Mvc</NuGetReference>
<NuGetReference>Microsoft.Extensions.Logging.Console</NuGetReference>
<NuGetReference>Microsoft.Extensions.Logging.Debug</NuGetReference>
<NuGetReference>xunit.assert</NuGetReference>
<Namespace>Microsoft.ApplicationInsights</Namespace>
<Namespace>Microsoft.AspNetCore.Http</Namespace>
<Namespace>Microsoft.AspNetCore.Http.Internal</Namespace>
<Namespace>Microsoft.AspNetCore.Mvc</Namespace>
<Namespace>Microsoft.Extensions.DependencyInjection</Namespace>
<Namespace>Microsoft.Extensions.Logging</Namespace>
<Namespace>Microsoft.Extensions.Logging.Console</Namespace>
<Namespace>Microsoft.Extensions.Logging.Debug</Namespace>
<Namespace>Microsoft.Extensions.Primitives</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>System.Net</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>Xunit</Namespace>
<Namespace>Microsoft.ApplicationInsights.Extensibility</Namespace>
<Namespace>Microsoft.ApplicationInsights.Channel</Namespace>
<Namespace>System.Net.Http</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>System.Configuration</Namespace>
<DisableMyExtensions>true</DisableMyExtensions>
</Query>

void Main()
{
Run<ContentResult>().Content.Dump();
}

static TActionResult Run<TActionResult>()
=> (TActionResult)Run();

static IActionResult Run()
=> Run(
new DefaultHttpRequest(new DefaultHttpContext()),
new ServiceCollection().AddLogging(builder => builder.AddConsole()).BuildServiceProvider().GetService<ILoggerFactory>().CreateLogger("Console")).Result;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
var http = new HttpClient();
http.DefaultRequestHeaders.Add("x-api-key", Environment.GetEnvironmentVariable("APPINSIGHTS_APIKEY"));
var json = await (await http.GetAsync($"https://api.applicationinsights.io/v1/apps/{Environment.GetEnvironmentVariable("APPINSIGHTS_APPID")}/query?query=" +
@"customEvents
| order by timestamp desc
| where name == 'redirect'
| project name, operation = operation_Name, org = tostring(customDimensions['org']), proj = tostring(customDimensions['project'])
| extend unique = strcat(org, proj)
| summarize total = count(), org = dcount(org), proj = dcount(unique)")).Content.ReadAsStringAsync();

dynamic stats = JObject.Parse(json);
return new ContentResult
{
Content = JsonConvert.SerializeObject(new
{
total = stats.tables[0].rows[0][0],
organizations = stats.tables[0].rows[0][1],
projects = stats.tables[0].rows[0][2],
}),
ContentType = "application/json",
StatusCode = 200
};
}
@@ -0,0 +1,41 @@
<html>

<body>
<table>
<tr>
<td><span id="total"></span></td>
<td>redirects</td>
</tr>
<tr>
<td><span id="projects"></span></td>
<td>projects</td>
</tr>
<tr>
<td><span id="organizations"></span></td>
<td>organizations</td>
</tr>
<tr>
<td colspan="2">in the last 90 days</td>
</tr>
</table>
<!-- <h3><span id="total"></span> redirects across <span id="projects"></span> projects in <span id="organizations"></span> organizations in the last 90 days</h3> -->
</body>
<script type="text/javascript">
setTimeout(refresh);
let url = "http://azdo.io/stats";
async function refresh() {
let stats = (await (await fetch(url)).json());
if (total.innerHTML != stats.total)
total.innerHTML = stats.total;
if (projects.innerHTML != stats.projects)
projects.innerHTML = stats.projects;
if (organizations.innerHTML != stats.organizations)
organizations.innerHTML = stats.organizations;
setTimeout(refresh, 60000);
}
</script>

</html>

0 comments on commit 8a38001

Please sign in to comment.
You can’t perform that action at this time.