commit
0aa14205cd
|
|
@ -200,7 +200,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
#endregion
|
||||
|
||||
#region Statistics
|
||||
BotSharpStats? GetGlobalStats(string metric, string dimension, DateTime recordTime, StatsInterval interval)
|
||||
BotSharpStats? GetGlobalStats(string metric, string dimension, string value, DateTime recordTime, StatsInterval interval)
|
||||
=> throw new NotImplementedException();
|
||||
bool SaveGlobalStats(BotSharpStats body)
|
||||
=> throw new NotImplementedException();
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@ public class BotSharpStats
|
|||
[JsonPropertyName("dimension")]
|
||||
public string Dimension { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("value")]
|
||||
public string Value { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("data")]
|
||||
public IDictionary<string, double> Data { get; set; } = new Dictionary<string, double>();
|
||||
|
||||
|
|
@ -43,7 +46,7 @@ public class BotSharpStats
|
|||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Metric}-{Dimension} ({Interval}): {Data?.Count ?? 0}";
|
||||
return $"{Metric}-{Dimension}-{Value} ({Interval}): {Data?.Count ?? 0}";
|
||||
}
|
||||
|
||||
public static (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsInterval interval)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ public class BotSharpStatsInput
|
|||
{
|
||||
public string Metric { get; set; }
|
||||
public string Dimension { get; set; }
|
||||
public string Value { get; set; }
|
||||
public List<StatsKeyValuePair> Data { get; set; } = [];
|
||||
public DateTime RecordTime { get; set; } = DateTime.UtcNow;
|
||||
public StatsInterval IntervalType { get; set; } = StatsInterval.Day;
|
||||
|
|
|
|||
|
|
@ -64,7 +64,8 @@ public class TokenStatistics : ITokenStatistics
|
|||
var body = new BotSharpStatsInput
|
||||
{
|
||||
Metric = StatsCategory.AgentLlmCost,
|
||||
Dimension = message.CurrentAgentId,
|
||||
Dimension = "agent",
|
||||
Value = message.CurrentAgentId,
|
||||
RecordTime = DateTime.UtcNow,
|
||||
IntervalType = StatsInterval.Day,
|
||||
Data = [
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace BotSharp.Core.Repository;
|
|||
|
||||
public partial class FileRepository
|
||||
{
|
||||
public BotSharpStats? GetGlobalStats(string metric, string dimension, DateTime recordTime, StatsInterval interval)
|
||||
public BotSharpStats? GetGlobalStats(string metric, string dimension, string value, DateTime recordTime, StatsInterval interval)
|
||||
{
|
||||
var baseDir = Path.Combine(_dbSettings.FileRepository, STATS_FOLDER);
|
||||
var (startTime, endTime) = BotSharpStats.BuildTimeInterval(recordTime, interval);
|
||||
|
|
@ -18,6 +18,7 @@ public partial class FileRepository
|
|||
var list = JsonSerializer.Deserialize<List<BotSharpStats>>(text, _options);
|
||||
var found = list?.FirstOrDefault(x => x.Metric.IsEqualTo(metric)
|
||||
&& x.Dimension.IsEqualTo(dimension)
|
||||
&& x.Value.IsEqualTo(value)
|
||||
&& x.StartTime == startTime
|
||||
&& x.EndTime == endTime);
|
||||
|
||||
|
|
@ -49,6 +50,7 @@ public partial class FileRepository
|
|||
var list = JsonSerializer.Deserialize<List<BotSharpStats>>(text, _options);
|
||||
var found = list?.FirstOrDefault(x => x.Metric.IsEqualTo(body.Metric)
|
||||
&& x.Dimension.IsEqualTo(body.Dimension)
|
||||
&& x.Value.IsEqualTo(body.Value)
|
||||
&& x.StartTime == startTime
|
||||
&& x.EndTime == endTime);
|
||||
|
||||
|
|
@ -56,6 +58,7 @@ public partial class FileRepository
|
|||
{
|
||||
found.Metric = body.Metric;
|
||||
found.Dimension = body.Dimension;
|
||||
found.Value = body.Value;
|
||||
found.Data = body.Data;
|
||||
found.RecordTime = body.RecordTime;
|
||||
found.StartTime = body.StartTime;
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ public class BotSharpStatsService : IBotSharpStatsService
|
|||
|| input == null
|
||||
|| string.IsNullOrEmpty(input.Metric)
|
||||
|| string.IsNullOrEmpty(input.Dimension)
|
||||
|| string.IsNullOrEmpty(input.Value)
|
||||
|| input.Data.IsNullOrEmpty())
|
||||
{
|
||||
return false;
|
||||
|
|
@ -40,13 +41,14 @@ public class BotSharpStatsService : IBotSharpStatsService
|
|||
var res = locker.Lock(resourceKey, () =>
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var body = db.GetGlobalStats(input.Metric, input.Dimension, input.RecordTime, input.IntervalType);
|
||||
var body = db.GetGlobalStats(input.Metric, input.Dimension, input.Value, input.RecordTime, input.IntervalType);
|
||||
if (body == null)
|
||||
{
|
||||
var stats = new BotSharpStats
|
||||
{
|
||||
Metric = input.Metric,
|
||||
Dimension = input.Dimension,
|
||||
Value = input.Value,
|
||||
RecordTime = input.RecordTime,
|
||||
IntervalType = input.IntervalType,
|
||||
Data = input.Data.ToDictionary(x => x.Key, x => x.Value)
|
||||
|
|
@ -87,7 +89,7 @@ public class BotSharpStatsService : IBotSharpStatsService
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Error when updating global stats {input.Metric}-{input.Dimension}. {ex.Message}\r\n{ex.InnerException}");
|
||||
_logger.LogError($"Error when updating global stats {input.Metric}-{input.Dimension}-{input.Value}. {ex.Message}\r\n{ex.InnerException}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,8 @@ public class GlobalStatsConversationHook : ConversationHookBase
|
|||
var body = new BotSharpStatsInput
|
||||
{
|
||||
Metric = StatsCategory.AgentCall,
|
||||
Dimension = message.CurrentAgentId,
|
||||
Dimension = "agent",
|
||||
Value = message.CurrentAgentId,
|
||||
RecordTime = DateTime.UtcNow,
|
||||
IntervalType = StatsInterval.Day,
|
||||
Data = [
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
using BotSharp.Abstraction.Statistics.Enums;
|
||||
|
||||
namespace BotSharp.Plugin.MongoStorage.Collections;
|
||||
|
||||
public class GlobalStatisticsDocument : MongoBase
|
||||
{
|
||||
public string Metric { get; set; }
|
||||
public string Dimension { get; set; }
|
||||
public string Value { get; set; }
|
||||
public IDictionary<string, double> Data { get; set; } = new Dictionary<string, double>();
|
||||
public DateTime RecordTime { get; set; }
|
||||
public DateTime StartTime { get; set; }
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository;
|
|||
|
||||
public partial class MongoRepository
|
||||
{
|
||||
public BotSharpStats? GetGlobalStats(string metric, string dimension, DateTime recordTime, StatsInterval interval)
|
||||
public BotSharpStats? GetGlobalStats(string metric, string dimension, string value, DateTime recordTime, StatsInterval interval)
|
||||
{
|
||||
var (startTime, endTime) = BotSharpStats.BuildTimeInterval(recordTime, interval);
|
||||
|
||||
|
|
@ -14,6 +14,7 @@ public partial class MongoRepository
|
|||
{
|
||||
builder.Eq(x => x.Metric, metric),
|
||||
builder.Eq(x => x.Dimension, dimension),
|
||||
builder.Eq(x => x.Value, value),
|
||||
builder.Eq(x => x.StartTime, startTime),
|
||||
builder.Eq(x => x.EndTime, endTime)
|
||||
};
|
||||
|
|
@ -26,6 +27,7 @@ public partial class MongoRepository
|
|||
{
|
||||
Metric = found.Metric,
|
||||
Dimension = found.Dimension,
|
||||
Value = found.Value,
|
||||
Data = found.Data,
|
||||
RecordTime = found.RecordTime,
|
||||
StartTime = startTime,
|
||||
|
|
@ -46,6 +48,7 @@ public partial class MongoRepository
|
|||
{
|
||||
builder.Eq(x => x.Metric, body.Metric),
|
||||
builder.Eq(x => x.Dimension, body.Dimension),
|
||||
builder.Eq(x => x.Value, body.Value),
|
||||
builder.Eq(x => x.StartTime, startTime),
|
||||
builder.Eq(x => x.EndTime, endTime)
|
||||
};
|
||||
|
|
@ -55,6 +58,7 @@ public partial class MongoRepository
|
|||
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
|
||||
.Set(x => x.Metric, body.Metric)
|
||||
.Set(x => x.Dimension, body.Dimension)
|
||||
.Set(x => x.Value, body.Value)
|
||||
.Set(x => x.Data, body.Data)
|
||||
.Set(x => x.StartTime, body.StartTime)
|
||||
.Set(x => x.EndTime, body.EndTime)
|
||||
|
|
|
|||
Loading…
Reference in a new issue