Skip to content

Instantly share code, notes, and snippets.

@myf9000
Created July 11, 2016 08:06
Show Gist options
  • Save myf9000/e0052c793daeb06099a543108779357b to your computer and use it in GitHub Desktop.
Save myf9000/e0052c793daeb06099a543108779357b to your computer and use it in GitHub Desktop.
namespace :test_for_stats do
desc "Create data for test displaying charts"
task create_data: :environment do
date = Time.now
year = date.strftime("%Y")
quarter = date.strftime("%Y")+"Q#{1 + ((date.month - 1) / 3)}"
month = date.strftime("%Y%m")
week = date.strftime("%Y%V")
yesterday = date.yesterday.strftime("%Y%m%d")
user = User.create(name: "user", email: "user@email.com", sso_id: "aaa")
user_second = User.create(name: "user2", email: "user2@email.com", sso_id: "bb")
10.times do |i|
Post.create(state: "published", tag_names: %w(tag iot vue rails),
slug: "posta#{i}", content: "aa", title: "post#{i}",
published_at: Time.zone.yesterday,
users: [user], excerpt: "a")
end
5.times do |i|
Post.create(state: "published", tag_names: %w(tag elixsir react),
slug: "article#{i}", content: "aa", title: "article#{i}",
published_at: Time.zone.yesterday,
users: [user_second], excerpt: "a")
end
Post.all.each_with_index do |post, index|
Comment.create(content: "comment", post_id: post.id, user_id: user.id,
created_at: Time.zone.yesterday - Random.rand(200))
if index % 2
Comment.create(content: "message", post_id: post.id, user_id: user_second.id,
created_at: Time.zone.yesterday - Random.rand(200))
end
if index % 3
Comment.create(content: "answer", post_id: post.id, user_id: user_second.id,
created_at: Time.zone.yesterday - Random.rand(200))
end
end
Post.all.each do |post|
REDIS.set "global:yearly:#{post.slug}", "{\"#{year}\":#{Random.rand(9..11)}}"
REDIS.set "global:quarterly:#{post.slug}", "{\"#{quarter}\":#{Random.rand(7..9)}}"
REDIS.set "global:monthly:#{post.slug}", "{\"#{month}\":#{Random.rand(5..7)}}"
REDIS.set "global:weekly:#{post.slug}", "{\"#{week}\":#{Random.rand(2..5)}}"
REDIS.set "global:daily:#{post.slug}", "{\"#{yesterday}\":#{Random.rand(0..2)}}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment