2022-01-20 00:29:51 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
2022-03-15 04:29:34 +00:00
|
|
|
using Newtonsoft.Json;
|
2022-01-20 00:29:51 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
namespace PluralKit.API;
|
|
|
|
|
|
|
|
// Internal API definitions
|
|
|
|
// I would prefer if you do not use any of these APIs in your own integrations.
|
|
|
|
// It is unstable and subject to change at any time (which is why it's not versioned)
|
|
|
|
|
|
|
|
// If for some reason you do need access to something defined here,
|
|
|
|
// let us know in #api-support on the support server (https://discord.com/invite/PczBt78) and I'll see if it can be made public
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("private")]
|
|
|
|
public class PrivateController: PKControllerBase
|
|
|
|
{
|
2022-01-22 08:52:52 +00:00
|
|
|
private readonly RedisService _redis;
|
2022-03-30 08:36:22 +00:00
|
|
|
|
2022-01-22 08:52:52 +00:00
|
|
|
public PrivateController(IServiceProvider svc) : base(svc)
|
|
|
|
{
|
|
|
|
_redis = svc.GetRequiredService<RedisService>();
|
|
|
|
}
|
2022-01-20 00:29:51 +00:00
|
|
|
|
|
|
|
[HttpGet("meta")]
|
|
|
|
public async Task<ActionResult<JObject>> Meta()
|
|
|
|
{
|
2022-01-22 08:52:52 +00:00
|
|
|
var db = _redis.Connection.GetDatabase();
|
|
|
|
var redisInfo = await db.HashGetAllAsync("pluralkit:shardstatus");
|
2022-01-23 07:17:13 +00:00
|
|
|
var shards = redisInfo.Select(x => Proto.Unmarshal<ShardState>(x.Value)).OrderBy(x => x.ShardId);
|
2022-01-22 08:52:52 +00:00
|
|
|
|
2022-03-15 04:09:27 +00:00
|
|
|
var redisClusterInfo = await db.HashGetAllAsync("pluralkit:cluster_stats");
|
|
|
|
var clusterInfo = redisClusterInfo.Select(x => JsonConvert.DeserializeObject<ClusterMetricInfo>(x.Value));
|
|
|
|
|
|
|
|
var guildCount = clusterInfo.Sum(x => x.GuildCount);
|
|
|
|
var channelCount = clusterInfo.Sum(x => x.ChannelCount);
|
|
|
|
|
2022-01-20 00:29:51 +00:00
|
|
|
var stats = await _repo.GetStats();
|
|
|
|
|
|
|
|
var o = new JObject();
|
2022-01-22 08:52:52 +00:00
|
|
|
o.Add("shards", shards.ToJson());
|
2022-03-15 04:09:27 +00:00
|
|
|
o.Add("stats", stats.ToJson(guildCount, channelCount));
|
2022-01-24 13:18:34 +00:00
|
|
|
o.Add("version", BuildInfoService.FullVersion);
|
2022-01-20 00:29:51 +00:00
|
|
|
|
|
|
|
return Ok(o);
|
|
|
|
}
|
2022-01-22 08:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class PrivateJsonExt
|
|
|
|
{
|
|
|
|
public static JArray ToJson(this IEnumerable<ShardState> shards)
|
|
|
|
{
|
|
|
|
var o = new JArray();
|
|
|
|
|
|
|
|
foreach (var shard in shards)
|
|
|
|
{
|
|
|
|
var s = new JObject();
|
|
|
|
s.Add("id", shard.ShardId);
|
|
|
|
|
|
|
|
if (!shard.Up)
|
|
|
|
s.Add("status", "down");
|
|
|
|
else
|
|
|
|
s.Add("status", "up");
|
|
|
|
|
|
|
|
s.Add("ping", shard.Latency);
|
2022-01-23 07:30:03 +00:00
|
|
|
s.Add("disconnection_count", shard.DisconnectionCount);
|
2022-01-22 08:52:52 +00:00
|
|
|
s.Add("last_heartbeat", shard.LastHeartbeat.ToString());
|
|
|
|
s.Add("last_connection", shard.LastConnection.ToString());
|
2022-02-07 16:05:55 +00:00
|
|
|
if (shard.HasClusterId)
|
|
|
|
s.Add("cluster_id", shard.ClusterId);
|
2022-01-22 08:52:52 +00:00
|
|
|
|
|
|
|
o.Add(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
2022-01-20 00:29:51 +00:00
|
|
|
}
|