Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
75317d7c
Commit
75317d7c
authored
Apr 07, 2020
by
Mayra Cabrera
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'users_statistics_worker' into 'master'
Users statistics worker See merge request gitlab-org/gitlab!27883
parents
affc9e4e
a9e9cc0f
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
158 additions
and
15 deletions
+158
-15
app/models/users_statistics.rb
app/models/users_statistics.rb
+42
-15
app/workers/all_queues.yml
app/workers/all_queues.yml
+7
-0
app/workers/users/create_statistics_worker.rb
app/workers/users/create_statistics_worker.rb
+19
-0
changelogs/unreleased/users_statistics_worker.yml
changelogs/unreleased/users_statistics_worker.yml
+5
-0
config/initializers/1_settings.rb
config/initializers/1_settings.rb
+3
-0
spec/factories/users_statistics.rb
spec/factories/users_statistics.rb
+6
-0
spec/models/users_statistics_spec.rb
spec/models/users_statistics_spec.rb
+43
-0
spec/workers/users/create_statistics_worker_spec.rb
spec/workers/users/create_statistics_worker_spec.rb
+33
-0
No files found.
app/models/users_statistics.rb
View file @
75317d7c
...
...
@@ -12,21 +12,48 @@ class UsersStatistics < ApplicationRecord
:blocked
].
freeze
private
def
highest_role_stats
return
unless
Feature
.
enabled?
(
:users_statistics
)
{
owner:
batch_count_for_access_level
(
Gitlab
::
Access
::
OWNER
),
maintainer:
batch_count_for_access_level
(
Gitlab
::
Access
::
MAINTAINER
),
developer:
batch_count_for_access_level
(
Gitlab
::
Access
::
DEVELOPER
),
reporter:
batch_count_for_access_level
(
Gitlab
::
Access
::
REPORTER
),
guest:
batch_count_for_access_level
(
Gitlab
::
Access
::
GUEST
)
}
end
class
<<
self
def
create_current_stats!
stats_by_role
=
highest_role_stats
create!
(
without_groups_and_projects:
without_groups_and_projects_stats
,
with_highest_role_guest:
stats_by_role
[
:guest
],
with_highest_role_reporter:
stats_by_role
[
:reporter
],
with_highest_role_developer:
stats_by_role
[
:developer
],
with_highest_role_maintainer:
stats_by_role
[
:maintainer
],
with_highest_role_owner:
stats_by_role
[
:owner
],
bots:
bot_stats
,
blocked:
blocked_stats
)
end
private
def
highest_role_stats
{
owner:
batch_count_for_access_level
(
Gitlab
::
Access
::
OWNER
),
maintainer:
batch_count_for_access_level
(
Gitlab
::
Access
::
MAINTAINER
),
developer:
batch_count_for_access_level
(
Gitlab
::
Access
::
DEVELOPER
),
reporter:
batch_count_for_access_level
(
Gitlab
::
Access
::
REPORTER
),
guest:
batch_count_for_access_level
(
Gitlab
::
Access
::
GUEST
)
}
end
def
without_groups_and_projects_stats
batch_count_for_access_level
(
nil
)
end
def
bot_stats
Gitlab
::
Database
::
BatchCount
.
batch_count
(
User
.
bots
)
end
def
blocked_stats
Gitlab
::
Database
::
BatchCount
.
batch_count
(
User
.
blocked
)
end
def
batch_count_for_access_level
(
access_level
)
Gitlab
::
Database
::
BatchCount
.
batch_count
(
UserHighestRole
.
with_highest_access_level
(
access_level
))
def
batch_count_for_access_level
(
access_level
)
Gitlab
::
Database
::
BatchCount
.
batch_count
(
UserHighestRole
.
with_highest_access_level
(
access_level
))
end
end
end
app/workers/all_queues.yml
View file @
75317d7c
...
...
@@ -262,6 +262,13 @@
:resource_boundary: :unknown
:weight:
1
:idempotent:
-
:name: cronjob:users_create_statistics
:feature_category: :users
:has_external_dependencies:
:urgency: :low
:resource_boundary: :unknown
:weight:
1
:idempotent:
-
:name: deployment:deployments_finished
:feature_category: :continuous_delivery
:has_external_dependencies:
...
...
app/workers/users/create_statistics_worker.rb
0 → 100644
View file @
75317d7c
# frozen_string_literal: true
module
Users
class
CreateStatisticsWorker
# rubocop:disable Scalability/IdempotentWorker
include
ApplicationWorker
# rubocop:disable Scalability/CronWorkerContext
# This worker does not perform work scoped to a context
include
CronjobQueue
# rubocop:enable Scalability/CronWorkerContext
feature_category
:users
def
perform
UsersStatistics
.
create_current_stats!
rescue
ActiveRecord
::
RecordInvalid
=>
exception
Gitlab
::
ErrorTracking
.
track_exception
(
exception
)
end
end
end
changelogs/unreleased/users_statistics_worker.yml
0 → 100644
View file @
75317d7c
---
title
:
Add daily job to create users statistics
merge_request
:
27883
author
:
type
:
added
config/initializers/1_settings.rb
View file @
75317d7c
...
...
@@ -552,6 +552,9 @@ Gitlab.ee do
Settings
.
cron_jobs
[
'sync_seat_link_worker'
]
||=
Settingslogic
.
new
({})
Settings
.
cron_jobs
[
'sync_seat_link_worker'
][
'cron'
]
||=
"
#{
rand
(
60
)
}
0 * * *"
Settings
.
cron_jobs
[
'sync_seat_link_worker'
][
'job_class'
]
=
'SyncSeatLinkWorker'
Settings
.
cron_jobs
[
'users_create_statistics_worker'
]
||=
Settingslogic
.
new
({})
Settings
.
cron_jobs
[
'users_create_statistics_worker'
][
'cron'
]
||=
'2 15 * * *'
Settings
.
cron_jobs
[
'users_create_statistics_worker'
][
'job_class'
]
=
'Users::CreateStatisticsWorker'
end
#
...
...
spec/factories/users_statistics.rb
0 → 100644
View file @
75317d7c
# frozen_string_literal: true
FactoryBot
.
define
do
factory
:users_statistics
do
end
end
spec/models/users_statistics_spec.rb
0 → 100644
View file @
75317d7c
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
UsersStatistics
do
describe
'.create_current_stats!'
do
before
do
create_list
(
:user_highest_role
,
4
)
create_list
(
:user_highest_role
,
2
,
:guest
)
create_list
(
:user_highest_role
,
3
,
:reporter
)
create_list
(
:user_highest_role
,
4
,
:developer
)
create_list
(
:user_highest_role
,
3
,
:maintainer
)
create_list
(
:user_highest_role
,
2
,
:owner
)
create_list
(
:user
,
2
,
:bot
)
create_list
(
:user
,
1
,
:blocked
)
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:transaction_open?
).
and_return
(
false
)
end
context
'when successful'
do
it
'creates an entry with the current statistics values'
do
expect
(
described_class
.
create_current_stats!
).
to
have_attributes
(
without_groups_and_projects:
4
,
with_highest_role_guest:
2
,
with_highest_role_reporter:
3
,
with_highest_role_developer:
4
,
with_highest_role_maintainer:
3
,
with_highest_role_owner:
2
,
bots:
2
,
blocked:
1
)
end
end
context
'when unsuccessful'
do
it
'raises an ActiveRecord::RecordInvalid exception'
do
allow
(
UsersStatistics
).
to
receive
(
:create!
).
and_raise
(
ActiveRecord
::
RecordInvalid
)
expect
{
described_class
.
create_current_stats!
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
end
end
end
end
spec/workers/users/create_statistics_worker_spec.rb
0 → 100644
View file @
75317d7c
# frozen_string_literal: true
require
'spec_helper'
describe
Users
::
CreateStatisticsWorker
do
describe
'#perform'
do
subject
{
described_class
.
new
.
perform
}
before
do
allow
(
ActiveRecord
::
Base
.
connection
).
to
receive
(
:transaction_open?
).
and_return
(
false
)
end
context
'when successful'
do
it
'create an users statistics entry'
do
expect
{
subject
}.
to
change
{
UsersStatistics
.
count
}.
from
(
0
).
to
(
1
)
end
end
context
'when unsuccessful'
do
it
'logs an error'
do
users_statistics
=
build
(
:users_statistics
)
users_statistics
.
errors
.
add
(
:base
,
'This is an error'
)
exception
=
ActiveRecord
::
RecordInvalid
.
new
(
users_statistics
)
allow
(
UsersStatistics
).
to
receive
(
:create_current_stats!
).
and_raise
(
exception
)
expect
(
Gitlab
::
ErrorTracking
).
to
receive
(
:track_exception
).
with
(
exception
).
and_call_original
subject
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment