|
1 | 1 | using ServiceStack.Configuration; |
2 | 2 | using ServiceStack.DataAnnotations; |
| 3 | +using ServiceStack.OrmLite; |
3 | 4 |
|
4 | 5 | namespace ServiceStack.AI; |
5 | 6 |
|
6 | 7 | [ExcludeMetadata, Tag(TagNames.Admin), ExplicitAutoQuery] |
7 | 8 | public class AdminQueryChatCompletionLogs : QueryDb<ChatCompletionLog> |
8 | 9 | { |
9 | | - |
10 | 10 | public DateTime? Month { get; set; } |
11 | 11 | } |
12 | 12 |
|
| 13 | +[ExcludeMetadata, Tag(TagNames.Admin)] |
| 14 | +public class AdminMonthlyChatCompletionAnalytics : IGet, IReturn<AdminMonthlyChatCompletionAnalyticsResponse> |
| 15 | +{ |
| 16 | + public DateTime? Month { get; set; } |
| 17 | +} |
| 18 | +public class AdminMonthlyChatCompletionAnalyticsResponse |
| 19 | +{ |
| 20 | + public string Month { get; set; } |
| 21 | + public List<ChatCompletionStat> ModelStats { get; set; } |
| 22 | + public List<ChatCompletionStat> ProviderStats { get; set; } |
| 23 | + public List<ChatCompletionStat> DailyStats { get; set; } |
| 24 | +} |
| 25 | + |
| 26 | +[ExcludeMetadata, Tag(TagNames.Admin)] |
| 27 | +public class AdminDailyChatCompletionAnalytics : IGet, IReturn<AdminDailyChatCompletionAnalyticsResponse> |
| 28 | +{ |
| 29 | + public DateTime? Day { get; set; } |
| 30 | +} |
| 31 | +public class AdminDailyChatCompletionAnalyticsResponse |
| 32 | +{ |
| 33 | + public List<ChatCompletionStat> ModelStats { get; set; } |
| 34 | + public List<ChatCompletionStat> ProviderStats { get; set; } |
| 35 | +} |
| 36 | + |
| 37 | +public class ChatCompletionStat |
| 38 | +{ |
| 39 | + public string Name { get; set; } |
| 40 | + public int Requests { get; set; } |
| 41 | + public int InputTokens { get; set; } |
| 42 | + public int OutputTokens { get; set; } |
| 43 | + public decimal Cost { get; set; } |
| 44 | +} |
| 45 | + |
13 | 46 | public class AdminChatServices(IAutoQueryDb autoQuery) |
14 | 47 | : Service |
15 | 48 | { |
16 | | - private ChatFeature AssertRequiredRole() |
| 49 | + private (ChatFeature,IChatStore) AssertRequiredRole() |
17 | 50 | { |
18 | 51 | var feature = AssertPlugin<ChatFeature>(); |
19 | 52 | RequiredRoleAttribute.AssertRequiredRoles(Request, RoleNames.Admin); |
20 | | - return feature; |
| 53 | + var chatStore = feature.ChatStore |
| 54 | + ?? throw new Exception("ChatStore is not configured"); |
| 55 | + return (feature, chatStore); |
21 | 56 | } |
22 | 57 |
|
23 | | - public object Any(AdminQueryChatCompletionLogs request) |
| 58 | + public async Task<object> Any(AdminQueryChatCompletionLogs request) |
24 | 59 | { |
25 | | - var feature = AssertRequiredRole(); |
26 | | - var chatStore = feature.ChatStore |
27 | | - ?? throw new Exception("ChatStore is not configured"); |
28 | | - var month = request.Month ?? DateTime.UtcNow; |
29 | | - using var monthDb = chatStore.OpenMonthDb(month); |
| 60 | + var (feature, chatStore) = AssertRequiredRole(); |
| 61 | + var monthDate = request.Month ?? DateTime.UtcNow; |
| 62 | + var monthStart = new DateTime(monthDate.Year, monthDate.Month, 1); |
| 63 | + using var monthDb = chatStore.OpenMonthDb(monthDate); |
30 | 64 | var q = autoQuery.CreateQuery(request, base.Request, monthDb); |
31 | | - return autoQuery.Execute(request, q, base.Request, monthDb); |
| 65 | + q.Ensure(x => x.CreatedDate >= monthStart && x.CreatedDate < monthStart.AddMonths(1)); |
| 66 | + return await autoQuery.ExecuteAsync(request, q, base.Request, monthDb); |
| 67 | + } |
| 68 | + |
| 69 | + public async Task<object> Any(AdminMonthlyChatCompletionAnalytics request) |
| 70 | + { |
| 71 | + var (feature, chatStore) = AssertRequiredRole(); |
| 72 | + var monthDate = (request.Month ?? DateTime.UtcNow); |
| 73 | + var month = new DateTime(monthDate.Year, monthDate.Month, 1); |
| 74 | + using var monthDb = chatStore.OpenMonthDb(month); |
| 75 | + |
| 76 | + var modelStats = await monthDb.SelectAsync<ChatCompletionStat>(monthDb.From<ChatCompletionLog>() |
| 77 | + .Where(x => x.CreatedDate >= month && x.CreatedDate < month.AddMonths(1)) |
| 78 | + .GroupBy(x => x.Model) |
| 79 | + .Select(x => new { |
| 80 | + Name = x.Model, |
| 81 | + Requests = Sql.Count("*"), |
| 82 | + InputTokens = Sql.Sum(x.PromptTokens ?? 0), |
| 83 | + OutputTokens = Sql.Sum(x.CompletionTokens ?? 0), |
| 84 | + Cost = Sql.Sum(x.Cost), |
| 85 | + })); |
| 86 | + var providerStats = await monthDb.SelectAsync<ChatCompletionStat>(monthDb.From<ChatCompletionLog>() |
| 87 | + .Where(x => x.CreatedDate >= month && x.CreatedDate < month.AddMonths(1)) |
| 88 | + .GroupBy(x => x.Provider) |
| 89 | + .Select(x => new { |
| 90 | + Name = x.Provider, |
| 91 | + Requests = Sql.Count("*"), |
| 92 | + InputTokens = Sql.Sum(x.PromptTokens ?? 0), |
| 93 | + OutputTokens = Sql.Sum(x.CompletionTokens ?? 0), |
| 94 | + Cost = Sql.Sum(x.Cost), |
| 95 | + })); |
| 96 | + |
| 97 | + var q = monthDb.From<ChatCompletionLog>(); |
| 98 | + var createdDate = q.Column<ChatCompletionLog>(c => c.CreatedDate); |
| 99 | + var dailyStatsForMonth = await monthDb.SelectAsync<ChatCompletionStat>(q |
| 100 | + .Where(x => x.CreatedDate >= month && x.CreatedDate < month.AddMonths(1)) |
| 101 | + .GroupBy(x => q.sql.DateFormat(createdDate, "%d")) |
| 102 | + .Select(x => new { |
| 103 | + Name = Sql.As(q.sql.DateFormat(createdDate, "%d"), "'Name'"), |
| 104 | + Requests = Sql.Count("*"), |
| 105 | + InputTokens = Sql.Sum(x.PromptTokens ?? 0), |
| 106 | + OutputTokens = Sql.Sum(x.CompletionTokens ?? 0), |
| 107 | + Cost = Sql.Sum(x.Cost), |
| 108 | + })); |
| 109 | + |
| 110 | + return new AdminMonthlyChatCompletionAnalyticsResponse |
| 111 | + { |
| 112 | + Month = month.ToString("yyyy-MM"), |
| 113 | + ModelStats = modelStats, |
| 114 | + ProviderStats = providerStats, |
| 115 | + DailyStats = dailyStatsForMonth, |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + public async Task<object> Any(AdminDailyChatCompletionAnalytics request) |
| 120 | + { |
| 121 | + var (feature, chatStore) = AssertRequiredRole(); |
| 122 | + var dayDate = (request.Day ?? DateTime.UtcNow); |
| 123 | + var month = new DateTime(dayDate.Year, dayDate.Month, dayDate.Day); |
| 124 | + using var monthDb = chatStore.OpenMonthDb(month); |
| 125 | + |
| 126 | + var modelStats = await monthDb.SelectAsync<ChatCompletionStat>(monthDb.From<ChatCompletionLog>() |
| 127 | + .Where(x => x.CreatedDate >= month && x.CreatedDate < month.AddDays(1)) |
| 128 | + .GroupBy(x => x.Model) |
| 129 | + .Select(x => new { |
| 130 | + Name = x.Model, |
| 131 | + Requests = Sql.Count("*"), |
| 132 | + InputTokens = Sql.Sum(x.PromptTokens ?? 0), |
| 133 | + OutputTokens = Sql.Sum(x.CompletionTokens ?? 0), |
| 134 | + Cost = Sql.Sum(x.Cost), |
| 135 | + })); |
| 136 | + var providerStats = await monthDb.SelectAsync<ChatCompletionStat>(monthDb.From<ChatCompletionLog>() |
| 137 | + .Where(x => x.CreatedDate >= month && x.CreatedDate < month.AddDays(1)) |
| 138 | + .GroupBy(x => x.Provider) |
| 139 | + .Select(x => new { |
| 140 | + Name = x.Provider, |
| 141 | + Requests = Sql.Count("*"), |
| 142 | + InputTokens = Sql.Sum(x.PromptTokens ?? 0), |
| 143 | + OutputTokens = Sql.Sum(x.CompletionTokens ?? 0), |
| 144 | + Cost = Sql.Sum(x.Cost), |
| 145 | + })); |
| 146 | + |
| 147 | + return new AdminDailyChatCompletionAnalyticsResponse |
| 148 | + { |
| 149 | + ModelStats = modelStats, |
| 150 | + ProviderStats = providerStats, |
| 151 | + }; |
32 | 152 | } |
33 | 153 | } |
0 commit comments