2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-06 10:07:33 -04:00
|
|
|
module Gitlab
|
|
|
|
module Prometheus
|
|
|
|
module Queries
|
|
|
|
class DeploymentQuery < BaseQuery
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-02-23 15:33:33 -05:00
|
|
|
def query(deployment_id)
|
2017-06-20 09:53:55 -04:00
|
|
|
Deployment.find_by(id: deployment_id).try do |deployment|
|
|
|
|
environment_slug = deployment.environment.slug
|
2017-04-26 16:09:03 -04:00
|
|
|
|
2017-06-20 09:53:55 -04:00
|
|
|
memory_query = raw_memory_usage_query(environment_slug)
|
|
|
|
memory_avg_query = %{avg(avg_over_time(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"}[30m]))}
|
|
|
|
cpu_query = raw_cpu_usage_query(environment_slug)
|
|
|
|
cpu_avg_query = %{avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="#{environment_slug}"}[30m])) * 100}
|
2017-04-26 16:09:03 -04:00
|
|
|
|
2017-06-20 09:53:55 -04:00
|
|
|
timeframe_start = (deployment.created_at - 30.minutes).to_f
|
|
|
|
timeframe_end = (deployment.created_at + 30.minutes).to_f
|
2017-04-26 16:09:03 -04:00
|
|
|
|
2017-06-20 09:53:55 -04:00
|
|
|
{
|
2020-03-23 20:09:24 -04:00
|
|
|
memory_values: client_query_range(memory_query, start_time: timeframe_start, end_time: timeframe_end),
|
2017-06-20 09:53:55 -04:00
|
|
|
memory_before: client_query(memory_avg_query, time: deployment.created_at.to_f),
|
|
|
|
memory_after: client_query(memory_avg_query, time: timeframe_end),
|
2017-04-26 16:09:03 -04:00
|
|
|
|
2020-03-23 20:09:24 -04:00
|
|
|
cpu_values: client_query_range(cpu_query, start_time: timeframe_start, end_time: timeframe_end),
|
2017-06-20 09:53:55 -04:00
|
|
|
cpu_before: client_query(cpu_avg_query, time: deployment.created_at.to_f),
|
|
|
|
cpu_after: client_query(cpu_avg_query, time: timeframe_end)
|
|
|
|
}
|
|
|
|
end
|
2017-06-06 10:07:33 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-02-23 15:33:33 -05:00
|
|
|
|
|
|
|
def self.transform_reactive_result(result)
|
|
|
|
result[:metrics] = result.delete :data
|
|
|
|
result
|
|
|
|
end
|
2017-06-06 10:07:33 -04:00
|
|
|
end
|
2017-04-26 16:09:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|