Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'usage-ping-port' into 'master'
Usage ping port Closes #27750 See merge request !10481
- Loading branch information
Showing
62 changed files
with
1,498 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function UsagePing() { | ||
const usageDataUrl = $('.usage-data').data('endpoint'); | ||
|
||
$.ajax({ | ||
type: 'GET', | ||
url: usageDataUrl, | ||
dataType: 'html', | ||
success(html) { | ||
$('.usage-data').html(html); | ||
}, | ||
}); | ||
} | ||
|
||
window.gl = window.gl || {}; | ||
window.gl.UsagePing = UsagePing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Admin::CohortsController < Admin::ApplicationController | ||
def index | ||
if current_application_settings.usage_ping_enabled | ||
cohorts_results = Rails.cache.fetch('cohorts', expires_in: 1.day) do | ||
CohortsService.new.execute | ||
end | ||
|
||
@cohorts = CohortsSerializer.new.represent(cohorts_results) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CohortActivityMonthEntity < Grape::Entity | ||
include ActionView::Helpers::NumberHelper | ||
|
||
expose :total do |cohort_activity_month| | ||
number_with_delimiter(cohort_activity_month[:total]) | ||
end | ||
|
||
expose :percentage do |cohort_activity_month| | ||
number_to_percentage(cohort_activity_month[:percentage], precision: 0) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class CohortEntity < Grape::Entity | ||
include ActionView::Helpers::NumberHelper | ||
|
||
expose :registration_month do |cohort| | ||
cohort[:registration_month].strftime('%b %Y') | ||
end | ||
|
||
expose :total do |cohort| | ||
number_with_delimiter(cohort[:total]) | ||
end | ||
|
||
expose :inactive do |cohort| | ||
number_with_delimiter(cohort[:inactive]) | ||
end | ||
|
||
expose :activity_months, using: CohortActivityMonthEntity | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class CohortsEntity < Grape::Entity | ||
expose :months_included | ||
expose :cohorts, using: CohortEntity | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class CohortsSerializer < AnalyticsGenericSerializer | ||
entity CohortsEntity | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
class CohortsService | ||
MONTHS_INCLUDED = 12 | ||
|
||
def execute | ||
{ | ||
months_included: MONTHS_INCLUDED, | ||
cohorts: cohorts | ||
} | ||
end | ||
|
||
# Get an array of hashes that looks like: | ||
# | ||
# [ | ||
# { | ||
# registration_month: Date.new(2017, 3), | ||
# activity_months: [3, 2, 1], | ||
# total: 3 | ||
# inactive: 0 | ||
# }, | ||
# etc. | ||
# | ||
# The `months` array is always from oldest to newest, so it's always | ||
# non-strictly decreasing from left to right. | ||
def cohorts | ||
months = Array.new(MONTHS_INCLUDED) { |i| i.months.ago.beginning_of_month.to_date } | ||
|
||
Array.new(MONTHS_INCLUDED) do | ||
registration_month = months.last | ||
activity_months = running_totals(months, registration_month) | ||
|
||
# Even if no users registered in this month, we always want to have a | ||
# value to fill in the table. | ||
inactive = counts_by_month[[registration_month, nil]].to_i | ||
|
||
months.pop | ||
|
||
{ | ||
registration_month: registration_month, | ||
activity_months: activity_months, | ||
total: activity_months.first[:total], | ||
inactive: inactive | ||
} | ||
end | ||
end | ||
|
||
private | ||
|
||
# Calculate a running sum of active users, so users active in later months | ||
# count as active in this month, too. Start with the most recent month first, | ||
# for calculating the running totals, and then reverse for displaying in the | ||
# table. | ||
# | ||
# Each month has a total, and a percentage of the overall total, as keys. | ||
def running_totals(all_months, registration_month) | ||
month_totals = | ||
all_months | ||
.map { |activity_month| counts_by_month[[registration_month, activity_month]] } | ||
.reduce([]) { |result, total| result << result.last.to_i + total.to_i } | ||
.reverse | ||
|
||
overall_total = month_totals.first | ||
|
||
month_totals.map do |total| | ||
{ total: total, percentage: total.zero? ? 0 : 100 * total / overall_total } | ||
end | ||
end | ||
|
||
# Get a hash that looks like: | ||
# | ||
# { | ||
# [created_at_month, last_activity_on_month] => count, | ||
# [created_at_month, last_activity_on_month_2] => count_2, | ||
# # etc. | ||
# } | ||
# | ||
# created_at_month can never be nil, but last_activity_on_month can (when a | ||
# user has never logged in, just been created). This covers the last | ||
# MONTHS_INCLUDED months. | ||
def counts_by_month | ||
@counts_by_month ||= | ||
begin | ||
created_at_month = column_to_date('created_at') | ||
last_activity_on_month = column_to_date('last_activity_on') | ||
|
||
User | ||
.where('created_at > ?', MONTHS_INCLUDED.months.ago.end_of_month) | ||
.group(created_at_month, last_activity_on_month) | ||
.reorder("#{created_at_month} ASC", "#{last_activity_on_month} ASC") | ||
.count | ||
end | ||
end | ||
|
||
def column_to_date(column) | ||
if Gitlab::Database.postgresql? | ||
"CAST(DATE_TRUNC('month', #{column}) AS date)" | ||
else | ||
"STR_TO_DATE(DATE_FORMAT(#{column}, '%Y-%m-01'), '%Y-%m-%d')" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Users | ||
class ActivityService | ||
def initialize(author, activity) | ||
@author = author.respond_to?(:user) ? author.user : author | ||
@activity = activity | ||
end | ||
|
||
def execute | ||
return unless @author && @author.is_a?(User) | ||
|
||
record_activity | ||
end | ||
|
||
private | ||
|
||
def record_activity | ||
Gitlab::UserActivities.record(@author.id) | ||
|
||
Rails.logger.debug("Recorded activity: #{@activity} for User ID: #{@author.id} (username: #{@author.username}") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
.bs-callout.clearfix | ||
%p | ||
User cohorts are shown for the last #{@cohorts[:months_included]} | ||
months. Only users with activity are counted in the cohort total; inactive | ||
users are counted separately. | ||
= link_to icon('question-circle'), help_page_path('user/admin_area/user_cohorts', anchor: 'cohorts'), title: 'About this feature', target: '_blank' | ||
|
||
.table-holder | ||
%table.table | ||
%thead | ||
%tr | ||
%th Registration month | ||
%th Inactive users | ||
%th Cohort total | ||
- @cohorts[:months_included].times do |i| | ||
%th Month #{i} | ||
%tbody | ||
- @cohorts[:cohorts].each do |cohort| | ||
%tr | ||
%td= cohort[:registration_month] | ||
%td= cohort[:inactive] | ||
%td= cohort[:total] | ||
- cohort[:activity_months].each do |activity_month| | ||
%td | ||
- next if cohort[:total] == '0' | ||
= activity_month[:percentage] | ||
%br | ||
= activity_month[:total] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
%h2#usage-ping Usage ping | ||
|
||
.bs-callout.clearfix | ||
%p | ||
User cohorts are shown because the usage ping is enabled. The data sent with | ||
this is shown below. To disable this, visit | ||
= succeed '.' do | ||
= link_to 'application settings', admin_application_settings_path(anchor: 'usage-statistics') | ||
|
||
%pre.usage-data.js-syntax-highlight.code.highlight{ data: { endpoint: usage_data_admin_application_settings_path(format: :html, pretty: true) } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
- @no_container = true | ||
= render "admin/dashboard/head" | ||
|
||
%div{ class: container_class } | ||
- if @cohorts | ||
= render 'cohorts_table' | ||
= render 'usage_ping' | ||
- else | ||
.bs-callout.bs-callout-warning.clearfix | ||
%p | ||
User cohorts are only shown when the | ||
= link_to 'usage ping', help_page_path('user/admin_area/usage_statistics'), target: '_blank' | ||
is enabled. To enable it and see user cohorts, | ||
visit | ||
= succeed '.' do | ||
= link_to 'application settings', admin_application_settings_path(anchor: 'usage-statistics') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.