Class: Ossert::Classifiers::Growing::ClassifiersInitializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ossert/classifiers/growing.rb

Constant Summary collapse

CLASSIFIERS_METRICS =
{
  agility_total: ->(project) { project.agility.total.metrics_to_hash },
  agility_last_year: ->(project) { project.agility.quarters.last_year_as_hash },
  community_total: ->(project) { project.community.total.metrics_to_hash },
  community_last_year: ->(project) { project.community.quarters.last_year_as_hash }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grouped_projects = nil) ⇒ ClassifiersInitializer

Returns a new instance of ClassifiersInitializer.



109
110
111
112
# File 'lib/ossert/classifiers/growing.rb', line 109

def initialize(grouped_projects = nil)
  @projects = grouped_projects
  @classifiers = []
end

Instance Attribute Details

#classifiersObject (readonly)

Returns the value of attribute classifiers.



107
108
109
# File 'lib/ossert/classifiers/growing.rb', line 107

def classifiers
  @classifiers
end

Class Method Details

.load_or_createObject



99
100
101
102
103
104
105
# File 'lib/ossert/classifiers/growing.rb', line 99

def self.load_or_create
  if ::Classifier.actual?
    new.load
  else
    new(Project.projects_by_reference)
  end
end

Instance Method Details

#loadObject



114
115
116
117
118
119
120
# File 'lib/ossert/classifiers/growing.rb', line 114

def load
  @classifiers = {}
  CLASSIFIERS_METRICS.keys.each do |section|
    @classifiers[section] = JSON.parse(::Classifier[section.to_s].reference_values)
  end
  self
end

#merge_metrics(storage, metrics) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/ossert/classifiers/growing.rb', line 122

def merge_metrics(storage, metrics)
  metrics.each do |metric, value|
    storage.store(
      metric.to_s,
      storage[metric.to_s].to_a << value.to_f
    )
  end
  storage
end

#new_classifiersObject



143
144
145
# File 'lib/ossert/classifiers/growing.rb', line 143

def new_classifiers
  CLASSIFIERS_METRICS.keys.map { |type| [type, {}] }.to_h
end

#runObject



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ossert/classifiers/growing.rb', line 147

def run
  return if @classifiers.present?

  @classifiers = GRADES.each_with_object(new_classifiers) do |grade, classifiers|
    @projects[grade].each do |project|
      CLASSIFIERS_METRICS.each do |type, metrics|
        classifiers[type][grade] = merge_metrics(classifiers[type][grade].to_h, metrics.call(project))
      end
    end
  end

  save
end

#saveObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/ossert/classifiers/growing.rb', line 132

def save
  ::Classifier.dataset.delete

  @classifiers.each do |section, reference_values|
    ::Classifier.create(
      section: section.to_s,
      reference_values: JSON.generate(reference_values)
    )
  end
end