Skip to content

Commit

Permalink
Merge pull request #314 from grosser/kaarolch-kch/slo_time_slice
Browse files Browse the repository at this point in the history
Kaarolch kch/slo time slice
  • Loading branch information
grosser committed May 27, 2024
2 parents aa24c93 + a8f7c5a commit 050861e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/kennel/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

module Kennel
class Importer
# title will have the lock symbol we need to remove when re-importing
TITLES = [:name, :title].freeze
SORT_ORDER = [*TITLES, :id, :kennel_id, :type, :tags, :query, *Models::Record.subclasses.map { |k| k::TRACKING_FIELDS }, :template_variables].freeze

# bring important fields to the top
SORT_ORDER = [
*TITLES, :id, :kennel_id, :type, :tags, :query, :sli_specification,
*Models::Record.subclasses.flat_map { |k| k::TRACKING_FIELDS },
:template_variables
].freeze

def initialize(api)
@api = api
Expand All @@ -19,6 +26,7 @@ def import(resource, id)
raise(ArgumentError, "#{resource} is not supported")

data = @api.show(model.api_resource, id)

id = data.fetch(:id) # keep native value
model.normalize({}, data) # removes id
data[:id] = id
Expand Down
11 changes: 8 additions & 3 deletions lib/kennel/models/slo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ module Models
class Slo < Record
include TagsValidation

READONLY_ATTRIBUTES = superclass::READONLY_ATTRIBUTES + [:type_id, :monitor_tags, :target_threshold, :timeframe, :warning_threshold]
READONLY_ATTRIBUTES = [
*superclass::READONLY_ATTRIBUTES,
:type_id, :monitor_tags, :target_threshold, :timeframe, :warning_threshold
].freeze
TRACKING_FIELD = :description
DEFAULTS = {
description: nil,
Expand All @@ -14,7 +17,7 @@ class Slo < Record
thresholds: []
}.freeze

settings :type, :description, :thresholds, :query, :tags, :monitor_ids, :monitor_tags, :name, :groups
settings :type, :description, :thresholds, :query, :tags, :monitor_ids, :monitor_tags, :name, :groups, :sli_specification

defaults(
tags: -> { @project.tags },
Expand All @@ -35,7 +38,9 @@ def build_json
type: type
)

if v = query
if type == "time_slice"
data[:sli_specification] = sli_specification
elsif v = query
data[:query] = v
end

Expand Down
28 changes: 28 additions & 0 deletions test/kennel/models/slo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ def slo(options = {})
expected_basic_json
)
end

it "includes sliSpecification for time slices" do
sli_spec = {
time_slice: {
query: {
formulas: [{ formula: "query1" }],
queries: [
{
data_source: "metrics",
name: "query1",
query: "ewma_7(avg:system_cpu{} by {env})"
}
]
},
query_interval_seconds: 300,
comparator: "<=",
threshold: 0.1,
no_data_strategy: "COUNT_AS_UPTIME"
}
}

expected_basic_json[:sli_specification] = sli_spec
expected_basic_json[:type] = "time_slice"
assert_json_equal(
slo(type: "time_slice", sli_specification: sli_spec).build_json,
expected_basic_json
)
end
end

describe "#resolve_linked_tracking_ids!" do
Expand Down

0 comments on commit 050861e

Please sign in to comment.