Commit aa44b7be authored by James Fargher's avatar James Fargher

Merge branch 'dz-product-analytics-graphs-activity' into 'master'

Add activity graph to Product Analytics

See merge request gitlab-org/gitlab!38156
parents 718eaa44 6bd773a8
...@@ -27,6 +27,10 @@ class Projects::ProductAnalyticsController < Projects::ApplicationController ...@@ -27,6 +27,10 @@ class Projects::ProductAnalyticsController < Projects::ApplicationController
.new(project, { graph: graph, timerange: @timerange }) .new(project, { graph: graph, timerange: @timerange })
.execute .execute
end end
@activity_graph = ProductAnalytics::BuildActivityGraphService
.new(project, { timerange: @timerange })
.execute
end end
private private
......
...@@ -26,6 +26,13 @@ class ProductAnalyticsEvent < ApplicationRecord ...@@ -26,6 +26,13 @@ class ProductAnalyticsEvent < ApplicationRecord
group(graph).timerange(days).count group(graph).timerange(days).count
end end
def self.count_collector_tstamp_by_day(days)
group("DATE_TRUNC('day', collector_tstamp)")
.reorder('date_trunc_day_collector_tstamp')
.timerange(days)
.count
end
def as_json_wo_empty def as_json_wo_empty
as_json.compact as_json.compact
end end
......
# frozen_string_literal: true
module ProductAnalytics
class BuildActivityGraphService < BuildGraphService
def execute
timerange = @params[:timerange].days
results = product_analytics_events.count_collector_tstamp_by_day(timerange)
format_results('collector_tstamp', results.transform_keys(&:to_date))
end
end
end
...@@ -13,15 +13,19 @@ module ProductAnalytics ...@@ -13,15 +13,19 @@ module ProductAnalytics
results = product_analytics_events.count_by_graph(graph, timerange) results = product_analytics_events.count_by_graph(graph, timerange)
format_results(graph, results)
end
private
def format_results(name, results)
{ {
id: graph, id: name,
keys: results.keys, keys: results.keys,
values: results.values values: results.values
} }
end end
private
def product_analytics_events def product_analytics_events
@project.product_analytics_events @project.product_analytics_events
end end
......
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
%p %p
= _('Showing graphs based on events of the last %{timerange} days.') % { timerange: @timerange } = _('Showing graphs based on events of the last %{timerange} days.') % { timerange: @timerange }
.gl-mb-3
= render 'graph', graph: @activity_graph
- @graphs.each_slice(2) do |pair| - @graphs.each_slice(2) do |pair|
.row.append-bottom-10 .row.append-bottom-10
- pair.each do |graph| - pair.each do |graph|
......
...@@ -46,4 +46,18 @@ RSpec.describe ProductAnalyticsEvent, type: :model do ...@@ -46,4 +46,18 @@ RSpec.describe ProductAnalyticsEvent, type: :model do
it { expect(described_class.by_category_and_action('catA', 'actA')).to match_array([event]) } it { expect(described_class.by_category_and_action('catA', 'actA')).to match_array([event]) }
end end
describe '.count_collector_tstamp_by_day' do
let_it_be(:time_now) { Time.zone.now }
let_it_be(:time_ago) { Time.zone.now - 5.days }
let_it_be(:events) do
create_list(:product_analytics_event, 3, collector_tstamp: time_now) +
create_list(:product_analytics_event, 2, collector_tstamp: time_ago)
end
subject { described_class.count_collector_tstamp_by_day(7.days) }
it { is_expected.to eq({ time_now.beginning_of_day => 3, time_ago.beginning_of_day => 2 }) }
end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ProductAnalytics::BuildActivityGraphService do
let_it_be(:project) { create(:project) }
let_it_be(:time_now) { Time.zone.now }
let_it_be(:time_ago) { Time.zone.now - 5.days }
let_it_be(:events) do
[
create(:product_analytics_event, project: project, collector_tstamp: time_now),
create(:product_analytics_event, project: project, collector_tstamp: time_now),
create(:product_analytics_event, project: project, collector_tstamp: time_now),
create(:product_analytics_event, project: project, collector_tstamp: time_ago),
create(:product_analytics_event, project: project, collector_tstamp: time_ago)
]
end
let(:params) { { timerange: 7 } }
subject { described_class.new(project, params).execute }
it 'returns a valid graph hash' do
expected_hash = {
id: 'collector_tstamp',
keys: [time_ago.to_date, time_now.to_date],
values: [2, 3]
}
expect(subject).to eq(expected_hash)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment