8784: Fix test for latest firefox.
[arvados.git] / apps / workbench / app / controllers / keep_disks_controller.rb
1 class KeepDisksController < ApplicationController
2   def create
3     defaults = { is_readable: true, is_writable: true }
4     @object = KeepDisk.new defaults.merge(params[:keep_disk] || {})
5     super
6   end
7
8   def index
9     # Retrieve cache age histogram info from logs.
10
11     # In the logs we expect to find it in an ordered list with entries
12     # of the form (mtime, disk proportion free).
13
14     # An entry of the form (1388747781, 0.52) means that if we deleted
15     # the oldest non-presisted blocks until we had 52% of the disk
16     # free, then all blocks with an mtime greater than 1388747781
17     # would be preserved.
18
19     # The chart we want to produce, will tell us how much of the disk
20     # will be free if we use a cache age of x days. Therefore we will
21     # produce output specifying the age, cache and persisted. age is
22     # specified in milliseconds. cache is the size of the cache if we
23     # delete all blocks older than age. persistent is the size of the
24     # persisted blocks. It is constant regardless of age, but it lets
25     # us show a stacked graph.
26
27     # Finally each entry in cache_age_histogram is a dictionary,
28     # because that's what our charting package wats.
29
30     @cache_age_histogram = []
31     @histogram_pretty_date = nil
32     histogram_log = Log.
33       filter([[:event_type, '=', 'block-age-free-space-histogram']]).
34       order(:created_at => :desc).
35       with_count('none').
36       limit(1)
37     histogram_log.each do |log_entry|
38       # We expect this block to only execute at most once since we
39       # specified limit(1)
40       @cache_age_histogram = log_entry['properties'][:histogram]
41       # Javascript wants dates in milliseconds.
42       histogram_date_ms = log_entry['event_at'].to_i * 1000
43       @histogram_pretty_date = log_entry['event_at'].strftime('%b %-d, %Y')
44
45       total_free_cache = @cache_age_histogram[-1][1]
46       persisted_storage = 1 - total_free_cache
47       @cache_age_histogram.map! { |x| {:age => histogram_date_ms - x[0]*1000,
48           :cache => total_free_cache - x[1],
49           :persisted => persisted_storage} }
50     end
51
52     # Do the regular control work needed.
53     super
54   end
55 end