2012-09-04 20:26:45 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2012-08-28 23:08:38 +02:00
|
|
|
from django.utils.timezone import utc
|
2012-08-28 18:45:10 +02:00
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
2012-09-05 18:38:35 +02:00
|
|
|
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, \
|
2012-09-21 16:40:46 +02:00
|
|
|
Subscription, Huddle, get_huddle, Realm, UserMessage, get_user_profile_by_id, \
|
2012-09-27 19:58:42 +02:00
|
|
|
create_user, do_send_zephyr, create_user_if_needed, create_class_if_needed
|
2012-09-14 18:31:11 +02:00
|
|
|
from zephyr.lib.parallel import run_parallel
|
|
|
|
from django.db import transaction
|
2012-09-21 16:10:36 +02:00
|
|
|
from django.conf import settings
|
2012-09-21 23:54:23 +02:00
|
|
|
from zephyr import mit_subs_list
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-09-27 19:58:42 +02:00
|
|
|
import simplejson
|
2012-08-28 18:45:10 +02:00
|
|
|
import datetime
|
|
|
|
import random
|
2012-09-21 16:10:36 +02:00
|
|
|
import hashlib
|
2012-09-04 20:26:45 +02:00
|
|
|
from optparse import make_option
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-09-21 00:03:43 +02:00
|
|
|
def create_users(name_list):
|
|
|
|
for name, email in name_list:
|
2012-09-21 16:40:46 +02:00
|
|
|
(short_name, domain) = email.split("@")
|
|
|
|
password = short_name
|
2012-09-21 16:10:36 +02:00
|
|
|
if User.objects.filter(email=email):
|
2012-09-07 17:54:00 +02:00
|
|
|
# We're trying to create the same user twice!
|
|
|
|
raise
|
2012-09-21 00:03:43 +02:00
|
|
|
realm = Realm.objects.get(domain=domain)
|
2012-09-21 16:40:46 +02:00
|
|
|
create_user(email, password, realm, name, short_name)
|
2012-09-07 17:54:00 +02:00
|
|
|
|
|
|
|
def create_classes(class_list, realm):
|
|
|
|
for name in class_list:
|
|
|
|
if ZephyrClass.objects.filter(name=name, realm=realm):
|
|
|
|
# We're trying to create the same zephyr class twice!
|
|
|
|
raise
|
2012-09-19 18:54:57 +02:00
|
|
|
ZephyrClass.create(name, realm)
|
2012-09-07 17:54:00 +02:00
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
class Command(BaseCommand):
|
2012-08-28 18:45:10 +02:00
|
|
|
help = "Populate a test database"
|
|
|
|
|
2012-09-04 20:26:45 +02:00
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('-n', '--num-zephyrs',
|
|
|
|
dest='num_zephyrs',
|
|
|
|
type='int',
|
2012-09-14 18:31:11 +02:00
|
|
|
default=600,
|
2012-09-04 20:26:45 +02:00
|
|
|
help='The number of zephyrs to create.'),
|
2012-09-20 06:19:37 +02:00
|
|
|
make_option('--extra-users',
|
|
|
|
dest='extra_users',
|
|
|
|
type='int',
|
|
|
|
default=0,
|
|
|
|
help='The number of extra users to create'),
|
2012-09-05 18:38:35 +02:00
|
|
|
make_option('--huddles',
|
|
|
|
dest='num_huddles',
|
|
|
|
type='int',
|
|
|
|
default=3,
|
|
|
|
help='The number of huddles to create.'),
|
|
|
|
make_option('--personals',
|
|
|
|
dest='num_personals',
|
|
|
|
type='int',
|
|
|
|
default=6,
|
|
|
|
help='The number of personal pairs to create.'),
|
2012-09-14 22:52:09 +02:00
|
|
|
make_option('--threads',
|
|
|
|
dest='threads',
|
|
|
|
type='int',
|
|
|
|
default=10,
|
|
|
|
help='The number of threads to use.'),
|
2012-09-05 18:38:35 +02:00
|
|
|
make_option('--percent-huddles',
|
|
|
|
dest='percent_huddles',
|
|
|
|
type='float',
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be huddles.'),
|
|
|
|
make_option('--percent-personals',
|
|
|
|
dest='percent_personals',
|
|
|
|
type='float',
|
|
|
|
default=15,
|
|
|
|
help='The percent of messages to be personals.'),
|
2012-09-05 19:38:48 +02:00
|
|
|
make_option('--stickyness',
|
|
|
|
dest='stickyness',
|
|
|
|
type='float',
|
|
|
|
default=20,
|
|
|
|
help='The percent of messages to repeat recent folks.'),
|
2012-09-10 20:38:29 +02:00
|
|
|
make_option('--nodelete',
|
|
|
|
action="store_false",
|
|
|
|
default=True,
|
|
|
|
dest='delete',
|
|
|
|
help='Whether to delete all the existing messages.'),
|
2012-09-27 19:58:42 +02:00
|
|
|
make_option('--replay-old-zephyrs',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
dest='replay_old_zephyrs',
|
|
|
|
help='Whether to replace the log of old messages.'),
|
2012-09-04 20:26:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, **options):
|
2012-09-05 18:38:35 +02:00
|
|
|
if options["percent_huddles"] + options["percent_personals"] > 100:
|
|
|
|
self.stderr.write("Error! More than 100% of messages allocated.\n")
|
|
|
|
return
|
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
class_list = ["Verona", "Denmark", "Scotland", "Venice", "Rome"]
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
|
|
|
for klass in [Zephyr, ZephyrClass, UserProfile, User, Recipient,
|
|
|
|
Realm, Subscription, Huddle, UserMessage]:
|
|
|
|
klass.objects.all().delete()
|
2012-09-05 21:49:56 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
# Create a test realm
|
2012-09-21 00:03:43 +02:00
|
|
|
humbug_realm = Realm(domain="humbughq.com")
|
|
|
|
humbug_realm.save()
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
# Create test Users (UserProfiles are automatically created,
|
|
|
|
# as are subscriptions to the ability to receive personals).
|
2012-09-21 00:03:43 +02:00
|
|
|
names = [("Othello, the Moor of Venice", "othello@humbughq.com"), ("Iago", "iago@humbughq.com"),
|
|
|
|
("Prospero from The Tempest", "prospero@humbughq.com"),
|
|
|
|
("Cordelia Lear", "cordelia@humbughq.com"), ("King Hamlet", "hamlet@humbughq.com")]
|
2012-09-20 17:22:39 +02:00
|
|
|
for i in xrange(options["extra_users"]):
|
2012-09-20 06:19:37 +02:00
|
|
|
names.append(('Extra User %d' % (i,), 'extrauser%d' % (i,)))
|
|
|
|
|
2012-09-21 00:03:43 +02:00
|
|
|
create_users(names)
|
2012-09-10 20:38:29 +02:00
|
|
|
|
|
|
|
# Create public classes.
|
2012-09-21 00:03:43 +02:00
|
|
|
create_classes(class_list, humbug_realm)
|
2012-09-10 20:38:29 +02:00
|
|
|
recipient_classes = [klass.type_id for klass in
|
|
|
|
Recipient.objects.filter(type=Recipient.CLASS)]
|
|
|
|
|
|
|
|
# Create subscriptions to classes
|
|
|
|
profiles = UserProfile.objects.all()
|
|
|
|
for i, profile in enumerate(profiles):
|
|
|
|
# Subscribe to some classes.
|
|
|
|
for recipient in recipient_classes[:int(len(recipient_classes) *
|
|
|
|
float(i)/len(profiles)) + 1]:
|
|
|
|
r = Recipient.objects.get(type=Recipient.CLASS, type_id=recipient)
|
|
|
|
new_subscription = Subscription(userprofile=profile,
|
|
|
|
recipient=r)
|
|
|
|
new_subscription.save()
|
|
|
|
else:
|
2012-09-21 00:03:43 +02:00
|
|
|
humbug_realm = Realm.objects.get(domain="humbughq.com")
|
2012-09-10 20:38:29 +02:00
|
|
|
recipient_classes = [klass.type_id for klass in
|
|
|
|
Recipient.objects.filter(type=Recipient.CLASS)]
|
|
|
|
|
|
|
|
# Extract a list of all users
|
|
|
|
users = [user.id for user in User.objects.all()]
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-09-05 18:38:35 +02:00
|
|
|
# Create several initial huddles
|
2012-09-20 17:22:39 +02:00
|
|
|
for i in xrange(options["num_huddles"]):
|
2012-09-10 20:38:29 +02:00
|
|
|
get_huddle(random.sample(users, random.randint(3, 4)))
|
2012-09-05 18:38:35 +02:00
|
|
|
|
|
|
|
# Create several initial pairs for personals
|
2012-09-20 17:22:39 +02:00
|
|
|
personals_pairs = [random.sample(users, 2)
|
|
|
|
for i in xrange(options["num_personals"])]
|
2012-09-14 17:52:38 +02:00
|
|
|
|
2012-09-14 22:52:09 +02:00
|
|
|
threads = options["threads"]
|
2012-09-14 18:31:11 +02:00
|
|
|
jobs = []
|
2012-09-20 17:22:39 +02:00
|
|
|
for i in xrange(threads):
|
2012-09-14 18:31:11 +02:00
|
|
|
count = options["num_zephyrs"] / threads
|
|
|
|
if i < options["num_zephyrs"] % threads:
|
|
|
|
count += 1
|
|
|
|
jobs.append((count, personals_pairs, options, self.stdout.write))
|
2012-09-20 17:22:39 +02:00
|
|
|
for status, job in run_parallel(send_zephyrs, jobs, threads=threads):
|
2012-09-14 18:31:11 +02:00
|
|
|
pass
|
2012-08-28 18:45:10 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
if options["delete"]:
|
2012-09-21 00:03:43 +02:00
|
|
|
mit_realm = Realm(domain="mit.edu")
|
|
|
|
mit_realm.save()
|
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
# Create internal users
|
2012-09-21 17:06:37 +02:00
|
|
|
internal_mit_users = []
|
|
|
|
create_users(internal_mit_users)
|
2012-09-07 17:54:00 +02:00
|
|
|
|
2012-09-21 23:54:23 +02:00
|
|
|
create_classes(mit_subs_list.all_subs, mit_realm)
|
2012-09-07 17:54:00 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
# Now subscribe everyone to these classes
|
2012-09-21 00:03:43 +02:00
|
|
|
profiles = UserProfile.objects.filter(realm=mit_realm)
|
2012-09-21 23:54:23 +02:00
|
|
|
for cls in mit_subs_list.all_subs:
|
2012-09-21 00:03:43 +02:00
|
|
|
zephyr_class = ZephyrClass.objects.get(name=cls, realm=mit_realm)
|
2012-09-10 20:38:29 +02:00
|
|
|
recipient = Recipient.objects.get(type=Recipient.CLASS, type_id=zephyr_class.id)
|
|
|
|
for i, profile in enumerate(profiles):
|
2012-09-21 23:54:23 +02:00
|
|
|
if profile.user.email in mit_subs_list.subs_lists:
|
|
|
|
key = profile.user.email
|
|
|
|
else:
|
|
|
|
key = "default"
|
|
|
|
if cls in mit_subs_list.subs_lists[key]:
|
|
|
|
new_subscription = Subscription(userprofile=profile, recipient=recipient)
|
|
|
|
new_subscription.save()
|
2012-09-21 17:06:37 +02:00
|
|
|
|
|
|
|
internal_humbug_users = []
|
|
|
|
create_users(internal_humbug_users)
|
2012-09-21 23:54:23 +02:00
|
|
|
humbug_class_list = ["devel", "all", "humbug", "design", "support"]
|
2012-09-21 17:06:37 +02:00
|
|
|
create_classes(humbug_class_list, humbug_realm)
|
|
|
|
|
|
|
|
# Now subscribe everyone to these classes
|
|
|
|
profiles = UserProfile.objects.filter(realm=humbug_realm)
|
|
|
|
for cls in humbug_class_list:
|
|
|
|
zephyr_class = ZephyrClass.objects.get(name=cls, realm=humbug_realm)
|
|
|
|
recipient = Recipient.objects.get(type=Recipient.CLASS, type_id=zephyr_class.id)
|
|
|
|
for i, profile in enumerate(profiles):
|
|
|
|
# Subscribe to some classes.
|
|
|
|
new_subscription = Subscription(userprofile=profile, recipient=recipient)
|
|
|
|
new_subscription.save()
|
2012-09-07 17:54:00 +02:00
|
|
|
|
2012-09-10 20:38:29 +02:00
|
|
|
self.stdout.write("Successfully populated test database.\n")
|
2012-09-27 19:58:42 +02:00
|
|
|
if options["replay_old_zephyrs"]:
|
|
|
|
restore_saved_zephyrs()
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-09-14 22:43:54 +02:00
|
|
|
recipient_hash = {}
|
|
|
|
def get_recipient_by_id(rid):
|
|
|
|
if rid in recipient_hash:
|
|
|
|
return recipient_hash[rid]
|
|
|
|
return Recipient.objects.get(id=rid)
|
|
|
|
|
2012-09-27 19:58:42 +02:00
|
|
|
def restore_saved_zephyrs():
|
|
|
|
old_zephyrs = file("all_zephyrs_log", "r").readlines()
|
|
|
|
for old_zephyr_json in old_zephyrs:
|
|
|
|
old_zephyr = simplejson.loads(old_zephyr_json.strip())
|
|
|
|
|
|
|
|
new_zephyr = Zephyr()
|
|
|
|
sender_email = old_zephyr["sender_email"]
|
|
|
|
realm = Realm.objects.get(domain=sender_email.split('@')[1])
|
|
|
|
create_user_if_needed(realm, sender_email, sender_email.split('@')[0],
|
|
|
|
old_zephyr["sender_full_name"],
|
|
|
|
old_zephyr["sender_short_name"])
|
|
|
|
new_zephyr.sender = UserProfile.objects.get(user__email=old_zephyr["sender_email"])
|
|
|
|
type_hash = {"class": Recipient.CLASS, "huddle": Recipient.HUDDLE, "personal": Recipient.PERSONAL}
|
|
|
|
new_zephyr.type = type_hash[old_zephyr["type"]]
|
|
|
|
new_zephyr.content = old_zephyr["content"]
|
|
|
|
new_zephyr.instance = old_zephyr["instance"]
|
|
|
|
new_zephyr.pub_date = datetime.datetime.utcfromtimestamp(float(old_zephyr["timestamp"])).replace(tzinfo=utc)
|
|
|
|
|
|
|
|
if new_zephyr.type == Recipient.PERSONAL:
|
|
|
|
u = old_zephyr["recipient"][0]
|
|
|
|
create_user_if_needed(realm, u["email"], u["email"].split('@')[0],
|
|
|
|
u["full_name"], u["short_name"])
|
|
|
|
user_profile = UserProfile.objects.get(user__email=u["email"])
|
|
|
|
new_zephyr.recipient = Recipient.objects.get(type=Recipient.PERSONAL,
|
|
|
|
type_id=user_profile.id)
|
|
|
|
elif new_zephyr.type == Recipient.CLASS:
|
|
|
|
zephyr_class = create_class_if_needed(realm, old_zephyr["recipient"])
|
|
|
|
new_zephyr.recipient = Recipient.objects.get(type=Recipient.CLASS,
|
|
|
|
type_id=zephyr_class.id)
|
|
|
|
elif new_zephyr.type == Recipient.HUDDLE:
|
|
|
|
for u in old_zephyr["recipient"]:
|
|
|
|
create_user_if_needed(realm, u["email"], u["email"].split('@')[0],
|
|
|
|
u["full_name"], u["short_name"])
|
|
|
|
target_huddle = get_huddle([UserProfile.objects.get(user__email=u["email"]).id
|
|
|
|
for u in old_zephyr["recipient"]])
|
|
|
|
new_zephyr.recipient = Recipient.objects.get(type=Recipient.HUDDLE,
|
|
|
|
type_id=target_huddle.id)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
do_send_zephyr(new_zephyr, synced_from_mit=True, no_log=True)
|
|
|
|
|
|
|
|
|
2012-09-14 18:31:11 +02:00
|
|
|
# Create some test zephyrs, including:
|
|
|
|
# - multiple classes
|
|
|
|
# - multiple instances per class
|
|
|
|
# - multiple huddles
|
|
|
|
# - multiple personals converastions
|
|
|
|
# - multiple zephyrs per instance
|
|
|
|
# - both single and multi-line content
|
|
|
|
def send_zephyrs(data):
|
|
|
|
(tot_zephyrs, personals_pairs, options, output) = data
|
|
|
|
from django.db import connection
|
|
|
|
connection.close()
|
|
|
|
texts = file("zephyr/management/commands/test_zephyrs.txt", "r").readlines()
|
2012-09-27 21:00:10 +02:00
|
|
|
offset = random.randint(0, len(texts))
|
2012-09-14 18:31:11 +02:00
|
|
|
|
2012-09-14 22:22:23 +02:00
|
|
|
recipient_classes = [klass.id for klass in
|
2012-09-14 18:31:11 +02:00
|
|
|
Recipient.objects.filter(type=Recipient.CLASS)]
|
2012-09-14 22:22:23 +02:00
|
|
|
recipient_huddles = [h.id for h in Recipient.objects.filter(type=Recipient.HUDDLE)]
|
2012-09-14 18:31:11 +02:00
|
|
|
|
|
|
|
huddle_members = {}
|
|
|
|
for h in recipient_huddles:
|
|
|
|
huddle_members[h] = [s.userprofile.id for s in
|
2012-09-14 22:22:23 +02:00
|
|
|
Subscription.objects.filter(recipient_id=h)]
|
2012-09-14 18:31:11 +02:00
|
|
|
|
|
|
|
num_zephyrs = 0
|
|
|
|
random_max = 1000000
|
|
|
|
recipients = {}
|
|
|
|
while num_zephyrs < tot_zephyrs:
|
|
|
|
with transaction.commit_on_success():
|
|
|
|
saved_data = ''
|
|
|
|
new_zephyr = Zephyr()
|
|
|
|
length = random.randint(1, 5)
|
2012-09-26 20:41:54 +02:00
|
|
|
lines = (t.strip() for t in texts[offset: offset + length])
|
|
|
|
new_zephyr.content = '\n'.join(lines)
|
2012-09-14 18:31:11 +02:00
|
|
|
offset += length
|
|
|
|
offset = offset % len(texts)
|
|
|
|
|
|
|
|
randkey = random.randint(1, random_max)
|
|
|
|
if (num_zephyrs > 0 and
|
|
|
|
random.randint(1, random_max) * 100. / random_max < options["stickyness"]):
|
|
|
|
# Use an old recipient
|
|
|
|
zephyr_type, recipient_id, saved_data = recipients[num_zephyrs - 1]
|
|
|
|
if zephyr_type == Recipient.PERSONAL:
|
|
|
|
personals_pair = saved_data
|
|
|
|
random.shuffle(personals_pair)
|
|
|
|
elif zephyr_type == Recipient.CLASS:
|
|
|
|
new_zephyr.instance = saved_data
|
2012-09-14 22:43:54 +02:00
|
|
|
new_zephyr.recipient = get_recipient_by_id(recipient_id)
|
2012-09-14 18:31:11 +02:00
|
|
|
elif zephyr_type == Recipient.HUDDLE:
|
2012-09-14 22:43:54 +02:00
|
|
|
new_zephyr.recipient = get_recipient_by_id(recipient_id)
|
2012-09-14 18:31:11 +02:00
|
|
|
elif (randkey <= random_max * options["percent_huddles"] / 100.):
|
|
|
|
zephyr_type = Recipient.HUDDLE
|
2012-09-14 22:43:54 +02:00
|
|
|
new_zephyr.recipient = get_recipient_by_id(random.choice(recipient_huddles))
|
2012-09-14 18:31:11 +02:00
|
|
|
elif (randkey <= random_max * (options["percent_huddles"] + options["percent_personals"]) / 100.):
|
|
|
|
zephyr_type = Recipient.PERSONAL
|
|
|
|
personals_pair = random.choice(personals_pairs)
|
|
|
|
random.shuffle(personals_pair)
|
|
|
|
elif (randkey <= random_max * 1.0):
|
|
|
|
zephyr_type = Recipient.CLASS
|
2012-09-14 22:43:54 +02:00
|
|
|
new_zephyr.recipient = get_recipient_by_id(random.choice(recipient_classes))
|
2012-09-14 18:31:11 +02:00
|
|
|
|
|
|
|
if zephyr_type == Recipient.HUDDLE:
|
2012-09-14 22:22:23 +02:00
|
|
|
sender_id = random.choice(huddle_members[new_zephyr.recipient.id])
|
2012-09-14 22:43:54 +02:00
|
|
|
new_zephyr.sender = get_user_profile_by_id(sender_id)
|
2012-09-14 18:31:11 +02:00
|
|
|
elif zephyr_type == Recipient.PERSONAL:
|
|
|
|
new_zephyr.recipient = Recipient.objects.get(type=Recipient.PERSONAL,
|
|
|
|
type_id=personals_pair[0])
|
2012-09-14 22:43:54 +02:00
|
|
|
new_zephyr.sender = get_user_profile_by_id(personals_pair[1])
|
2012-09-14 18:31:11 +02:00
|
|
|
saved_data = personals_pair
|
|
|
|
elif zephyr_type == Recipient.CLASS:
|
|
|
|
zephyr_class = ZephyrClass.objects.get(id=new_zephyr.recipient.type_id)
|
|
|
|
# Pick a random subscriber to the class
|
|
|
|
new_zephyr.sender = random.choice(Subscription.objects.filter(
|
|
|
|
recipient=new_zephyr.recipient)).userprofile
|
|
|
|
new_zephyr.instance = zephyr_class.name + str(random.randint(1, 3))
|
|
|
|
saved_data = new_zephyr.instance
|
|
|
|
|
|
|
|
new_zephyr.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc)
|
2012-09-24 20:29:33 +02:00
|
|
|
do_send_zephyr(new_zephyr)
|
2012-09-14 18:31:11 +02:00
|
|
|
|
|
|
|
recipients[num_zephyrs] = [zephyr_type, new_zephyr.recipient.id, saved_data]
|
|
|
|
num_zephyrs += 1
|
|
|
|
return tot_zephyrs
|