Skip to content

Business intelligence made simple

Added support for forecasting

Andrew Kane75e22b8e4d31
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8f46a3a6..9efd7694 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
- Added navbar
- Added favicon
- Added Trend anomaly detection
+- Added forecasting
- Added search for checks and schema
- Improved docs for new installs
- Fixed error with canceling queries
diff --git a/README.md b/README.md
index 574b18d0..eae66374 100644
--- a/README.md
+++ b/README.md
@@ -403,6 +403,22 @@ If upgrading from version 1.4 or below, also follow the [upgrade instructions](#
If you’re on Heroku, follow [these additional instructions](#anomaly-detection-on-heroku).
+## Forecasting [master]
+
+Blazer supports forecasting through [Trend](https://trendapi.org/).
+
+Add [trend](https://github.com/ankane/trend) to your Gemfile:
+
+```ruby
+gem 'trend'
+```
+
+And add to `config/blazer.yml`:
+
+```yml
+forecasting: trend
+```
+
## Data Sources
Blazer supports multiple data sources :tada:
diff --git a/app/controllers/blazer/queries_controller.rb b/app/controllers/blazer/queries_controller.rb
index 4405b810..6140460d 100644
--- a/app/controllers/blazer/queries_controller.rb
+++ b/app/controllers/blazer/queries_controller.rb
@@ -133,6 +133,13 @@ module Blazer
@cached_at = @result.cached_at
@just_cached = @result.just_cached
+ @forecast = @query && @result.forecastable? && params[:forecast]
+ if @forecast
+ @result.forecast
+ @forecast_error = @result.forecast_error
+ @forecast = @forecast_error.nil?
+ end
+
render_run
else
@timestamp = Time.now.to_i
diff --git a/app/views/blazer/queries/run.html.erb b/app/views/blazer/queries/run.html.erb
index 4e1981bc..326fd21f 100644
--- a/app/views/blazer/queries/run.html.erb
+++ b/app/views/blazer/queries/run.html.erb
@@ -33,8 +33,16 @@
&middot; <%= check.message %>
<% end %>
<% end %>
+
+ <% if @query && @result.forecastable? && !params[:forecast] %>
+ &middot;
+ <%= link_to "Forecast", query_path(@query, {forecast: "t"}.merge(variable_params)) %>
+ <% end %>
</p>
<% end %>
+ <% if @forecast_error %>
+ <div class="alert alert-danger"><%= @forecast_error %></div>
+ <% end %>
<% if @rows.any? %>
<% values = @rows.first %>
<% chart_id = SecureRandom.hex %>
@@ -52,6 +60,10 @@
<% if target_index %>
<% series_library[target_index - 1] = {pointStyle: "line", hitRadius: 5, borderColor: "#109618", pointBackgroundColor: "#109618", backgroundColor: "#109618"} %>
<% end %>
+ <% if @forecast %>
+ <% color = "#54a3ee" %>
+ <% series_library[1] = {borderDash: [8], borderColor: color, pointBackgroundColor: color, backgroundColor: color, pointHoverBackgroundColor: color} %>
+ <% end %>
<% if blazer_maps? && @markers.any? %>
<div id="map" style="height: <%= @only_chart ? 300 : 500 %>px;"></div>
<script>
@@ -82,7 +94,11 @@
map.fitBounds(featureLayer.getBounds());
</script>
<% elsif chart_type == "line" %>
- <%= line_chart @columns[1..-1].each_with_index.map{ |k, i| {name: blazer_series_name(k), data: @rows.map{ |r| [r[0], r[i + 1]] }, library: series_library[i]} }, chart_options %>
+ <% chart_data = @columns[1..-1].each_with_index.map{ |k, i| {name: blazer_series_name(k), data: @rows.map{ |r| [r[0], r[i + 1]] }, library: series_library[i]} } %>
+ <% if @forecast %>
+ <% chart_data[1][:data] << chart_data[0][:data].select { |v| v[1] }.max_by { |v| v[0] } %>
+ <% end %>
+ <%= line_chart chart_data, chart_options %>
<% elsif chart_type == "line2" %>
<%= line_chart @rows.group_by { |r| v = r[1]; (@boom[@columns[1]] || {})[v.to_s] || v }.each_with_index.map { |(name, v), i| {name: blazer_series_name(name), data: v.map { |v2| [v2[0], v2[2]] }, library: series_library[i]} }, chart_options %>
<% elsif chart_type == "bar" %>
diff --git a/app/views/blazer/queries/show.html.erb b/app/views/blazer/queries/show.html.erb
index 681591d9..adb90212 100644
--- a/app/views/blazer/queries/show.html.erb
+++ b/app/views/blazer/queries/show.html.erb
@@ -9,7 +9,7 @@
<%= link_to "Fork", new_query_path(variable_params.merge(fork_query_id: @query.id, data_source: @query.data_source, name: @query.name)), class: "btn btn-info" %>
<% if !@error && @success %>
- <%= button_to "Download", run_queries_path(query_id: @query.id, format: "csv"), params: {statement: @statement}, class: "btn btn-primary" %>
+ <%= button_to "Download", run_queries_path(query_id: @query.id, format: "csv", forecast: params[:forecast]), params: {statement: @statement}, class: "btn btn-primary" %>
<% end %>
</div>
</div>
diff --git a/lib/blazer.rb b/lib/blazer.rb
index e6a2cfe0..067046f7 100644
--- a/lib/blazer.rb
+++ b/lib/blazer.rb
@@ -35,6 +35,7 @@ module Blazer
attr_accessor :transform_statement
attr_accessor :check_schedules
attr_accessor :anomaly_checks
+ attr_accessor :forecasting
attr_accessor :async
attr_accessor :images
attr_accessor :query_viewable
@@ -46,6 +47,7 @@ module Blazer
self.user_name = :name
self.check_schedules = ["5 minutes", "1 hour", "1 day"]
self.anomaly_checks = false
+ self.forecasting = false
self.async = false
self.images = false
self.override_csp = false
diff --git a/lib/blazer/engine.rb b/lib/blazer/engine.rb
index d9d5df4b..18f06c0a 100644
--- a/lib/blazer/engine.rb
+++ b/lib/blazer/engine.rb
@@ -17,6 +17,7 @@ module Blazer
Blazer.cache ||= Rails.cache
Blazer.anomaly_checks = Blazer.settings["anomaly_checks"] || false
+ Blazer.forecasting = Blazer.settings["forecasting"] || false
Blazer.async = Blazer.settings["async"] || false
if Blazer.async
require "blazer/run_statement_job"
diff --git a/lib/blazer/result.rb b/lib/blazer/result.rb
index a89c8b8a..7edbfd4c 100644
--- a/lib/blazer/result.rb
+++ b/lib/blazer/result.rb
@@ -1,6 +1,6 @@
module Blazer
class Result
- attr_reader :data_source, :columns, :rows, :error, :cached_at, :just_cached
+ attr_reader :data_source, :columns, :rows, :error, :cached_at, :just_cached, :forecast_error
def initialize(data_source, columns, rows, error, cached_at, just_cached)
@data_source = data_source
@@ -79,6 +79,30 @@ module Blazer
end
end
+ def forecastable?
+ @forecastable ||= Blazer.forecasting && column_types == ["time", "numeric"] && @rows.size >= 10
+ end
+
+ def forecast
+ # TODO cache it?
+ forecast = Trend.forecast(Hash[@rows], count: 30)
+ @rows.each do |row|
+ row[2] = nil
+ end
+ @rows.unshift(*forecast.map { |k, v| [k, nil, v] })
+ @columns << "forecast"
+
+ # reset cache
+ @column_types = nil
+ @chart_type = nil
+
+ forecast
+ rescue => e
+ @forecast_error = String.new("Error generating forecast")
+ @forecast_error << ": #{e.message.sub("Invalid parameter: ", "")}"
+ nil
+ end
+
def detect_anomaly
anomaly = nil
message = nil
diff --git a/lib/generators/blazer/templates/config.yml.tt b/lib/generators/blazer/templates/config.yml.tt
index c0eb8146..91b43cd9 100644
--- a/lib/generators/blazer/templates/config.yml.tt
+++ b/lib/generators/blazer/templates/config.yml.tt
@@ -60,3 +60,11 @@ check_schedules:
- "1 day"
- "1 hour"
- "5 minutes"
+
+# enable anomaly detection
+# note: with trend, time series are sent to an external service
+# anomaly_checks: trend / r
+
+# enable forecasting
+# note: with trend, time series are sent to an external service
+# forecasting: trend