Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'riak'
- require 'json'
- require 'pp'
- class Time
- def ms_from_epoc
- m = 1000
- (to_i * m) + (subsec * m).to_i
- end
- end
- class Event < Hash
- def initialize time, record_type, record_id, record_attrs={}
- self['created_at'] = time.ms_from_epoc.to_s(16)
- self['type'] = self.class.name
- self['nonce'] = rand(1024**3).to_s(16)
- self['record'] = {
- 'type' => record_type,
- 'id' => record_id,
- 'attributes' => record_attrs
- }
- end
- def key
- vals = values_at('nonce', 'created_at', 'type')
- record_vals = self['record'].values_at('type', 'id')
- (vals + record_vals).join('-').downcase
- end
- def to_s
- e.key + "\n" + e.to_json
- end
- end
- class Imported < Event; end
- class Created < Event; end
- class Updated < Event; end
- class Deleted < Event; end
- DAY = 1440
- MONTH = DAY * 30
- YEAR = MONTH * 12
- def random_device_events
- type = %w(Ups Pdu Switch Firewall).sample
- id = rand 10_000
- time = Time.now - (YEAR * rand)
- [Created, Updated, Deleted].map do |k|
- time += MONTH * rand
- k.new time, type, id, :volt_rating => 400.0, :amp_rating => 630.0
- end
- end
- client = Riak::Client.new
- events = client.bucket 'events'
- 100.times do
- random_device_events.each do |e|
- ro = Riak::RObject.new events, e.key
- ro.data = e
- ro.content_type = 'application/json'
- ro.store
- end
- end
Add Comment
Please, Sign In to add comment