import: Speed up create_subscription_events().

The code was needlessly querying the DB to get full
objects for entities where we only needed user_id,
realm_id, and stream_id.

With my test data of ~1000 records this sped up the
function from ~8s to ~0.5s.  The speedup would probably
be even more for larger data sets.
This commit is contained in:
Steve Howell 2018-09-30 10:54:16 +00:00 committed by Tim Abbott
parent 875f48913f
commit 329154da32

View File

@ -113,7 +113,7 @@ def fix_upload_links(data: TableData, message_table: TableName) -> None:
if message['rendered_content']:
message['rendered_content'] = message['rendered_content'].replace(key, value)
def create_subscription_events(data: TableData, table: TableName) -> None:
def create_subscription_events(data: TableData, realm_id: int) -> None:
"""
When the export data doesn't contain the table `zerver_realmauditlog`,
this function creates RealmAuditLog objects for `subscription_created`
@ -132,18 +132,25 @@ def create_subscription_events(data: TableData, table: TableName) -> None:
event_last_message_id = -1
event_time = timezone_now()
for item in data[table]:
recipient = Recipient.objects.get(id=item['recipient_id'])
if recipient.type != Recipient.STREAM:
recipient_id_to_stream_id = {
d['id']: d['type_id']
for d in data['zerver_recipient']
if d['type'] == Recipient.STREAM
}
for sub in data['zerver_subscription']:
recipient_id = sub['recipient_id']
stream_id = recipient_id_to_stream_id.get(recipient_id)
if stream_id is None:
continue
stream = Stream.objects.get(id=recipient.type_id)
user = UserProfile.objects.get(id=item['user_profile_id'])
user_id = sub['user_profile_id']
all_subscription_logs.append(RealmAuditLog(realm=user.realm,
acting_user=user,
modified_user=user,
modified_stream=stream,
all_subscription_logs.append(RealmAuditLog(realm_id=realm_id,
acting_user_id=user_id,
modified_user_id=user_id,
modified_stream_id=stream_id,
event_last_message_id=event_last_message_id,
event_time=event_time,
event_type=RealmAuditLog.SUBSCRIPTION_CREATED))
@ -745,7 +752,12 @@ def do_import_realm(import_dir: Path, subdomain: str) -> Realm:
update_model_ids(RealmAuditLog, data, related_table="realmauditlog")
bulk_import_model(data, RealmAuditLog)
else:
create_subscription_events(data, 'zerver_subscription')
logging.info('about to call create_subscription_events')
create_subscription_events(
data=data,
realm_id=realm.id,
)
logging.info('done with create_subscription_events')
if 'zerver_huddle' in data:
process_huddle_hash(data, 'zerver_huddle')