1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/services/footprint_service.rb

79 lines
3.0 KiB
Ruby
Raw Normal View History

2019-09-18 15:09:14 +02:00
# frozen_string_literal: true
2021-04-16 10:34:02 +02:00
require 'integrity/checksum'
2019-09-18 15:09:14 +02:00
# Provides helper methods to compute footprints
class FootprintService
class << self
# Compute the footprint
2021-04-16 16:03:10 +02:00
# @param klass {Class} a class inheriting from Footprintable
# @param item {Footprintable} an instance of the provided class
def compute_footprint(klass, item)
Integrity::Checksum.text(FootprintService.footprint_data(klass, item))
end
2020-07-21 19:25:21 +02:00
# Return the original data string used to compute the footprint
2021-04-16 16:03:10 +02:00
# @param klass {Class} a class inheriting from Footprintable
# @param item {Footprintable} an instance of the provided class
# @param array {Boolean} if true, the result is return on the form of an array, otherwise a concatenated string is returned
def footprint_data(klass, item, array: false)
raise TypeError unless item.is_a? klass
2019-09-18 15:09:14 +02:00
2021-04-16 16:03:10 +02:00
sort_on = item.sort_on_field
previous = klass.where("#{sort_on} < ?", item[sort_on])
.order("#{sort_on} DESC")
.limit(1)
2019-09-18 15:09:14 +02:00
columns = FootprintService.footprint_columns(klass)
columns = columns.map do |c|
comparable(item[c])
rescue ActiveModel::MissingAttributeError
nil
end
2019-09-18 15:09:14 +02:00
res = columns.push(previous.first ? previous.first.footprint : '')
2021-04-16 16:03:10 +02:00
array ? res.map(&:to_s) : res.join.to_s
end
2020-07-21 19:25:21 +02:00
# Return an ordered array of the columns used in the footprint computation
2021-04-16 16:03:10 +02:00
# @param klass {Class} a class inheriting from Footprintable
def footprint_columns(klass)
%w[id].concat(klass.columns.map(&:name).delete_if { |c| %w[id footprint updated_at].concat(klass.columns_out_of_footprint).include? c }.sort)
end
# Logs a debugging message to help finding why a footprint is invalid
2021-04-16 16:03:10 +02:00
# @param klass {Class} a class inheriting from Footprintable
# @param item {Footprintable} an instance of the provided class
def debug_footprint(klass, item)
columns = FootprintService.footprint_columns(klass)
2021-04-16 16:03:10 +02:00
current = FootprintService.footprint_data(klass, item, array: true)
saved = FootprintDebug.find_by(footprint: item.footprint, klass: klass.name)
2021-05-31 12:19:28 +02:00
return saved if Rails.env.test?
2021-05-17 17:04:52 +02:00
if saved.nil?
puts "Debug data not found for #{klass} [ id: #{item.id} ]"
else
puts "Debug footprint for #{klass} [ id: #{item.id} ]"
puts '-----------------------------------------'
puts "columns: [ #{columns.join(', ')} ]"
puts "current: #{current}"
puts " saved: #{saved.format_data(item.id)}"
puts '-----------------------------------------'
item.footprint_children.map(&:debug_footprint)
end
2021-05-31 12:19:28 +02:00
others = FootprintDebug.where('klass = ? AND data LIKE ? AND id != ?', klass, "#{item.id}%", saved&.id)
puts "other possible matches IDs: #{others.map(&:id)}"
puts '-----------------------------------------'
end
private
# Return a comparable value for jsonb fields (with keys ordered alphabetically)
def comparable(value)
2021-04-26 11:40:26 +02:00
return value unless value.is_a? Hash
2020-07-21 19:25:21 +02:00
value.sort.to_h
end
2019-09-18 15:09:14 +02:00
end
end