Skip to content

Commit

Permalink
anomaly-detector (brabster#188)
Browse files Browse the repository at this point in the history
* Add support for cloudwatch anomaly detector

* Add test for cloudwatch anomaly detector

* Remove need for ->key on name space spec

* Remove vector requirement on dimensions spec
  • Loading branch information
Cameron Sumpter authored and Sam Hewitt committed Oct 7, 2019
1 parent 0dfb4b0 commit 37b7de8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/crucible/aws/cloudwatch/anomaly_detector.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(ns crucible.aws.cloudwatch.anomaly-detector
(:require [clojure.spec.alpha :as s]
[crucible.aws.cloudwatch :as cloudwatch]
[crucible.encoding.keys :refer [->key]]
[crucible.resources :refer [defresource spec-or-ref]]))

(s/def ::value (spec-or-ref string?))

(s/def ::name (spec-or-ref string?))

(s/def ::dimension (s/keys :req [::name ::value]))

(s/def ::dimensions (s/coll-of ::dimension))

(s/def ::stat ::cloudwatch/statistic)

(def date-time-regex
#"(19|20)[0-9][0-9]-(0[0-9]|1[0-2])-(0[1-9]|([12][0-9]|3[01]))T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]")

(s/def ::date-time (spec-or-ref (s/and string? #(re-matches date-time-regex %))))

(s/def ::end-time ::date-time)

(s/def ::start-time ::date-time)

(s/def ::range (s/keys :req [::start-time ::end-time]))

(s/def ::ranges (s/coll-of ::range))

(s/def ::namespace (spec-or-ref string?))

(s/def ::metric-name (spec-or-ref string?))

(s/def ::excluded-time-ranges (spec-or-ref ::ranges))

(s/def ::metric-time-zone (spec-or-ref string?))

(s/def ::configuration (s/keys :opt [::excluded-time-ranges ::metric-time-zone]))

(s/def ::anomaly-detector (s/keys :req [::metric-name
::namespace
::stat]))

(defresource anomaly-detector "AWS::CloudWatch::AnomalyDetector" ::anomaly-detector)
19 changes: 19 additions & 0 deletions test-resources/aws/cloudwatch/anomaly_detector.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Type" : "AWS::CloudWatch::AnomalyDetector",
"Properties" : {
"Configuration" : {
"ExcludedTimeRanges": [{
"EndTime": "2019-07-01T23:59:59",
"StartTime": "2019-07-01T23:59:59"
}],
"MetricTimeZone": "America/New_York"
},
"Dimensions" : [{
"Name": "Memory",
"Value": "UsedMemory"
}],
"MetricName" : "JvmMetric",
"Namespace" : "AWSSDK/Java",
"Stat" : "Average"
}
}
22 changes: 22 additions & 0 deletions test/crucible/aws/cloudwatch/anomaly_detector_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(ns crucible.aws.cloudwatch.anomaly-detector-test
(:require [cheshire.core :as json]
[clojure.java.io :as io]
[clojure.test :as t :refer :all]
[crucible.assertion :refer [resource=]]
[crucible.aws.cloudwatch.anomaly-detector :as ad]))

(deftest anomaly-detection-test
(testing "encode"
(is (resource=
(json/decode (slurp (io/resource "aws/cloudwatch/anomaly_detector.json")))
(ad/anomaly-detector
{::ad/namespace "AWSSDK/Java"
::ad/stat "Average"
::ad/metric-name "JvmMetric"
::ad/configuration
{::ad/excluded-time-ranges
[{::ad/end-time "2019-07-01T23:59:59"
::ad/start-time "2019-07-01T23:59:59"}]
::ad/metric-time-zone "America/New_York"}
::ad/dimensions [{::ad/name "Memory"
::ad/value "UsedMemory"}]})))))

0 comments on commit 37b7de8

Please sign in to comment.