-
Notifications
You must be signed in to change notification settings - Fork 29
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
lib-user: Use a getDefaultDateRange() utility #6420
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,29 @@ | ||
import { getStatsDateString } from '@utils' | ||
|
||
export function getDateInterval({ endDate, startDate }) { | ||
const today = new Date() | ||
const todayUTC = getStatsDateString(today) | ||
const end_date = endDate ? endDate : todayUTC | ||
const defaultStartDate = new Date() | ||
defaultStartDate.setUTCDate(today.getUTCDate() - 6) | ||
const start_date = startDate ? startDate : getStatsDateString(defaultStartDate) | ||
|
||
const differenceInDays = (new Date(end_date) - new Date(start_date)) / (1000 * 60 * 60 * 24) | ||
|
||
/* | ||
ERAS accepts queries in time interval "buckets" of day, week, month, or year. | ||
*/ | ||
function getInterval(differenceInDays) { | ||
if (differenceInDays <= 31) { | ||
return { | ||
end_date, | ||
period: 'day', | ||
start_date | ||
} | ||
return 'day' | ||
} | ||
|
||
if (differenceInDays <= 183) { | ||
return { | ||
end_date, | ||
period: 'week', | ||
start_date | ||
} | ||
return 'week' | ||
} | ||
|
||
if (differenceInDays <= 1460) { | ||
return { | ||
end_date, | ||
period: 'month', | ||
start_date | ||
} | ||
return 'month' | ||
} | ||
|
||
return 'year' | ||
} | ||
|
||
export function getDateInterval({ endDate, startDate }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we need to 1) check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Does that sound like a good plan to you? I'm definitely willing to disregard this PR in favor of keeping the old There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds good to me! I think the separate functions are preferable. |
||
// Get new Date timestamps based on UTC strings, and then convert from ms to days. | ||
const differenceInDays = (new Date(endDate) - new Date(startDate)) / (1000 * 60 * 60 * 24) | ||
|
||
return { | ||
end_date, | ||
period: 'year', | ||
start_date | ||
} | ||
end_date: endDate, | ||
period: getInterval(differenceInDays), | ||
start_date: startDate | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { getStatsDateString } from '@utils' | ||
|
||
/* | ||
Get the default end date (UTC) for stats query | ||
*/ | ||
function getDefaultEndDate() { | ||
// Construct a new Javascript Date object | ||
const today = new Date() | ||
|
||
// .toISOString() in this function returns a UTC string like 2024-05-05 | ||
return getStatsDateString(today) | ||
} | ||
|
||
/* | ||
Get the default start date (UTC) for stats query | ||
*/ | ||
function getDefaultStartDate() { | ||
// Construct a new Javascript Date object | ||
const defaultStartDate = new Date() | ||
|
||
// .getUTCDate() returns numeric day of the month (UTC). | ||
// If a negative number is provided to .setUTCDate(), the date will be | ||
// set counting backwards from the last day of the previous month. | ||
const sevenDaysAgo = defaultStartDate.getUTCDate() - 6 | ||
defaultStartDate.setUTCDate(sevenDaysAgo) | ||
|
||
// .toISOString() in this function returns a UTC string | ||
return getStatsDateString(defaultStartDate) | ||
} | ||
|
||
export function getDefaultDateRange() { | ||
return { | ||
endDate: getDefaultEndDate(), | ||
startDate: getDefaultStartDate() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,10 @@ | ||||||
/* | ||||||
Stats dates are always handled in UTC strings such as 24-09-23. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to other comment
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shoot I just realized I never included these suggested changes. I'll include it in #6472 |
||||||
*/ | ||||||
|
||||||
export function getStatsDateString(date) { | ||||||
if (date instanceof Date) { | ||||||
return date.toISOString().substring(0, 10) | ||||||
return date.toISOString().substring(0, 10) // .toISOString returns UTC | ||||||
} else { | ||||||
return date?.substring(0, 10) | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pedantic, but not sure if the
20
in2024
should be included