2025-01-28 22:46:59 +00:00
|
|
|
using BotSharp.Abstraction.Statistics.Enums;
|
2025-01-24 05:45:43 +00:00
|
|
|
using BotSharp.Abstraction.Statistics.Models;
|
2024-01-29 04:18:40 +00:00
|
|
|
|
2025-01-24 05:45:43 +00:00
|
|
|
namespace BotSharp.Plugin.MongoStorage.Repository;
|
|
|
|
|
|
|
|
|
|
public partial class MongoRepository
|
2024-01-29 04:18:40 +00:00
|
|
|
{
|
2025-01-28 22:46:59 +00:00
|
|
|
public BotSharpStats? GetGlobalStats(string metric, string dimension, DateTime recordTime, StatsInterval interval)
|
2025-01-24 05:45:43 +00:00
|
|
|
{
|
2025-01-28 22:46:59 +00:00
|
|
|
var (startTime, endTime) = BuildTimeInterval(recordTime, interval);
|
2025-01-24 05:45:43 +00:00
|
|
|
|
|
|
|
|
var builder = Builders<GlobalStatisticsDocument>.Filter;
|
|
|
|
|
var filters = new List<FilterDefinition<GlobalStatisticsDocument>>()
|
|
|
|
|
{
|
2025-01-28 16:56:45 +00:00
|
|
|
builder.Eq(x => x.Metric, metric),
|
|
|
|
|
builder.Eq(x => x.Dimension, dimension),
|
2025-01-28 22:46:59 +00:00
|
|
|
builder.Eq(x => x.StartTime, startTime),
|
|
|
|
|
builder.Eq(x => x.EndTime, endTime)
|
2025-01-24 05:45:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var filterDef = builder.And(filters);
|
|
|
|
|
var found = _dc.GlobalStatistics.Find(filterDef).FirstOrDefault();
|
|
|
|
|
if (found == null) return null;
|
|
|
|
|
|
|
|
|
|
return new BotSharpStats
|
|
|
|
|
{
|
2025-01-28 16:56:45 +00:00
|
|
|
Metric = found.Metric,
|
|
|
|
|
Dimension = found.Dimension,
|
2025-01-24 05:45:43 +00:00
|
|
|
Data = found.Data,
|
2025-01-28 22:46:59 +00:00
|
|
|
RecordTime = found.RecordTime,
|
|
|
|
|
StartTime = startTime,
|
|
|
|
|
EndTime = endTime,
|
|
|
|
|
Interval = interval.ToString()
|
2025-01-24 05:45:43 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SaveGlobalStats(BotSharpStats body)
|
2024-01-29 04:18:40 +00:00
|
|
|
{
|
2025-01-28 22:46:59 +00:00
|
|
|
var (startTime, endTime) = BuildTimeInterval(body.RecordTime, body.IntervalType);
|
|
|
|
|
body.RecordTime = DateTime.SpecifyKind(body.RecordTime, DateTimeKind.Utc);
|
|
|
|
|
body.StartTime = startTime;
|
|
|
|
|
body.EndTime = endTime;
|
|
|
|
|
|
2025-01-24 05:45:43 +00:00
|
|
|
var builder = Builders<GlobalStatisticsDocument>.Filter;
|
|
|
|
|
var filters = new List<FilterDefinition<GlobalStatisticsDocument>>()
|
2024-01-29 04:18:40 +00:00
|
|
|
{
|
2025-01-28 16:56:45 +00:00
|
|
|
builder.Eq(x => x.Metric, body.Metric),
|
|
|
|
|
builder.Eq(x => x.Dimension, body.Dimension),
|
2025-01-28 22:46:59 +00:00
|
|
|
builder.Eq(x => x.StartTime, startTime),
|
|
|
|
|
builder.Eq(x => x.EndTime, endTime)
|
2025-01-24 05:45:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var filterDef = builder.And(filters);
|
|
|
|
|
var updateDef = Builders<GlobalStatisticsDocument>.Update
|
|
|
|
|
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
|
2025-01-28 16:56:45 +00:00
|
|
|
.Set(x => x.Metric, body.Metric)
|
|
|
|
|
.Set(x => x.Dimension, body.Dimension)
|
2025-01-24 05:45:43 +00:00
|
|
|
.Set(x => x.Data, body.Data)
|
2025-01-28 22:46:59 +00:00
|
|
|
.Set(x => x.StartTime, body.StartTime)
|
|
|
|
|
.Set(x => x.EndTime, body.EndTime)
|
|
|
|
|
.Set(x => x.Interval, body.Interval)
|
|
|
|
|
.Set(x => x.RecordTime, body.RecordTime);
|
2025-01-24 05:45:43 +00:00
|
|
|
|
|
|
|
|
_dc.GlobalStatistics.UpdateOne(filterDef, updateDef, _options);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Private methods
|
2025-01-28 22:46:59 +00:00
|
|
|
private (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsInterval interval)
|
2025-01-24 05:45:43 +00:00
|
|
|
{
|
2025-01-28 22:46:59 +00:00
|
|
|
DateTime startTime = recordTime;
|
|
|
|
|
DateTime endTime = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
switch (interval)
|
|
|
|
|
{
|
|
|
|
|
case StatsInterval.Hour:
|
|
|
|
|
startTime = new DateTime(recordTime.Year, recordTime.Month, recordTime.Day, recordTime.Hour, 0, 0);
|
|
|
|
|
endTime = startTime.AddHours(1);
|
|
|
|
|
break;
|
|
|
|
|
case StatsInterval.Week:
|
|
|
|
|
var dayOfWeek = startTime.DayOfWeek;
|
|
|
|
|
var firstDayOfWeek = startTime.AddDays(-(int)dayOfWeek);
|
|
|
|
|
startTime = new DateTime(firstDayOfWeek.Year, firstDayOfWeek.Month, firstDayOfWeek.Day, 0, 0, 0);
|
|
|
|
|
endTime = startTime.AddDays(7);
|
|
|
|
|
break;
|
|
|
|
|
case StatsInterval.Month:
|
|
|
|
|
startTime = new DateTime(recordTime.Year, recordTime.Month, 1);
|
|
|
|
|
endTime = startTime.AddMonths(1);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
startTime = new DateTime(recordTime.Year, recordTime.Month, recordTime.Day, 0, 0, 0);
|
|
|
|
|
endTime = startTime.AddDays(1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
|
|
|
|
|
endTime = DateTime.SpecifyKind(endTime, DateTimeKind.Utc);
|
|
|
|
|
return (startTime, endTime);
|
2024-01-29 04:18:40 +00:00
|
|
|
}
|
2025-01-24 05:45:43 +00:00
|
|
|
#endregion
|
2024-01-29 04:18:40 +00:00
|
|
|
}
|