Skip to content

Commit

Permalink
feat(view): 🗃️ invalidate reports caches on command actions
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed May 18, 2023
1 parent 50148e5 commit 2d0a793
Showing 1 changed file with 32 additions and 46 deletions.
78 changes: 32 additions & 46 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal class TogglTrack
private (bool IsValid, string Token) _lastToken = (false, string.Empty);

private MemoryCache _cache = MemoryCache.Default;
private List<string> _summaryTimeEntriesCacheKeys = new List<string>();

private long? _selectedProjectId = -1;
private long? _selectedClientId = -1;
Expand Down Expand Up @@ -125,7 +126,6 @@ internal TogglTrack(PluginInitContext context, Settings settings)
}
}

// TODO: need way to invalidate all caches
private async ValueTask<SummaryTimeEntry?> _GetSummaryTimeEntries(long workspaceId, long userId, Settings.ViewGroupingKeys reportGrouping, DateTimeOffset start, DateTimeOffset? end, bool force = false)
{
string cacheKey = $"SummaryTimeEntries{workspaceId}{userId}{(int)reportGrouping}{start.ToString("yyyy-MM-dd")}{end?.ToString("yyyy-MM-dd")}";
Expand All @@ -145,6 +145,8 @@ internal TogglTrack(PluginInitContext context, Settings settings)
this._cache.Set(cacheKey, summary, DateTimeOffset.Now.AddSeconds(30));
#pragma warning restore CS8604 // Possible null reference argument

this._summaryTimeEntriesCacheKeys.Add(cacheKey);

return summary;
}
catch (Exception exception)
Expand All @@ -154,14 +156,21 @@ internal TogglTrack(PluginInitContext context, Settings settings)
}
}

internal void RefreshCache()
private void _ClearSummaryTimeEntriesCache()
{
this._summaryTimeEntriesCacheKeys.ForEach(key => this._cache.Remove(key));
this._summaryTimeEntriesCacheKeys.Clear();
}

internal void RefreshCache(bool refreshMe = false)
{
_ = Task.Run(() =>
{
// This is the main one that needs to be run
_ = this._GetMe(true);
_ = this._GetMe(refreshMe);
_ = this._GetRunningTimeEntry(true);
_ = this._GetTimeEntries(true);
this._ClearSummaryTimeEntriesCache();
});
}

Expand All @@ -172,6 +181,8 @@ internal async ValueTask<bool> VerifyApiToken()
return false;
}

// TODO: this equal does not work

if (this._settings.ApiToken.Equals(this._lastToken.Token))
{
return this._lastToken.IsValid;
Expand All @@ -185,9 +196,14 @@ internal async ValueTask<bool> VerifyApiToken()
}

this._client.UpdateToken(this._settings.ApiToken);
// TODO: refresh all caches

return this._lastToken.IsValid = (await this._GetMe(true))?.api_token?.Equals(this._settings.ApiToken) ?? false;
this._lastToken.IsValid = (await this._GetMe(true))?.api_token?.Equals(this._settings.ApiToken) ?? false;
if (this._lastToken.IsValid)
{
this.RefreshCache(true);
}

return this._lastToken.IsValid;
}

internal List<Result> NotifyMissingToken()
Expand Down Expand Up @@ -329,7 +345,7 @@ internal async ValueTask<List<Result>> GetDefaultHotKeys()
Score = -100,
Action = c =>
{
this.RefreshCache();
this.RefreshCache(true);
return true;
},
},
Expand Down Expand Up @@ -511,11 +527,7 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
this._context.API.ShowMsg($"Started {createdTimeEntry.description}", projectName, "start.png");
// Update cached running time entry state
_ = Task.Run(() =>
{
_ = this._GetRunningTimeEntry(true);
_ = this._GetTimeEntries(true);
});
this.RefreshCache();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -595,11 +607,7 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
this._context.API.ShowMsg($"Started {createdTimeEntry.description}{((string.IsNullOrEmpty(sanitisedDescription) ? string.Empty : " "))}{startTime.Humanize()}", projectName, "start.png");
// Update cached running time entry state
_ = Task.Run(() =>
{
_ = this._GetRunningTimeEntry(true);
_ = this._GetTimeEntries(true);
});
this.RefreshCache();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -682,11 +690,7 @@ internal async ValueTask<List<Result>> RequestStartEntry(CancellationToken token
this._context.API.ShowMsg($"Started {createdTimeEntry.description}{((string.IsNullOrEmpty(description) ? string.Empty : " "))}at previous stop time", projectName, "start.png");
// Update cached running time entry state
_ = Task.Run(() =>
{
_ = this._GetRunningTimeEntry(true);
_ = this._GetTimeEntries(true);
});
this.RefreshCache();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -873,11 +877,7 @@ internal async ValueTask<List<Result>> RequestEditEntry(CancellationToken token,
this._context.API.ShowMsg($"Edited {editedTimeEntry.description}", $"{projectName} | {(int)elapsed.TotalHours}:{elapsed.ToString(@"mm\:ss")}", "edit.png");
// Update cached running time entry state
_ = Task.Run(() =>
{
_ = this._GetRunningTimeEntry(true);
_ = this._GetTimeEntries(true);
});
this.RefreshCache();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -967,11 +967,7 @@ internal async ValueTask<List<Result>> RequestEditEntry(CancellationToken token,
this._context.API.ShowMsg($"Edited {editedTimeEntry.description}", $"{projectName} | {(int)newElapsed.TotalHours}:{newElapsed.ToString(@"mm\:ss")}", "edit.png");
// Update cached running time entry state
_ = Task.Run(() =>
{
_ = this._GetRunningTimeEntry(true);
_ = this._GetTimeEntries(true);
});
this.RefreshCache();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -1103,11 +1099,7 @@ internal async ValueTask<List<Result>> RequestStopEntry(CancellationToken token,
this._context.API.ShowMsg($"Stopped {stoppedTimeEntry.description}", $"{(int)elapsed.TotalHours}:{elapsed.ToString(@"mm\:ss")} elapsed", "stop.png");
// Update cached running time entry state
_ = Task.Run(() =>
{
_ = this._GetRunningTimeEntry(true);
_ = this._GetTimeEntries(true);
});
this.RefreshCache();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -1187,11 +1179,7 @@ internal async ValueTask<List<Result>> RequestStopEntry(CancellationToken token,
this._context.API.ShowMsg($"Stopped {stoppedTimeEntry.description}", $"{(int)newElapsed.TotalHours}:{newElapsed.ToString(@"mm\:ss")} elapsed", "stop.png");
// Update cached running time entry state
_ = Task.Run(() =>
{
_ = this._GetRunningTimeEntry(true);
_ = this._GetTimeEntries(true);
});
this.RefreshCache();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -1295,11 +1283,7 @@ internal async ValueTask<List<Result>> RequestDeleteEntry(CancellationToken toke
this._context.API.ShowMsg($"Deleted {runningTimeEntry.description}", $"{(int)elapsed.TotalHours}:{elapsed.ToString(@"mm\:ss")} elapsed", "delete.png");
// Update cached running time entry state
_ = Task.Run(() =>
{
_ = this._GetRunningTimeEntry(true);
_ = this._GetTimeEntries(true);
});
this.RefreshCache();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -1653,6 +1637,7 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
IcoPath = (project?.color is not null)
? new ColourIcon(this._context, project.color, "view.png").GetColourIcon()
: "view.png",
// TODO: project name
AutoCompleteText = $"{query.ActionKeyword} {Settings.ViewCommand} {spanConfiguration.Argument} {groupingConfiguration.Argument} {((string.IsNullOrEmpty(subGroup.title)) ? "(no description)" : subGroup.title)}",
Score = (int)elapsed.TotalSeconds,
Action = c =>
Expand Down Expand Up @@ -1760,6 +1745,7 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
IcoPath = (highestProject?.color is not null)
? new ColourIcon(this._context, highestProject.color, "view.png").GetColourIcon()
: "view.png",
// TODO: client name
AutoCompleteText = $"{query.ActionKeyword} {Settings.ViewCommand} {spanConfiguration.Argument} {groupingConfiguration.Argument} ",
Score = (int)group.seconds,
Action = c =>
Expand Down

0 comments on commit 2d0a793

Please sign in to comment.