2025-04-02 03:44:23 +00:00
|
|
|
namespace ServiceLib.Handler;
|
|
|
|
|
|
|
|
public class StatisticsHandler
|
2019-10-11 06:15:20 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
private static readonly Lazy<StatisticsHandler> instance = new(() => new());
|
|
|
|
public static StatisticsHandler Instance => instance.Value;
|
2024-07-18 09:39:11 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
private Config _config;
|
|
|
|
private ServerStatItem? _serverStatItem;
|
|
|
|
private List<ServerStatItem> _lstServerStat;
|
|
|
|
private Action<ServerSpeedItem>? _updateFunc;
|
2024-11-19 07:52:08 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
private StatisticsXrayService? _statisticsXray;
|
|
|
|
private StatisticsSingboxService? _statisticsSingbox;
|
|
|
|
private static readonly string _tag = "StatisticsHandler";
|
|
|
|
public List<ServerStatItem> ServerStat => _lstServerStat;
|
2019-10-11 06:15:20 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public async Task Init(Config config, Action<ServerSpeedItem> updateFunc)
|
|
|
|
{
|
|
|
|
_config = config;
|
|
|
|
_updateFunc = updateFunc;
|
|
|
|
if (config.GuiItem.EnableStatistics || _config.GuiItem.DisplayRealTimeSpeed)
|
2020-04-20 11:49:27 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
await InitData();
|
2019-10-11 06:15:20 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
_statisticsXray = new StatisticsXrayService(config, UpdateServerStatHandler);
|
|
|
|
_statisticsSingbox = new StatisticsSingboxService(config, UpdateServerStatHandler);
|
2019-10-11 06:15:20 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public void Close()
|
|
|
|
{
|
|
|
|
try
|
2019-10-11 06:15:20 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
_statisticsXray?.Close();
|
|
|
|
_statisticsSingbox?.Close();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logging.SaveLog(_tag, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task ClearAllServerStatistics()
|
|
|
|
{
|
|
|
|
await SQLiteHelper.Instance.ExecuteAsync($"delete from ServerStatItem ");
|
|
|
|
_serverStatItem = null;
|
|
|
|
_lstServerStat = new();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task SaveTo()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_lstServerStat != null)
|
2019-10-11 06:15:20 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
await SQLiteHelper.Instance.UpdateAllAsync(_lstServerStat);
|
2019-10-11 06:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logging.SaveLog(_tag, ex);
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public async Task CloneServerStatItem(string indexId, string toIndexId)
|
|
|
|
{
|
|
|
|
if (_lstServerStat == null)
|
2019-10-11 06:15:20 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
return;
|
2023-01-04 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
if (indexId == toIndexId)
|
2023-01-04 02:21:26 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
return;
|
2019-10-11 06:15:20 +00:00
|
|
|
}
|
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
var stat = _lstServerStat.FirstOrDefault(t => t.IndexId == indexId);
|
|
|
|
if (stat == null)
|
2024-11-20 07:29:06 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-11-20 07:29:06 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
var toStat = JsonUtils.DeepCopy(stat);
|
|
|
|
toStat.IndexId = toIndexId;
|
|
|
|
await SQLiteHelper.Instance.ReplaceAsync(toStat);
|
|
|
|
_lstServerStat.Add(toStat);
|
|
|
|
}
|
2025-01-11 06:44:09 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
private async Task InitData()
|
|
|
|
{
|
|
|
|
await SQLiteHelper.Instance.ExecuteAsync($"delete from ServerStatItem where indexId not in ( select indexId from ProfileItem )");
|
2024-11-20 07:29:06 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
long ticks = DateTime.Now.Date.Ticks;
|
|
|
|
await SQLiteHelper.Instance.ExecuteAsync($"update ServerStatItem set todayUp = 0,todayDown=0,dateNow={ticks} where dateNow<>{ticks}");
|
2024-11-20 07:29:06 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
_lstServerStat = await SQLiteHelper.Instance.TableAsync<ServerStatItem>().ToListAsync();
|
|
|
|
}
|
2023-04-12 07:30:07 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
private void UpdateServerStatHandler(ServerSpeedItem server)
|
|
|
|
{
|
|
|
|
_ = UpdateServerStat(server);
|
|
|
|
}
|
2023-01-04 02:21:26 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
private async Task UpdateServerStat(ServerSpeedItem server)
|
|
|
|
{
|
|
|
|
await GetServerStatItem(_config.IndexId);
|
2019-10-11 06:15:20 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
if (_serverStatItem is null)
|
2023-05-09 07:51:32 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
return;
|
2024-10-20 03:51:05 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
if (server.ProxyUp != 0 || server.ProxyDown != 0)
|
2024-10-20 03:51:05 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
_serverStatItem.TodayUp += server.ProxyUp;
|
|
|
|
_serverStatItem.TodayDown += server.ProxyDown;
|
|
|
|
_serverStatItem.TotalUp += server.ProxyUp;
|
|
|
|
_serverStatItem.TotalDown += server.ProxyDown;
|
|
|
|
}
|
2023-05-09 07:51:32 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
server.IndexId = _config.IndexId;
|
|
|
|
server.TodayUp = _serverStatItem.TodayUp;
|
|
|
|
server.TodayDown = _serverStatItem.TodayDown;
|
|
|
|
server.TotalUp = _serverStatItem.TotalUp;
|
|
|
|
server.TotalDown = _serverStatItem.TotalDown;
|
|
|
|
_updateFunc?.Invoke(server);
|
|
|
|
}
|
2024-02-08 06:01:33 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
private async Task GetServerStatItem(string indexId)
|
|
|
|
{
|
|
|
|
long ticks = DateTime.Now.Date.Ticks;
|
|
|
|
if (_serverStatItem != null && _serverStatItem.IndexId != indexId)
|
|
|
|
{
|
|
|
|
_serverStatItem = null;
|
2023-05-09 07:51:32 +00:00
|
|
|
}
|
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
if (_serverStatItem == null)
|
2021-01-15 05:38:06 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
_serverStatItem = _lstServerStat.FirstOrDefault(t => t.IndexId == indexId);
|
2023-01-01 11:42:01 +00:00
|
|
|
if (_serverStatItem == null)
|
2019-10-11 06:15:20 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
_serverStatItem = new ServerStatItem
|
2019-10-11 06:15:20 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
IndexId = indexId,
|
|
|
|
TotalUp = 0,
|
|
|
|
TotalDown = 0,
|
|
|
|
TodayUp = 0,
|
|
|
|
TodayDown = 0,
|
|
|
|
DateNow = ticks
|
|
|
|
};
|
|
|
|
await SQLiteHelper.Instance.ReplaceAsync(_serverStatItem);
|
|
|
|
_lstServerStat.Add(_serverStatItem);
|
2019-10-11 06:15:20 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2023-01-01 11:42:01 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
if (_serverStatItem.DateNow != ticks)
|
|
|
|
{
|
|
|
|
_serverStatItem.TodayUp = 0;
|
|
|
|
_serverStatItem.TodayDown = 0;
|
|
|
|
_serverStatItem.DateNow = ticks;
|
2024-03-12 01:14:21 +00:00
|
|
|
}
|
2019-10-11 06:15:20 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|