BotSharp/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Stats.cs

72 lines
2.8 KiB
C#
Raw Normal View History

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-02-04 02:16:01 +00:00
public BotSharpStats? GetGlobalStats(string metric, string dimension, string value, DateTime recordTime, StatsInterval interval)
2025-01-24 05:45:43 +00:00
{
2025-01-29 05:37:06 +00:00
var (startTime, endTime) = BotSharpStats.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-02-04 02:16:01 +00:00
builder.Eq(x => x.Value, value),
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-02-04 02:16:01 +00:00
Value = found.Value,
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-29 05:37:06 +00:00
var (startTime, endTime) = BotSharpStats.BuildTimeInterval(body.RecordTime, body.IntervalType);
2025-01-28 22:46:59 +00:00
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-02-04 02:16:01 +00:00
builder.Eq(x => x.Value, body.Value),
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-02-04 02:16:01 +00:00
.Set(x => x.Value, body.Value)
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;
}
2024-01-29 04:18:40 +00:00
}