From 8da55e593010eaf7f7ece89a8099d441124f3bbd Mon Sep 17 00:00:00 2001 From: Geonhyuk Seo Date: Thu, 5 Dec 2024 02:16:39 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=F0=9F=9A=9A=20Setting(server):=20clouds?= =?UTF-8?q?=EB=82=98=20ncloud-resources=EA=B0=99=EC=9D=B4=20=EB=B3=B5?= =?UTF-8?q?=EC=88=98=20=EC=B2=98=EB=A6=AC=20=EB=90=98=EC=96=B4=20=EC=9E=88?= =?UTF-8?q?=EB=8A=94=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EB=8B=A8=EC=88=98?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/app.module.ts | 7 +++---- .../cloud.controller.spec.ts} | 10 +++++----- apps/server/src/cloud/cloud.controller.ts | 4 ++++ apps/server/src/cloud/cloud.module.ts | 9 +++++++++ .../cloud.service.spec.ts} | 10 +++++----- .../clouds.service.ts => cloud/cloud.service.ts} | 2 +- apps/server/src/clouds/clouds.controller.ts | 4 ---- apps/server/src/clouds/clouds.module.ts | 9 --------- .../ncloud-resource.service.spec.ts} | 2 +- .../ncloud-resource.service.ts} | 0 10 files changed, 28 insertions(+), 29 deletions(-) rename apps/server/src/{clouds/clouds.controller.spec.ts => cloud/cloud.controller.spec.ts} (53%) create mode 100644 apps/server/src/cloud/cloud.controller.ts create mode 100644 apps/server/src/cloud/cloud.module.ts rename apps/server/src/{clouds/clouds.service.spec.ts => cloud/cloud.service.spec.ts} (56%) rename apps/server/src/{clouds/clouds.service.ts => cloud/cloud.service.ts} (66%) delete mode 100644 apps/server/src/clouds/clouds.controller.ts delete mode 100644 apps/server/src/clouds/clouds.module.ts rename apps/server/src/{ncloud-resources/ncloud-resources.service.spec.ts => ncloud-resource/ncloud-resource.service.spec.ts} (87%) rename apps/server/src/{ncloud-resources/ncloud-resources.service.ts => ncloud-resource/ncloud-resource.service.ts} (100%) diff --git a/apps/server/src/app.module.ts b/apps/server/src/app.module.ts index ffeeecad..5e972ef2 100644 --- a/apps/server/src/app.module.ts +++ b/apps/server/src/app.module.ts @@ -9,9 +9,8 @@ import { PrivateArchitectureModule } from 'src/private-architecture/private-arch import { PrismaService } from 'src/prisma/prisma.service'; import { MyModule } from './my/my.module'; import { ScheduleModule } from '@nestjs/schedule'; -import { NcloudResourcesService } from './ncloud-resources/ncloud-resources.service.js'; -import { CloudsModule } from './clouds/clouds.module'; -import { resourceUsage } from 'process'; +import { NcloudResourcesService } from './ncloud-resource/ncloud-resource.service.js'; +import { CloudModule } from './cloud/cloud.module'; @Module({ imports: [ @@ -22,7 +21,7 @@ import { resourceUsage } from 'process'; PrivateArchitectureModule, MyModule, ScheduleModule.forRoot(), - CloudsModule, + CloudModule, ], controllers: [AppController], providers: [AppService, PrismaService, NcloudResourcesService], diff --git a/apps/server/src/clouds/clouds.controller.spec.ts b/apps/server/src/cloud/cloud.controller.spec.ts similarity index 53% rename from apps/server/src/clouds/clouds.controller.spec.ts rename to apps/server/src/cloud/cloud.controller.spec.ts index e9d76d7b..2bd10063 100644 --- a/apps/server/src/clouds/clouds.controller.spec.ts +++ b/apps/server/src/cloud/cloud.controller.spec.ts @@ -1,15 +1,15 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { CloudsController } from './clouds.controller'; +import { CloudController } from './cloud.controller'; -describe('CloudsController', () => { - let controller: CloudsController; +describe('CloudController', () => { + let controller: CloudController; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - controllers: [CloudsController], + controllers: [CloudController], }).compile(); - controller = module.get(CloudsController); + controller = module.get(CloudController); }); it('should be defined', () => { diff --git a/apps/server/src/cloud/cloud.controller.ts b/apps/server/src/cloud/cloud.controller.ts new file mode 100644 index 00000000..a51a2c1f --- /dev/null +++ b/apps/server/src/cloud/cloud.controller.ts @@ -0,0 +1,4 @@ +import { Controller } from '@nestjs/common'; + +@Controller('cloud') +export class CloudController {} diff --git a/apps/server/src/cloud/cloud.module.ts b/apps/server/src/cloud/cloud.module.ts new file mode 100644 index 00000000..63b21a1b --- /dev/null +++ b/apps/server/src/cloud/cloud.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { CloudController } from './cloud.controller'; +import { CloudService } from './cloud.service'; + +@Module({ + controllers: [CloudController], + providers: [CloudService], +}) +export class CloudModule {} diff --git a/apps/server/src/clouds/clouds.service.spec.ts b/apps/server/src/cloud/cloud.service.spec.ts similarity index 56% rename from apps/server/src/clouds/clouds.service.spec.ts rename to apps/server/src/cloud/cloud.service.spec.ts index e9aceb2c..2d1fe80a 100644 --- a/apps/server/src/clouds/clouds.service.spec.ts +++ b/apps/server/src/cloud/cloud.service.spec.ts @@ -1,15 +1,15 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { CloudsService } from './clouds.service'; +import { CloudService } from './cloud.service'; -describe('CloudsService', () => { - let service: CloudsService; +describe('CloudService', () => { + let service: CloudService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [CloudsService], + providers: [CloudService], }).compile(); - service = module.get(CloudsService); + service = module.get(CloudService); }); it('should be defined', () => { diff --git a/apps/server/src/clouds/clouds.service.ts b/apps/server/src/cloud/cloud.service.ts similarity index 66% rename from apps/server/src/clouds/clouds.service.ts rename to apps/server/src/cloud/cloud.service.ts index 28e059e3..af2524bc 100644 --- a/apps/server/src/clouds/clouds.service.ts +++ b/apps/server/src/cloud/cloud.service.ts @@ -1,4 +1,4 @@ import { Injectable } from '@nestjs/common'; @Injectable() -export class CloudsService {} +export class CloudService {} diff --git a/apps/server/src/clouds/clouds.controller.ts b/apps/server/src/clouds/clouds.controller.ts deleted file mode 100644 index 688c2fc2..00000000 --- a/apps/server/src/clouds/clouds.controller.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { Controller } from '@nestjs/common'; - -@Controller('clouds') -export class CloudsController {} diff --git a/apps/server/src/clouds/clouds.module.ts b/apps/server/src/clouds/clouds.module.ts deleted file mode 100644 index 868d44bb..00000000 --- a/apps/server/src/clouds/clouds.module.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Module } from '@nestjs/common'; -import { CloudsController } from './clouds.controller'; -import { CloudsService } from './clouds.service'; - -@Module({ - controllers: [CloudsController], - providers: [CloudsService], -}) -export class CloudsModule {} diff --git a/apps/server/src/ncloud-resources/ncloud-resources.service.spec.ts b/apps/server/src/ncloud-resource/ncloud-resource.service.spec.ts similarity index 87% rename from apps/server/src/ncloud-resources/ncloud-resources.service.spec.ts rename to apps/server/src/ncloud-resource/ncloud-resource.service.spec.ts index ff55a8ca..a45bd279 100644 --- a/apps/server/src/ncloud-resources/ncloud-resources.service.spec.ts +++ b/apps/server/src/ncloud-resource/ncloud-resource.service.spec.ts @@ -1,5 +1,5 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { NcloudResourcesService } from './ncloud-resources.service'; +import { NcloudResourcesService } from './ncloud-resource.service'; describe('NcloudResourcesService', () => { let service: NcloudResourcesService; diff --git a/apps/server/src/ncloud-resources/ncloud-resources.service.ts b/apps/server/src/ncloud-resource/ncloud-resource.service.ts similarity index 100% rename from apps/server/src/ncloud-resources/ncloud-resources.service.ts rename to apps/server/src/ncloud-resource/ncloud-resource.service.ts From d6dffcab8eaffa98a305ec41bb668614d6c5fbe3 Mon Sep 17 00:00:00 2001 From: Geonhyuk Seo Date: Thu, 5 Dec 2024 03:15:57 +0900 Subject: [PATCH 2/7] =?UTF-8?q?=E2=9C=A8=20Feat(server):=20=EA=B0=80?= =?UTF-8?q?=EA=B2=A9=20=EA=B3=84=EC=82=B0=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/app.module.ts | 2 +- apps/server/src/cloud/cloud.controller.ts | 17 ++++++- apps/server/src/cloud/cloud.module.ts | 2 + apps/server/src/cloud/cloud.service.ts | 45 ++++++++++++++++++- .../ncloud-resource.service.ts | 1 - apps/server/tsconfig.json | 3 +- 6 files changed, 64 insertions(+), 6 deletions(-) diff --git a/apps/server/src/app.module.ts b/apps/server/src/app.module.ts index 5e972ef2..1b44ec24 100644 --- a/apps/server/src/app.module.ts +++ b/apps/server/src/app.module.ts @@ -24,6 +24,6 @@ import { CloudModule } from './cloud/cloud.module'; CloudModule, ], controllers: [AppController], - providers: [AppService, PrismaService, NcloudResourcesService], + providers: [AppService, PrismaService], }) export class AppModule {} diff --git a/apps/server/src/cloud/cloud.controller.ts b/apps/server/src/cloud/cloud.controller.ts index a51a2c1f..ef0ea93c 100644 --- a/apps/server/src/cloud/cloud.controller.ts +++ b/apps/server/src/cloud/cloud.controller.ts @@ -1,4 +1,17 @@ -import { Controller } from '@nestjs/common'; +import { Controller, Get } from '@nestjs/common'; +import { CloudService } from './cloud.service'; @Controller('cloud') -export class CloudController {} +export class CloudController { + constructor(private readonly service: CloudService) {} + + @Get('/prices') + getCloudResorucePrices() { + return this.service.findCloudResourcePrices(); + } + + // @Get('/test') + // getTestPrices() { + // return this.service.calculatePrice() + // } +} diff --git a/apps/server/src/cloud/cloud.module.ts b/apps/server/src/cloud/cloud.module.ts index 63b21a1b..53cdcf81 100644 --- a/apps/server/src/cloud/cloud.module.ts +++ b/apps/server/src/cloud/cloud.module.ts @@ -1,8 +1,10 @@ import { Module } from '@nestjs/common'; import { CloudController } from './cloud.controller'; import { CloudService } from './cloud.service'; +import { PrismaModule } from 'src/prisma/prisma.module'; @Module({ + imports: [PrismaModule], controllers: [CloudController], providers: [CloudService], }) diff --git a/apps/server/src/cloud/cloud.service.ts b/apps/server/src/cloud/cloud.service.ts index af2524bc..51155604 100644 --- a/apps/server/src/cloud/cloud.service.ts +++ b/apps/server/src/cloud/cloud.service.ts @@ -1,4 +1,47 @@ import { Injectable } from '@nestjs/common'; +import { PrismaService } from 'src/prisma/prisma.service'; +import * as data from '../../node.json'; @Injectable() -export class CloudService {} +export class CloudService { + constructor(private readonly prisma: PrismaService) {} + + async findCloudResourcePrices() { + // console.log(Object.values(data)); + const prices = await this.prisma.ncloudServerResource.findMany({ + select: { + serverSpecCode: true, + productName: true, + hourCost: true, + monthCost: true, + }, + }); + const priceMap = new Map>(); + prices.map((price) => { + const { productName, hourCost, monthCost } = price; + priceMap.set(price.serverSpecCode, { + productName, + hourCost, + monthCost, + }); + }); + return Object.fromEntries(priceMap); + } + + async calculatePrice(nodes?: Record) { + const prices = await this.findCloudResourcePrices(); + const nodeValues = Object.values(data); + const totalMonthPrice = nodeValues.reduce( + (price, nodeValue): number => { + if (nodeValue.properties['server_spec_code']) { + price += prices[nodeValue.properties['server_spec_code']] + .monthCost as number; + return price; + } + return price; + }, + 0, + ); + return { totalMonthPrice }; + } +} diff --git a/apps/server/src/ncloud-resource/ncloud-resource.service.ts b/apps/server/src/ncloud-resource/ncloud-resource.service.ts index 453444ee..feb04857 100644 --- a/apps/server/src/ncloud-resource/ncloud-resource.service.ts +++ b/apps/server/src/ncloud-resource/ncloud-resource.service.ts @@ -2,7 +2,6 @@ import { Ncloud, PriceApi, ApiKeyCredentials } from '@cloud-canvas/ncloud-sdk'; import { Injectable } from '@nestjs/common'; import { Cron, CronExpression } from '@nestjs/schedule'; import { PrismaService } from 'src/prisma/prisma.service'; -import { writeFile } from 'fs/promises'; @Injectable() export class NcloudResourcesService { diff --git a/apps/server/tsconfig.json b/apps/server/tsconfig.json index dd264f7e..918effdd 100644 --- a/apps/server/tsconfig.json +++ b/apps/server/tsconfig.json @@ -16,6 +16,7 @@ "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, - "noFallthroughCasesInSwitch": false + "noFallthroughCasesInSwitch": false, + "resolveJsonModule": true } } From f1c92c082686232dd5d399f736a5c4807185a25d Mon Sep 17 00:00:00 2001 From: Geonhyuk Seo Date: Thu, 5 Dec 2024 03:30:03 +0900 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=A4=96=20Refactor(server):=20cloud=20?= =?UTF-8?q?resource=EB=A5=BC=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=B2=A0?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=EC=97=90=EC=84=9C=20=EA=B0=80=EC=A0=B8?= =?UTF-8?q?=EC=98=A4=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EB=94=B0=EB=A1=9C=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=EB=A1=9C=20=EB=B6=84=EB=A6=AC=20=EB=B0=8F=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/cloud/cloud.controller.ts | 8 ++-- apps/server/src/cloud/cloud.service.ts | 51 ++++++++++++++--------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/apps/server/src/cloud/cloud.controller.ts b/apps/server/src/cloud/cloud.controller.ts index ef0ea93c..886d0c36 100644 --- a/apps/server/src/cloud/cloud.controller.ts +++ b/apps/server/src/cloud/cloud.controller.ts @@ -10,8 +10,8 @@ export class CloudController { return this.service.findCloudResourcePrices(); } - // @Get('/test') - // getTestPrices() { - // return this.service.calculatePrice() - // } + @Get('/test') + getTestPrices() { + return this.service.calculatePrice(); + } } diff --git a/apps/server/src/cloud/cloud.service.ts b/apps/server/src/cloud/cloud.service.ts index 51155604..d97ca1d9 100644 --- a/apps/server/src/cloud/cloud.service.ts +++ b/apps/server/src/cloud/cloud.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { PrismaService } from 'src/prisma/prisma.service'; import * as data from '../../node.json'; @@ -7,7 +7,35 @@ export class CloudService { constructor(private readonly prisma: PrismaService) {} async findCloudResourcePrices() { - // console.log(Object.values(data)); + return Object.fromEntries(await this.getCloudResourcesMap()); + } + + async calculatePrice(nodes?: Record) { + if (nodes && !Object.keys(nodes).length) throw new NotFoundException(); + const cloudResourcesPriceMap = await this.getCloudResourcesMap(); + const nodeValues = Object.values(data); + const totalMonthPrice = nodeValues.reduce( + (price, nodeValue): number => { + if (nodeValue.properties['server_spec_code']) { + if ( + !cloudResourcesPriceMap.has( + nodeValue.properties['server_spec_code'], + ) + ) + throw new NotFoundException(); + price += cloudResourcesPriceMap.get( + nodeValue.properties['server_spec_code'], + ).monthCost as number; + return price; + } + return price; + }, + 0, + ); + return { totalMonthPrice }; + } + + private async getCloudResourcesMap() { const prices = await this.prisma.ncloudServerResource.findMany({ select: { serverSpecCode: true, @@ -25,23 +53,6 @@ export class CloudService { monthCost, }); }); - return Object.fromEntries(priceMap); - } - - async calculatePrice(nodes?: Record) { - const prices = await this.findCloudResourcePrices(); - const nodeValues = Object.values(data); - const totalMonthPrice = nodeValues.reduce( - (price, nodeValue): number => { - if (nodeValue.properties['server_spec_code']) { - price += prices[nodeValue.properties['server_spec_code']] - .monthCost as number; - return price; - } - return price; - }, - 0, - ); - return { totalMonthPrice }; + return priceMap; } } From 02537b6576231312312f370ab29f6ccd323a7261 Mon Sep 17 00:00:00 2001 From: Geonhyuk Seo Date: Thu, 5 Dec 2024 03:59:06 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=A4=96=20Refactor(server):=20nodes?= =?UTF-8?q?=EB=A5=BC=20=EB=B3=B4=EA=B3=A0=20=EA=B0=80=EA=B2=A9=EC=9D=84=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=ED=95=B4=EC=A3=BC=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/cloud/cloud.controller.ts | 8 ++++---- apps/server/src/cloud/cloud.module.ts | 1 + apps/server/src/cloud/cloud.service.ts | 4 ++-- .../private-architecture.module.ts | 3 ++- .../private-architecture.service.ts | 20 +++++++++++-------- .../public-architecture.module.ts | 3 ++- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/apps/server/src/cloud/cloud.controller.ts b/apps/server/src/cloud/cloud.controller.ts index 886d0c36..94239ddc 100644 --- a/apps/server/src/cloud/cloud.controller.ts +++ b/apps/server/src/cloud/cloud.controller.ts @@ -10,8 +10,8 @@ export class CloudController { return this.service.findCloudResourcePrices(); } - @Get('/test') - getTestPrices() { - return this.service.calculatePrice(); - } + // @Get('/test') + // getTestPrices() { + // return this.service.calculatePrice(); + // } } diff --git a/apps/server/src/cloud/cloud.module.ts b/apps/server/src/cloud/cloud.module.ts index 53cdcf81..883c9a03 100644 --- a/apps/server/src/cloud/cloud.module.ts +++ b/apps/server/src/cloud/cloud.module.ts @@ -7,5 +7,6 @@ import { PrismaModule } from 'src/prisma/prisma.module'; imports: [PrismaModule], controllers: [CloudController], providers: [CloudService], + exports: [CloudService], }) export class CloudModule {} diff --git a/apps/server/src/cloud/cloud.service.ts b/apps/server/src/cloud/cloud.service.ts index d97ca1d9..13ac7a53 100644 --- a/apps/server/src/cloud/cloud.service.ts +++ b/apps/server/src/cloud/cloud.service.ts @@ -10,10 +10,10 @@ export class CloudService { return Object.fromEntries(await this.getCloudResourcesMap()); } - async calculatePrice(nodes?: Record) { + async calculatePrice(nodes: Record) { if (nodes && !Object.keys(nodes).length) throw new NotFoundException(); const cloudResourcesPriceMap = await this.getCloudResourcesMap(); - const nodeValues = Object.values(data); + const nodeValues = Object.values(nodes); const totalMonthPrice = nodeValues.reduce( (price, nodeValue): number => { if (nodeValue.properties['server_spec_code']) { diff --git a/apps/server/src/private-architecture/private-architecture.module.ts b/apps/server/src/private-architecture/private-architecture.module.ts index 31b13f88..18dc9e47 100644 --- a/apps/server/src/private-architecture/private-architecture.module.ts +++ b/apps/server/src/private-architecture/private-architecture.module.ts @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common'; import { PrivateArchitectureService } from './private-architecture.service'; import { PrivateArchitectureController } from './private-architecture.controller'; import { PrismaModule } from 'src/prisma/prisma.module'; +import { CloudModule } from 'src/cloud/cloud.module'; @Module({ - imports: [PrismaModule], + imports: [PrismaModule, CloudModule], controllers: [PrivateArchitectureController], providers: [PrivateArchitectureService], }) diff --git a/apps/server/src/private-architecture/private-architecture.service.ts b/apps/server/src/private-architecture/private-architecture.service.ts index b9b44cb7..84ec5249 100644 --- a/apps/server/src/private-architecture/private-architecture.service.ts +++ b/apps/server/src/private-architecture/private-architecture.service.ts @@ -8,23 +8,27 @@ import { RemoveArchitectureDto } from './dto/remove-architecture.dto'; import { RemoveVersionDto } from './dto/remove-version.dto'; import { SaveArchitectureDto } from './dto/save-architecture.dto'; import { SaveVersionDto } from './dto/save-version.dto'; +import { CloudService } from 'src/cloud/cloud.service'; @Injectable() export class PrivateArchitectureService { - constructor(private readonly prisma: PrismaService) {} + constructor( + private readonly prisma: PrismaService, + private readonly cloud: CloudService, + ) {} - saveArchitecture({ + async saveArchitecture({ title, userId: authorId, architecture, - cost, }: SaveArchitectureDto) { + const cost = await this.cloud.calculatePrice(architecture?.nodes); return this.prisma.privateArchitecture.create({ data: { title, authorId, architecture, - cost, + cost: cost.totalMonthPrice, }, }); } @@ -53,9 +57,9 @@ export class PrivateArchitectureService { userId: authorId, title, architecture, - cost, }: ModifyArchitectureDto) { const isAuthor = await this.isArchitectureAuthor(id, authorId); + const cost = await this.cloud.calculatePrice(architecture?.nodes); if (!isAuthor) throw new ForbiddenException(); return this.prisma.privateArchitecture.update({ where: { @@ -65,7 +69,7 @@ export class PrivateArchitectureService { data: { title, architecture, - cost, + cost: cost.totalMonthPrice, }, }); } @@ -108,16 +112,16 @@ export class PrivateArchitectureService { userId: authorId, title, architecture, - cost, }: SaveVersionDto) { const isAuthor = await this.isArchitectureAuthor(id, authorId); + const cost = await this.cloud.calculatePrice(architecture?.nodes); if (!isAuthor) throw new ForbiddenException(); return this.prisma.version.create({ data: { privateArchitectureId: id, title, architecture, - cost, + cost: cost.totalMonthPrice, }, }); } diff --git a/apps/server/src/public-architecture/public-architecture.module.ts b/apps/server/src/public-architecture/public-architecture.module.ts index 1cf85520..8ff6220a 100644 --- a/apps/server/src/public-architecture/public-architecture.module.ts +++ b/apps/server/src/public-architecture/public-architecture.module.ts @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common'; import { PublicArchitectureService } from './public-architecture.service'; import { PublicArchitectureController } from './public-architecture.controller'; import { PrismaModule } from 'src/prisma/prisma.module'; +import { CloudModule } from 'src/cloud/cloud.module'; @Module({ - imports: [PrismaModule], + imports: [PrismaModule, CloudModule], controllers: [PublicArchitectureController], providers: [PublicArchitectureService], }) From efa2e823e4a2fa7e2dd3645c6892d52f4d7dc694 Mon Sep 17 00:00:00 2001 From: Geonhyuk Seo Date: Thu, 5 Dec 2024 04:19:38 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=A4=96=20Refactor(server):=20?= =?UTF-8?q?=ED=8D=BC=EB=B8=94=EB=A6=AD=20=EC=95=84=ED=82=A4=ED=85=8D?= =?UTF-8?q?=EC=B2=98=20=EA=B0=80=EA=B2=A9=20=EA=B3=84=EC=82=B0=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/app.module.ts | 2 +- .../public-architecture.service.ts | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/apps/server/src/app.module.ts b/apps/server/src/app.module.ts index 1b44ec24..5e972ef2 100644 --- a/apps/server/src/app.module.ts +++ b/apps/server/src/app.module.ts @@ -24,6 +24,6 @@ import { CloudModule } from './cloud/cloud.module'; CloudModule, ], controllers: [AppController], - providers: [AppService, PrismaService], + providers: [AppService, PrismaService, NcloudResourcesService], }) export class AppModule {} diff --git a/apps/server/src/public-architecture/public-architecture.service.ts b/apps/server/src/public-architecture/public-architecture.service.ts index 74d7a44b..02ed2b70 100644 --- a/apps/server/src/public-architecture/public-architecture.service.ts +++ b/apps/server/src/public-architecture/public-architecture.service.ts @@ -13,10 +13,14 @@ import { RemoveArchitectureDto } from './dto/remove-architecture.dto'; import { UnstarDto } from './dto/unstar.dto'; import { StarDto } from './dto/star.dto'; import { ImportDto } from './dto/import.dto'; +import { CloudService } from 'src/cloud/cloud.service'; @Injectable() export class PublicArchitectureService { - constructor(private readonly prisma: PrismaService) {} + constructor( + private readonly prisma: PrismaService, + private readonly cloud: CloudService, + ) {} async findArchitectures(queryParams: FindArchitecturesDto) { const [data, total] = await this.prisma.$transaction([ @@ -55,18 +59,18 @@ export class PublicArchitectureService { return { data, total }; } - saveArchitecture({ + async saveArchitecture({ title, architecture, - cost, tags, userId: authorId, }: SaveArchitectureDto) { + const cost = await this.cloud.calculatePrice(architecture?.nodes); return this.prisma.publicArchitecture.create({ data: { title, architecture, - cost, + cost: cost.totalMonthPrice, authorId, tags: { create: tags.map((name) => ({ From 6fa73a29b5bf84a6bad921198f32918ecf77c9da Mon Sep 17 00:00:00 2001 From: Geonhyuk Seo Date: Thu, 5 Dec 2024 04:21:59 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=90=9E=20Fix(server):=20=EC=95=88=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EB=8A=94=20json=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EB=95=8C=EB=AC=B8=EC=97=90=20=EB=B9=8C=EB=93=9C=20?= =?UTF-8?q?=EC=95=88=EB=90=98=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/cloud/cloud.service.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/server/src/cloud/cloud.service.ts b/apps/server/src/cloud/cloud.service.ts index 13ac7a53..7226798c 100644 --- a/apps/server/src/cloud/cloud.service.ts +++ b/apps/server/src/cloud/cloud.service.ts @@ -1,6 +1,5 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { PrismaService } from 'src/prisma/prisma.service'; -import * as data from '../../node.json'; @Injectable() export class CloudService { From 3593d518db695016b13c6aea2e5afda9d3c00506 Mon Sep 17 00:00:00 2001 From: Geonhyuk Seo Date: Thu, 5 Dec 2024 04:42:23 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=E2=9C=A8=20Feat(server):=20ioredis=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20price=20?= =?UTF-8?q?=EC=BA=90=EC=8B=B1=20=EC=9E=91=EC=97=85=20=EC=A7=84=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/app.module.ts | 5 ++++ apps/server/src/cloud/cloud.service.ts | 26 ++++++++++++------- .../ncloud-resource.service.ts | 11 +++++++- pnpm-lock.yaml | 18 ++++++------- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/apps/server/src/app.module.ts b/apps/server/src/app.module.ts index 5e972ef2..157c6f0b 100644 --- a/apps/server/src/app.module.ts +++ b/apps/server/src/app.module.ts @@ -11,9 +11,14 @@ import { MyModule } from './my/my.module'; import { ScheduleModule } from '@nestjs/schedule'; import { NcloudResourcesService } from './ncloud-resource/ncloud-resource.service.js'; import { CloudModule } from './cloud/cloud.module'; +import { RedisModule } from '@nestjs-modules/ioredis'; @Module({ imports: [ + RedisModule.forRoot({ + type: 'single', + url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`, + }), ConfigModule.forRoot(), AuthModule, UserModule, diff --git a/apps/server/src/cloud/cloud.service.ts b/apps/server/src/cloud/cloud.service.ts index 7226798c..bfea8df2 100644 --- a/apps/server/src/cloud/cloud.service.ts +++ b/apps/server/src/cloud/cloud.service.ts @@ -1,9 +1,14 @@ +import { InjectRedis } from '@nestjs-modules/ioredis'; import { Injectable, NotFoundException } from '@nestjs/common'; +import Redis from 'ioredis'; import { PrismaService } from 'src/prisma/prisma.service'; @Injectable() export class CloudService { - constructor(private readonly prisma: PrismaService) {} + constructor( + private readonly prisma: PrismaService, + @InjectRedis() private readonly redis: Redis, + ) {} async findCloudResourcePrices() { return Object.fromEntries(await this.getCloudResourcesMap()); @@ -35,14 +40,17 @@ export class CloudService { } private async getCloudResourcesMap() { - const prices = await this.prisma.ncloudServerResource.findMany({ - select: { - serverSpecCode: true, - productName: true, - hourCost: true, - monthCost: true, - }, - }); + const cachePrices = JSON.parse(await this.redis.get('cloudResource')); + const prices = + cachePrices || + (await this.prisma.ncloudServerResource.findMany({ + select: { + serverSpecCode: true, + productName: true, + hourCost: true, + monthCost: true, + }, + })); const priceMap = new Map>(); prices.map((price) => { const { productName, hourCost, monthCost } = price; diff --git a/apps/server/src/ncloud-resource/ncloud-resource.service.ts b/apps/server/src/ncloud-resource/ncloud-resource.service.ts index feb04857..d580bc05 100644 --- a/apps/server/src/ncloud-resource/ncloud-resource.service.ts +++ b/apps/server/src/ncloud-resource/ncloud-resource.service.ts @@ -1,11 +1,16 @@ import { Ncloud, PriceApi, ApiKeyCredentials } from '@cloud-canvas/ncloud-sdk'; +import { InjectRedis } from '@nestjs-modules/ioredis'; import { Injectable } from '@nestjs/common'; import { Cron, CronExpression } from '@nestjs/schedule'; +import Redis from 'ioredis'; import { PrismaService } from 'src/prisma/prisma.service'; @Injectable() export class NcloudResourcesService { - constructor(private readonly prisma: PrismaService) {} + constructor( + private readonly prisma: PrismaService, + @InjectRedis() private readonly redis: Redis, + ) {} @Cron(CronExpression.EVERY_DAY_AT_3AM, { name: 'Insert Ncloud Resource Cron Job', @@ -92,6 +97,10 @@ export class NcloudResourcesService { await tx.ncloudServerResource.createMany({ data: flattenedResources, }); + await this.redis.set( + 'cloudResource', + JSON.stringify(flattenedResources), + ); }); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a5147a6d..e0049d76 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9497,8 +9497,8 @@ snapshots: '@typescript-eslint/parser': 6.21.0(eslint@8.57.1)(typescript@5.6.3) eslint: 8.57.1 eslint-import-resolver-node: 0.3.9 - eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1) - eslint-plugin-import: 2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1) + eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0)(eslint@8.57.1) + eslint-plugin-import: 2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1) eslint-plugin-jsx-a11y: 6.10.2(eslint@8.57.1) eslint-plugin-react: 7.37.2(eslint@8.57.1) eslint-plugin-react-hooks: 5.0.0(eslint@8.57.1) @@ -9521,37 +9521,37 @@ snapshots: transitivePeerDependencies: - supports-color - eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1): + eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0)(eslint@8.57.1): dependencies: '@nolyfill/is-core-module': 1.0.39 debug: 4.3.7 enhanced-resolve: 5.17.1 eslint: 8.57.1 - eslint-module-utils: 2.12.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1) + eslint-module-utils: 2.12.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1) fast-glob: 3.3.2 get-tsconfig: 4.8.1 is-bun-module: 1.2.1 is-glob: 4.0.3 optionalDependencies: - eslint-plugin-import: 2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1) + eslint-plugin-import: 2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1) transitivePeerDependencies: - '@typescript-eslint/parser' - eslint-import-resolver-node - eslint-import-resolver-webpack - supports-color - eslint-module-utils@2.12.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1): + eslint-module-utils@2.12.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1): dependencies: debug: 3.2.7 optionalDependencies: '@typescript-eslint/parser': 6.21.0(eslint@8.57.1)(typescript@5.6.3) eslint: 8.57.1 eslint-import-resolver-node: 0.3.9 - eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1) + eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0)(eslint@8.57.1) transitivePeerDependencies: - supports-color - eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1): + eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1): dependencies: '@rtsao/scc': 1.1.0 array-includes: 3.1.8 @@ -9562,7 +9562,7 @@ snapshots: doctrine: 2.1.0 eslint: 8.57.1 eslint-import-resolver-node: 0.3.9 - eslint-module-utils: 2.12.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.31.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint@8.57.1))(eslint@8.57.1))(eslint@8.57.1) + eslint-module-utils: 2.12.0(@typescript-eslint/parser@6.21.0(eslint@8.57.1)(typescript@5.6.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1) hasown: 2.0.2 is-core-module: 2.15.1 is-glob: 4.0.3