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
a97a4905
Commit
a97a4905
authored
Apr 18, 2018
by
Bob Van Landuyt
Committed by
Kushal Pandya
Apr 18, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a serializer to represent Analytics for groups
parent
e50d7588
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
128 additions
and
19 deletions
+128
-19
ee/app/controllers/groups/analytics_controller.rb
ee/app/controllers/groups/analytics_controller.rb
+36
-17
ee/app/serializers/group_analytics_serializer.rb
ee/app/serializers/group_analytics_serializer.rb
+3
-0
ee/app/serializers/user_analytics_entity.rb
ee/app/serializers/user_analytics_entity.rb
+20
-0
ee/spec/controllers/groups/analytics_controller_spec.rb
ee/spec/controllers/groups/analytics_controller_spec.rb
+30
-2
ee/spec/serializers/user_analytics_entity_spec.rb
ee/spec/serializers/user_analytics_entity_spec.rb
+39
-0
No files found.
ee/app/controllers/groups/analytics_controller.rb
View file @
a97a4905
class
Groups::AnalyticsController
<
Groups
::
ApplicationController
class
Groups::AnalyticsController
<
Groups
::
ApplicationController
before_action
:group
before_action
:group
before_action
:check_contribution_analytics_available!
before_action
:check_contribution_analytics_available!
before_action
:load_events
layout
'group'
layout
'group'
def
show
def
show
@users
=
@group
.
users
.
select
(
:id
,
:name
,
:username
).
reorder
(
:id
)
respond_to
do
|
format
|
@start_date
=
params
[
:start_date
]
||
Date
.
today
-
1
.
week
format
.
html
do
@events
=
Event
.
contributions
@stats
=
{}
.
where
(
"created_at > ?"
,
@start_date
)
@stats
[
:push
]
=
count_by_user
(
event_totals
[
:push
]
)
.
where
(
project_id:
@group
.
projects
)
@stats
[
:merge_requests_created
]
=
count_by_user
(
event_totals
[
:merge_requests_created
]
)
@stats
[
:issues_closed
]
=
count_by_user
(
event_totals
[
:issues_closed
])
@stats
=
{}
end
@stats
[
:total_events
]
=
count_by_user
(
@events
.
totals_by_author
)
format
.
json
do
@stats
[
:push
]
=
count_by_user
(
@events
.
code_push
.
totals_by_author
)
render
json:
GroupAnalyticsSerializer
@stats
[
:merge_requests_created
]
=
count_by_user
(
@events
.
merge_requests
.
created
.
totals_by_author
)
.
new
(
events:
event_totals
)
@stats
[
:merge_requests_merged
]
=
count_by_user
(
@events
.
merge_requests
.
merged
.
totals_by_author
)
.
represent
(
users
),
status:
200
@stats
[
:issues_created
]
=
count_by_user
(
@events
.
issues
.
created
.
totals_by_author
)
end
@stats
[
:issues_closed
]
=
count_by_user
(
@events
.
issues
.
closed
.
totals_by_author
)
end
end
end
private
private
def
count_by_user
(
data
)
def
count_by_user
(
data
)
user_ids
.
map
{
|
id
|
data
.
fetch
(
id
,
0
)
}
users
.
map
{
|
user
|
data
.
fetch
(
user
.
id
,
0
)
}
end
def
users
@users
||=
@group
.
users
.
select
(
:id
,
:name
,
:username
).
reorder
(
:id
)
end
def
load_events
@start_date
=
params
[
:start_date
]
||
Date
.
today
-
1
.
week
@events
=
Event
.
contributions
.
where
(
"created_at > ?"
,
@start_date
)
.
where
(
project_id:
@group
.
projects
)
end
end
def
user_ids
def
event_totals
@user_ids
||=
@users
.
map
(
&
:id
)
@event_totals
||=
{
push:
@events
.
code_push
.
totals_by_author
,
issues_created:
@events
.
issues
.
created
.
totals_by_author
,
issues_closed:
@events
.
issues
.
closed
.
totals_by_author
,
merge_requests_created:
@events
.
merge_requests
.
created
.
totals_by_author
,
merge_requests_merged:
@events
.
merge_requests
.
merged
.
totals_by_author
,
total_events:
@events
.
totals_by_author
}
end
end
def
check_contribution_analytics_available!
def
check_contribution_analytics_available!
...
...
ee/app/serializers/group_analytics_serializer.rb
0 → 100644
View file @
a97a4905
class
GroupAnalyticsSerializer
<
BaseSerializer
entity
UserAnalyticsEntity
end
ee/app/serializers/user_analytics_entity.rb
0 → 100644
View file @
a97a4905
class
UserAnalyticsEntity
<
Grape
::
Entity
include
RequestAwareEntity
EVENT_TYPES
=
[
:push
,
:issues_created
,
:issues_closed
,
:merge_requests_created
,
:merge_requests_merged
,
:total_events
].
freeze
expose
:username
expose
:name
,
as: :fullname
expose
:user_web_url
do
|
user
|
user_path
(
user
)
end
EVENT_TYPES
.
each
do
|
event_type
|
expose
event_type
do
|
user
|
request
.
events
[
event_type
].
fetch
(
user
.
id
,
0
)
end
end
end
ee/spec/controllers/groups/analytics_controller_spec.rb
View file @
a97a4905
...
@@ -72,13 +72,41 @@ describe Groups::AnalyticsController do
...
@@ -72,13 +72,41 @@ describe Groups::AnalyticsController do
stats
=
assigns
[
:stats
]
stats
=
assigns
[
:stats
]
# NOTE: The array ordering matters! The view references them all by index
# NOTE: The array ordering matters! The view references them all by index
expect
(
stats
[
:total_events
]).
to
eq
([
2
,
2
,
2
])
expect
(
stats
[
:merge_requests_merged
]).
to
eq
([
0
,
0
,
0
])
expect
(
stats
[
:merge_requests_created
]).
to
eq
([
0
,
1
,
1
])
expect
(
stats
[
:merge_requests_created
]).
to
eq
([
0
,
1
,
1
])
expect
(
stats
[
:issues_closed
]).
to
eq
([
1
,
1
,
0
])
expect
(
stats
[
:issues_closed
]).
to
eq
([
1
,
1
,
0
])
expect
(
stats
[
:push
]).
to
eq
([
1
,
0
,
1
])
expect
(
stats
[
:push
]).
to
eq
([
1
,
0
,
1
])
end
end
it
"returns member contributions JSON when format is JSON"
do
get
:show
,
group_id:
group
.
path
,
format: :json
expect
(
json_response
.
length
).
to
eq
(
3
)
first_user
=
json_response
.
at
(
0
)
expect
(
first_user
[
"username"
]).
to
eq
(
user
.
username
)
expect
(
first_user
[
"user_web_url"
]).
to
eq
(
"/
#{
user
.
username
}
"
)
expect
(
first_user
[
"fullname"
]).
to
eq
(
user
.
name
)
expect
(
first_user
[
"push"
]).
to
eq
(
1
)
expect
(
first_user
[
"issues_created"
]).
to
eq
(
0
)
expect
(
first_user
[
"issues_closed"
]).
to
eq
(
1
)
expect
(
first_user
[
"merge_requests_created"
]).
to
eq
(
0
)
expect
(
first_user
[
"merge_requests_merged"
]).
to
eq
(
0
)
expect
(
first_user
[
"total_events"
]).
to
eq
(
2
)
end
it
'does not cause N+1 queries when the format is JSON'
do
control_count
=
ActiveRecord
::
QueryRecorder
.
new
do
get
:show
,
group_id:
group
.
path
,
format: :json
end
controller
.
instance_variable_set
(
:@group
,
nil
)
user4
=
create
(
:user
)
group
.
add_user
(
user4
,
GroupMember
::
DEVELOPER
)
expect
{
get
:show
,
group_id:
group
.
path
,
format: :json
}
.
not_to
exceed_query_limit
(
control_count
)
end
describe
'with views'
do
describe
'with views'
do
render_views
render_views
...
...
ee/spec/serializers/user_analytics_entity_spec.rb
0 → 100644
View file @
a97a4905
require
'spec_helper'
describe
UserAnalyticsEntity
do
let
(
:user
)
{
build_stubbed
(
:user
)
}
let
(
:events
)
do
{
push:
{},
issues_created:
{},
issues_closed:
{},
merge_requests_created:
{},
merge_requests_merged:
{},
total_events:
{}
}
end
let
(
:request
)
{
double
(
'request'
)
}
subject
(
:json
)
{
described_class
.
new
(
user
,
request:
request
).
as_json
}
before
do
allow
(
request
).
to
receive
(
:events
).
and_return
(
events
)
end
it
'has all the user attributes'
do
is_expected
.
to
include
(
:username
,
:fullname
,
:user_web_url
)
end
[
:push
,
:issues_created
,
:issues_closed
,
:merge_requests_created
,
:merge_requests_merged
,
:total_events
].
each
do
|
event_type
|
it
"fetches
#{
event_type
}
events for the user from the request"
do
events
[
event_type
]
=
{
user
.
id
=>
42
}
expect
(
json
[
event_type
]).
to
eq
(
42
)
end
end
it
'sets 0 as the total when there were no events for a type'
do
expect
(
json
[
:total_events
]).
to
eq
(
0
)
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