Commit 6bd773a8 authored by Dmytro Zaporozhets (DZ)'s avatar Dmytro Zaporozhets (DZ) Committed by James Fargher

Add activity graph to PA

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 8672da3c
......@@ -27,6 +27,10 @@ class Projects::ProductAnalyticsController < Projects::ApplicationController
.new(project, { graph: graph, timerange: @timerange })
.execute
end
@activity_graph = ProductAnalytics::BuildActivityGraphService
.new(project, { timerange: @timerange })
.execute
end
private
......
......@@ -26,6 +26,13 @@ class ProductAnalyticsEvent < ApplicationRecord
group(graph).timerange(days).count
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
as_json.compact
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
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,
values: results.values
}
end
private
def product_analytics_events
@project.product_analytics_events
end
......
......@@ -5,6 +5,10 @@
%p
= _('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|
.row.append-bottom-10
- pair.each do |graph|
......
......@@ -46,4 +46,18 @@ RSpec.describe ProductAnalyticsEvent, type: :model do
it { expect(described_class.by_category_and_action('catA', 'actA')).to match_array([event]) }
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
# 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