1
0
mirror of https://github.com/chylex/Discord-History-Tracker.git synced 2025-04-10 17:15:43 +02:00

Speed up opening database by deferring the initial refresh of total message count

This commit is contained in:
chylex 2022-03-21 14:12:32 +01:00
parent 3b41ea7b5f
commit 277e241183
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
5 changed files with 25 additions and 14 deletions

View File

@ -5,7 +5,7 @@ using Avalonia.Data.Converters;
namespace DHT.Desktop.Common {
sealed class NumberValueConverter : IValueConverter {
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
return string.Format(Program.Culture, "{0:n0}", value);
return value == null ? "-" : string.Format(Program.Culture, "{0:n0}", value);
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {

View File

@ -35,7 +35,7 @@ namespace DHT.Desktop.Main.Pages {
private readonly IDatabaseFile db;
private bool isPageVisible = false;
[Obsolete("Designer")]
public ViewerPageModel() : this(null!, DummyDatabaseFile.Instance) {}
@ -46,7 +46,6 @@ namespace DHT.Desktop.Main.Pages {
FilterModel = new FilterPanelModel(window, db);
FilterModel.FilterPropertyChanged += OnFilterPropertyChanged;
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
UpdateStatistics();
}
public void Dispose() {
@ -73,13 +72,17 @@ namespace DHT.Desktop.Main.Pages {
}
private void UpdateStatistics() {
ExportedMessageText = "Will export " + db.CountMessages(FilterModel.CreateFilter()).Format() + " out of " + db.Statistics.TotalMessages.Format() + " message(s).";
var filter = FilterModel.CreateFilter();
var allMessagesCount = db.Statistics.TotalMessages?.Format() ?? "?";
var filteredMessagesCount = filter.IsEmpty ? allMessagesCount : db.CountMessages(filter).Format();
ExportedMessageText = "Will export " + filteredMessagesCount + " out of " + allMessagesCount + " message(s).";
OnPropertyChanged(nameof(ExportedMessageText));
}
private async Task<string> GenerateViewerContents() {
string json = ViewerJsonExport.Generate(db, FilterModel.CreateFilter());
string index = await Resources.ReadTextAsync("Viewer/index.html");
string viewer = index.Replace("/*[JS]*/", await Resources.ReadJoinedAsync("Viewer/scripts/", '\n'))
.Replace("/*[CSS]*/", await Resources.ReadJoinedAsync("Viewer/styles/", '\n'))

View File

@ -3,11 +3,17 @@ using System.Collections.Generic;
namespace DHT.Server.Data.Filters {
public sealed class MessageFilter {
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public DateTime? StartDate { get; set; } = null;
public DateTime? EndDate { get; set; } = null;
public HashSet<ulong>? ChannelIds { get; set; } = null;
public HashSet<ulong>? UserIds { get; set; } = null;
public HashSet<ulong>? MessageIds { get; set; } = null;
public bool IsEmpty => StartDate == null &&
EndDate == null &&
ChannelIds == null &&
UserIds == null &&
MessageIds == null;
}
}

View File

@ -5,7 +5,7 @@ namespace DHT.Server.Database {
private long totalServers;
private long totalChannels;
private long totalUsers;
private long totalMessages;
private long? totalMessages;
public long TotalServers {
get => totalServers;
@ -22,7 +22,7 @@ namespace DHT.Server.Database {
internal set => Change(ref totalUsers, value);
}
public long TotalMessages {
public long? TotalMessages {
get => totalMessages;
internal set => Change(ref totalMessages, value);
}

View File

@ -46,11 +46,13 @@ namespace DHT.Server.Database.Sqlite {
this.Path = path;
this.Statistics = new DatabaseStatistics();
using var conn = pool.Take();
UpdateServerStatistics(conn);
UpdateChannelStatistics(conn);
UpdateUserStatistics(conn);
UpdateMessageStatistics(conn);
using (var conn = pool.Take()) {
UpdateServerStatistics(conn);
UpdateChannelStatistics(conn);
UpdateUserStatistics(conn);
}
messageStatisticsThread.RequestUpdate();
}
public void Dispose() {