From dac0b44b9948588036c2b02962befbf1a03d73ff Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 13 Jul 2022 16:36:49 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0093_auto_20220711_1413.py | 45 +++++++++++++++++++ apps/assets/models/account.py | 3 ++ 2 files changed, 48 insertions(+) diff --git a/apps/assets/migrations/0093_auto_20220711_1413.py b/apps/assets/migrations/0093_auto_20220711_1413.py index c8cb17160..2a9e1b856 100644 --- a/apps/assets/migrations/0093_auto_20220711_1413.py +++ b/apps/assets/migrations/0093_auto_20220711_1413.py @@ -1,5 +1,6 @@ # Generated by Django 3.2.12 on 2022-07-11 06:13 +import time from django.db import migrations @@ -7,6 +8,49 @@ def migrate_accounts(apps, schema_editor): auth_book_model = apps.get_model('assets', 'AuthBook') account_model = apps.get_model('assets', 'Account') + count = 0 + bulk_size = 1000 + while True: + auth_books = auth_book_model.objects \ + .prefetch_related('systemuser') \ + .all()[count:count+bulk_size] + if not auth_books: + break + + accounts = [] + start = time.time() + # authbook 和 account 相同的属性 + same_attrs = [ + 'id', 'comment', 'date_created', 'date_updated', + 'created_by', 'asset_id', 'org_id', + ] + # 认证的属性,可能是 authbook 的,可能是 systemuser 的 + auth_attrs = ['username', 'password', 'private_key', 'public_key'] + + for auth_book in auth_books: + values = {attr: getattr(auth_book, attr) for attr in same_attrs} + values['protocol'] = 'ssh' + values['version'] = 1 + + system_user = auth_book.systemuser + if auth_book.systemuser: + values.update({attr: getattr(system_user, attr) for attr in auth_attrs}) + values['protocol'] = system_user.protocol + values['created_by'] = str(system_user.id) + + auth_book_auth = {attr: getattr(auth_book, attr) for attr in auth_attrs} + auth_book_auth = {attr: value for attr, value in auth_book_auth.items() if value} + values.update(auth_book_auth) + + account = account_model(**values) + accounts.append(account) + + account_model.objects.bulk_create(accounts, ignore_conflicts=True) + print("Create accounts: {}-{} using: {:.2f}s".format( + count, count + len(accounts), time.time()-start + )) + count += len(auth_books) + class Migration(migrations.Migration): @@ -15,4 +59,5 @@ class Migration(migrations.Migration): ] operations = [ + migrations.RunPython(migrate_accounts) ] diff --git a/apps/assets/models/account.py b/apps/assets/models/account.py index 0d6e999cc..7732c50a2 100644 --- a/apps/assets/models/account.py +++ b/apps/assets/models/account.py @@ -25,3 +25,6 @@ class Account(BaseUser, ProtocolMixin): ('view_assethistoryaccount', _('Can view asset history account')), ('view_assethistoryaccountsecret', _('Can view asset history account secret')), ] + + def __str__(self): + return '{}//{}@{}'.format(self.protocol, self.username, self.asset.hostname)