Merge pull request #372 from abf/rosa-build:clean-api-defender-statistics

Clean ApiDefender statistics
This commit is contained in:
avm 2014-04-07 17:43:28 +04:00
commit 62e7c9a5e5
5 changed files with 59 additions and 1 deletions

View File

@ -125,4 +125,5 @@ group :test do
gem 'mock_redis', '~> 0.11'
gem 'rake'
gem 'test_after_commit'
gem 'timecop'
end

View File

@ -432,6 +432,7 @@ GEM
time_diff (0.3.0)
activesupport
i18n
timecop (0.7.1)
tmp_cache (0.1.1)
treetop (1.4.15)
polyglot
@ -546,6 +547,7 @@ DEPENDENCIES
therubyracer (~> 0.12.1)
therubyrhino (~> 1.73.1)
time_diff
timecop
uglifier (~> 2.4)
underscore-rails
whenever (~> 0.9.0)

View File

@ -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:uname1:2014-01-25T20", "throttle:uname1:2014-01-25"
date = key.gsub(/.*:/, '').gsub(/T.*/, '')
Redis.current.del(key) if Date.parse(date) < deadline
end
end
end

View File

@ -17,4 +17,11 @@ build_lists_queues_monitoring:
- '1m'
class: 'BuildListsQueuesMonitoringJob'
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'

View File

@ -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