Pull data from Grafana

This commit is contained in:
Gervasio Marchand 2023-01-12 16:11:24 -03:00
parent d6bc994416
commit 95d72cf0bd
No known key found for this signature in database
GPG Key ID: B7736CB188DD0A38
3 changed files with 104 additions and 2 deletions

View File

@ -13,6 +13,9 @@ public class Config
public string ConfigPath { get; } public string ConfigPath { get; }
public string? HomeRedirect { get; } public string? HomeRedirect { get; }
public string? GrafanaHost { get; }
public string? GrafanaKey { get; }
public int? GrafanaDataSourceId { get; }
private Config() private Config()
{ {
@ -20,13 +23,16 @@ public class Config
PublicKey = Host = ConfigPath = ""; PublicKey = Host = ConfigPath = "";
} }
private Config(string publicKey, byte[] privateKey, string host, string configPath, string? homeRedirect) private Config(string publicKey, byte[] privateKey, string host, string configPath, string? homeRedirect, string? grafanaHost, string? grafanaKey, int? grafanaDataSourceId)
{ {
PrivateKey = privateKey; PrivateKey = privateKey;
PublicKey = publicKey; PublicKey = publicKey;
Host = host; Host = host;
ConfigPath = configPath; ConfigPath = configPath;
HomeRedirect = homeRedirect; HomeRedirect = homeRedirect;
GrafanaHost = grafanaHost;
GrafanaKey = grafanaKey;
GrafanaDataSourceId = grafanaDataSourceId;
} }
public static void Init(string path) public static void Init(string path)
@ -50,7 +56,7 @@ public class Config
using var rsa = RSA.Create(); using var rsa = RSA.Create();
rsa.ImportFromPem(data.PrivateKey.ToCharArray()); rsa.ImportFromPem(data.PrivateKey.ToCharArray());
Instance = new Config(data.PublicKey, rsa.ExportRSAPrivateKey(), data.Host, path, data.HomeRedirect); Instance = new Config(data.PublicKey, rsa.ExportRSAPrivateKey(), data.Host, path, data.HomeRedirect, data.GrafanaHost, data.GrafanaKey, data.GrafanaDataSourceId);
} }
public static void CreateConfig(string path, string host, string publicKey, string privateKey) public static void CreateConfig(string path, string host, string publicKey, string privateKey)
@ -70,5 +76,8 @@ public class Config
public string? PrivateKey { get; set; } public string? PrivateKey { get; set; }
public string? Host { get; set; } public string? Host { get; set; }
public string? HomeRedirect { get; set; } public string? HomeRedirect { get; set; }
public string? GrafanaHost { get; set; }
public string? GrafanaKey { get; set; }
public int? GrafanaDataSourceId { get; set; }
} }
} }

View File

@ -0,0 +1,68 @@
using System.Net.Http.Headers;
using System.Runtime.Serialization;
using Jil;
namespace FakeRelay.Core.Helpers;
public static class GrafanaHelper
{
private static HttpClient? _httpClient;
private static HttpClient HttpClient => _httpClient ??= GetHttpClient();
private static HttpClient GetHttpClient()
{
var httpClient = new HttpClient { BaseAddress = new Uri("https://" + Config.Instance.GrafanaHost) };
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Config.Instance.GrafanaKey);
return httpClient;
}
public static async Task<int> GetCountInPeriod(string hostname, string period)
{
if (Config.Instance.GrafanaHost.IsNullOrEmpty() || Config.Instance.GrafanaKey.IsNullOrEmpty() || !Config.Instance.GrafanaDataSourceId.HasValue)
{
return -1;
}
var requestContent = new StringContent($@"
{{""queries"":[{{""datasourceId"":{Config.Instance.GrafanaDataSourceId.Value},""expr"":""increase(fr_index_requests{{exported_instance=\""{hostname}\""}}[{period}])""}}],""to"":""now"",""from"":""now""}}
");
requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = await HttpClient.PostAsync("/api/ds/query", requestContent);
if (!result.IsSuccessStatusCode)
{
return -1;
}
var json = await result.Content.ReadAsStringAsync();
var data = JSON.Deserialize<GrafanaResponse>(json, Options.CamelCase);
return (int)Math.Round(data.Results.A.Frames[0].Data.Values[1][0]);
}
}
public class GrafanaResponse
{
public ResultData Results { get; set; }
public class ResultData
{
[DataMember(Name = "A")]
public AData A { get; set; }
public class AData
{
public FrameData[] Frames { get; set; }
public class FrameData
{
public DataData Data { get; set; }
public class DataData
{
public float[][] Values { get; set; }
}
}
}
}
}

View File

@ -1,4 +1,6 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Text.RegularExpressions;
using FakeRelay.Core;
using FakeRelay.Core.Helpers; using FakeRelay.Core.Helpers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
@ -53,4 +55,27 @@ public class ApiController : Controller
Response.Headers["instance"] = host; Response.Headers["instance"] = host;
return Content(response, "application/activity+json"); return Content(response, "application/activity+json");
} }
[Route("index-posts-count")]
public async Task<ActionResult> IndexedPostsCount(string period)
{
if (Config.Instance.GrafanaHost.IsNullOrEmpty() || Config.Instance.GrafanaKey.IsNullOrEmpty())
{
return NotFound();
}
if (!Regex.IsMatch(period, "^[0-9]+[mhd]$"))
{
return BadRequest();
}
var host = await GetHostFromRequest();
if (host == null)
{
return Unauthorized();
}
var count = await GrafanaHelper.GetCountInPeriod(host, period);
return Content(count.ToString());
}
} }