Skip to content

Commit

Permalink
Revert "feat: remove mongo analytics (too slow), make CF KV not expire"
Browse files Browse the repository at this point in the history
This reverts commit 8a7d457.
  • Loading branch information
sznowicki committed Dec 26, 2023
1 parent 9227a2f commit f49e8ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ export const onRequestGet = async (context) => {
q,
JSON.stringify({
hasResults,
})
}),
{
expirationTtl: 86400, // 24h
}
);
await trackQuery(context.env, { q, hasResults });
}
const view = {
...viewDefaults,
Expand Down
24 changes: 24 additions & 0 deletions lib/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,27 @@ export const getUnchecked = async (envs) => {

return response?.documents?.[0]?.count ?? null;
};

export const trackQuery = async (envs, {q, hasResults}) => {
const d = Date.now();
await callMongo(envs, 'updateOne', {
collection: 'queries',
database: envs.ATLAS_DB,
dataSource: envs.ATLAS_SOURCE,
filter: {
q,
},
update: {
$inc: {
used: 1,
},
$set: {
q,
hasResults,
lastUse: Date.now()
}
},
upsert: true,
});
console.log(`Analytics via mongo took ${Date.now() - d}ms`);
};

0 comments on commit f49e8ce

Please sign in to comment.