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
1254cd91
Commit
1254cd91
authored
Sep 25, 2020
by
Ryan Cobb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add scopes and specs
Adds scopes to avoid CodeReuse and adds more specs
parent
204f4191
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
44 additions
and
23 deletions
+44
-23
app/models/environment.rb
app/models/environment.rb
+1
-0
app/models/prometheus_alert.rb
app/models/prometheus_alert.rb
+1
-0
app/services/git/branch_hooks_service.rb
app/services/git/branch_hooks_service.rb
+4
-1
app/workers/metrics/dashboard/sync_dashboards_worker.rb
app/workers/metrics/dashboard/sync_dashboards_worker.rb
+0
-2
lib/gitlab/metrics/dashboard/importers/prometheus_metrics.rb
lib/gitlab/metrics/dashboard/importers/prometheus_metrics.rb
+7
-10
spec/services/git/branch_hooks_service_spec.rb
spec/services/git/branch_hooks_service_spec.rb
+22
-0
spec/workers/metrics/dashboard/sync_dashboards_worker_spec.rb
.../workers/metrics/dashboard/sync_dashboards_worker_spec.rb
+9
-10
No files found.
app/models/environment.rb
View file @
1254cd91
...
...
@@ -86,6 +86,7 @@ class Environment < ApplicationRecord
scope
:with_rank
,
->
do
select
(
'environments.*, rank() OVER (PARTITION BY project_id ORDER BY id DESC)'
)
end
scope
:for_id
,
->
(
id
)
{
where
(
id:
id
)
}
state_machine
:state
,
initial: :available
do
event
:start
do
...
...
app/models/prometheus_alert.rb
View file @
1254cd91
...
...
@@ -36,6 +36,7 @@ class PrometheusAlert < ApplicationRecord
scope
:for_metric
,
->
(
metric
)
{
where
(
prometheus_metric:
metric
)
}
scope
:for_project
,
->
(
project
)
{
where
(
project_id:
project
)
}
scope
:for_environment
,
->
(
environment
)
{
where
(
environment_id:
environment
)
}
scope
:get_environment_id
,
->
{
select
(
:environment_id
).
pluck
(
:environment_id
)
}
def
self
.
distinct_projects
sub_query
=
self
.
group
(
:project_id
).
select
(
1
)
...
...
app/services/git/branch_hooks_service.rb
View file @
1254cd91
...
...
@@ -76,7 +76,7 @@ module Git
def
branch_change_hooks
enqueue_process_commit_messages
enqueue_jira_connect_sync_messages
enqueue_metrics_dashboard_sync
if
Feature
.
enabled?
(
:sync_metrics_dashboards
,
project
)
enqueue_metrics_dashboard_sync
end
def
branch_remove_hooks
...
...
@@ -84,6 +84,9 @@ module Git
end
def
enqueue_metrics_dashboard_sync
return
unless
Feature
.
enabled?
(
:sync_metrics_dashboards
,
project
)
return
unless
default_branch?
::
Metrics
::
Dashboard
::
SyncDashboardsWorker
.
perform_async
(
project
.
id
)
end
...
...
app/workers/metrics/dashboard/sync_dashboards_worker.rb
View file @
1254cd91
...
...
@@ -13,8 +13,6 @@ module Metrics
project
=
Project
.
find
(
project_id
)
dashboard_paths
=
::
Gitlab
::
Metrics
::
Dashboard
::
RepoDashboardFinder
.
list_dashboards
(
project
)
return
unless
dashboard_paths
.
present?
dashboard_paths
.
each
do
|
dashboard_path
|
::
Gitlab
::
Metrics
::
Dashboard
::
Importer
.
new
(
dashboard_path
,
project
).
execute!
end
...
...
lib/gitlab/metrics/dashboard/importers/prometheus_metrics.rb
View file @
1254cd91
...
...
@@ -48,7 +48,7 @@ module Gitlab
affected_metric_ids
<<
prometheus_metric
.
id
end
@affected_environment_ids
+=
find_alerts
(
affected_metric_ids
).
pluck
(
:environment_id
)
@affected_environment_ids
+=
find_alerts
(
affected_metric_ids
).
get_environment_id
end
# rubocop: enable CodeReuse/ActiveRecord
...
...
@@ -60,22 +60,21 @@ module Gitlab
.
for_group
(
Enums
::
PrometheusMetric
.
groups
[
:custom
])
.
not_identifier
(
identifiers_from_yml
)
return
unless
stale_metrics
.
present
?
return
unless
stale_metrics
.
exists
?
delete_stale_alerts
(
stale_metrics
)
stale_metrics
.
each_batch
{
|
batch
|
batch
.
delete_all
}
end
# rubocop: disable CodeReuse/ActiveRecord
def
delete_stale_alerts
(
stale_metrics
)
stale_alerts
=
find_alerts
(
stale_metrics
)
return
unless
stale_alerts
.
present?
affected_environment_ids
=
stale_alerts
.
get_environment_id
return
unless
affected_environment_ids
.
present?
@affected_environment_ids
+=
stale_alerts
.
pluck
(
:environment_id
)
@affected_environment_ids
+=
affected_environment_ids
stale_alerts
.
each_batch
{
|
batch
|
batch
.
delete_all
}
end
# rubocop: enable CodeReuse/ActiveRecord
def
find_alerts
(
metrics
)
Projects
::
Prometheus
::
AlertsFinder
.
new
(
project:
project
,
metric:
metrics
).
execute
...
...
@@ -91,11 +90,10 @@ module Gitlab
end
end
# rubocop: disable CodeReuse/ActiveRecord
def
update_prometheus_environments
affected_environments
=
::
Environment
.
where
(
id:
@affected_environment_ids
.
flatten
.
uniq
,
project:
project
)
affected_environments
=
::
Environment
.
for_id
(
@affected_environment_ids
.
flatten
.
uniq
).
for_project
(
project
)
return
unless
affected_environments
.
present
?
return
unless
affected_environments
.
exists
?
affected_environments
.
each
do
|
affected_environment
|
::
Clusters
::
Applications
::
ScheduleUpdateService
.
new
(
...
...
@@ -104,7 +102,6 @@ module Gitlab
).
execute
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
...
...
spec/services/git/branch_hooks_service_spec.rb
View file @
1254cd91
...
...
@@ -429,4 +429,26 @@ RSpec.describe Git::BranchHooksService do
end
end
end
describe
'Metrics dashboard sync'
do
context
'with feature flag enabled'
do
before
do
Feature
.
enable
(
:metrics_dashboards_sync
)
end
it
'imports metrics to database'
do
expect
(
Metrics
::
Dashboard
::
SyncDashboardsWorker
).
to
receive
(
:perform_async
)
service
.
execute
end
end
context
'with feature flag disabled'
do
it
'imports metrics to database'
do
expect
(
Metrics
::
Dashboard
::
SyncDashboardsWorker
).
to
receive
(
:perform_async
)
service
.
execute
end
end
end
end
spec/workers/metrics/dashboard/sync_dashboards_worker_spec.rb
View file @
1254cd91
...
...
@@ -3,24 +3,23 @@
require
'spec_helper'
RSpec
.
describe
Metrics
::
Dashboard
::
SyncDashboardsWorker
do
include
MetricsDashboardHelpers
subject
(
:worker
)
{
described_class
.
new
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:dashboard_path
s
)
{
[
".gitlab/dashboards/dashboard1.yml"
,
".gitlab/dashboards/dashboard2.yml"
]
}
let
(
:project
)
{
project_with_dashboard
(
dashboard_path
)
}
let
(
:dashboard_path
)
{
'.gitlab/dashboards/test.yml'
}
describe
".perform"
do
before
do
expect
(
::
Gitlab
::
Metrics
::
Dashboard
::
RepoDashboardFinder
).
to
receive
(
:list_dashboards
).
with
(
project
)
.
and_return
(
dashboard_paths
)
it
'imports metrics'
do
expect
{
worker
.
perform
(
project
.
id
)
}.
to
change
(
PrometheusMetric
,
:count
).
by
(
3
)
end
it
'calls importer for each dashboard path'
do
dashboard_paths
.
each
do
|
dashboard_path
|
expect
(
::
Gitlab
::
Metrics
::
Dashboard
::
Importer
).
to
receive
(
:new
)
.
with
(
dashboard_path
,
project
).
and_return
(
double
(
'importer'
,
execute!:
true
))
it
'is idempotent'
do
2
.
times
do
worker
.
perform
(
project
.
id
)
end
worker
.
perform
(
project
.
id
)
expect
(
PrometheusMetric
.
count
).
to
eq
(
3
)
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