This commit is contained in:
Jicheng Lu 2025-01-28 23:37:06 -06:00
parent 01f5af4c75
commit ae8796e8c5
4 changed files with 44 additions and 73 deletions

View file

@ -45,4 +45,36 @@ public class BotSharpStats
{
return $"{Metric}-{Dimension} ({Interval}): {Data?.Count ?? 0}";
}
public static (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsInterval interval)
{
DateTime startTime = recordTime;
DateTime endTime = DateTime.UtcNow;
switch (interval)
{
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);
break;
}
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
endTime = DateTime.SpecifyKind(endTime, DateTimeKind.Utc);
return (startTime, endTime);
}
}

View file

@ -211,11 +211,11 @@ public class ConversationStateService : IConversationStateService, IDisposable
{
if (_conversationId == null)
{
Reset();
return;
}
var states = new List<StateKeyValue>();
foreach (var pair in _curStates)
{
var key = pair.Key;
@ -244,6 +244,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
}
_db.UpdateConversationStates(_conversationId, states);
Reset();
_logger.LogInformation($"Saved states of conversation {_conversationId}");
}
@ -421,4 +422,10 @@ public class ConversationStateService : IConversationStateService, IDisposable
{
_curStates.Clear();
}
private void Reset()
{
_curStates.Clear();
_historyStates.Clear();
}
}

View file

@ -7,7 +7,7 @@ public partial class FileRepository
public BotSharpStats? GetGlobalStats(string metric, string dimension, DateTime recordTime, StatsInterval interval)
{
var baseDir = Path.Combine(_dbSettings.FileRepository, STATS_FOLDER);
var (startTime, endTime) = BuildTimeInterval(recordTime, interval);
var (startTime, endTime) = BotSharpStats.BuildTimeInterval(recordTime, interval);
var dir = Path.Combine(baseDir, metric, startTime.Year.ToString(), startTime.Month.ToString("D2"));
if (!Directory.Exists(dir)) return null;
@ -27,7 +27,7 @@ public partial class FileRepository
public bool SaveGlobalStats(BotSharpStats body)
{
var baseDir = Path.Combine(_dbSettings.FileRepository, STATS_FOLDER);
var (startTime, endTime) = BuildTimeInterval(body.RecordTime, body.IntervalType);
var (startTime, endTime) = BotSharpStats.BuildTimeInterval(body.RecordTime, body.IntervalType);
body.StartTime = startTime;
body.EndTime = endTime;
@ -76,38 +76,4 @@ public partial class FileRepository
return true;
}
#region Private methods
private (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsInterval interval)
{
DateTime startTime = recordTime;
DateTime endTime = DateTime.UtcNow;
switch (interval)
{
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);
break;
}
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
endTime = DateTime.SpecifyKind(endTime, DateTimeKind.Utc);
return (startTime, endTime);
}
#endregion
}

View file

@ -7,7 +7,7 @@ public partial class MongoRepository
{
public BotSharpStats? GetGlobalStats(string metric, string dimension, DateTime recordTime, StatsInterval interval)
{
var (startTime, endTime) = BuildTimeInterval(recordTime, interval);
var (startTime, endTime) = BotSharpStats.BuildTimeInterval(recordTime, interval);
var builder = Builders<GlobalStatisticsDocument>.Filter;
var filters = new List<FilterDefinition<GlobalStatisticsDocument>>()
@ -36,7 +36,7 @@ public partial class MongoRepository
public bool SaveGlobalStats(BotSharpStats body)
{
var (startTime, endTime) = BuildTimeInterval(body.RecordTime, body.IntervalType);
var (startTime, endTime) = BotSharpStats.BuildTimeInterval(body.RecordTime, body.IntervalType);
body.RecordTime = DateTime.SpecifyKind(body.RecordTime, DateTimeKind.Utc);
body.StartTime = startTime;
body.EndTime = endTime;
@ -64,38 +64,4 @@ public partial class MongoRepository
_dc.GlobalStatistics.UpdateOne(filterDef, updateDef, _options);
return true;
}
#region Private methods
private (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsInterval interval)
{
DateTime startTime = recordTime;
DateTime endTime = DateTime.UtcNow;
switch (interval)
{
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);
break;
}
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
endTime = DateTime.SpecifyKind(endTime, DateTimeKind.Utc);
return (startTime, endTime);
}
#endregion
}