Skip to content

Commit

Permalink
fix: Fix calendar range end (#20154)
Browse files Browse the repository at this point in the history
* Fix calendar range end

Use the end of the day (= 23:59:59) as the range end for the calendar
range picker.

The reasoning is that we probably don't want to adjust all queries
to treat just a day that is given as then end as the full day.
Rather this change of the date picker seems pretty straightforward.

* Update mypy baseline
  • Loading branch information
webjunkie authored Feb 7, 2024
1 parent cbf09b5 commit cf31c27
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 35 deletions.
2 changes: 1 addition & 1 deletion frontend/src/lib/components/DateFilter/DateFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function DateFilter({
const popoverOverlay =
view === DateFilterView.FixedRange ? (
<LemonCalendarRange
value={[rangeDateTo ?? dayjs(), rangeDateTo ?? dayjs()]}
value={[rangeDateFrom ?? dayjs(), rangeDateTo ?? dayjs()]}
onChange={([from, to]) => {
setRangeDateFrom(from)
setRangeDateTo(to)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/components/DateFilter/dateFilterLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const dateFilterLogic = kea<dateFilterLogicType>([
if (values.rangeDateFrom) {
actions.setDate(
dayjs(values.rangeDateFrom).format('YYYY-MM-DD'),
values.rangeDateTo ? dayjs(values.rangeDateTo).format('YYYY-MM-DD') : null
values.rangeDateTo ? dayjs(values.rangeDateTo).format() : null
)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,31 @@ describe('LemonCalendarRange', () => {

// click on 15
await clickOn('15')
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-15'), dayjs('2022-02-28')])
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-15'), dayjs('2022-02-28T23:59:59.999Z')])

// click on 27
await clickOn('27')
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-15'), dayjs('2022-02-27')])
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-15'), dayjs('2022-02-27T23:59:59.999Z')])

// click on 16
await clickOn('16')
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-16'), dayjs('2022-02-27')])
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-16'), dayjs('2022-02-27T23:59:59.999Z')])

// click on 26
await clickOn('26')
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-16'), dayjs('2022-02-26')])
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-16'), dayjs('2022-02-26T23:59:59.999Z')])

// click on 10
await clickOn('10')
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-10'), dayjs('2022-02-26')])
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-10'), dayjs('2022-02-26T23:59:59.999Z')])

// click on 28
await clickOn('28')
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-10'), dayjs('2022-02-28')])
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-10'), dayjs('2022-02-28T23:59:59.999Z')])

// click on 20
await clickOn('20')
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-20'), dayjs('2022-02-28')])
expect(onChange).toHaveBeenCalledWith([dayjs('2022-02-20'), dayjs('2022-02-28T23:59:59.999Z')])

userEvent.click(getByDataAttr(container, 'lemon-calendar-range-cancel'))
expect(onClose).toHaveBeenCalled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function LemonCalendarRange({ value, onChange, onClose, months }: LemonCa
// Keep a sanitised and cached copy of the selected range
const [[rangeStart, rangeEnd], setRange] = useState([
value?.[0] ? value[0].startOf('day') : null,
value?.[1] ? value[1].startOf('day') : null,
value?.[1] ? value[1].endOf('day') : null,
])

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export function LemonCalendarRangeInline({
])

function setRange([rangeStart, rangeEnd, lastChanged]: RangeState): void {
_setRange([rangeStart, rangeEnd, lastChanged])
_setRange([rangeStart, rangeEnd ? rangeEnd.endOf('day') : null, lastChanged])
if (rangeStart && rangeEnd) {
onChange([rangeStart, rangeEnd])
onChange([rangeStart, rangeEnd.endOf('day')])
}
}

Expand Down
6 changes: 0 additions & 6 deletions mypy-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ posthog/models/feature_flag/flag_matching.py:0: error: Statement is unreachable
posthog/hogql_queries/utils/query_date_range.py:0: error: Incompatible return value type (got "str", expected "Literal['hour', 'day', 'week', 'month']") [return-value]
posthog/hogql_queries/utils/query_date_range.py:0: error: Item "None" of "dict[str, int] | None" has no attribute "get" [union-attr]
posthog/hogql_queries/utils/query_date_range.py:0: error: Statement is unreachable [unreachable]
posthog/hogql_queries/utils/query_date_range.py:0: error: Argument "chain" to "Field" has incompatible type "list[str]"; expected "list[str | int]" [arg-type]
posthog/hogql_queries/utils/query_date_range.py:0: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
posthog/hogql_queries/utils/query_date_range.py:0: note: Consider using "Sequence" instead, which is covariant
posthog/hogql_queries/utils/query_date_range.py:0: error: Argument "chain" to "Field" has incompatible type "list[str]"; expected "list[str | int]" [arg-type]
posthog/hogql_queries/utils/query_date_range.py:0: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
posthog/hogql_queries/utils/query_date_range.py:0: note: Consider using "Sequence" instead, which is covariant
posthog/hogql_queries/utils/query_date_range.py:0: error: Unsupported operand types for * ("object" and "int") [operator]
posthog/hogql_queries/utils/query_date_range.py:0: error: Incompatible return value type (got "int", expected "timedelta") [return-value]
posthog/hogql_queries/utils/query_date_range.py:0: error: Item "None" of "IntervalType | None" has no attribute "name" [union-attr]
Expand Down
17 changes: 0 additions & 17 deletions posthog/hogql_queries/utils/query_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from dateutil.relativedelta import relativedelta

from posthog.hogql.ast import CompareOperationOp
from posthog.hogql.errors import HogQLException
from posthog.hogql.parser import ast
from posthog.models.team import Team, WeekStartDay
Expand Down Expand Up @@ -250,22 +249,6 @@ def to_placeholders(self) -> Dict[str, ast.Expr]:
else self.date_from_as_hogql(),
}

def to_properties(self, field: Optional[List[str]] = None) -> List[ast.Expr]:
if not field:
field = ["timestamp"]
return [
ast.CompareOperation(
left=ast.Field(chain=field),
op=CompareOperationOp.LtEq,
right=self.date_to_as_hogql(),
),
ast.CompareOperation(
left=ast.Field(chain=field),
op=CompareOperationOp.Gt,
right=self.date_to_as_hogql(),
),
]


class QueryDateRangeWithIntervals(QueryDateRange):
def __init__(
Expand Down

0 comments on commit cf31c27

Please sign in to comment.