Module: Preservation::Report::Transfer

Defined in:
lib/preservation/report/transfer.rb

Overview

Transfer reporting

Class Method Summary collapse

Class Method Details

.complete_countInteger

Count of complete transfers

Returns:

  • (Integer)


52
53
54
55
56
57
58
# File 'lib/preservation/report/transfer.rb', line 52

def self.complete_count
  query = 'SELECT count(*) FROM unit WHERE status = ?'

  status_to_find = 'COMPLETE'
  db.results_as_hash = true
  db.get_first_value( query, [status_to_find] )
end

.currentHash

Current transfer

Returns:

  • (Hash)


38
39
40
41
42
43
44
45
46
47
# File 'lib/preservation/report/transfer.rb', line 38

def self.current
  query = "SELECT id, uuid, hex(path) as hex_path, unit_type, status, microservice, current FROM unit WHERE current = 1"

  o = {}
  db.results_as_hash = true
  db.execute( query ) do |row|
    o = row_to_hash(row)
  end
  o
end

.exceptionHash

Compilation of statistics and data, with focus on exceptions

Returns:

  • (Hash)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/preservation/report/transfer.rb', line 103

def self.exception
  incomplete_result = status(status_to_find: 'COMPLETE', status_presence: false)
  failed_result = status(status_to_find: 'FAILED', status_presence: true)
  pending_result = pending
  current_result = current
  complete_count_result = complete_count
  report = {}
  report['pending'] = {}
  report['pending']['count'] = pending_result.count
  report['pending']['data'] = pending_result if !pending_result.empty?
  report['current'] = current_result if !current_result.empty?
  report['failed'] = {}
  report['failed']['count'] = failed_result.count
  report['failed']['data'] = failed_result if !failed_result.empty?
  report['incomplete'] = {}
  report['incomplete']['count'] = incomplete_result.count
  report['incomplete']['data'] = incomplete_result if !incomplete_result.empty?
  report['complete'] = {}
  report['complete']['count'] = complete_count_result if complete_count_result
  report
end

.in_db?(path_to_find) ⇒ Boolean

Is it in database?

Parameters:

  • path_to_find (String)

    directory name within ingest path

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/preservation/report/transfer.rb', line 128

def self.in_db?(path_to_find)
  in_db = false

  # Get path out of DB as a hex string
  query = 'SELECT hex(path) FROM unit'

  # Archivematica stores path as BLOB, so need to convert path to Hex, to search for it
  # and use hex function in DB query
  db.execute( query ) do |row|
    bin_path = Preservation::Conversion.hex_to_bin row[0]
    if bin_path === path_to_find
      in_db = true
    end
  end

  in_db
end

.pendingHash

Pending transfers

Returns:

  • (Hash)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/preservation/report/transfer.rb', line 63

def self.pending
  entries = Dir.entries Preservation.ingest_path
  dirs = []
  entries.each do |entry|
    path = File.join(Preservation.ingest_path, entry)
    if File.directory?(path)
      dirs << entry unless File.basename(path).start_with?('.')
    end
  end
  a = []
  # For each directory, if it isn't in the db, add it to list
  dirs.each do |dir|
    if in_db?(dir) === false
      o = {}
      o['path'] = dir
      o['path_timestamp'] = File.mtime File.join(Preservation.ingest_path, dir)
      a << o
    end
  end
  a
end

.pending?(path_to_find) ⇒ Boolean

Is there a pending transfer with this path?

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
# File 'lib/preservation/report/transfer.rb', line 88

def self.pending?(path_to_find)
  is_pending = false
  pending.each do |i|
    if i['path'] == path_to_find
      is_pending = true
      break
    end
  end
  is_pending
end

.preserved?(path_to_find) ⇒ Boolean

Has preservation been done?

Parameters:

  • path_to_find (String)

    directory name within ingest path

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/preservation/report/transfer.rb', line 149

def self.preserved?(path_to_find)
  preserved = false

  # 'ingest' value in unit_type and 'COMPLETE' value in status DB fields
  # indicates completed
  unit_type_to_find = 'ingest'
  status_to_find = 'COMPLETE'

  # Get path out of DB as a hex string for completed ingests
  query = 'SELECT hex(path) FROM unit WHERE unit_type = ? AND status = ?'

  # Archivematica stores path as BLOB, so need to convert path to Hex, to search for it
  # and use hex function in DB query
  db.execute( query, [ unit_type_to_find, status_to_find ] ) do |row|
    bin_path = Preservation::Conversion.hex_to_bin row[0]
    if bin_path === path_to_find
      preserved = true
    end
  end

  preserved
end

.status(status_to_find: nil, status_presence: true) ⇒ Array<Hash>

Transfers based on presence (or not) of a particular status

Parameters:

  • status_to_find (String) (defaults to: nil)
  • status_presence (Boolean) (defaults to: true)

Returns:

  • (Array<Hash>)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/preservation/report/transfer.rb', line 14

def self.status(status_to_find: nil, status_presence: true)
  if status_presence === true
    status_presence = '='
  else
    status_presence = '<>'
  end

  query = "SELECT id, uuid, hex(path) as hex_path, unit_type, status, microservice, current FROM unit WHERE status #{status_presence} ?"

  records = []
  db.results_as_hash = true
  db.execute( query, [ status_to_find ] ) do |row|
    bin_path = Preservation::Conversion.hex_to_bin row['hex_path']
    if !bin_path.nil? && !bin_path.empty?
      records << row_to_hash(row)
    end
  end

  records
end