8 def to_s; sprintf '%.2f', self; end
10 infinite? ? "unknown" : super
17 sprintf "%d:%02d:%02d", i / 3600, (i / 60) % 60, i % 60
27 opts = Trollop::options do
28 version "sup-tweak-labels (sup #{Redwood::VERSION})"
30 Batch modification of message state for messages already in the index.
33 sup-tweak-labels [options] <source>*
35 where <source>* is zero or more source URIs. Supported source URI schemes can
36 be seen by running "sup-add --help".
40 opt :add, "One or more labels (comma-separated) to add to every message from the specified sources", :type => String
41 opt :remove, "One or more labels (comma-separated) to remove from every message from the specified sources, if those labels are present", :type => String
47 opt :verbose, "Print message ids as they're processed."
48 opt :all_sources, "Scan over all sources.", :short => :none
49 opt :dry_run, "Don't actually modify the index. Probably only useful with --verbose.", :short => "-n"
50 opt :version, "Show version information", :short => :none
53 add_labels = (opts[:add] || "").split(",").map { |l| l.intern }.uniq
54 remove_labels = (opts[:remove] || "").split(",").map { |l| l.intern }.uniq
56 Trollop::die "nothing to do: no labels to add or remove" if add_labels.empty? && remove_labels.empty?
60 index = Redwood::Index.new
68 index.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
71 Trollop::die "nothing to do: no sources" if source_ids.empty?
73 query = "+(" + source_ids.map { |id| "source_id:#{id}" }.join(" ") + ")"
75 ## if all we're doing is removing labels, we can further restrict the
76 ## query to only messages with those labels
77 query += " +(" + remove_labels.map { |l| "label:#{l}" }.join(" ") + ")"
80 results = index.ferret.search query, :limit => :all
81 num_total = results.total_hits
83 $stderr.puts "Found #{num_total} documents across #{source_ids.length} sources. Scanning..."
85 num_changed = num_scanned = 0
86 last_info_time = start_time = Time.now
87 results.hits.each do |hit|
91 m = index.build_message id
92 old_labels = m.labels.clone
94 m.labels += add_labels
95 m.labels -= remove_labels
96 m.labels = m.labels.uniq
98 unless m.labels.sort_by { |s| s.to_s } == old_labels.sort_by { |s| s.to_s }
100 puts "#{m.id}: {#{old_labels.join ','}} => {#{m.labels.join ','}}" if opts[:verbose]
101 index.sync_message m unless opts[:dry_run]
104 if Time.now - last_info_time > 60
105 last_info_time = Time.now
106 elapsed = last_info_time - start_time
107 pctdone = 100.0 * num_scanned.to_f / num_total.to_f
108 remaining = (100.0 - pctdone) * (elapsed.to_f / pctdone)
109 $stderr.puts "## #{num_scanned} (#{pctdone}%) read; #{elapsed.to_time_s} elapsed; #{remaining.to_time_s} remaining"
112 $stderr.puts "Scanned #{num_scanned} / #{num_total} messages and changed #{num_changed}."
114 unless num_changed == 0
115 $stderr.puts "Optimizing index..."
116 index.ferret.optimize unless opts[:dry_run]
119 rescue Exception => e
120 File.open("sup-exception-log.txt", "w") { |f| f.puts e.backtrace }