-
Notifications
You must be signed in to change notification settings - Fork 183
Add: Prune reports that match a specific status #311
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,13 +13,34 @@ namespace :reports do | |
| } | ||
| known_units = units.keys.join(',') | ||
|
|
||
| statuses = [ | ||
| 'changed', | ||
| 'unchanged', | ||
| 'pending', | ||
| 'failed' | ||
| ] | ||
| known_statuses = statuses.join(',') | ||
|
|
||
| conditions = { | ||
| 'is' => '=', | ||
| 'not' => '!=' | ||
| } | ||
| known_conditions = conditions.keys.join(',') | ||
|
|
||
| usage = %{ | ||
| EXAMPLE: | ||
| # Prune records upto 1 month old: | ||
| rake reports:prune upto=1 unit=mon | ||
|
|
||
| # Prune records upto 1 month old and with status 'unchanged' | ||
| rake reports:prune upto=1 unit=mon condition=is status=unchanged | ||
|
|
||
| UNITS: | ||
| Valid units of time are: #{known_units} | ||
|
|
||
| STATUS & CONDITION: | ||
| Valid status values are: #{known_statuses} | ||
| Valid condition values are: #{known_conditions} | ||
| }.strip | ||
|
|
||
| unless ENV['upto'] || ENV['unit'] | ||
|
|
@@ -43,30 +64,54 @@ UNITS: | |
| errors << "You must specify the unit of time, .e.g.: unit={#{known_units}}" \ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This trailing \ appears to be a mistake, please remove in your PR to help clean up the existing code. |
||
| end | ||
|
|
||
| if ( ENV['status'] && ! ENV['condition'] ) || ( ENV['condition'] && ! ENV['status'] ) | ||
| errors << "You must specify status AND condition!"\ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The trailing \ isn't needed. (I see it above in the time units line, that looks like a mistake.) |
||
| end | ||
|
|
||
| if status = ENV['status'] | ||
| unless statuses.include?(status) | ||
| errors << "I don't know that status. Valid statuses are: #{known_statuses}"\ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trailing \ |
||
| end | ||
| else | ||
| status = "" | ||
| end | ||
|
|
||
| if condition = ENV['condition'] | ||
| unless conditions.has_key?(condition) | ||
| errors << "I don't know that condition. Valid conditionss are: #{known_conditions}"\ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra 's' on conditions. |
||
| end | ||
| else | ||
| condition = "!=" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this |
||
| end | ||
|
|
||
| if errors.present? | ||
| $stderr.puts errors.map { |error| "ERROR: #{error}" } | ||
| $stderr.puts | ||
| $stderr.puts usage | ||
| exit 1 | ||
| end | ||
|
|
||
| if condition != "!=" | ||
| condition = conditions[condition] | ||
| end | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the |
||
| # Thin query for nodes with reports that may be pruned. | ||
| # By selecting the 'id' column only, it does not eager load all of the | ||
| # nodes and definitely not all of the reports, making this much faster. | ||
| cutoff = Time.now.gmtime - (upto * units[unit].to_i) | ||
| affected_nodes = Node.select('DISTINCT nodes.id') \ | ||
| .joins('LEFT OUTER JOIN reports ON reports.node_id = nodes.id') \ | ||
| .where(['reports.time < ?', cutoff]) | ||
| .where("reports.time < \"#{cutoff}\" and reports.status #{condition} \"#{status}\"") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though it's interpolating on .where("reports.time < ? AND reports.status #{condition} ?", cutoff, status) |
||
| deletion_count = affected_nodes.count | ||
| puts "#{Time.now.to_s(:db)}: Deleting reports before #{cutoff} for #{deletion_count} nodes" | ||
| puts "#{Time.now.to_s(:db)}: Deleting reports before #{cutoff} and with report status #{condition} \"#{status}\" for #{deletion_count} nodes" | ||
|
|
||
| pbar = ProgressBar.new('Deleting', deletion_count, STDOUT) | ||
| deleted_count = 0 | ||
|
|
||
| begin | ||
| affected_nodes.each do |node| | ||
| node.reload # Become a complete object (the query above returns shalow objects with 'id' only) | ||
| deleted_count += node.prune_reports(cutoff) | ||
| deleted_count += node.prune_reports(cutoff, condition, status) | ||
| pbar.inc | ||
| end | ||
| rescue SignalException | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed this one, same change as in the other file:
.where("reports.time < ? AND reports.status #{condition} ?", cutoff, status)