diff --git a/apps/herd/services/rancher_dashboard_service.py b/apps/herd/services/rancher_dashboard_service.py index c9aeb63..b0346ab 100644 --- a/apps/herd/services/rancher_dashboard_service.py +++ b/apps/herd/services/rancher_dashboard_service.py @@ -95,3 +95,52 @@ class RancherDashboardService: rancher_data_by_quota_usage.append(rancher_quota_data) 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 diff --git a/apps/herd/web/api/v1/api.py b/apps/herd/web/api/v1/api.py index c950a2c..298f3b7 100644 --- a/apps/herd/web/api/v1/api.py +++ b/apps/herd/web/api/v1/api.py @@ -239,3 +239,25 @@ class RancherViewSet(BaseViewSet, RancherDashboardService, SoftDeleteMixin, view rancher=rancher ) 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)