|
| 1 | +import { ProjectStatus } from '@prisma/client'; |
| 2 | +import _sum from 'lodash-es/sum'; |
| 3 | +import createApiHandler from '@/core/api-handler'; |
| 4 | +import prisma from '@/core/prisma'; |
| 5 | +import { CsvResponse, NoContent } from '@/core/responses'; |
| 6 | +import { ministryKeyToName } from '@/helpers/product'; |
| 7 | +import { PermissionsEnum } from '@/types/permissions'; |
| 8 | +import { extractNumbers } from '@/utils/string'; |
| 9 | + |
| 10 | +const apiHandler = createApiHandler({ |
| 11 | + permissions: [PermissionsEnum.ViewGeneralAnalytics], |
| 12 | +}); |
| 13 | + |
| 14 | +export const GET = apiHandler(async () => { |
| 15 | + const products = await prisma.privateCloudProject.findMany({ |
| 16 | + where: { status: ProjectStatus.ACTIVE }, |
| 17 | + select: { |
| 18 | + name: true, |
| 19 | + ministry: true, |
| 20 | + developmentQuota: true, |
| 21 | + testQuota: true, |
| 22 | + productionQuota: true, |
| 23 | + toolsQuota: true, |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + return CsvResponse( |
| 28 | + products.map((row) => { |
| 29 | + const devCpu = extractNumbers(row.developmentQuota.cpu); |
| 30 | + const devMemory = extractNumbers(row.developmentQuota.memory); |
| 31 | + const devStorage = extractNumbers(row.developmentQuota.storage); |
| 32 | + |
| 33 | + const testCpu = extractNumbers(row.testQuota.cpu); |
| 34 | + const testMemory = extractNumbers(row.testQuota.memory); |
| 35 | + const testStorage = extractNumbers(row.testQuota.storage); |
| 36 | + |
| 37 | + const prodCpu = extractNumbers(row.productionQuota.cpu); |
| 38 | + const prodMemory = extractNumbers(row.productionQuota.memory); |
| 39 | + const prodStorage = extractNumbers(row.productionQuota.storage); |
| 40 | + |
| 41 | + const toolsCpu = extractNumbers(row.toolsQuota.cpu); |
| 42 | + const toolsMemory = extractNumbers(row.toolsQuota.memory); |
| 43 | + const toolsStorage = extractNumbers(row.toolsQuota.storage); |
| 44 | + |
| 45 | + const cpuRequestTotal = _sum([devCpu[0], testCpu[0], prodCpu[0], toolsCpu[0]]); |
| 46 | + const cpuLimitTotal = _sum([devCpu[1], testCpu[1], prodCpu[1], toolsCpu[1]]); |
| 47 | + const memoryRequestTotal = _sum([devMemory[0], testMemory[0], prodMemory[0], toolsMemory[0]]); |
| 48 | + const memoryLimitTotal = _sum([devMemory[1], testMemory[1], prodMemory[1], toolsMemory[1]]); |
| 49 | + const storageTotal = _sum([devStorage[0], testStorage[0], prodStorage[0], toolsStorage[0]]); |
| 50 | + |
| 51 | + return { |
| 52 | + 'Product Name': row.name, |
| 53 | + 'Ministry Short': row.ministry, |
| 54 | + 'Ministry Name': ministryKeyToName(row.ministry), |
| 55 | + 'CPU Request Total': cpuRequestTotal, |
| 56 | + 'CPU Limit Total': cpuLimitTotal, |
| 57 | + 'Memory Request Total': memoryRequestTotal, |
| 58 | + 'Memory Limit Total': memoryLimitTotal, |
| 59 | + 'Storage Total': storageTotal, |
| 60 | + }; |
| 61 | + }), |
| 62 | + 'quota-summary.csv', |
| 63 | + ); |
| 64 | +}); |
0 commit comments