Guest User

Untitled

a guest
Feb 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. require 'rubygems'
  2. require 'riak'
  3. require 'json'
  4. require 'pp'
  5.  
  6. class Time
  7.  
  8. def ms_from_epoc
  9. m = 1000
  10. (to_i * m) + (subsec * m).to_i
  11. end
  12.  
  13. end
  14.  
  15. class Event < Hash
  16.  
  17. def initialize time, record_type, record_id, record_attrs={}
  18. self['created_at'] = time.ms_from_epoc.to_s(16)
  19. self['type'] = self.class.name
  20. self['nonce'] = rand(1024**3).to_s(16)
  21.  
  22. self['record'] = {
  23. 'type' => record_type,
  24. 'id' => record_id,
  25. 'attributes' => record_attrs
  26. }
  27.  
  28. end
  29.  
  30. def key
  31. vals = values_at('nonce', 'created_at', 'type')
  32. record_vals = self['record'].values_at('type', 'id')
  33. (vals + record_vals).join('-').downcase
  34. end
  35.  
  36. def to_s
  37. e.key + "\n" + e.to_json
  38. end
  39.  
  40. end
  41.  
  42. class Imported < Event; end
  43. class Created < Event; end
  44. class Updated < Event; end
  45. class Deleted < Event; end
  46.  
  47. DAY = 1440
  48. MONTH = DAY * 30
  49. YEAR = MONTH * 12
  50.  
  51. def random_device_events
  52. type = %w(Ups Pdu Switch Firewall).sample
  53. id = rand 10_000
  54. time = Time.now - (YEAR * rand)
  55.  
  56. [Created, Updated, Deleted].map do |k|
  57. time += MONTH * rand
  58. k.new time, type, id, :volt_rating => 400.0, :amp_rating => 630.0
  59. end
  60.  
  61. end
  62.  
  63. client = Riak::Client.new
  64. events = client.bucket 'events'
  65.  
  66. 100.times do
  67.  
  68. random_device_events.each do |e|
  69. ro = Riak::RObject.new events, e.key
  70. ro.data = e
  71. ro.content_type = 'application/json'
  72. ro.store
  73. end
  74.  
  75. end
Add Comment
Please, Sign In to add comment