Business intelligence made simple
Added Trend anomaly detection
Andrew Kanefe6be61515a3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cf9bde14..f40cdc00 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1,6 +1,7 @@## 2.0.1 [unreleased]- Added favicon+- Added Trend anomaly detection- Improved docs for new installs- Fixed error with canceling queriesdiff --git a/README.md b/README.mdindex 496fb43d..e093559f 100644--- a/README.md+++ b/README.md@@ -364,7 +364,27 @@ Then create check with optional emails if you want to be notified. Emails are se## Anomaly Detection-Anomaly detection is supported thanks to Twitter’s [AnomalyDetection](https://github.com/twitter/AnomalyDetection) library.+Blazer supports two different approaches to anomaly detection.++### Trend [master]++[Trend](https://trendapi.org/) is easiest to set up but uses an external service.++Add [trend](https://github.com/ankane/trend) to your Gemfile:++```ruby+gem 'trend'+```++And add to `config/blazer.yml`:++```yml+anomaly_checks: trend+```++### R++R is harder to set up but doesn’t use an external service. It uses Twitter’s [AnomalyDetection](https://github.com/twitter/AnomalyDetection) library.First, [install R](https://cloud.r-project.org/). Then, run:diff --git a/lib/blazer/result.rb b/lib/blazer/result.rbindex 94d7bbcd..a89c8b8a 100644--- a/lib/blazer/result.rb+++ b/lib/blazer/result.rb@@ -131,39 +131,44 @@ module Blazerdef anomaly?(series)series = series.reject { |v| v[0].nil? }.sort_by { |v| v[0] }- csv_str =- CSV.generate do |csv|- csv << ["timestamp", "count"]- series.each do |row|- csv << row+ if Blazer.anomaly_checks == "trend"+ anomalies = Trend.anomalies(Hash[series])+ anomalies.include?(series.last[0])+ else+ csv_str =+ CSV.generate do |csv|+ csv << ["timestamp", "count"]+ series.each do |row|+ csv << row+ endend- end- r_script = %x[which Rscript].chomp- type = series.any? && series.last.first.to_time - series.first.first.to_time >= 2.weeks ? "ts" : "vec"- args = [type, csv_str]- raise "R not found" if r_script.empty?- command = "#{r_script} --vanilla #{File.expand_path("../detect_anomalies.R", __FILE__)} #{args.map { |a| Shellwords.escape(a) }.join(" ")}"- output = %x[#{command}]- if output.empty?- raise "Unknown R error"- end+ r_script = %x[which Rscript].chomp+ type = series.any? && series.last.first.to_time - series.first.first.to_time >= 2.weeks ? "ts" : "vec"+ args = [type, csv_str]+ raise "R not found" if r_script.empty?+ command = "#{r_script} --vanilla #{File.expand_path("../detect_anomalies.R", __FILE__)} #{args.map { |a| Shellwords.escape(a) }.join(" ")}"+ output = %x[#{command}]+ if output.empty?+ raise "Unknown R error"+ end- rows = CSV.parse(output, headers: true)- error = rows.first && rows.first["x"]- raise error if error+ rows = CSV.parse(output, headers: true)+ error = rows.first && rows.first["x"]+ raise error if error- timestamps = []- if type == "ts"- rows.each do |row|- timestamps << Time.parse(row["timestamp"])- end- timestamps.include?(series.last[0].to_time)- else- rows.each do |row|- timestamps << row["index"].to_i+ timestamps = []+ if type == "ts"+ rows.each do |row|+ timestamps << Time.parse(row["timestamp"])+ end+ timestamps.include?(series.last[0].to_time)+ else+ rows.each do |row|+ timestamps << row["index"].to_i+ end+ timestamps.include?(series.length)end- timestamps.include?(series.length)endendend