Skip to content

refine global stats #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ List<ConversationStateLogModel> GetConversationStateLogs(string conversationId)
#endregion

#region Statistics
BotSharpStats? GetGlobalStats(string metric, string dimension, string value, DateTime recordTime, StatsInterval interval)
BotSharpStats? GetGlobalStats(string metric, string dimension, string dimRefVal, DateTime recordTime, StatsInterval interval)
=> throw new NotImplementedException();
bool SaveGlobalStats(BotSharpStats body)
=> throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ namespace BotSharp.Abstraction.Statistics.Enums;

public enum StatsInterval
{
Hour = 1,
Day = 2,
Week = 3,
Month = 4
Minute = 1,
Hour = 2,
Day = 3
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BotSharp.Abstraction.Statistics.Enums;

public static class StatsCategory
public static class StatsMetric
{
public static string AgentLlmCost = "agent-llm-cost";
public static string AgentCall = "agent-call";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class BotSharpStats
[JsonPropertyName("dimension")]
public string Dimension { get; set; } = null!;

[JsonPropertyName("value")]
public string Value { get; set; } = null!;
[JsonPropertyName("dim_ref_val")]
public string DimRefVal { get; set; } = null!;

[JsonPropertyName("data")]
public IDictionary<string, double> Data { get; set; } = new Dictionary<string, double>();
Expand Down Expand Up @@ -46,7 +46,7 @@ public string Interval

public override string ToString()
{
return $"{Metric}-{Dimension}-{Value} ({Interval}): {Data?.Count ?? 0}";
return $"{Metric}-{Dimension}-{DimRefVal} ({Interval}): {Data?.Count ?? 0}";
}

public static (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsInterval interval)
Expand All @@ -56,20 +56,14 @@ public static (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsI

switch (interval)
{
case StatsInterval.Minute:
startTime = new DateTime(recordTime.Year, recordTime.Month, recordTime.Day, recordTime.Hour, recordTime.Minute, 0);
endTime = startTime.AddMinutes(1);
break;
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class BotSharpStatsInput
{
public string Metric { get; set; }
public string Dimension { get; set; }
public string Value { get; set; }
public string DimRefVal { get; set; }
public List<StatsKeyValuePair> Data { get; set; } = [];
public DateTime RecordTime { get; set; } = DateTime.UtcNow;
public StatsInterval IntervalType { get; set; } = StatsInterval.Day;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void AddToken(TokenStatsModel stats, RoleDialogModel message)
var globalStats = _services.GetRequiredService<IBotSharpStatsService>();
var body = new BotSharpStatsInput
{
Metric = StatsCategory.AgentLlmCost,
Metric = StatsMetric.AgentLlmCost,
Dimension = "agent",
Value = message.CurrentAgentId,
DimRefVal = message.CurrentAgentId,
RecordTime = DateTime.UtcNow,
IntervalType = StatsInterval.Day,
Data = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace BotSharp.Core.Repository;

public partial class FileRepository
{
public BotSharpStats? GetGlobalStats(string metric, string dimension, string value, DateTime recordTime, StatsInterval interval)
public BotSharpStats? GetGlobalStats(string metric, string dimension, string dimRefVal, DateTime recordTime, StatsInterval interval)
{
var baseDir = Path.Combine(_dbSettings.FileRepository, STATS_FOLDER);
var (startTime, endTime) = BotSharpStats.BuildTimeInterval(recordTime, interval);
Expand All @@ -18,7 +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.DimRefVal.IsEqualTo(dimRefVal)
&& x.StartTime == startTime
&& x.EndTime == endTime);

Expand Down Expand Up @@ -50,15 +50,15 @@ public bool SaveGlobalStats(BotSharpStats body)
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.DimRefVal.IsEqualTo(body.DimRefVal)
&& x.StartTime == startTime
&& x.EndTime == endTime);

if (found != null)
{
found.Metric = body.Metric;
found.Dimension = body.Dimension;
found.Value = body.Value;
found.DimRefVal = body.DimRefVal;
found.Data = body.Data;
found.RecordTime = body.RecordTime;
found.StartTime = body.StartTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public bool UpdateStats(string resourceKey, BotSharpStatsInput input)
|| input == null
|| string.IsNullOrEmpty(input.Metric)
|| string.IsNullOrEmpty(input.Dimension)
|| string.IsNullOrEmpty(input.Value)
|| string.IsNullOrEmpty(input.DimRefVal)
|| input.Data.IsNullOrEmpty())
{
return false;
Expand All @@ -41,14 +41,14 @@ public bool UpdateStats(string resourceKey, BotSharpStatsInput input)
var res = locker.Lock(resourceKey, () =>
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var body = db.GetGlobalStats(input.Metric, input.Dimension, input.Value, input.RecordTime, input.IntervalType);
var body = db.GetGlobalStats(input.Metric, input.Dimension, input.DimRefVal, input.RecordTime, input.IntervalType);
if (body == null)
{
var stats = new BotSharpStats
{
Metric = input.Metric,
Dimension = input.Dimension,
Value = input.Value,
DimRefVal = input.DimRefVal,
RecordTime = input.RecordTime,
IntervalType = input.IntervalType,
Data = input.Data.ToDictionary(x => x.Key, x => x.Value)
Expand Down Expand Up @@ -89,7 +89,7 @@ public bool UpdateStats(string resourceKey, BotSharpStatsInput input)
}
catch (Exception ex)
{
_logger.LogError($"Error when updating global stats {input.Metric}-{input.Dimension}-{input.Value}. {ex.Message}\r\n{ex.InnerException}");
_logger.LogError($"Error when updating global stats {input.Metric}-{input.Dimension}-{input.DimRefVal}. {ex.Message}\r\n{ex.InnerException}");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ private void UpdateAgentCall(RoleDialogModel message)

var body = new BotSharpStatsInput
{
Metric = StatsCategory.AgentCall,
Metric = StatsMetric.AgentCall,
Dimension = "agent",
Value = message.CurrentAgentId,
DimRefVal = message.CurrentAgentId,
RecordTime = DateTime.UtcNow,
IntervalType = StatsInterval.Day,
Data = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class GlobalStatisticsDocument : MongoBase
{
public string Metric { get; set; }
public string Dimension { get; set; }
public string Value { get; set; }
public string DimRefVal { get; set; }
public IDictionary<string, double> Data { get; set; } = new Dictionary<string, double>();
public DateTime RecordTime { get; set; }
public DateTime StartTime { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository;

public partial class MongoRepository
{
public BotSharpStats? GetGlobalStats(string metric, string dimension, string value, DateTime recordTime, StatsInterval interval)
public BotSharpStats? GetGlobalStats(string metric, string dimension, string dimRefVal, DateTime recordTime, StatsInterval interval)
{
var (startTime, endTime) = BotSharpStats.BuildTimeInterval(recordTime, interval);

Expand All @@ -14,7 +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.DimRefVal, dimRefVal),
builder.Eq(x => x.StartTime, startTime),
builder.Eq(x => x.EndTime, endTime)
};
Expand All @@ -27,7 +27,7 @@ public partial class MongoRepository
{
Metric = found.Metric,
Dimension = found.Dimension,
Value = found.Value,
DimRefVal = found.DimRefVal,
Data = found.Data,
RecordTime = found.RecordTime,
StartTime = startTime,
Expand All @@ -48,7 +48,7 @@ public bool SaveGlobalStats(BotSharpStats body)
{
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.DimRefVal, body.DimRefVal),
builder.Eq(x => x.StartTime, startTime),
builder.Eq(x => x.EndTime, endTime)
};
Expand All @@ -58,7 +58,7 @@ public bool SaveGlobalStats(BotSharpStats body)
.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.DimRefVal, body.DimRefVal)
.Set(x => x.Data, body.Data)
.Set(x => x.StartTime, body.StartTime)
.Set(x => x.EndTime, body.EndTime)
Expand Down