From ff9fb85467ff85ae6b3acb0bdd712c522686969c Mon Sep 17 00:00:00 2001 From: pycook Date: Thu, 28 Nov 2024 19:28:22 +0800 Subject: [PATCH] feat(api): calc all racks free u count --- cmdb-api/api/lib/cmdb/dcim/rack.py | 19 +++++++++---------- cmdb-api/api/tasks/cmdb.py | 24 +++++++++++++++++++++++- cmdb-api/api/views/cmdb/dcim/rack.py | 13 +++++++++++++ 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/cmdb-api/api/lib/cmdb/dcim/rack.py b/cmdb-api/api/lib/cmdb/dcim/rack.py index 4d9586c2..ac1dfaa9 100644 --- a/cmdb-api/api/lib/cmdb/dcim/rack.py +++ b/cmdb-api/api/lib/cmdb/dcim/rack.py @@ -44,7 +44,7 @@ def update(cls, _id, **kwargs): CIManager().update(_id, _sync=True, **kwargs) if RackBuiltinAttributes.U_COUNT in kwargs: - payload = {RackBuiltinAttributes.FREE_U_COUNT: cls._calc_u_free_count(_id)} + payload = {RackBuiltinAttributes.FREE_U_COUNT: cls.calc_u_free_count(_id)} CIManager().update(_id, _sync=True, **payload) @@ -57,7 +57,7 @@ def delete(self, _id): CIManager().update(ci['_id'], **payload) @staticmethod - def _calc_u_free_count(rack_id, device_id=None, u_start=None, u_count=None): + def calc_u_free_count(rack_id, device_id=None, u_start=None, u_count=None): rack = CIManager.get_ci_by_id(rack_id, need_children=False) if not rack.get(RackBuiltinAttributes.U_COUNT): return 0 @@ -123,7 +123,7 @@ def check_u_slot(self): def add_device(self, rack_id, device_id, u_start, u_count=None): with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))): - self._calc_u_free_count(rack_id, device_id, u_start, u_count) + self.calc_u_free_count(rack_id, device_id, u_start, u_count) self.add_relation(rack_id, device_id) @@ -133,7 +133,7 @@ def add_device(self, rack_id, device_id, u_start, u_count=None): CIManager().update(device_id, _sync=True, **payload) payload = { - RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(rack_id, device_id, u_start, u_count)} + RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id, device_id, u_start, u_count)} CIManager().update(rack_id, _sync=True, **payload) OperateHistoryManager().add(operate_type=OperateTypeEnum.ADD_DEVICE, rack_id=rack_id, ci_id=device_id) @@ -142,7 +142,7 @@ def remove_device(self, rack_id, device_id): with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))): CIRelationManager.delete_3(rack_id, device_id, apply_async=False, valid=False) - payload = {RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(rack_id)} + payload = {RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id)} CIManager().update(rack_id, _sync=True, **payload) payload = {RackBuiltinAttributes.U_START: None} @@ -152,7 +152,7 @@ def remove_device(self, rack_id, device_id): def move_device(self, rack_id, device_id, to_u_start): with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))): - payload = {RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(rack_id, device_id, to_u_start)} + payload = {RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id, device_id, to_u_start)} CIManager().update(rack_id, _sync=True, **payload) CIManager().update(device_id, _sync=True, **{RackBuiltinAttributes.U_START: to_u_start}) @@ -161,7 +161,7 @@ def move_device(self, rack_id, device_id, to_u_start): def migrate_device(self, rack_id, device_id, to_rack_id, to_u_start): with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))): - self._calc_u_free_count(to_rack_id, device_id, to_u_start) + self.calc_u_free_count(to_rack_id, device_id, to_u_start) if rack_id != to_rack_id: CIRelationManager.delete_3(rack_id, device_id, apply_async=False, valid=False) @@ -169,15 +169,14 @@ def migrate_device(self, rack_id, device_id, to_rack_id, to_u_start): self.add_relation(to_rack_id, device_id) payload = { - RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(to_rack_id, device_id, to_u_start)} + RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(to_rack_id, device_id, to_u_start)} CIManager().update(to_rack_id, _sync=True, **payload) CIManager().update(device_id, _sync=True, **{RackBuiltinAttributes.U_START: to_u_start}) if rack_id != to_rack_id: - payload = {RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(rack_id)} + payload = {RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id)} CIManager().update(rack_id, _sync=True, **payload) OperateHistoryManager().add(operate_type=OperateTypeEnum.REMOVE_DEVICE, rack_id=rack_id, ci_id=device_id) OperateHistoryManager().add(operate_type=OperateTypeEnum.ADD_DEVICE, rack_id=to_rack_id, ci_id=device_id) - diff --git a/cmdb-api/api/tasks/cmdb.py b/cmdb-api/api/tasks/cmdb.py index 7c2a0e06..35f2332a 100644 --- a/cmdb-api/api/tasks/cmdb.py +++ b/cmdb-api/api/tasks/cmdb.py @@ -1,4 +1,4 @@ -# -*- coding:utf-8 -*- +# -*- coding:utf-8 -*- import datetime @@ -374,3 +374,25 @@ def build_relations_for_ad_accept(adc, ci_id, ad_key2attr): source=RelationSourceEnum.AUTO_DISCOVERY) except: pass + + +@celery.task(name="cmdb.dcim_calc_u_free_count", queue=CMDB_QUEUE) +@reconnect_db +def dcim_calc_u_free_count(): + from api.lib.cmdb.ci import CIManager + from api.lib.cmdb.dcim.rack import RackManager + from api.lib.cmdb.dcim.const import RackBuiltinAttributes + + if not has_request_context(): + current_app.test_request_context().push() + login_user(UserCache.get('worker')) + + try: + rack_m = RackManager() + except Exception: + return + + racks = CI.get_by(type_id=rack_m.type_id, to_dict=False) + for rack in racks: + payload = {RackBuiltinAttributes.FREE_U_COUNT: rack_m.calc_u_free_count(rack.id)} + CIManager().update(rack.id, **payload) diff --git a/cmdb-api/api/views/cmdb/dcim/rack.py b/cmdb-api/api/views/cmdb/dcim/rack.py index 8441869d..ee4f5ff9 100644 --- a/cmdb-api/api/views/cmdb/dcim/rack.py +++ b/cmdb-api/api/views/cmdb/dcim/rack.py @@ -2,12 +2,14 @@ from flask import request +from api.lib.cmdb.const import CMDB_QUEUE from api.lib.cmdb.dcim.const import RackBuiltinAttributes from api.lib.cmdb.dcim.rack import RackManager from api.lib.common_setting.decorator import perms_role_required from api.lib.common_setting.role_perm_base import CMDBApp from api.lib.decorator import args_required from api.resource import APIView +from api.tasks.cmdb import dcim_calc_u_free_count app_cli = CMDBApp() @@ -87,3 +89,14 @@ def put(self, rack_id, device_id): device_id=device_id, to_u_start=to_u_start, to_rack_id=to_rack_id) + + +class RackCalcUFreeCountView(APIView): + url_prefix = ("/dcim/rack/calc_u_free_count",) + + @perms_role_required(app_cli.app_name, app_cli.resource_type_name, app_cli.op.DCIM, + app_cli.op.read, app_cli.admin_name) + def post(self): + dcim_calc_u_free_count.apply_async(queue=CMDB_QUEUE) + + return self.jsonify(code=200)