Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usage: restore cron usage reporting #1841

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion packages/api/src/controllers/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,53 @@ app.post(
async (req, res) => {
let { fromTime, toTime } = req.query;

/* New usage report for billing
let token = req.token;

// New automated billing usage report
let result = await reportUsage(req, token);

res.status(200);
res.json(result);
let { fromTime, toTime } = req.query;
*/

// if time range isn't specified return all usage
if (!fromTime) {
let rows = (
await db.usage.find(
{},
{ limit: 1, order: "data->>'date' DESC", useReplica: true }
)
)[0];

if (rows.length) {
fromTime = rows[0].date; // get last updated date from cache
} else {
fromTime = +new Date(2020, 0); // start at beginning
}
}

if (!toTime) {
toTime = +new Date();
}

let usageHistory = await db.stream.usageHistory(fromTime, toTime, {
useReplica: true,
});

// store each day of usage
for (const row of usageHistory) {
const dbRow = await req.store.get(`usage/${row.id}`);
// if row already exists in cache, update it, otherwise create it
if (dbRow) {
await req.store.replace({ kind: "usage", ...row });
} else {
await req.store.create({ kind: "usage", ...row });
}
}

res.status(200);
res.json(usageHistory);
}
);

Expand Down
Loading