import - rancher dashboard by product & quota usage

This commit is contained in:
2025-12-16 11:28:51 +03:30
parent ca61ce4d84
commit 0cc686b94d
2 changed files with 71 additions and 0 deletions

View File

@@ -95,3 +95,52 @@ class RancherDashboardService:
rancher_data_by_quota_usage.append(rancher_quota_data) rancher_data_by_quota_usage.append(rancher_quota_data)
return rancher_data_by_quota_usage return rancher_data_by_quota_usage
@staticmethod
def get_rancher_dashboard_by_product_usage(self, rancher: Rancher):
"""
get rancher dashboard by product usage
"""
# get organization quota stats of transaction item
quota_stat = OrganizationQuotaStats.objects.filter(stat_type='quota', quota__is_closed=False)
available_stats = [
stat for stat in quota_stat if (
can_buy_from_inventory(rancher, quota_stat=stat) and rancher is not None
)
]
rancher_data_by_quota_usage = []
for stat in available_stats:
rancher_quota_data = rancher_quota_weight(rancher=rancher, quota=stat.quota, quota_stat=stat)
rancher_quota_data.update({'product': stat.quota.product.name, 'product_id': stat.quota.product.id})
rancher_data_by_quota_usage.append(rancher_quota_data)
product_list = set(stat.quota.product.id for stat in quota_stat)
rancher_data_by_product_usage_list = []
for prod in product_list:
product_summary = {
'product_id': prod,
'product': None,
'total_weight': 0,
'remaining_weight': 0,
'free_sale': 0,
'total_purchase': 0,
'items': []
}
for item in rancher_data_by_quota_usage:
if item['product_id'] == prod:
product_summary['product'] = item['product']
product_summary['total_weight'] += item['total_weight']
product_summary['remaining_weight'] += item['remaining_weight']
product_summary['free_sale'] += item['free_sale']
product_summary['total_purchase'] += item['total_purchase']
product_summary['items'].append(item)
rancher_data_by_product_usage_list.append(product_summary)
return rancher_data_by_product_usage_list

View File

@@ -239,3 +239,25 @@ class RancherViewSet(BaseViewSet, RancherDashboardService, SoftDeleteMixin, view
rancher=rancher rancher=rancher
) )
return Response(rancher_dashboard_data) return Response(rancher_dashboard_data)
@action(
methods=['get'],
detail=True,
url_path='rancher_dashboard_by_product_usage',
url_name='rancher_dashboard_by_product_usage',
name='rancher_dashboard_by_product_usage'
)
def rancher_dashboard_by_product_usage(self, request, pk=None):
""" rancher dashboard """
rancher = self.get_object()
query_param = self.request.query_params # noqa
query_string = query_param.get('search', None)
rancher_dashboard_data = self.get_rancher_dashboard_by_product_usage(
self,
rancher=rancher
)
return Response(rancher_dashboard_data)