Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
e63524f4
Unverified
Commit
e63524f4
authored
2 years ago
by
Matt Jankowski
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Update retention model sql query (#24249)
parent
9bda9337
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/lib/admin/metrics/retention.rb
+33
-23
33 additions, 23 deletions
app/lib/admin/metrics/retention.rb
with
33 additions
and
23 deletions
app/lib/admin/metrics/retention.rb
+
33
−
23
View file @
e63524f4
...
...
@@ -42,25 +42,54 @@ class Admin::Metrics::Retention
end
def
perform_query
sql
=
<<-
SQL
.
squish
report_rows
.
each_with_object
([])
do
|
row
,
arr
|
current_cohort
=
arr
.
last
if
current_cohort
.
nil?
||
current_cohort
.
period
!=
row
[
'cohort_period'
]
current_cohort
=
Cohort
.
new
(
period:
row
[
'cohort_period'
],
frequency:
@frequency
,
data:
[])
arr
<<
current_cohort
end
value
,
rate
=
row
[
'retention_value_and_rate'
].
delete
(
'{}'
).
split
(
','
)
current_cohort
.
data
<<
CohortData
.
new
(
date:
row
[
'retention_period'
],
rate:
rate
.
to_f
,
value:
value
.
to_s
)
end
end
def
report_rows
ActiveRecord
::
Base
.
connection
.
select_all
(
sanitized_sql_string
)
end
def
sanitized_sql_string
ActiveRecord
::
Base
.
sanitize_sql_array
(
[
sql_query_string
,
{
start_at:
@start_at
,
end_at:
@end_at
,
frequency:
@frequency
}]
)
end
def
sql_query_string
<<~
SQL
.
squish
SELECT axis.*, (
WITH new_users AS (
SELECT users.id
FROM users
WHERE date_trunc(
$3
, users.created_at)::date = axis.cohort_period
WHERE date_trunc(
:frequency
, users.created_at)::date = axis.cohort_period
),
retained_users AS (
SELECT users.id
FROM users
INNER JOIN new_users on new_users.id = users.id
WHERE date_trunc(
$3
, users.current_sign_in_at) >= axis.retention_period
WHERE date_trunc(
:frequency
, users.current_sign_in_at) >= axis.retention_period
)
SELECT ARRAY[count(*), (count(*))::float / (SELECT GREATEST(count(*), 1) FROM new_users)] AS retention_value_and_rate
FROM retained_users
)
FROM (
WITH cohort_periods AS (
SELECT generate_series(date_trunc(
$3, $1
::timestamp)::date, date_trunc(
$3, $2
::timestamp)::date, ('1 ' ||
$3
)::interval) AS cohort_period
SELECT generate_series(date_trunc(
:frequency, :start_at
::timestamp)::date, date_trunc(
:frequency, :end_at
::timestamp)::date, ('1 ' ||
:frequency
)::interval) AS cohort_period
),
retention_periods AS (
SELECT cohort_period AS retention_period FROM cohort_periods
...
...
@@ -70,24 +99,5 @@ class Admin::Metrics::Retention
WHERE retention_period >= cohort_period
) as axis
SQL
rows
=
ActiveRecord
::
Base
.
connection
.
select_all
(
sql
,
nil
,
[[
nil
,
@start_at
],
[
nil
,
@end_at
],
[
nil
,
@frequency
]])
rows
.
each_with_object
([])
do
|
row
,
arr
|
current_cohort
=
arr
.
last
if
current_cohort
.
nil?
||
current_cohort
.
period
!=
row
[
'cohort_period'
]
current_cohort
=
Cohort
.
new
(
period:
row
[
'cohort_period'
],
frequency:
@frequency
,
data:
[])
arr
<<
current_cohort
end
value
,
rate
=
row
[
'retention_value_and_rate'
].
delete
(
'{}'
).
split
(
','
)
current_cohort
.
data
<<
CohortData
.
new
(
date:
row
[
'retention_period'
],
rate:
rate
.
to_f
,
value:
value
.
to_s
)
end
end
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment