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

feat(hogql): Format actors modal date labels #21069

Merged
merged 12 commits into from
Mar 24, 2024
10 changes: 10 additions & 0 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,10 @@
},
"type": "object"
},
"DatetimeDay": {
"format": "date-time",
"type": "string"
},
"Day": {
"type": "integer"
},
Expand Down Expand Up @@ -2788,6 +2792,9 @@
{
"type": "string"
},
{
"$ref": "#/definitions/DatetimeDay"
},
{
"$ref": "#/definitions/Day"
}
Expand Down Expand Up @@ -3839,6 +3846,9 @@
{
"type": "string"
},
{
"$ref": "#/definitions/DatetimeDay"
},
{
"$ref": "#/definitions/Day"
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,9 @@ export interface FunnelCorrelationQuery {
response?: FunnelCorrelationResponse
}

/** @format date-time */
export type DatetimeDay = string

export type BreakdownValueInt = integer
export interface InsightActorsQueryOptionsResponse {
day?: { label: string; value: string | Day }[]
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql_queries/insights/lifecycle_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def to_actors_query(

def to_actors_query_options(self) -> InsightActorsQueryOptionsResponse:
return InsightActorsQueryOptionsResponse(
day=[{"label": day, "value": day} for day in self.query_date_range.all_values()],
day=[{"label": format_label_date(value), "value": value} for value in self.query_date_range.all_values()],
status=[
{
"label": "Dormant",
Expand Down
8 changes: 7 additions & 1 deletion posthog/hogql_queries/insights/trends/trends_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ def to_actors_query_options(self) -> InsightActorsQueryOptionsResponse:
res_compare: List[CompareItem] | None = None

# Days
res_days: List[DayItem] = [DayItem(label=day, value=day) for day in self.query_date_range.all_values()]
res_days: list[DayItem] = [
DayItem(
label=format_label_date(value, self.query_date_range.interval_name),
value=value,
)
for value in self.query_date_range.all_values()
]

# Series
for index, series in enumerate(self.query.series):
Expand Down
15 changes: 7 additions & 8 deletions posthog/hogql_queries/utils/query_date_range.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from datetime import datetime, timedelta
from functools import cached_property
from typing import Literal, Optional, Dict, List
from typing import Literal, Optional, Dict
from zoneinfo import ZoneInfo

from dateutil.parser import parse
Expand Down Expand Up @@ -140,16 +140,15 @@ def interval_relativedelta(self) -> relativedelta:
hours=1 if self.interval_name == "hour" else 0,
)

def all_values(self) -> List[str]:
def all_values(self) -> list[datetime]:
start = self.align_with_interval(self.date_from())
end: datetime = self.date_to()
values: List[str] = []
delta = self.interval_relativedelta()

values: list[datetime] = []
while start <= end:
if self.interval_name == "hour":
values.append(start.strftime("%Y-%m-%d %H:%M:%S"))
else:
values.append(start.strftime("%Y-%m-%d"))
start += self.interval_relativedelta()
values.append(start)
start += delta
return values

def date_to_as_hogql(self) -> ast.Expr:
Expand Down
25 changes: 20 additions & 5 deletions posthog/hogql_queries/utils/test/test_query_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,47 @@ def test_all_values(self):
QueryDateRange(
team=self.team, date_range=DateRange(date_from="-20h"), interval=IntervalType.day, now=now
).all_values(),
["2021-08-24", "2021-08-25"],
[parser.isoparse("2021-08-24T00:00:00Z"), parser.isoparse("2021-08-25T00:00:00Z")],
)
self.assertEqual(
QueryDateRange(
team=self.team, date_range=DateRange(date_from="-20d"), interval=IntervalType.week, now=now
).all_values(),
["2021-08-01", "2021-08-08", "2021-08-15", "2021-08-22"],
[
parser.isoparse("2021-08-01T00:00:00Z"),
parser.isoparse("2021-08-08T00:00:00Z"),
parser.isoparse("2021-08-15T00:00:00Z"),
parser.isoparse("2021-08-22T00:00:00Z"),
],
)
self.team.week_start_day = WeekStartDay.MONDAY
self.assertEqual(
QueryDateRange(
team=self.team, date_range=DateRange(date_from="-20d"), interval=IntervalType.week, now=now
).all_values(),
["2021-08-02", "2021-08-09", "2021-08-16", "2021-08-23"],
[
parser.isoparse("2021-08-02T00:00:00Z"),
parser.isoparse("2021-08-09T00:00:00Z"),
parser.isoparse("2021-08-16T00:00:00Z"),
parser.isoparse("2021-08-23T00:00:00Z"),
],
)
self.assertEqual(
QueryDateRange(
team=self.team, date_range=DateRange(date_from="-50d"), interval=IntervalType.month, now=now
).all_values(),
["2021-07-01", "2021-08-01"],
[parser.isoparse("2021-07-01T00:00:00Z"), parser.isoparse("2021-08-01T00:00:00Z")],
)
self.assertEqual(
QueryDateRange(
team=self.team, date_range=DateRange(date_from="-3h"), interval=IntervalType.hour, now=now
).all_values(),
["2021-08-24 21:00:00", "2021-08-24 22:00:00", "2021-08-24 23:00:00", "2021-08-25 00:00:00"],
[
parser.isoparse("2021-08-24T21:00:00Z"),
parser.isoparse("2021-08-24T22:00:00Z"),
parser.isoparse("2021-08-24T23:00:00Z"),
parser.isoparse("2021-08-25T00:00:00Z"),
],
)


Expand Down
6 changes: 5 additions & 1 deletion posthog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ class DateRange(BaseModel):
date_to: Optional[str] = None


class DatetimeDay(RootModel[AwareDatetime]):
root: AwareDatetime


class Day(RootModel[int]):
root: int

Expand Down Expand Up @@ -457,7 +461,7 @@ class DayItem(BaseModel):
extra="forbid",
)
label: str
value: Union[str, int]
value: Union[str, AwareDatetime, int]
webjunkie marked this conversation as resolved.
Show resolved Hide resolved


class IntervalItem(BaseModel):
Expand Down
Loading