Fix the fake data bugs & add progress bar (#1086)
This commit is contained in:
parent
a475d9b593
commit
36cee94b69
|
@ -1,16 +1,17 @@
|
|||
require 'faker'
|
||||
|
||||
class FakeDataGenerator
|
||||
def initialize(users_count, stories_count, categories_count)
|
||||
def initialize(users_count = 50, stories_count = 100, categories_count = 5)
|
||||
@users_count = users_count
|
||||
@stories_count = stories_count
|
||||
@categories_count = categories_count
|
||||
end
|
||||
|
||||
def generate
|
||||
# Users and Moderators
|
||||
print 'Users '
|
||||
users = []
|
||||
0.upto(@users_count).each do |i|
|
||||
print '.'
|
||||
mod = User.moderators.sample
|
||||
name = Faker::Name.name
|
||||
password = Faker::Internet.password
|
||||
|
@ -18,7 +19,7 @@ class FakeDataGenerator
|
|||
email: Faker::Internet.email(name: name),
|
||||
password: password,
|
||||
password_confirmation: password,
|
||||
username: Faker::Internet.user_name(specifier: name, separators: %w(_)),
|
||||
username: Faker::Internet.user_name(specifier: 5..40, separators: %w(_)),
|
||||
created_at: (User::NEW_USER_DAYS + 1).days.ago,
|
||||
karma: Random.rand(User::MIN_KARMA_TO_FLAG * 2),
|
||||
about: Faker::Lorem.sentence(word_count: 7),
|
||||
|
@ -34,20 +35,24 @@ class FakeDataGenerator
|
|||
users[i].disable_invite_by_user_for_reason!(mod, Faker::Lorem.sentence(word_count: 5))
|
||||
end
|
||||
end
|
||||
puts
|
||||
|
||||
# Categories
|
||||
print 'Categories '
|
||||
categories = []
|
||||
@categories_count.times do
|
||||
print '.'
|
||||
cat = Faker::Lorem.word.capitalize
|
||||
categories << Category.create!(category: cat) unless Category.find_by(category: cat)
|
||||
end
|
||||
puts
|
||||
|
||||
# Stories
|
||||
print 'Stories '
|
||||
stories = []
|
||||
@stories_count.times do |i|
|
||||
print '.'
|
||||
user = users[Random.rand(@users_count-1)]
|
||||
title = Faker::Lorem.sentence(word_count: 3)
|
||||
category = categories[Random.rand(@categories_count)]
|
||||
category = categories.sample
|
||||
tag_name = title.split(' ').first.downcase
|
||||
tag = Tag.find_by tag: tag_name
|
||||
tag ||= Tag.create! tag: tag_name, category: category
|
||||
|
@ -66,11 +71,11 @@ class FakeDataGenerator
|
|||
}
|
||||
stories << Story.create!(create_args)
|
||||
end
|
||||
puts
|
||||
|
||||
# User-deleted stories. The stories are created here and deleted after
|
||||
# adding comments and other interactions.
|
||||
# The stories are created here and deleted after adding comments and other interactions.
|
||||
deleted_stories = []
|
||||
(@stories_count / 10).times do
|
||||
(@stories_count / 10).times do |i|
|
||||
user = users[Random.rand(@users_count-1)]
|
||||
title = Faker::Lorem.sentence(word_count: 3)
|
||||
category = categories[Random.rand(@categories_count-1)]
|
||||
|
@ -84,23 +89,27 @@ class FakeDataGenerator
|
|||
url: url,
|
||||
description: Faker::Lorem.paragraphs(number: 1),
|
||||
tags_a: [tag.tag],
|
||||
is_expired: true,
|
||||
is_deleted: true,
|
||||
editor: user,
|
||||
}
|
||||
stories << Story.create!(create_args)
|
||||
deleted_stories << story if i % 30 == 0
|
||||
deleted_stories << stories.last if i % 30 == 0
|
||||
end
|
||||
|
||||
# User-saved stories
|
||||
print 'SavedStories '
|
||||
(@stories_count / 10).times do
|
||||
print '.'
|
||||
user = users[Random.rand(@users_count-1)]
|
||||
story = stories[Random.rand(@stories_count - 1)]
|
||||
SavedStory.save_story_for_user(story.id, user.id)
|
||||
end
|
||||
puts
|
||||
|
||||
# Comments
|
||||
print 'Comments '
|
||||
comments = []
|
||||
stories.each do |x|
|
||||
print '.'
|
||||
next unless x.accepting_comments?
|
||||
Random.rand(1..3).times do |i|
|
||||
create_args = {
|
||||
user: users[Random.rand(@users_count-1)],
|
||||
|
@ -121,9 +130,11 @@ class FakeDataGenerator
|
|||
end
|
||||
end
|
||||
end
|
||||
puts
|
||||
|
||||
# Comment Flags
|
||||
print 'Comment Flags '
|
||||
comments.each do |c|
|
||||
print '.'
|
||||
if Random.rand(100) > 95
|
||||
u = users[Random.rand(@users_count-1)]
|
||||
Vote.vote_thusly_on_story_or_comment_for_user_because(
|
||||
|
@ -135,9 +146,11 @@ class FakeDataGenerator
|
|||
)
|
||||
end
|
||||
end
|
||||
puts
|
||||
|
||||
# Story Flags
|
||||
print 'Story Flags '
|
||||
stories.each do |s|
|
||||
print '.'
|
||||
if Random.rand(100) > 95
|
||||
u = users[Random.rand(@users_count-1)]
|
||||
Vote.vote_thusly_on_story_or_comment_for_user_because(
|
||||
|
@ -149,10 +162,12 @@ class FakeDataGenerator
|
|||
)
|
||||
end
|
||||
end
|
||||
puts
|
||||
|
||||
# Hats
|
||||
print 'Hats '
|
||||
hats = []
|
||||
(@users_count / 2).times do |i|
|
||||
print '.'
|
||||
suffixes = ["Developer", "Founder", "User", "Contributor", "Creator"]
|
||||
hat_wearer = users[i + 1]
|
||||
create_args = {
|
||||
|
@ -167,21 +182,25 @@ class FakeDataGenerator
|
|||
end
|
||||
hats << hat
|
||||
end
|
||||
puts
|
||||
|
||||
### Moderation ###
|
||||
|
||||
# Comments (delete/undelete)
|
||||
print 'Comments (delete/undelete) '
|
||||
comments.each_with_index do |comment, i|
|
||||
print '.'
|
||||
comment_mod = User.moderators.sample
|
||||
if i % 7 == 0
|
||||
comment.delete_for_user(comment_mod, Faker::Lorem.paragraphs(number: 1))
|
||||
comment.undelete_for_user(comment_mod) if i.even?
|
||||
end
|
||||
end
|
||||
puts
|
||||
|
||||
# delete stories by submitter/mods
|
||||
print 'Delete stories by submitter/mods '
|
||||
deleted_stories.each do |story|
|
||||
if i.even?
|
||||
print '.'
|
||||
if story.id.even?
|
||||
story.editor = story.user
|
||||
else
|
||||
story.editor = User.moderators.sample
|
||||
|
@ -189,25 +208,31 @@ class FakeDataGenerator
|
|||
end
|
||||
story.update(is_deleted: true)
|
||||
end
|
||||
puts
|
||||
|
||||
# Hats
|
||||
print 'Doff hats '
|
||||
2.times do
|
||||
print '.'
|
||||
hat = hats[Random.rand(hats.length - 1)]
|
||||
hat.doff_by_user_with_reason(User.moderators.sample,
|
||||
Faker::Lorem.sentence(word_count: 5))
|
||||
end
|
||||
puts
|
||||
|
||||
# Users, don't delete inactive-user or test
|
||||
print 'Ban Users ' # don't delete inactive-user or test
|
||||
User.where('id > 2').sample(2).each_with_index do |user, i|
|
||||
print '.'
|
||||
user.ban_by_user_for_reason!(User.moderators.sample,
|
||||
Faker::Lorem.sentence(word_count: 5))
|
||||
if i.even?
|
||||
user.unban_by_user!(User.moderators.sample)
|
||||
end
|
||||
end
|
||||
puts
|
||||
|
||||
# Merging Stories
|
||||
print 'Merge Stories '
|
||||
5.times do
|
||||
print '.'
|
||||
story = stories[Random.rand(stories.length - 1)]
|
||||
second_story = stories[Random.rand(stories.length - 1)]
|
||||
while second_story == story
|
||||
|
@ -218,24 +243,29 @@ class FakeDataGenerator
|
|||
story.moderation_reason = Faker::Lorem.sentence(word_count: 5)
|
||||
story.save
|
||||
end
|
||||
puts
|
||||
|
||||
# Editing Stories
|
||||
print 'Editing Stories '
|
||||
5.times do
|
||||
print '.'
|
||||
story = stories[Random.rand(stories.length - 1)]
|
||||
story.title = Faker::Lorem.sentence(word_count: 4)
|
||||
story.editing_from_suggestions = true
|
||||
story.moderation_reason = Faker::Lorem.sentence(word_count: 5)
|
||||
story.save
|
||||
end
|
||||
puts
|
||||
|
||||
# Deleting stories
|
||||
print 'Deleting stories '
|
||||
5.times do
|
||||
print '.'
|
||||
story = stories[Random.rand(stories.length - 1)]
|
||||
story.is_expired = true
|
||||
story.is_deleted = true
|
||||
story.editing_from_suggestions = true
|
||||
story.moderation_reason = Faker::Lorem.sentence(word_count: 5)
|
||||
story.save
|
||||
end
|
||||
puts
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue