Class: Blazer::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/blazer/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_source, columns, rows, error, cached_at, just_cached) ⇒ Result

Returns a new instance of Result.



5
6
7
8
9
10
11
12
# File 'lib/blazer/result.rb', line 5

def initialize(data_source, columns, rows, error, cached_at, just_cached)
  @data_source = data_source
  @columns = columns
  @rows = rows
  @error = error
  @cached_at = cached_at
  @just_cached = just_cached
end

Instance Attribute Details

#cached_atObject (readonly)

Returns the value of attribute cached_at.



3
4
5
# File 'lib/blazer/result.rb', line 3

def cached_at
  @cached_at
end

#columnsObject (readonly)

Returns the value of attribute columns.



3
4
5
# File 'lib/blazer/result.rb', line 3

def columns
  @columns
end

#data_sourceObject (readonly)

Returns the value of attribute data_source.



3
4
5
# File 'lib/blazer/result.rb', line 3

def data_source
  @data_source
end

#errorObject (readonly)

Returns the value of attribute error.



3
4
5
# File 'lib/blazer/result.rb', line 3

def error
  @error
end

#forecast_errorObject (readonly)

Returns the value of attribute forecast_error.



3
4
5
# File 'lib/blazer/result.rb', line 3

def forecast_error
  @forecast_error
end

#just_cachedObject (readonly)

Returns the value of attribute just_cached.



3
4
5
# File 'lib/blazer/result.rb', line 3

def just_cached
  @just_cached
end

#rowsObject (readonly)

Returns the value of attribute rows.



3
4
5
# File 'lib/blazer/result.rb', line 3

def rows
  @rows
end

Instance Method Details

#anomaly?(series) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/blazer/result.rb', line 174

def anomaly?(series)
  series = series.reject { |v| v[0].nil? }.sort_by { |v| v[0] }

  case Blazer.anomaly_checks
  when "prophet"
    df = Rover::DataFrame.new(series[0..-2].map { |v| {"ds" => v[0], "y" => v[1]} })
    m = Prophet.new(interval_width: 0.99)
    m.logger.level = ::Logger::FATAL # no logging
    m.fit(df)
    future = Rover::DataFrame.new(series[-1..-1].map { |v| {"ds" => v[0]} })
    forecast = m.predict(future).to_a[0]
    lower = forecast["yhat_lower"]
    upper = forecast["yhat_upper"]
    value = series.last[1]
    value < lower || value > upper
  when "trend"
    anomalies = Trend.anomalies(Hash[series])
    anomalies.include?(series.last[0])
  when "anomaly_detection"
    period = 7 # TODO determine period
    anomalies = AnomalyDetection.detect(Hash[series], period: period)
    anomalies.include?(series.last[0])
  else
    csv_str =
      CSV.generate do |csv|
        csv << ["timestamp", "count"]
        series.each do |row|
          csv << row
        end
      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

    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
  end
end

#boomObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/blazer/result.rb', line 22

def boom
  @boom ||= begin
    boom = {}
    columns.each_with_index do |key, i|
      smart_columns_data_source =
        ([data_source] + Array(data_source.settings["inherit_smart_settings"]).map { |ds| Blazer.data_sources[ds] }).find { |ds| ds.smart_columns[key] }

      if smart_columns_data_source
        query = smart_columns_data_source.smart_columns[key]
        res =
          if query.is_a?(Hash)
            query
          else
            values = rows.map { |r| r[i] }.compact.uniq
            result = smart_columns_data_source.run_statement(ActiveRecord::Base.send(:sanitize_sql_array, [query.sub("{value}", "(?)"), values]))
            result.rows
          end

        boom[key] = Hash[res.map { |k, v| [k.nil? ? k : k.to_s, v] }]
      end
    end
    boom
  end
end

#cached?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/blazer/result.rb', line 18

def cached?
  cached_at.present?
end

#chart_typeObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/blazer/result.rb', line 66

def chart_type
  @chart_type ||= begin
    if column_types.compact.size >= 2 && column_types.compact == ["time"] + (column_types.compact.size - 1).times.map { "numeric" }
      "line"
    elsif column_types == ["time", "string", "numeric"]
      "line2"
    elsif column_types == ["string", "numeric"] && @columns.last == "pie"
      "pie"
    elsif column_types.compact.size >= 2 && column_types == ["string"] + (column_types.compact.size - 1).times.map { "numeric" }
      "bar"
    elsif column_types == ["string", "string", "numeric"]
      "bar2"
    elsif column_types == ["numeric", "numeric"]
      "scatter"
    end
  end
end

#column_typesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/blazer/result.rb', line 47

def column_types
  @column_types ||= begin
    columns.each_with_index.map do |k, i|
      v = (rows.find { |r| r[i] } || {})[i]
      if boom[k]
        "string"
      elsif v.is_a?(Numeric)
        "numeric"
      elsif v.is_a?(Time) || v.is_a?(Date)
        "time"
      elsif v.nil?
        nil
      else
        "string"
      end
    end
  end
end

#detect_anomalyObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/blazer/result.rb', line 125

def detect_anomaly
  anomaly = nil
  message = nil

  if rows.empty?
    message = "No data"
  else
    if chart_type == "line" || chart_type == "line2"
      series = []

      if chart_type == "line"
        columns[1..-1].each_with_index.each do |k, i|
          series << {name: k, data: rows.map{ |r| [r[0], r[i + 1]] }}
        end
      else
        rows.group_by { |r| v = r[1]; (boom[columns[1]] || {})[v.to_s] || v }.each_with_index.map do |(name, v), i|
          series << {name: name, data: v.map { |v2| [v2[0], v2[2]] }}
        end
      end

      current_series = nil
      begin
        anomalies = []
        series.each do |s|
          current_series = s[:name]
          anomalies << s[:name] if anomaly?(s[:data])
        end
        anomaly = anomalies.any?
        if anomaly
          if anomalies.size == 1
            message = "Anomaly detected in #{anomalies.first}"
          else
            message = "Anomalies detected in #{anomalies.to_sentence}"
          end
        else
          message = "No anomalies detected"
        end
      rescue => e
        message = "#{current_series}: #{e.message}"
        raise e if Rails.env.development?
      end
    else
      message = "Bad format"
    end
  end

  [anomaly, message]
end

#forecastObject

TODO cache it? don’t want to put result data (even hashed version) into cache without developer opt-in



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/blazer/result.rb', line 91

def forecast
  count = (@rows.size * 0.25).round.clamp(30, 365)

  case Blazer.forecasting
  when "prophet"
    require "prophet"
    forecast = Prophet.forecast(@rows.to_h, count: count)
  else
    require "trend"
    forecast = Trend.forecast(@rows.to_h, count: count)
  end

  # round integers
  if @rows[0][1].is_a?(Integer)
    forecast = forecast.map { |k, v| [k, v.round] }.to_h
  end

  @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

#forecastable?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/blazer/result.rb', line 84

def forecastable?
  @forecastable ||= Blazer.forecasting && column_types == ["time", "numeric"] && @rows.size >= 10
end

#timed_out?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/blazer/result.rb', line 14

def timed_out?
  error == Blazer::TIMEOUT_MESSAGE
end