Class: JGrouper::AuditArchiver
- Inherits:
-
Object
- Object
- JGrouper::AuditArchiver
- Defined in:
- lib/jgrouper/audit_archiver.rb
Overview
JGrouper::AuditArchiver - Archive Grouper audit log entries
Usage
require 'jgrouper'
require 'jgrouper/audit_archiver'
JGrouper::AuditArchiver.new do |archiver|
# Write output to this directory
archiver.directory = '/some/directory'
# Archive this many days of audit log entries
archive.number_of_days = 5
# Omit these columns from the archive
archiver.skip_columns = %w( duration_microseconds hibernate_version_number query_count )
# Do not archive from this date onward.
archiver.stop_date = '2013-01-01'
# Perform archive, optionally yielding audit log entries being archived if block given
archiver.archive { |entry| ... audit log entry being archived ... }
end
Constant Summary collapse
- GROUPER_AUDIT_ENTRY =
'grouper_audit_entry'- GROUPER_AUDIT_TYPE =
'grouper_audit_type'
Instance Attribute Summary collapse
-
#directory ⇒ Object
writeonly
Sets the attribute directory.
-
#number_of_days ⇒ Object
writeonly
Sets the attribute number_of_days.
-
#verbose ⇒ Object
writeonly
Sets the attribute verbose.
Instance Method Summary collapse
-
#archive ⇒ Object
Archive oldest date from ‘grouper_audit_entry’ table to file.
-
#initialize {|_self| ... } ⇒ AuditArchiver
constructor
A new instance of AuditArchiver.
-
#skip_columns=(columns) ⇒ Object
Array of column names to omit from archive.
-
#stop_date=(date) ⇒ Object
Do not archive from this date onward.
Constructor Details
#initialize {|_self| ... } ⇒ AuditArchiver
Returns a new instance of AuditArchiver.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jgrouper/audit_archiver.rb', line 37 def initialize @config = {} @conn = nil @date = nil @directory = Dir.pwd @fh = nil @mappings = {} @number_of_days = 1 @skip_columns = [] @stop_date = nil @verbose = false yield self if block_given? self end |
Instance Attribute Details
#directory=(value) ⇒ Object (writeonly)
Sets the attribute directory
35 36 37 |
# File 'lib/jgrouper/audit_archiver.rb', line 35 def directory=(value) @directory = value end |
#number_of_days=(value) ⇒ Object (writeonly)
Sets the attribute number_of_days
35 36 37 |
# File 'lib/jgrouper/audit_archiver.rb', line 35 def number_of_days=(value) @number_of_days = value end |
#verbose=(value) ⇒ Object (writeonly)
Sets the attribute verbose
35 36 37 |
# File 'lib/jgrouper/audit_archiver.rb', line 35 def verbose=(value) @verbose = value end |
Instance Method Details
#archive ⇒ Object
Archive oldest date from ‘grouper_audit_entry’ table to file.
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/jgrouper/audit_archiver.rb', line 55 def archive log 'starting ...' connect do mappings do 1.upto @number_of_days do @date = oldest_entry # Date of oldest entry break if stop? start_at = time_to_microseconds @date.to_time # @date start-of-day stop_at = time_to_microseconds ( @date + 1 ).to_time - 1 # @date end-of-day entries = [] filehandle @directory, "grouper-audit-entries-#{ @date }.csv" do log "archiving #{ @date } ..." # TODO Extract! # TODO Use prepared statement when I get JRuby, JDBC & Oracle to better cooperate on BigDecimal-ish data types qry = "SELECT * FROM #{GROUPER_AUDIT_ENTRY} WHERE created_on BETWEEN #{start_at} AND #{stop_at} ORDER BY created_on" stmt = @conn.create_statement rs = stmt.execute_query qry md = rs. num_cols = md.column_count while rs.next entry = [] 1.upto(num_cols) do |n| k = md.column_name(n).downcase.to_sym v = rs.get_object(n) next if v.nil? entry << k << v.to_s.gsub(/\n/, ', ') end entry = transform(entry) entries << entry.each_slice(2).reject { |slice| skip? slice.first }.collect { |slice| "#{slice.first}=#{slice.last}" } @fh.puts CSV.generate_line( entries.last, col_sep: "\t" ) yield entries.last if block_given? end rs.close stmt.close log "archiving #{ @date } (#{entries.size} entries) - done" end prune(entries.size, start_at, stop_at) unless entries.empty? end end end log 'done' end |
#skip_columns=(columns) ⇒ Object
Array of column names to omit from archive.
111 112 113 |
# File 'lib/jgrouper/audit_archiver.rb', line 111 def skip_columns=(columns) @skip_columns = columns.collect { |c| c.downcase.to_sym } end |
#stop_date=(date) ⇒ Object
Do not archive from this date onward.
118 119 120 |
# File 'lib/jgrouper/audit_archiver.rb', line 118 def stop_date=(date) @stop_date = Date.parse date end |