Class: ExportStatsJob::StatsExporter
- Inherits:
-
Object
- Object
- ExportStatsJob::StatsExporter
- Defined in:
- lib/palava_machine/jobs/export_stats_job.rb
Constant Summary collapse
- STATS_NAMESPACE =
"store:stats"
Instance Method Summary collapse
- #get_and_delete_from_redis(ns, time) ⇒ Object
- #import_timestamps!(ns) ⇒ Object
-
#initialize(redis_address, mongo_address) ⇒ StatsExporter
constructor
A new instance of StatsExporter.
-
#prune_timestamps! ⇒ Object
remove timestamps which are not closed (+ grace time).
- #store_in_mongo! ⇒ Object
Constructor Details
#initialize(redis_address, mongo_address) ⇒ StatsExporter
Returns a new instance of StatsExporter.
9 10 11 12 13 |
# File 'lib/palava_machine/jobs/export_stats_job.rb', line 9 def initialize(redis_address, mongo_address) @redis = Redis.new(host: 'localhost', port: 6379) @mongo = Mongo::MongoClient.new#(mongo_address) @times = Set.new end |
Instance Method Details
#get_and_delete_from_redis(ns, time) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/palava_machine/jobs/export_stats_job.rb', line 42 def get_and_delete_from_redis(ns, time) key = "#{STATS_NAMESPACE}:#{ns}:#{time}" data = @redis.hgetall(key) || {} @redis.del(key) data end |
#import_timestamps!(ns) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/palava_machine/jobs/export_stats_job.rb', line 15 def (ns) redis_pattern = "#{STATS_NAMESPACE}:#{ns}:*" offset = redis_pattern.size - 1 @times.merge @redis.keys(redis_pattern).map{ |key| key[offset..-1].to_i } end |
#prune_timestamps! ⇒ Object
remove timestamps which are not closed (+ grace time)
24 25 26 27 28 |
# File 'lib/palava_machine/jobs/export_stats_job.rb', line 24 def limit = Time.now.utc.to_i - 3660 @times.select!{ |time| time < limit } puts "Transfering #{@times.length} timespans" end |
#store_in_mongo! ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/palava_machine/jobs/export_stats_job.rb', line 30 def store_in_mongo! collection = @mongo.db("plv_stats").collection("rtc") @times.each { |time| collection.insert( "c_at" => time, "connection_time" => get_and_delete_from_redis("connection_time", time), "room_peaks" => get_and_delete_from_redis("room_peaks", time), ) } end |