Skip to content

Commit

Permalink
feat: support 'quarter' in get_retention_data
Browse files Browse the repository at this point in the history
  • Loading branch information
nextchamp-saqib committed Jan 13, 2025
1 parent 9cae946 commit 368366c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,23 @@ def week_start(column)
return week_start


def quarter_start(column: ir.DateValue):
"""
def quarter_start(column)
Get the start date of the quarter for a given date.
Examples:
- quarter_start(order_date)
"""

year = column.year()
quarter = column.quarter()
month = (quarter * 3) - 2
quarter_start = ibis.date(year, month, 1)
return quarter_start


def get_retention_data(date_column: ir.DateValue, id_column: ir.Column, unit: str):
"""
def get_retention_data(date_column, id_column, unit)
Expand All @@ -983,13 +1000,17 @@ def get_retention_data(date_column, id_column, unit)
if isinstance(id_column, str):
id_column = getattr(query, id_column)

if date_column.type().is_timestamp():
date_column = date_column.cast("date")

if not date_column.type().is_date():
frappe.throw(f"Invalid date column. Expected date, got {date_column.type()}")

unit_start = {
"day": lambda column: column.strftime("%Y-%m-%d").cast("date"),
"week": week_start,
"month": lambda column: column.strftime("%Y-%m-01").cast("date"),
"quarter": quarter_start,
"year": lambda column: column.strftime("%Y-01-01").cast("date"),
}[unit]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from insights.utils import create_execution_log
from insights.utils import deep_convert_dict_to_dict as _dict

from .ibis.functions import week_start
from .ibis.functions import quarter_start, week_start
from .ibis.utils import get_functions


Expand Down Expand Up @@ -494,11 +494,7 @@ def apply_granularity(self, column, granularity):
if granularity == "week":
return week_start(column).strftime("%Y-%m-%d").name(column.get_name())
if granularity == "quarter":
year = column.year()
quarter = column.quarter()
month = (quarter * 3) - 2
quarter_start = ibis.date(year, month, 1)
return quarter_start.strftime("%Y-%m-%d").name(column.get_name())
return quarter_start(column).strftime("%Y-%m-01").name(column.get_name())

format_str = {
"day": "%Y-%m-%d",
Expand Down

0 comments on commit 368366c

Please sign in to comment.