├── .ruby-gemset ├── .ruby-version ├── Gemfile ├── mock.rb ├── accounts.rb ├── Gemfile.lock ├── LICENSE ├── README.md ├── preact_account_mock.rb └── preact_model_mock.rb /.ruby-gemset: -------------------------------------------------------------------------------- 1 | ruby-mock -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | ruby-2.1.5 -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gem 'preact' 4 | gem 'demode' 5 | gem 'rest-client' -------------------------------------------------------------------------------- /mock.rb: -------------------------------------------------------------------------------- 1 | require './preact_model_mock.rb' 2 | 3 | p = PreactModelMock.new(ARGV[0], ARGV[1]) 4 | p.generate_batch -------------------------------------------------------------------------------- /accounts.rb: -------------------------------------------------------------------------------- 1 | require './preact_account_mock.rb' 2 | 3 | p = PreactAccountMock.new(ARGV[0], ARGV[1]) 4 | p.update_accounts -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: http://rubygems.org/ 3 | specs: 4 | celluloid (0.16.0) 5 | timers (~> 4.0.0) 6 | demode (0.0.3) 7 | domain_name (0.5.24) 8 | unf (>= 0.0.5, < 1.0.0) 9 | hitimes (1.2.2) 10 | http-cookie (1.0.2) 11 | domain_name (~> 0.5) 12 | mime-types (2.6.1) 13 | multi_json (1.11.0) 14 | netrc (0.10.3) 15 | preact (1.1.1) 16 | multi_json (~> 1.0) 17 | rest-client 18 | sucker_punch (~> 1.0) 19 | rest-client (1.8.0) 20 | http-cookie (>= 1.0.2, < 2.0) 21 | mime-types (>= 1.16, < 3.0) 22 | netrc (~> 0.7) 23 | sucker_punch (1.5.0) 24 | celluloid (~> 0.16.0) 25 | timers (4.0.1) 26 | hitimes 27 | unf (0.1.4) 28 | unf_ext 29 | unf_ext (0.0.7.1) 30 | 31 | PLATFORMS 32 | ruby 33 | 34 | DEPENDENCIES 35 | demode 36 | preact 37 | rest-client 38 | 39 | BUNDLED WITH 40 | 1.10.6 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Preact, Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # preact-mock-data 2 | Example of how to push a bunch of fake generated data to Preact 3 | 4 | This will create ~50 dummy accounts with 1-10 people in each account. The same account ids and person ids will be used each time, though there is randomness about how many accounts are created each day (to simulate real-world behavior). 5 | 6 | For each account 10-100 events will randomly be created and assigned randomly to the people associated with the account. 7 | 8 | Takes api code/secret on execution and one or more integers of how many days ago to log the events from. 9 | 10 | ``` 11 | $ bundle 12 | $ ruby mock.rb code secret 13 | ``` 14 | 15 | ## Advanced 16 | 17 | If you wish to customize the names of the generated events, you should edit the preactmodelmock.rb before running it to update the event_names array to include names relevant to your business or intended usage. 18 | 19 | The default is 20 | 21 | ``` 22 | @event_names = [ 23 | "logged-in", 24 | "logged-out", 25 | "forgot-password", 26 | "changed-password", 27 | "updated-profile", 28 | "updated-payment", 29 | "created-document", 30 | "uploaded-media", 31 | "modified-dashboard", 32 | "viewed-dashboard", 33 | "purchased-item", 34 | "changed-login", 35 | "created-profile", 36 | "downgraded", 37 | "upgraded", 38 | "signed-up" 39 | ] 40 | 41 | ``` 42 | 43 | ## License 44 | 45 | Copyright (c) 2015 Preact, Inc. 46 | 47 | Permission is hereby granted, free of charge, to any person obtaining 48 | a copy of this software and associated documentation files (the 49 | "Software"), to deal in the Software without restriction, including 50 | without limitation the rights to use, copy, modify, merge, publish, 51 | distribute, sublicense, and/or sell copies of the Software, and to 52 | permit persons to whom the Software is furnished to do so, subject to 53 | the following conditions: 54 | 55 | The above copyright notice and this permission notice shall be 56 | included in all copies or substantial portions of the Software. 57 | 58 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 59 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 60 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 61 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 62 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 63 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 64 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 65 | -------------------------------------------------------------------------------- /preact_account_mock.rb: -------------------------------------------------------------------------------- 1 | require 'preact' 2 | require 'rest-client' 3 | 4 | class PreactAccountMock 5 | 6 | def initialize(code, secret, configuration = {}, defaults= {}) 7 | @code = code 8 | @secret = secret 9 | ::Preact.configure do |config| 10 | config.code = code 11 | config.secret = secret 12 | config.request_timeout = nil 13 | config.scheme = configuration[:scheme] if configuration[:scheme] 14 | config.host = configuration[:host] if configuration[:host] 15 | end 16 | 17 | @account_manager_names = [ 18 | "John Smith", 19 | "James Bridger", 20 | "Sam Jackson", 21 | "Tom Wilson", 22 | "Billy McCool" 23 | ] 24 | 25 | @account_owner_names = [ 26 | "George Washington", 27 | "John Adams", 28 | "John Kennedy", 29 | "Wilson Phillips" 30 | ] 31 | 32 | @account_templates = [ 33 | [4, { license_status: ["Active", "Active", "Active", "Cancelled"], 34 | license_mrr: [9, 99, 99, 499, 499, 999], 35 | license_type: ["Annual"], 36 | account_manager_name: @account_manager_names, 37 | account_owner_name: @account_owner_names, 38 | trial_end: 12.times.map{ |i| ago(i, MONTHS) }, 39 | license_renewal: 52.times.map{ |i| from_now(i, WEEKS) } 40 | }], 41 | [4, { license_status: ["Active", "Active", "Active", "Cancelled"], 42 | license_mrr: [9, 99, 99, 499, 499, 999], 43 | license_type: ["Monthly"], 44 | account_manager_name: @account_manager_names, 45 | account_owner_name: @account_owner_names, 46 | trial_end: 12.times.map{ |i| ago(i, MONTHS) }, 47 | license_renewal: 30.times.map{ |i| from_now(i, DAYS) } 48 | }], 49 | [1, { license_status: "Trial", 50 | trial_end: 14.times.map{ |i| from_now(i, DAYS) } 51 | }] 52 | ] 53 | 54 | if defaults && defaults.is_a?(Hash) 55 | defaults.each do |k,v| 56 | instance_variable_set("@#{k}", v) unless v.nil? 57 | end 58 | end 59 | end 60 | 61 | DAYS = 3600*24 62 | WEEKS = DAYS * 7 63 | MONTHS = WEEKS * 4 64 | 65 | def from_now(i, incr) 66 | Time.now + i * incr 67 | end 68 | 69 | def ago(i, incr) 70 | Time.now - i * incr 71 | end 72 | 73 | def update_accounts 74 | account_report = JSON.parse(RestClient.get "https://#{@code}:#{@secret}@secure.preact.com/api/v2/reports/53514b8f4443ae11f3000001/list?limit=5000", {:accept => :json}) 75 | 76 | accounts = account_report['results'].map do |a| 77 | account = { id: a['external_identifier'] }.merge(get_random_account_properties) 78 | ::Preact.log_account_event({ name: '___update' }, account) 79 | end 80 | end 81 | 82 | def get_random_account_properties 83 | templates = [] 84 | @account_templates.each{ |i, v| i.times.each{ templates << v } } 85 | 86 | template = templates[rand(templates.length)] 87 | 88 | props = Hash[template.map do |k,v| 89 | [k, v.is_a?(Array) ? v[rand(v.length)] : v] 90 | end] 91 | end 92 | 93 | end 94 | 95 | module Preact 96 | class << self 97 | protected 98 | def send_log(person, event=nil) 99 | psn = person.nil? ? nil : person.to_hash 100 | evt = event.nil? ? nil : event.to_hash 101 | 102 | print "." 103 | ::Preact.client.create_event(psn, evt) 104 | end 105 | end 106 | end -------------------------------------------------------------------------------- /preact_model_mock.rb: -------------------------------------------------------------------------------- 1 | require 'preact' 2 | require 'demode' 3 | 4 | class PreactModelMock 5 | 6 | def initialize(code, secret, configuration = {}, defaults= {}) 7 | ::Preact.configure do |config| 8 | config.code = code 9 | config.secret = secret 10 | config.request_timeout = nil 11 | config.scheme = configuration[:scheme] if configuration[:scheme] 12 | config.host = configuration[:host] if configuration[:host] 13 | end 14 | 15 | @event_names = [ 16 | "logged-in", 17 | "logged-out", 18 | "forgot-password", 19 | "changed-password", 20 | "updated-profile", 21 | "updated-payment", 22 | "created-document", 23 | "uploaded-media", 24 | "modified-dashboard", 25 | "viewed-dashboard", 26 | "purchased-item", 27 | "changed-login", 28 | "created-profile", 29 | "downgraded", 30 | "upgraded", 31 | "signed-up" 32 | ] 33 | @account_num = 50 34 | @person_num = 10 35 | @event_multiplier = 2 36 | 37 | if defaults && defaults.is_a?(Hash) 38 | defaults.each do |k,v| 39 | instance_variable_set("@#{k}", v) unless v.nil? 40 | end 41 | end 42 | end 43 | 44 | def self.generate_accounts(account_num) 45 | accounts = [] 46 | account_num.times do |n| 47 | account = { 48 | name: Demode::Generator.company_name(n), 49 | id: n, 50 | people: [] 51 | } 52 | accounts << account 53 | end 54 | accounts 55 | end 56 | 57 | def self.generate_people(person_num, multiplier = 1) 58 | people = [] 59 | person_num.times do |n| 60 | person = { 61 | name: Demode::Generator.name((multiplier*10) + n*900), 62 | email: Demode::Generator.email((multiplier*10) + n*900), 63 | } 64 | people << person 65 | end 66 | people 67 | end 68 | 69 | def self.log_events(logs) 70 | logs.flatten!(1) 71 | logs.sort! {|x,y| x[:event][:timestamp]<=>y[:event][:timestamp]} 72 | logs.each do |log| 73 | ::Preact.log_event(log[:person], log[:event], log[:account]) 74 | end 75 | puts "RUNNING, IGNORE ALL ERRORS AFTER THIS" 76 | end 77 | 78 | def generate_random_account_events(account, event_names_count) 79 | # To not have multiple events at the exact same time 80 | time_offset = 0 81 | logs = [] 82 | #Will set event_max to at least 10*multiplier 83 | event_max = rand((10 * @event_multiplier)..(100 * @event_multiplier)) 84 | event_max_init_half = event_max/2 + 1 85 | while event_max > 1 do 86 | account[:people].each do |person| 87 | # no person should have more than half of the potential events for an account 88 | person_events = rand([event_max, event_max_init_half].min) 89 | person_events.times do 90 | event = { 91 | name: @event_names[rand(event_names_count)], 92 | # Sets timestamp to a random subset of days, offset by time_offset 93 | timestamp: Time.now.to_i - ((rand(@event_multiplier * 2)) * 24 * 3600) + time_offset 94 | } 95 | logs << { person: person, event: event, account: account } 96 | time_offset += 1 97 | end 98 | event_max = event_max - person_events 99 | end 100 | end 101 | logs 102 | end 103 | 104 | def generate_batch 105 | accounts = [] 106 | logs = [] 107 | accounts = PreactModelMock.generate_accounts(@account_num) 108 | event_names_count = @event_names.count 109 | accounts.each do |account| 110 | #Will vary the person number per account the same every execution 111 | account[:people] = PreactModelMock.generate_people(((account[:id] % @person_num) + 1), account[:id]) 112 | logs << generate_random_account_events(account, event_names_count) 113 | end 114 | PreactModelMock.log_events(logs) 115 | end 116 | end 117 | 118 | module Preact 119 | class << self 120 | protected 121 | def send_log(person, event=nil) 122 | psn = person.nil? ? nil : person.to_hash 123 | evt = event.nil? ? nil : event.to_hash 124 | 125 | print "." 126 | ::Preact.client.create_event(psn, evt) 127 | end 128 | end 129 | end 130 | # Preact.log_event( 131 | # { :email => "gooley@preact.com", 132 | # :name => "Christopher Gooley", 133 | # :created_at => 1367259292, 134 | # :uid => "gooley" 135 | # }, { 136 | # :name => "registered" 137 | # } 138 | # ) 139 | --------------------------------------------------------------------------------