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