Skip to content

Commit b66ac96

Browse files
authored
Merge pull request #842 from iceljc/features/add-global-stats
add global stats
2 parents 7bb6e7c + 7e38b4a commit b66ac96

File tree

26 files changed

+422
-107
lines changed

26 files changed

+422
-107
lines changed
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace BotSharp.Abstraction.Infrastructures.Enums;
62

7-
namespace BotSharp.Abstraction.Infrastructures.Enums
3+
public enum CacheType
84
{
9-
public enum CacheType
10-
{
11-
MemoryCache,
12-
RedisCache
13-
}
5+
MemoryCache = 1,
6+
RedisCache = 2
147
}

src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ namespace BotSharp.Abstraction.Infrastructures;
33
public class SharpCacheSettings
44
{
55
public bool Enabled { get; set; } = true;
6-
public CacheType CacheType { get; set; } = Enums.CacheType.MemoryCache;
6+
public CacheType CacheType { get; set; } = CacheType.MemoryCache;
77
public string Prefix { get; set; } = "cache";
88
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using BotSharp.Abstraction.Repositories.Filters;
44
using BotSharp.Abstraction.Roles.Models;
55
using BotSharp.Abstraction.Shared;
6+
using BotSharp.Abstraction.Statistics.Models;
67
using BotSharp.Abstraction.Tasks.Models;
78
using BotSharp.Abstraction.Translation.Models;
89
using BotSharp.Abstraction.Users.Enums;
@@ -119,7 +120,9 @@ public interface IBotSharpRepository : IHaveServiceProvider
119120
#endregion
120121

121122
#region Statistics
122-
void IncrementConversationCount();
123+
BotSharpStats? GetGlobalStats(string category, string group, DateTime recordTime) => throw new NotImplementedException();
124+
bool SaveGlobalStats(BotSharpStats body) => throw new NotImplementedException();
125+
123126
#endregion
124127

125128
#region Translation
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.Abstraction.Statistics.Enums;
2+
3+
public static class StatCategory
4+
{
5+
public static string LlmCost = "llm-cost";
6+
public static string AgentCall = "agent-call";
7+
}

src/Infrastructure/BotSharp.Abstraction/Statistics/Model/Statistics.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace BotSharp.Abstraction.Statistics.Models;
2+
3+
public class BotSharpStats
4+
{
5+
[JsonPropertyName("category")]
6+
public string Category { get; set; } = null!;
7+
8+
[JsonPropertyName("group")]
9+
public string Group { get; set; } = null!;
10+
11+
[JsonPropertyName("data")]
12+
public IDictionary<string, object> Data { get; set; } = new Dictionary<string, object>();
13+
14+
private DateTime innerRecordTime;
15+
16+
[JsonPropertyName("record_time")]
17+
public DateTime RecordTime
18+
{
19+
get
20+
{
21+
return innerRecordTime;
22+
}
23+
set
24+
{
25+
var date = new DateTime(value.Year, value.Month, value.Day, value.Hour, 0, 0);
26+
innerRecordTime = DateTime.SpecifyKind(date, DateTimeKind.Utc);
27+
}
28+
}
29+
30+
public override string ToString()
31+
{
32+
return $"{Category}-{Group}: {Data?.Count ?? 0} ({RecordTime})";
33+
}
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using BotSharp.Abstraction.Statistics.Models;
2+
3+
namespace BotSharp.Abstraction.Statistics.Services;
4+
5+
public interface IBotSharpStatService
6+
{
7+
bool UpdateLlmCost(BotSharpStats stats);
8+
bool UpdateAgentCall(BotSharpStats stats);
9+
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
namespace BotSharp.Abstraction.Statistics.Settings;
42

5-
namespace BotSharp.Abstraction.Statistics.Settings
3+
public class StatisticsSettings
64
{
7-
public class StatisticsSettings
8-
{
9-
public string DataDir { get; set; }
10-
}
5+
public bool Enabled { get; set; }
116
}

src/Infrastructure/BotSharp.Abstraction/Utilities/MathExt.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ public static long Max(long a, long b, long c)
1111
{
1212
return Math.Max(Math.Max(a, b), c);
1313
}
14+
15+
public static decimal Round(decimal value, MidpointRounding rouding = MidpointRounding.AwayFromZero)
16+
{
17+
return Math.Round(value, rouding);
18+
}
1419
}

src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
3232
services.AddScoped<ILlmProviderService, LlmProviderService>();
3333
services.AddScoped<IAgentService, AgentService>();
3434
services.AddScoped<IAgentHook, BasicAgentHook>();
35+
services.AddScoped<IBotSharpStatService, BotSharpStatService>();
3536

3637
services.AddScoped(provider =>
3738
{

0 commit comments

Comments
 (0)