Merge pull request 'peformance verbesserungen' (#3) from wasm-integration into main
Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
@@ -358,20 +358,39 @@ else
|
|||||||
_userId = int.Parse(claim.Value);
|
_userId = int.Parse(claim.Value);
|
||||||
_monday = GetMonday(DateOnly.FromDateTime(DateTime.Today));
|
_monday = GetMonday(DateOnly.FromDateTime(DateTime.Today));
|
||||||
_settings = await TrackerService.GetSettingsAsync(_userId);
|
_settings = await TrackerService.GetSettingsAsync(_userId);
|
||||||
await LoadWeek();
|
|
||||||
_totalOvertime = await TrackerService.GetTotalOvertimeAsync(_userId, _settings);
|
var loadWeekTask = LoadWeek();
|
||||||
|
var overtimeTask = TrackerService.GetTotalOvertimeAsync(_userId, _settings);
|
||||||
|
|
||||||
|
await Task.WhenAll(loadWeekTask, overtimeTask);
|
||||||
|
_totalOvertime = await overtimeTask;
|
||||||
_loading = false;
|
_loading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task LoadWeek()
|
private async Task LoadWeek()
|
||||||
{
|
{
|
||||||
|
Task<List<PublicHoliday>> holidaysTask;
|
||||||
if (_monday.Year != _holidayYear)
|
if (_monday.Year != _holidayYear)
|
||||||
{
|
{
|
||||||
var list = await HolidayService.GetHolidaysAsync(_monday.Year, _settings.GermanState);
|
holidaysTask = HolidayService.GetHolidaysAsync(_monday.Year, _settings.GermanState);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
holidaysTask = Task.FromResult(new List<PublicHoliday>());
|
||||||
|
}
|
||||||
|
|
||||||
|
var dbDaysTask = TrackerService.GetWeekAsync(_userId, _monday);
|
||||||
|
|
||||||
|
await Task.WhenAll(holidaysTask, dbDaysTask);
|
||||||
|
|
||||||
|
if (_monday.Year != _holidayYear)
|
||||||
|
{
|
||||||
|
var list = await holidaysTask;
|
||||||
_holidays = list.ToDictionary(h => h.Date, h => h.Name);
|
_holidays = list.ToDictionary(h => h.Date, h => h.Name);
|
||||||
_holidayYear = _monday.Year;
|
_holidayYear = _monday.Year;
|
||||||
}
|
}
|
||||||
var dbDays = await TrackerService.GetWeekAsync(_userId, _monday);
|
|
||||||
|
var dbDays = await dbDaysTask;
|
||||||
_days = Enumerable.Range(0, 7).Select(i =>
|
_days = Enumerable.Range(0, 7).Select(i =>
|
||||||
{
|
{
|
||||||
var date = _monday.AddDays(i);
|
var date = _monday.AddDays(i);
|
||||||
|
|||||||
@@ -181,9 +181,15 @@ else
|
|||||||
|
|
||||||
private async Task LoadMonth()
|
private async Task LoadMonth()
|
||||||
{
|
{
|
||||||
var workDays = await TrackerService.GetMonthAsync(_userId, _year, _month);
|
var workDaysTask = TrackerService.GetMonthAsync(_userId, _year, _month);
|
||||||
var holidays = await HolidayService.GetHolidaysAsync(_year, _settings.GermanState);
|
var holidaysTask = HolidayService.GetHolidaysAsync(_year, _settings.GermanState);
|
||||||
var vacations = await TrackerService.GetVacationDaysAsync(_userId, _year);
|
var vacationsTask = TrackerService.GetVacationDaysAsync(_userId, _year);
|
||||||
|
|
||||||
|
await Task.WhenAll(workDaysTask, holidaysTask, vacationsTask);
|
||||||
|
|
||||||
|
var workDays = await workDaysTask;
|
||||||
|
var holidays = await holidaysTask;
|
||||||
|
var vacations = await vacationsTask;
|
||||||
|
|
||||||
var holidayMap = holidays.ToDictionary(h => h.Date, h => h.Name);
|
var holidayMap = holidays.ToDictionary(h => h.Date, h => h.Name);
|
||||||
var vacationSet = vacations.Select(v => v.Date).ToHashSet();
|
var vacationSet = vacations.Select(v => v.Date).ToHashSet();
|
||||||
|
|||||||
@@ -495,8 +495,12 @@ else
|
|||||||
if (claim == null) return;
|
if (claim == null) return;
|
||||||
_userId = int.Parse(claim.Value);
|
_userId = int.Parse(claim.Value);
|
||||||
_settings = await TrackerService.GetSettingsAsync(_userId);
|
_settings = await TrackerService.GetSettingsAsync(_userId);
|
||||||
await LoadVacations();
|
|
||||||
_holHolidays = await HolidayService.GetHolidaysAsync(_holYear, _settings.GermanState);
|
var loadVacationsTask = LoadVacations();
|
||||||
|
var loadHolidaysTask = HolidayService.GetHolidaysAsync(_holYear, _settings.GermanState);
|
||||||
|
|
||||||
|
await Task.WhenAll(loadVacationsTask, loadHolidaysTask);
|
||||||
|
_holHolidays = await loadHolidaysTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task LoadVacations()
|
private async Task LoadVacations()
|
||||||
|
|||||||
@@ -249,8 +249,13 @@ else
|
|||||||
|
|
||||||
private async Task LoadYear()
|
private async Task LoadYear()
|
||||||
{
|
{
|
||||||
var holidays = await HolidayService.GetHolidaysAsync(_year, _settings.GermanState);
|
var holidaysTask = HolidayService.GetHolidaysAsync(_year, _settings.GermanState);
|
||||||
var vacations = await TrackerService.GetVacationDaysAsync(_userId, _year);
|
var vacationsTask = TrackerService.GetVacationDaysAsync(_userId, _year);
|
||||||
|
|
||||||
|
await Task.WhenAll(holidaysTask, vacationsTask);
|
||||||
|
|
||||||
|
var holidays = await holidaysTask;
|
||||||
|
var vacations = await vacationsTask;
|
||||||
_holidays = holidays.ToDictionary(h => h.Date, h => h.Name);
|
_holidays = holidays.ToDictionary(h => h.Date, h => h.Name);
|
||||||
_vacationSet = vacations.Select(v => v.Date).ToHashSet();
|
_vacationSet = vacations.Select(v => v.Date).ToHashSet();
|
||||||
_remainingDays = Math.Max(0, _settings.VacationDaysPerYear - vacations.Count);
|
_remainingDays = Math.Max(0, _settings.VacationDaysPerYear - vacations.Count);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ namespace timetracker.Client.Services;
|
|||||||
public class ClientHolidayService : IHolidayService
|
public class ClientHolidayService : IHolidayService
|
||||||
{
|
{
|
||||||
private readonly HttpClient _http;
|
private readonly HttpClient _http;
|
||||||
|
private readonly Dictionary<(int Year, string StateCode), List<PublicHoliday>> _holidayCache = new();
|
||||||
|
|
||||||
public ClientHolidayService(HttpClient http)
|
public ClientHolidayService(HttpClient http)
|
||||||
{
|
{
|
||||||
@@ -14,12 +15,21 @@ public class ClientHolidayService : IHolidayService
|
|||||||
|
|
||||||
public async Task<List<PublicHoliday>> GetHolidaysAsync(int year, string? stateCode = null)
|
public async Task<List<PublicHoliday>> GetHolidaysAsync(int year, string? stateCode = null)
|
||||||
{
|
{
|
||||||
|
var sc = stateCode ?? "";
|
||||||
|
var key = (year, sc);
|
||||||
|
if (_holidayCache.TryGetValue(key, out var cached))
|
||||||
|
{
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
var url = $"api/holidays?year={year}";
|
var url = $"api/holidays?year={year}";
|
||||||
if (!string.IsNullOrEmpty(stateCode))
|
if (!string.IsNullOrEmpty(stateCode))
|
||||||
{
|
{
|
||||||
url += $"&stateCode={stateCode}";
|
url += $"&stateCode={stateCode}";
|
||||||
}
|
}
|
||||||
return await _http.GetFromJsonAsync<List<PublicHoliday>>(url) ?? [];
|
var result = await _http.GetFromJsonAsync<List<PublicHoliday>>(url) ?? [];
|
||||||
|
_holidayCache[key] = result;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<(bool Success, string Message)> FetchAndStoreAsync(int year)
|
public async Task<(bool Success, string Message)> FetchAndStoreAsync(int year)
|
||||||
@@ -30,6 +40,12 @@ public class ClientHolidayService : IHolidayService
|
|||||||
var result = await response.Content.ReadFromJsonAsync<FetchResponse>();
|
var result = await response.Content.ReadFromJsonAsync<FetchResponse>();
|
||||||
if (result != null)
|
if (result != null)
|
||||||
{
|
{
|
||||||
|
// Invalidate the cache for this year
|
||||||
|
var keysToRemove = _holidayCache.Keys.Where(k => k.Year == year).ToList();
|
||||||
|
foreach (var k in keysToRemove)
|
||||||
|
{
|
||||||
|
_holidayCache.Remove(k);
|
||||||
|
}
|
||||||
return (result.Success, result.Message);
|
return (result.Success, result.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,6 +55,8 @@ public class ClientHolidayService : IHolidayService
|
|||||||
public async Task DeleteAsync(int id)
|
public async Task DeleteAsync(int id)
|
||||||
{
|
{
|
||||||
await _http.DeleteAsync($"api/holidays/{id}");
|
await _http.DeleteAsync($"api/holidays/{id}");
|
||||||
|
// Invalidate all caches since we deleted a holiday
|
||||||
|
_holidayCache.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class FetchResponse
|
private class FetchResponse
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ namespace timetracker.Client.Services;
|
|||||||
public class ClientTimetrackerService : ITimetrackerService
|
public class ClientTimetrackerService : ITimetrackerService
|
||||||
{
|
{
|
||||||
private readonly HttpClient _http;
|
private readonly HttpClient _http;
|
||||||
|
private AppSettings? _cachedSettings;
|
||||||
|
|
||||||
public ClientTimetrackerService(HttpClient http)
|
public ClientTimetrackerService(HttpClient http)
|
||||||
{
|
{
|
||||||
@@ -24,12 +25,19 @@ public class ClientTimetrackerService : ITimetrackerService
|
|||||||
|
|
||||||
public async Task<AppSettings> GetSettingsAsync(int userId)
|
public async Task<AppSettings> GetSettingsAsync(int userId)
|
||||||
{
|
{
|
||||||
return await _http.GetFromJsonAsync<AppSettings>($"api/tracker/settings/{userId}") ?? new AppSettings { UserId = userId };
|
if (_cachedSettings != null && _cachedSettings.UserId == userId)
|
||||||
|
{
|
||||||
|
return _cachedSettings;
|
||||||
|
}
|
||||||
|
var settings = await _http.GetFromJsonAsync<AppSettings>($"api/tracker/settings/{userId}");
|
||||||
|
_cachedSettings = settings ?? new AppSettings { UserId = userId };
|
||||||
|
return _cachedSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SaveSettingsAsync(AppSettings settings)
|
public async Task SaveSettingsAsync(AppSettings settings)
|
||||||
{
|
{
|
||||||
await _http.PostAsJsonAsync("api/tracker/settings", settings);
|
await _http.PostAsJsonAsync("api/tracker/settings", settings);
|
||||||
|
_cachedSettings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<VacationDay>> GetVacationDaysAsync(int userId, int year)
|
public async Task<List<VacationDay>> GetVacationDaysAsync(int userId, int year)
|
||||||
|
|||||||
Reference in New Issue
Block a user