Skip to content

Commit

Permalink
Change day from timestamp to date
Browse files Browse the repository at this point in the history
  • Loading branch information
cavis committed May 3, 2024
1 parent d0813e3 commit 0067d13
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/bigquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exports.latestRollupDay = async () => {
lower.setDate(lower.getDate() - logExp);

// see if BQ has a day later than that
const sql = `SELECT MAX(day) AS day FROM dt_bytes WHERE day >= @lower`;
const sql = `SELECT MAX(day) AS day FROM dt_bytes WHERE day >= DATE(@lower)`;
const rows = await exports.query(sql, { lower });
if (rows[0]?.day) {
return new Date(rows[0].day.value);
Expand All @@ -46,7 +46,7 @@ exports.latestRollupDay = async () => {
* Get the current total bytes for a day
*/
exports.totalBytes = async (day) => {
const sql = `SELECT SUM(bytes) AS total_bytes FROM dt_bytes WHERE day = @day`;
const sql = `SELECT SUM(bytes) AS total_bytes FROM dt_bytes WHERE day = DATE(@day)`;
const rows = await exports.query(sql, { day });
return rows[0]?.total_bytes || 0;
};
Expand All @@ -55,7 +55,7 @@ exports.totalBytes = async (day) => {
* Delete a day of bytes data
*/
exports.deleteDay = async (day) => {
const sql = `DELETE FROM dt_bytes WHERE day = @day`;
const sql = `DELETE FROM dt_bytes WHERE day = DATE(@day)`;
await exports.query(sql, { day });
return true;
};
Expand Down
3 changes: 2 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ exports.elapsed = async (fn) => {
* <feeder_podcast>/<feeder_episode>
* <feeder_podcast>/<feeder_feed>/<feeder_episode>
*/
exports.parseUsage = (day, rows) =>
exports.parseUsage = (dayDate, rows) =>
rows
.map(([uri, byteStr]) => {
const parts = uri.split("/");
const bytes = parseInt(byteStr, 10);
const day = dayDate.toISOString().split("T").shift();

if (parts[0].match(/^[0-9]+$/)) {
/* eslint-disable camelcase */
Expand Down
6 changes: 3 additions & 3 deletions lib/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ describe("util", () => {
feeder_podcast: 1234,
feeder_episode: "some-guid",
bytes: 99,
day,
day: "2024-03-04",
});
expect(rows[1]).toEqual({
feeder_podcast: 1234,
feeder_episode: "another-guid",
bytes: 88,
day,
day: "2024-03-04",
});
expect(rows[2]).toEqual({
feeder_podcast: 5678,
feeder_feed: "my-feed",
feeder_episode: "the-guid",
bytes: 77,
day,
day: "2024-03-04",
});
});

Expand Down

0 comments on commit 0067d13

Please sign in to comment.