Class: ChecksumAuditLog

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/checksum_audit_log.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_and_prune!(passed:, checked_uri:, file_set_id:, file_id:, expected_result:) ⇒ ChecksumAuditLog

Responsible for coordinating the creation (and possible pruning) of checksum log entries

Parameters:

  • passed (Boolean)
    • Did the fixity test pass?

  • checked_uri (#to_s)
    • Which URI did we check?

  • file_set_id (String)
    • The file set ID of the file we are checking

  • file_id (String)
    • The file ID that was checked

  • expected_result (String)
    • The expected checksum

Returns:

See Also:


36
37
38
39
40
41
42
# File 'app/models/checksum_audit_log.rb', line 36

def self.create_and_prune!(passed:, checked_uri:, file_set_id:, file_id:, expected_result:)
  log = create!(passed: passed, checked_uri: checked_uri, file_set_id: file_set_id, file_id: file_id, expected_result: expected_result)
  # A short-circuit. Given that .prune_history keeps all failing logs, there is no need to call prune history
  # unless the check passed.
  prune_history(file_set_id, checked_uri: checked_uri) if log.passed?
  log
end

.latest_checksObject

Only the latest rows for a given file_set_id/checked_uri pair. Uses a join, so you might have to be careful combining. You would normally combine this with other conditions, this alone will return LOTS of records.


10
11
12
13
14
15
16
17
18
19
# File 'app/models/checksum_audit_log.rb', line 10

def self.latest_checks
  # one crazy SQL trick to get the latest for each fileset/checked_uri combo
  # where there's no other self-join created_at greater -- only the greatest.
  joins("LEFT JOIN checksum_audit_logs c2 ON
          (checksum_audit_logs.file_set_id = c2.file_set_id AND
           checksum_audit_logs.checked_uri = c2.checked_uri AND
           checksum_audit_logs.created_at < c2.created_at)")
    .where("c2.id is NULL")
    .order("created_at desc, id desc")
end

.latest_for_file_set_id(file_set_id) ⇒ Object

From all ChecksumAuditLogs related to this file set, returns only the LATEST for each file_set_id/checked_uri pair.


23
24
25
# File 'app/models/checksum_audit_log.rb', line 23

def self.latest_for_file_set_id(file_set_id)
  latest_checks.where(file_set_id: file_set_id)
end

.logs_for(file_set_id, checked_uri:) ⇒ Object

All logs for a particular file or version in a give file set, sorted by date descending.


63
64
65
# File 'app/models/checksum_audit_log.rb', line 63

def self.logs_for(file_set_id, checked_uri:)
  ChecksumAuditLog.where(file_set_id: file_set_id, checked_uri: checked_uri).order('created_at desc, id desc')
end

.prune_history(file_set_id, checked_uri:) ⇒ Object

Prune old ChecksumAuditLog records. We keep only:

  • Latest check

  • failing checks

  • any checks immediately before or after a failing check, to provide context on known good dates surrounding failing.


49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/checksum_audit_log.rb', line 49

def self.prune_history(file_set_id, checked_uri:)
  all_logs = logs_for(file_set_id, checked_uri: checked_uri).reorder("created_at asc").to_a

  0.upto(all_logs.length - 2).each do |i|
    next if all_logs[i].failed?
    next if i.positive? && all_logs[i - 1].failed?
    next if all_logs[i + 1].failed?

    all_logs[i].destroy!
  end
end

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)

2
3
4
# File 'app/models/checksum_audit_log.rb', line 2

def failed?
  !passed?
end