Commit 98a6e114 authored by Aakriti Gupta's avatar Aakriti Gupta Committed by Dmytro Zaporozhets

Add a calculator for Group Activity analytics

This class would run queries for all stats we
want to show, as authorized for the current user.
parent 55d0427e
# frozen_string_literal: true
module Analytics
class GroupActivityCalculator
DURATION = 90.days
def initialize(group, current_user)
@group = group
@current_user = current_user
end
def issues_count
@issues_count ||=
IssuesFinder.new(@current_user, params).execute.count
end
def merge_requests_count
@merge_requests_count ||=
MergeRequestsFinder.new(@current_user, params).execute.count
end
private
def params
{ group_id: @group.id,
state: 'all',
created_after: DURATION.ago,
include_subgroups: true,
attempt_group_search_optimizations: true,
attempt_project_search_optimizations: true }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Analytics::GroupActivityCalculator do
subject { described_class.new(group, current_user) }
set(:group) { create(:group) }
set(:current_user) { create(:user) }
set(:subgroup) { create(:group, parent: group) }
set(:project) { create(:project, group: subgroup) }
set(:secret_subgroup) { create(:group, parent: group) }
set(:secret_project) { create(:project, group: secret_subgroup) }
before do
subgroup.add_developer(current_user)
project.add_developer(current_user)
end
context 'with issues' do
set(:recent_issue) { create(:issue, project: project) }
set(:old_issue) do
create(:issue,
project: project,
created_at: 100.days.ago)
end
it 'only returns the count of recent issues' do
expect(subject.issues_count).to eq 1
end
context 'when user does not have access to some issues' do
let(:secret_issue) { create(:issue, project: secret_project) }
it 'does not include those issues' do
expect { secret_issue }.not_to change { subject.issues_count}
end
end
end
context 'with merge requests' do
let!(:recent_mr) do
create(:merge_request,
source_project: project,
source_branch: "my-personal-branch-1")
end
let!(:old_mr) do
create(:merge_request,
source_project: project,
source_branch: "my-personal-branch-2",
created_at: 100.days.ago)
end
it 'only returns the count of recent MRs' do
expect(subject.merge_requests_count).to eq 1
end
context 'when user does not have access to some MRs' do
let(:secret_mr) { create(:merge_request, source_project: secret_project) }
it 'does not include those MRs' do
expect { secret_mr }.not_to change { subject.merge_requests_count}
end
end
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