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
- BATCH_SIZE =
1000- GROUPER_AUDIT_ENTRY =
'grouper_audit_entry'- GROUPER_AUDIT_TYPE =
'grouper_audit_type'- NUMBER_OF_DAYS =
1
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
writeonly
Sets the attribute batch_size.
-
#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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/jgrouper/audit_archiver.rb', line 39 def initialize @batch_size = BATCH_SIZE @config = {} @conn = nil @date = nil @directory = Dir.pwd @fh = nil @mappings = {} @number_of_days = NUMBER_OF_DAYS @skip_columns = [] @stop_date = nil @verbose = false yield self if block_given? self end |
Instance Attribute Details
#batch_size=(value) ⇒ Object (writeonly)
Sets the attribute batch_size
37 38 39 |
# File 'lib/jgrouper/audit_archiver.rb', line 37 def batch_size=(value) @batch_size = value end |
#directory=(value) ⇒ Object (writeonly)
Sets the attribute directory
37 38 39 |
# File 'lib/jgrouper/audit_archiver.rb', line 37 def directory=(value) @directory = value end |
#number_of_days=(value) ⇒ Object (writeonly)
Sets the attribute number_of_days
37 38 39 |
# File 'lib/jgrouper/audit_archiver.rb', line 37 def number_of_days=(value) @number_of_days = value end |
#verbose=(value) ⇒ Object (writeonly)
Sets the attribute verbose
37 38 39 |
# File 'lib/jgrouper/audit_archiver.rb', line 37 def verbose=(value) @verbose = value end |
Instance Method Details
#archive ⇒ Object
Archive oldest date from ‘grouper_audit_entry’ table to file.
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/jgrouper/audit_archiver.rb', line 58 def archive log 'starting ...' connect do mappings do last_date = Date.new 1.upto @number_of_days do @date = oldest_entry # Date of oldest entry if @date <= last_date warn "WARNING: attempt to re-archive #{ @date }, aborting ..." break end 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 # TODO Verify number of entries is greater than 0? num_entries = 0 entries = [] total_entries = entries_within_interval start_at, stop_at log "archiving #{ @date } (#{total_entries} entries) ..." filehandle @directory, "grouper-audit-entries-#{ @date }.csv" do # 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? entries << v if :id == k entry << k << v.to_s.gsub(/\n/, ', ') end entry = transform(entry) entry = entry.each_slice(2).reject { |slice| skip? slice.first }.collect { |slice| "#{slice.first}=#{slice.last}" } num_entries = num_entries + 1 @fh.puts CSV.generate_line( entry, col_sep: "\t" ) if entries.size > 0 && ( entries.size % @batch_size ) == 0 prune entries, num_entries, total_entries entries.clear @fh.fsync end yield entry if block_given? end rs.close stmt.close end prune(entries, num_entries, total_entries) unless entries.empty? log "archiving #{ @date } (#{num_entries}/#{total_entries} entries) - done" last_date = @date end end end log 'done' end |
#skip_columns=(columns) ⇒ Object
Array of column names to omit from archive.
133 134 135 |
# File 'lib/jgrouper/audit_archiver.rb', line 133 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.
140 141 142 |
# File 'lib/jgrouper/audit_archiver.rb', line 140 def stop_date=(date) @stop_date = Date.parse date end |