Skip to content

Commit

Permalink
Add display of currently cached values
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlopain committed Sep 29, 2023
1 parent dbb733b commit 727531c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/logical/scraper/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def fetch_api_identifier
def self.cache(method_name, expires_in)
raise ArgumentError, "#{method_name} must have arity == 0" unless instance_method(method_name).arity == 0

@_cached_methods ||= []
@_cached_methods << method_name

alias_method "#{method_name}_old", method_name
define_method(method_name) do
key = self.class.cache_key(method_name)
Expand All @@ -69,6 +72,10 @@ def self.cache_key(method_name)
"#{name}.#{method_name}/#{config_checksum}"
end

def self.cached_methods
@_cached_methods || []
end

protected

def end_reached
Expand Down
4 changes: 4 additions & 0 deletions app/logical/sites/manual_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def missing_config_keys
[]
end

def cached_values
{}
end

def manually_disabled?
false
end
Expand Down
9 changes: 9 additions & 0 deletions app/logical/sites/scraper_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def missing_config_keys
@scraper.required_config_keys.select { |key| Config.send(key).blank? }
end

def cached_values
@scraper.cached_methods.filter_map do |method|
key = @scraper.cache_key(method)
next unless Rails.cache.exist?(key)

[method, Rails.cache.fetch(key)]
end.to_h
end

def manually_disabled?
Config.send("#{enum_value}_disabled?")
end
Expand Down
10 changes: 10 additions & 0 deletions app/views/stats/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<th>Submission Count</th>
<th>File Count</th>
<th>Status</th>
<th>Cache</th>
</tr>
</thead>
<tbody>
Expand All @@ -27,6 +28,15 @@
Enabled
<% end %>
</td>
<td>
<% cached = definition.cached_values %>
<% if cached.any? %>
<details>
<summary><%= cached.size %> <%= "value".pluralize(cached.size) %></summary>
<pre><%= cached.to_json %></pre>
</details>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Expand Down

0 comments on commit 727531c

Please sign in to comment.