Skip to content

Commit

Permalink
Improve refresher logic
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Jan 4, 2024
1 parent d75ca36 commit e9bfdc9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions datastores/active_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const datastore = DefineDatastore(
view_id: { type: Schema.types.string, required: true },
user_id: { type: Schema.types.string, required: true },
last_updated_at: { type: Schema.types.number, required: true }, // epoch time in seconds
last_accessed_at: { type: Schema.types.number, required: false }, // epoch time in seconds
last_updated_callback_id: { type: Schema.types.string, required: true },
},
} as const,
Expand Down
19 changes: 17 additions & 2 deletions functions/internals/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,17 +509,22 @@ interface saveLastActiveViewArgs {
view_id: string;
user_id: string;
callback_id: string;
is_active_view_refresher?: boolean;
}
export async function saveLastActiveView(
{ av, view_id, user_id, callback_id }: saveLastActiveViewArgs,
{ av, view_id, user_id, callback_id, is_active_view_refresher }:
saveLastActiveViewArgs,
) {
const last_updated_at = Math.floor(new Date().getTime() / 1000);
const attributes: Attributes<AV> = {
let attributes: Attributes<AV> = {
view_id,
user_id,
last_updated_callback_id: callback_id,
last_updated_at,
};
if (!is_active_view_refresher) {
attributes = { ...attributes, last_accessed_at: last_updated_at };
}
await av.save({ attributes });
}

Expand Down Expand Up @@ -550,3 +555,13 @@ export async function cleanUpOldActiveViews(
}
}
}

interface deleteActiveViewArgs {
av: DataMapper<AV>;
view_id: string;
}
export async function deleteActiveView(
{ av, view_id }: deleteActiveViewArgs,
) {
await av.deleteById(view_id);
}
11 changes: 11 additions & 0 deletions functions/refresh_main_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AV,
AVMapper,
cleanUpOldActiveViews,
deleteActiveView,
fetchLifelog,
fetchTimeEntry,
L,
Expand Down Expand Up @@ -86,7 +87,16 @@ export default SlackFunction(def, async ({ token, env, client }) => {
const foundUsers: string[] = [];
const tasks: Promise<void>[] = [];
try {
const now = Math.floor(new Date().getTime() / 1000);
const twoDays = 48 * 60 * 60;
for (const activeView of activeViews) {
if (
activeView.last_accessed_at === undefined ||
activeView.last_accessed_at < now - twoDays
) {
await deleteActiveView({ av, view_id: activeView.view_id });
continue;
}
tasks.push(updateActiveView({
isDebugMode,
activeView,
Expand Down Expand Up @@ -190,6 +200,7 @@ async function updateActiveView({
callback_id: result.view!.callback_id!,
view_id: result.view!.id!,
user_id: user,
is_active_view_refresher: true,
});
} catch (e) {
console.log(`Failed to update an active view: ${e}`);
Expand Down

0 comments on commit e9bfdc9

Please sign in to comment.