Skip to content

Instantly share code, notes, and snippets.

@john-diaz
Last active April 8, 2022 21:19
Show Gist options
  • Save john-diaz/779de223e337571e5a80fb0724879c8f to your computer and use it in GitHub Desktop.
Save john-diaz/779de223e337571e5a80fb0724879c8f to your computer and use it in GitHub Desktop.
prod = true
begin
def getRand(x, y)
rand(x...y)
end
user_types = ["User (Free)", "User (Pro)", "User (Premium)", "User (Light)", "Visitor", "Admin"]
usage = [
{
source: "okta_sso_session_token_issued",
source_description: "Okta SSO",
absolute_value_template: "{0} sessions",
absolute_value: getRand(30, 250),
relative_value: getRand(0.1, 0.9).round(1),
absolute_usage_score_weight: getRand(1.0, 5.0).round(1),
},
{
source: "gmail_emails_received",
source_description: "Emails received",
absolute_value_template: "{0} emails sent",
absolute_value: getRand(5, 80),
relative_value: getRand(0.1, 0.9).round(1),
absolute_usage_score_weight: getRand(1.0, 5.0).round(1),
},
{
source: "browser_traffic",
source_description: "Browser extension traffic",
absolute_value_template: "{0} hours",
absolute_value: getRand(1, 34),
relative_value: getRand(0.1, 0.9).round(1),
absolute_usage_score_weight: getRand(1.0, 5.0).round(1),
},
]
def get_usage_report(usage, number_of_usage)
usage_length = usage.length
return (1...usage_length).to_a.sample(number_of_usage).map { |m| usage[m] }
end
def get_user_types_report(user_types, num_of_types)
return (1...user_types.length).to_a.sample(num_of_types).map { |m| user_types[m] }
end
monthly_data = []
daily_data = []
puts "Generating data"
date = 8.months.ago.beginning_of_month
while date <= Date.today.beginning_of_month
puts "Generating data for month #{date}"
day = date.beginning_of_month
while day <= date.end_of_month
puts " - Generating data for day #{day}"
workspace.workspace_vendors.includes(users: { workspace_member: [:user_profile] }).each do |workspace_vendor|
puts " - Generating data for software #{workspace_vendor.name} (#{workspace_vendor.users.count} users)"
workspace_vendor.users.each do |user|
next if rand(1..10) > 8
member = user.workspace_member
# puts " - #{member.name}"
if day == date.beginning_of_month
# puts " - Generating monthly..."
monthly_data.append({
vendor_name: workspace_vendor.name,
vendor_id: workspace_vendor.vid,
user_id: member.id,
user_name: member.user_profile.name,
pictureEndpoint: member.user_profile.picture_endpoint,
user_email: member.user_profile.email,
user_types: get_user_types_report(user_types, rand(1...user_types.length)),
total_usage_score: rand(0.1...1.0).round(1),
active_usage_threshold: rand(0.1...1.0).round(1),
usage: get_usage_report(usage, rand(1...3)),
month: date.strftime("%B"),
year: date.year,
objectID: SecureRandom.uuid,
})
end
puts " - Generating day (#{day.day})..."
daily_data.append({
vendor_name: workspace_vendor.name,
vendor_id: workspace_vendor.vid,
user_id: member.id,
user_name: member.user_profile.name,
pictureEndpoint: member.user_profile.picture_endpoint,
user_email: member.user_profile.email,
user_types: get_user_types_report(user_types, rand(1...user_types.length)),
total_usage_score: rand(0.1...1.0).round(1),
active_usage_threshold: rand(0.1...1.0).round(1),
usage: get_usage_report(usage, rand(1...3)),
month: date.strftime("%B"),
year: date.year,
day: day.day,
objectID: SecureRandom.uuid,
})
end
end
day += 1.day
end
date += 1.month
end
puts "SAVING>>>"
if prod
index_client = $algolia_client.init_index("prod_136_workspace_vendor_monthly_usage_reports")
else
index_client = $algolia_client.init_index("env_johndiaz_1_workspace_vendor_monthly_usage_reports")
end
index_client.clear_objects
index_client.save_objects(monthly_data)
index_client.set_settings({
searchableAttributes: ["user_name", "user_email"],
attributesForFaceting: [
"vendor_id",
"user_id",
"usage.source_description",
"year",
"month",
"user_types",
],
})
if prod
index_client = $algolia_client.init_index("prod_136_workspace_vendor_daily_usage_reports")
else
index_client = $algolia_client.init_index("env_johndiaz_1_workspace_vendor_daily_usage_reports")
end
index_client.clear_objects
index_client.save_objects(daily_data)
index_client.set_settings({
searchableAttributes: ["user_name", "user_email"],
attributesForFaceting: [
"vendor_id",
"user_id",
"usage.source_description",
"year",
"month",
"user_types",
],
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment