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

Cohorts: Group by quarter #462

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ You can also use an array or hash for static data and enums.

```yml
smart_variables:
period: ["day", "week", "month"]
period: ["day", "week", "month", "quarter"]
status: {0: "Active", 1: "Archived"}
```

Expand Down
6 changes: 4 additions & 2 deletions app/controllers/blazer/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def refresh_query(query)

def add_cohort_analysis_vars
@bind_vars << "cohort_period" unless @bind_vars.include?("cohort_period")
@smart_vars["cohort_period"] = ["day", "week", "month"] if @smart_vars
@smart_vars["cohort_period"] = ["day", "week", "month", "quarter"] if @smart_vars
# TODO create var_params method
request.query_parameters["cohort_period"] ||= "week"
end
Expand Down Expand Up @@ -87,7 +87,7 @@ def parse_smart_variables(var, data_source)

def cohort_analysis_statement(statement)
@cohort_period = params["cohort_period"] || "week"
@cohort_period = "week" unless ["day", "week", "month"].include?(@cohort_period)
@cohort_period = "week" unless ["day", "week", "month", "quarter"].include?(@cohort_period)

# for now
@conversion_period = @cohort_period
Expand All @@ -99,6 +99,8 @@ def cohort_analysis_statement(statement)
7
when "month"
30
when "quarter"
90
end

statement.apply_cohort_analysis(period: @cohort_period, days: @cohort_days)
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/blazer/queries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ def render_cohort_analysis
current_date - 1
when "week"
current_date - 7
when "quarter"
current_date.prev_month(3)
else
current_date.prev_month
end
Expand Down
3 changes: 3 additions & 0 deletions lib/blazer/adapters/sql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ def cohort_analysis_statement(statement, period:, days:)
when "week"
date_sql = "CAST(DATE_FORMAT(#{time_sql} - INTERVAL ((5 + DAYOFWEEK(#{time_sql})) % 7) DAY, '%Y-%m-%d') AS DATE)"
date_params = [tzname, tzname]
when "quarter"
date_sql = "CAST(DATE_FORMAT(CONCAT(YEAR(#{time_sql}), '-', QUARTER(#{time_sql}) * 3 - 2, '-01'), '%Y-%m-%d') AS DATE)"
date_params = [tzname]
else
date_sql = "CAST(DATE_FORMAT(#{time_sql}, '%Y-%m-01') AS DATE)"
date_params = [tzname]
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/blazer/templates/config.yml.tt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data_sources:

smart_variables:
# zone_id: "SELECT id, name FROM zones ORDER BY name ASC"
# period: ["day", "week", "month"]
# period: ["day", "week", "month", "quarter"]
# status: {0: "Active", 1: "Archived"}

linked_columns:
Expand Down
7 changes: 7 additions & 0 deletions test/cohort_analysis_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ def test_cohort_period_default
assert_response :success
assert_match %{selected="selected" value="week"}, response.body
end

def test_quarter
query = create_query(statement: "SELECT 1 AS user_id, NOW() AS conversion_time /* cohort analysis */")
get blazer.query_path(query)
assert_response :success
assert_match %{value="quarter"}, response.body
end
end
2 changes: 1 addition & 1 deletion test/internal/config/blazer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data_sources:

smart_variables:
# zone_id: "SELECT id, name FROM zones ORDER BY name ASC"
period: ["day", "week", "month"]
period: ["day", "week", "month", "quarter"]
# status: {0: "Active", 1: "Archived"}

linked_columns:
Expand Down