Class: HawkEye::Graph::DateBinVariable

Inherits:
Object
  • Object
show all
Includes:
DateBinHelpers
Defined in:
app/services/hawk_eye/graph/date_bin_variable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DateBinVariable

Returns a new instance of DateBinVariable.



5
6
7
# File 'app/services/hawk_eye/graph/date_bin_variable.rb', line 5

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'app/services/hawk_eye/graph/date_bin_variable.rb', line 3

def options
  @options
end

Instance Method Details

#aggregation_methodObject



27
28
29
# File 'app/services/hawk_eye/graph/date_bin_variable.rb', line 27

def aggregation_method
  :sum # sum, count, ...
end

#data_for_clientObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/hawk_eye/graph/date_bin_variable.rb', line 31

def data_for_client
  found_facets = {}
  facets.each{|f| found_facets[f] = Set.new } # my guess is that this is more efficient than a complex default 1 vs N issue
  result = {
    variables: variables.map{|variable| {name: variable, label: WorkflowInstance.human_attribute_name(variable)} },
    payload: {},
    bin_type: bin_type,
    date_column: {
      name: date_column,
      label: WorkflowInstance.human_attribute_name(date_column)
    },
    aggregation_method: aggregation_method
  }

  base_stats = WorkflowInstance.group(date_column).group(*facets)
  base_stats = base_stats.where(date_column => date_boundaries) if date_boundaries
  base_stats = base_stats.where(baan_number: 222)

  variables.each do |variable|
    variable_stats = base_stats.public_send(aggregation_method, variable)

    grouped_by_bin = variable_stats.group_by do |(date, _facet_values), stat|
      if date
        date = date.public_send("beginning_of_#{bin_type}")
        if result[:min_date]
          result[:min_date] = date if date < result[:min_date]
        else
          result[:min_date] = date
        end
        if result[:max_date]
          result[:max_date] = date if date > result[:max_date]
        else
          result[:max_date] = date
        end
        date
      else
        'no_date'
      end
    end

    grouped_by_bin.each do |bin, group_result|
      result[:payload][bin] ||= []
      group_result.each do |facet_values, stat|
        facet_values.shift # first argument is the unprocessed date
        facet_values.each.with_index{|fv, i| found_facets[facets[i]] << fv} # add to found results
        if existing_data = result[:payload][bin].find{|p| p[:facets] == facet_values }
          existing_data[variable] = stat
        else
          result[:payload][bin] << {facets: facet_values, variable => stat}
        end
      end
    end
    result[:facets] = found_facets.map{|facet_name, facet_values| {name: facet_name, label: WorkflowInstance.human_attribute_name(facet_name), values: facet_values}}
  end
  #result[:min_date] = result[:min_date].beginning_of_year
  #result[:max_date] = result[:max_date].end_of_year.beginning_of_month
  result
end

#date_boundariesObject



13
14
15
16
17
# File 'app/services/hawk_eye/graph/date_bin_variable.rb', line 13

def date_boundaries
  3.years.ago.to_date..1.year.from_now.to_date
  Time.now.beginning_of_year.to_date..Time.now.end_of_year.to_date
  nil
end

#date_columnObject



19
20
21
# File 'app/services/hawk_eye/graph/date_bin_variable.rb', line 19

def date_column
  :plan_date
end

#facetsObject



9
10
11
# File 'app/services/hawk_eye/graph/date_bin_variable.rb', line 9

def facets
  @facets ||= options[:facets] || []
end

#variablesObject



23
24
25
# File 'app/services/hawk_eye/graph/date_bin_variable.rb', line 23

def variables
  @variables ||= Array.wrap(options[:variables])
end