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

Enhance Date Interval Handling and Add "Yesterday" Interval Support #98

Merged
merged 7 commits into from
Oct 26, 2024

Conversation

DylanPetrey
Copy link
Contributor

@DylanPetrey DylanPetrey commented Oct 7, 2024

Refs #3

Description:

This pull request introduces several improvements and new features:

1. Enhanced Date Interval Handling

  • Refactor intervalToSql function to return both start and end interval SQL for better accuracy.
  • Update test cases to reflect changes in interval handling.
  • Adjust various API calls to utilize both start and end interval SQL.

2. "Yesterday" Interval Support

  • Add support for "yesterday" interval in analytics queries.
  • Update relevant test cases to cover the new "yesterday" interval.
  • Extend generateEmptyRowsOverInterval function to handle "yesterday" interval correctly.
  • Modify the dashboard loader and UI to support the "yesterday" interval option.

Changes:

  • Files Modified:
    • app/analytics/__tests__/query.test.ts
    • app/analytics/query.ts
    • app/routes/dashboard.tsx
    • package-lock.json

@DylanPetrey
Copy link
Contributor Author

image

@DylanPetrey
Copy link
Contributor Author

Should I have made this on the v2 route?

@benvinegar
Copy link
Owner

Thanks for the PR. Sorry, I've been travelling for the past few weeks and haven't had an opportunity to review it.

Should I have made this on the v2 route?

Nope – you're good. That branch became main. I should delete it.

while (startDateTime.getTime() < Date.now()) {
let endIntervalDate = Date.now();
if (interval === "yesterday") {
endIntervalDate = startDateTime.getTime() + 25 * 60 * 60 * 1000;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's potentially a bug here: you can't rely on +24 hours math to get endIntervalDate, because in timezones where daylight savings exists, it might actually be +23 hours or +25 hours on days when it changes.

But turns out, I thought dayjs handled this but apparently it does not: iamkun/dayjs#1271. So this is likely an issue everywhere.

Still, it might be a good idea to just use dayjs to give you the explicit "end of day" time, in case this is ever patched, so that it matches the calculations elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding test cases for daylight savings. I'll look into the workaround mentioned in that PR and see where I can apply it

Copy link
Contributor Author

@DylanPetrey DylanPetrey Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to use dayjs

localDateTime = localDateTime.tz(tz).startOf("day").subtract(1, "day");
localEndDateTime = localDateTime.endOf("day").add(2, "ms");

@@ -188,6 +203,7 @@ export class AnalyticsEngineAPI {

// note interval count hard-coded to hours at the moment
const initialRows = generateEmptyRowsOverInterval(
interval,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It strikes me that you're passing in interval into a few functions to strictly do if (interval) === "yesterday".

I think it would be better to just pass an optional endDateTime, just as startDateTime is being passed. Basically, determine the value in dashboard.tsx and pass it in. That way any arbitrary endDateTime could be used.

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I'll refactor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much cleaner

Copy link
Owner

@benvinegar benvinegar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much cleaner! I have one comment that would be nice if you can update, but I intend to merge.


const filterStr = filtersToSql(filters);

const _column = ColumnMappings[column];
const query = `
SELECT ${_column}, SUM(_sample_interval) as count
FROM metricsDataset
WHERE timestamp > ${intervalSql}
WHERE timestamp < ${endIntervalSql} AND timestamp >= ${startIntervalSql}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I notice in some inconsistency in these WHERE clauses where sometimes you start the statement off with timestamp >= ${startIntervalSql} and sometimes you start them off with timestamp < ${endIntervalSql} (for example L231).

I'd prefer if it was consistent all the way through, starting with the startInterval boundary, and ending with the endInterval boundary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I have switched them around

@benvinegar
Copy link
Owner

Going to merge this. As a follow on, IMO, the query should be both >= and <= on both start/end boundaries, but doubt second-level precision is going to matter to anyone.

@benvinegar benvinegar merged commit 56518f8 into benvinegar:main Oct 26, 2024
1 check passed
@benvinegar
Copy link
Owner

Thank you @DylanPetrey!

benvinegar pushed a commit that referenced this pull request Dec 20, 2024
)

* Add yesterday functionality to the dashboard

* Remove added utc

* Add removed utc

* endDateTime optional parameter

* Move end date calc up one level

* Clean up dayjs use

* Clean up sql query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants