Cleans ApiDefender statistics
This commit is contained in:
parent
3ad56638b7
commit
773c1701ad
1
Gemfile
1
Gemfile
|
@ -125,4 +125,5 @@ group :test do
|
||||||
gem 'mock_redis', '~> 0.11'
|
gem 'mock_redis', '~> 0.11'
|
||||||
gem 'rake'
|
gem 'rake'
|
||||||
gem 'test_after_commit'
|
gem 'test_after_commit'
|
||||||
|
gem 'timecop'
|
||||||
end
|
end
|
||||||
|
|
|
@ -432,6 +432,7 @@ GEM
|
||||||
time_diff (0.3.0)
|
time_diff (0.3.0)
|
||||||
activesupport
|
activesupport
|
||||||
i18n
|
i18n
|
||||||
|
timecop (0.7.1)
|
||||||
tmp_cache (0.1.1)
|
tmp_cache (0.1.1)
|
||||||
treetop (1.4.15)
|
treetop (1.4.15)
|
||||||
polyglot
|
polyglot
|
||||||
|
@ -546,6 +547,7 @@ DEPENDENCIES
|
||||||
therubyracer (~> 0.12.1)
|
therubyracer (~> 0.12.1)
|
||||||
therubyrhino (~> 1.73.1)
|
therubyrhino (~> 1.73.1)
|
||||||
time_diff
|
time_diff
|
||||||
|
timecop
|
||||||
uglifier (~> 2.4)
|
uglifier (~> 2.4)
|
||||||
underscore-rails
|
underscore-rails
|
||||||
whenever (~> 0.9.0)
|
whenever (~> 0.9.0)
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
class CleanApiDefenderStatisticsJob
|
||||||
|
@queue = :clone_build
|
||||||
|
|
||||||
|
def self.perform
|
||||||
|
deadline = Date.today - 1.month
|
||||||
|
Redis.current.keys.select do |key|
|
||||||
|
next if key !~ /^throttle:/
|
||||||
|
# See: https://github.com/datagraph/rack-throttle/blob/master/lib/rack/throttle/daily.rb#L41
|
||||||
|
# Formats:
|
||||||
|
# 'throttle:uname:%Y-%m-%dT%H', 'throttle:uname:%Y-%m-%d'
|
||||||
|
# example: "throttle:proyvind:2014-01-25T20", "throttle:proyvind:2014-01-25"
|
||||||
|
date = key.gsub(/.*:/, '').gsub(/T.*/, '')
|
||||||
|
Redis.current.del(key) if Date.parse(date) < deadline
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -18,3 +18,10 @@ build_lists_queues_monitoring:
|
||||||
class: 'BuildListsQueuesMonitoringJob'
|
class: 'BuildListsQueuesMonitoringJob'
|
||||||
queue: hook
|
queue: hook
|
||||||
description: 'Monitoring for "user/mass-build" queues'
|
description: 'Monitoring for "user/mass-build" queues'
|
||||||
|
|
||||||
|
clean_api_defender_statistics:
|
||||||
|
every:
|
||||||
|
- '1d'
|
||||||
|
class: 'CleanApiDefenderStatisticsJob'
|
||||||
|
queue: clone_build
|
||||||
|
description: 'Cleans ApiDefender statistics'
|
|
@ -0,0 +1,31 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe CleanApiDefenderStatisticsJob do
|
||||||
|
|
||||||
|
it 'ensures that not raises error' do
|
||||||
|
lambda do
|
||||||
|
CleanApiDefenderStatisticsJob.perform
|
||||||
|
end.should_not raise_exception
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'ensures that cleans only old statistics' do
|
||||||
|
today = Date.today
|
||||||
|
Timecop.freeze(today) do
|
||||||
|
key1 = "throttle:key1:#{today.strftime('%Y-%m-%d')}"
|
||||||
|
key2 = "throttle:key2:#{today.strftime('%Y-%m-%d')}T01"
|
||||||
|
key3 = "throttle:key1:#{(today - 32.days).strftime('%Y-%m-%d')}"
|
||||||
|
key4 = "throttle:key2:#{(today - 32.days).strftime('%Y-%m-%d')}T01"
|
||||||
|
key5 = "other:throttle:key:#{(today - 32.days).strftime('%Y-%m-%d')}"
|
||||||
|
@redis_instance.set key1, 1
|
||||||
|
@redis_instance.set key2, 1
|
||||||
|
@redis_instance.set key3, 1
|
||||||
|
@redis_instance.set key4, 1
|
||||||
|
@redis_instance.set key5, 1
|
||||||
|
|
||||||
|
CleanApiDefenderStatisticsJob.perform
|
||||||
|
@redis_instance.keys.should include(key1, key2, key5)
|
||||||
|
@redis_instance.keys.should_not include(key3, key4)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue