Skip to content

Commit

Permalink
feat: optimize code (#57)
Browse files Browse the repository at this point in the history
* feat: optimize code

* feat: optimize code
  • Loading branch information
Hoanle396 authored Aug 8, 2024
1 parent 3edf359 commit bcf773e
Show file tree
Hide file tree
Showing 24 changed files with 523 additions and 384 deletions.
28 changes: 18 additions & 10 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
include:
- project: foundation/templates/ci-template
file: backend-ci.yml
inputs:
branch: develop
- project: foundation/templates/ci-template
file: update-manifest.yml

# stages:
# - test
# - build
Expand All @@ -22,7 +30,7 @@
# image: $RUNNER_IMAGE
# script:
# - sh ./scripts/test.sh
# cache:
# cache:
# - <<: *global_cache_node_mods
# tags:
# - vm-docker
Expand All @@ -33,7 +41,7 @@
# .build_template: &build
# stage: build
# image: $RUNNER_IMAGE
# cache:
# cache:
# - <<: *global_cache_node_mods
# script:
# - sh ./scripts/build.sh
Expand All @@ -52,14 +60,14 @@
# image: $KANIKO_RUNNER_IMAGE
# script:
# - sh ./scripts/dockerize.sh
# cache:
# cache:
# - <<: *global_cache_node_mods
# tags:
# - vm-docker
# allow_failure: false
# when: on_success

# ##### node_module install step
# ##### node_module install step
# ## Stage: .pre
# ######
# install:
Expand All @@ -76,7 +84,7 @@
# # - nvm use
# - pnpm install
# - turbo prune server --docker
# tags:
# tags:
# - vm-docker
# rules: #add this to bypass merge requests
# - if: '$CI_PIPELINE_SOURCE =~ /.*/'
Expand Down Expand Up @@ -120,7 +128,7 @@
# only: # update this for staging
# - dev
# - develop
# needs:
# needs:
# - job: build:be
# artifacts: true

Expand All @@ -132,7 +140,7 @@
# only: # update this for staging
# - dev
# - develop
# needs:
# needs:
# - job: build:fe
# artifacts: true

Expand All @@ -155,13 +163,13 @@ dockerize:be:
variables:
REGISTRY: $REGISTRY_BE
DOCKERFILE_DIR: apps/server
only: # update this for staging
only: # update this for staging
- develop

dockerize:fe:
<<: *dockerize
variables:
REGISTRY: $REGISTRY_FE
DOCKERFILE_DIR: apps/web
only: # update this for staging
- develop
only: # update this for staging
- develop
30 changes: 25 additions & 5 deletions apps/server/src/controllers/crawl.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@ import { Request, Response } from 'express'
export class CrawlController {
constructor(private crawlService = new CrawlService()) {}

public crawl = (req: Request, res: Response) => {
public crawl = async (
req: Request<any, any, any, { dataRaw: Array<string> }>,
res: Response,
) => {
if (!Object.keys(req.query).includes('dataRaw')) {
return res.status(200).send('successful')
return res.status(400).send({
message: 'dataRaw is required',
code: 400,
})
}
// @ts-ignore
const fileChanges: Array<string> = req.query.dataRaw
try {
const fileChanges: Array<string> = req.query.dataRaw

if (fileChanges.length !== 0) this.crawlService.crawl(fileChanges)
if (fileChanges.length !== 0) {
const result = await this.crawlService.crawl(fileChanges)

return res.status(200).send('successful')
return res.status(200).json(result)
}

return res.status(200).json({
code: 200,
message: 'no file changes found',
})
} catch (error) {
return res.status(400).json({
code: 400,
message: error?.message ?? 'Something went wrong',
})
}
}
}
27 changes: 16 additions & 11 deletions apps/server/src/crawl/classificationCase.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { logger } from '@/utils/logger'
import 'dotenv/config'
import { filterNewFeaturesAndGlossaries } from './utils/filterNewFeaturesAndGlossaries'
import { filterNewParent } from './utils/filterNewParent'
import { filterNewSub } from './utils/filterNewSub'
import { filterNewSocial } from './utils/filterNewSocials'
import { filterNewFeaturesAndGlossaries } from './utils/filterNewFeaturesAndGlossaries'
import { filterNewSub } from './utils/filterNewSub'
import { filterProject } from './utils/filterProject'
import { removeTrash } from './utils/removeTrash'

Expand All @@ -20,14 +21,18 @@ export async function classificationCase(
fileChanges: Array<string>,
): Promise<DataReturn> {
// fileChange is an array of file path
try {
const dataReturn = new DataReturn()
const fileLay0 = removeTrash(fileChanges)
const fileLay1 = filterNewSocial(dataReturn, fileLay0)
const fileLay2 = await filterNewParent(dataReturn, fileLay1)
const fileLay3 = filterNewFeaturesAndGlossaries(dataReturn, fileLay2)
const fileLay4 = await filterNewSub(dataReturn, fileLay3)
await filterProject(dataReturn, fileLay4)

const dataReturn = new DataReturn()
const fileLay0 = removeTrash(fileChanges)
const fileLay1 = filterNewSocial(dataReturn, fileLay0)
const fileLay2 = await filterNewParent(dataReturn, fileLay1)
const fileLay3 = filterNewFeaturesAndGlossaries(dataReturn, fileLay2)
const fileLay4 = await filterNewSub(dataReturn, fileLay3)
await filterProject(dataReturn, fileLay4)

return dataReturn
return dataReturn
} catch (error) {
logger.info('Classification case error', error)
throw error
}
}
148 changes: 99 additions & 49 deletions apps/server/src/crawl/handleCase.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import 'dotenv/config'
import { DataReturn } from './classificationCase'
import { creatorFeature } from '@/creator/creatorFeature'
import { creatorGlossary } from '@/creator/creatorGlossary'
import { creatorParentCategory } from '@/creator/creatorParentCategory'
import { creatorSubCategory } from '@/creator/creatorSubCategory'
import { fsWrapper } from '@/utils/fs/fsWrapper'
import { deleteProject } from './utils/deleteProject'
import { creatorProject } from '@/creator/creatorProject'
import { creatorSocial } from '@/creator/creatorSocial'
import { creatorSubCategory } from '@/creator/creatorSubCategory'
import { connection } from '@/databases/connection'
import { Categories } from '@/databases/entities/Categories'
import { Projects } from '@/databases/entities/Projects'
import { ProjectJSON } from '@/shared/schema/ProjectJSON'
import { creatorSocial } from '@/creator/creatorSocial'
import { creatorFeature } from '@/creator/creatorFeature'
import { creatorGlossary } from '@/creator/creatorGlossary'
import { fsWrapper } from '@/utils/fs/fsWrapper'
import { getFileName } from '@/utils/getFileName'
import { logger } from '@/utils/logger'
import 'dotenv/config'
import { DataReturn } from './classificationCase'
import { deleteProject } from './utils/deleteProject'

function getCategoryName(projectFolder: string): string {
const array = projectFolder.split('/')
Expand All @@ -31,64 +32,113 @@ async function getCategory(projectFolder: string) {

export async function handleCase(caseData: DataReturn) {
if (caseData.isSocialCreate == true) {
creatorSocial()
try {
await creatorSocial()
} catch (error) {
logger.info('Social create error', error)
throw error
}
}

if (caseData.parentCategory.length !== 0) {
caseData.parentCategory.map((parent) => {
creatorParentCategory(`/${parent}`)
})
try {
await Promise.all(
caseData.parentCategory.map(async (parent) => {
await creatorParentCategory(`/${parent}`)
}),
)
} catch (error) {
logger.info('Parent category create error', error)
throw error
}
}

if (caseData.features.length !== 0) {
caseData.features.map(async (filePath) => {
const array = filePath.split('/')
const path = `/${array[0]}/${array[1]}`
await creatorFeature(path)
})
try {
await Promise.all(
caseData.features.map(async (filePath) => {
const array = filePath.split('/')
const path = `/${array[0]}/${array[1]}`
await creatorFeature(path)
}),
)
} catch (error) {
logger.info('Feature create error', error)
throw error
}
}

if (caseData.glossaries.length !== 0) {
caseData.glossaries.map(async (filePath) => {
const array = filePath.split('/')
const path = `/${array[0]}/${array[1]}`
await creatorGlossary(path)
})
try {
await Promise.all(
caseData.glossaries.map(async (filePath) => {
const array = filePath.split('/')
const path = `/${array[0]}/${array[1]}`
await creatorGlossary(path)
}),
)
} catch (error) {
logger.info('Glossary create error', error)
throw error
}
}

if (caseData.subCategory.length !== 0) {
caseData.subCategory.map((subPath) => {
creatorSubCategory(`/${subPath}`)
})
try {
await Promise.all(
caseData.subCategory.map((subPath) => {
creatorSubCategory(`/${subPath}`)
}),
)
} catch (error) {
logger.info('Sub category create error', error)
throw error
}
}

if (caseData.projectUpdate.length !== 0) {
caseData.projectUpdate.map(async (projectPath) => {
const detailRaw = await fsWrapper.readFile(`/${projectPath}/info.json`)
const detail: ProjectJSON = JSON.parse(detailRaw)

const project = await connection
.getRepository(Projects)
.findOneBy({ name: detail.display_term })

if (project) {
await deleteProject(project)
}

const category = await getCategory(projectPath)

creatorProject(`/${projectPath}`, category)
})
try {
await Promise.all(
caseData.projectUpdate.map(async (projectPath) => {
const detailRaw = await fsWrapper.readFile(
`/${projectPath}/info.json`,
)
const detail: ProjectJSON = JSON.parse(detailRaw)

const project = await connection
.getRepository(Projects)
.findOneBy({ name: detail.display_term })

if (project) {
await deleteProject(project)
}

const category = await getCategory(projectPath)

creatorProject(`/${projectPath}`, category)
}),
)
} catch (error) {
logger.info('Project update error', error)
throw error
}
}

if (caseData.projectDelete.length !== 0) {
caseData.projectDelete.map(async (path) => {
const name = getFileName(path)
const project = await connection
.getRepository(Projects)
.findOneBy({ name })

deleteProject(project)
})
try {
await Promise.all(
caseData.projectDelete.map(async (path) => {
const name = getFileName(path)
const project = await connection
.getRepository(Projects)
.findOneBy({ name })

await deleteProject(project)
}),
)
} catch (error) {
logger.info('Project delete error', error)
throw error
}
}
}
29 changes: 16 additions & 13 deletions apps/server/src/crawl/utils/deleteProject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { connection } from '@/databases/connection'
import { Projects } from '@/databases/entities/Projects'
import { logger } from '@/utils/logger'

export function deleteProject(project: Projects) {
export async function deleteProject(project: Projects) {
const projectSocialQuery = `
DELETE FROM project_socials
WHERE project_socials.project_id = ${project.id};
Expand Down Expand Up @@ -36,16 +37,18 @@ export function deleteProject(project: Projects) {
WHERE projects.id = ${project.id}
`

Promise.all([
connection.manager.query(projectSocialQuery),
connection.manager.query(partnershipQuery),
connection.manager.query(projectTagQuery),
connection.manager.query(projectFeatureQuery),
connection.manager.query(projectGlossaryQuery),
connection.manager.query(projectDescriptionQuery),
])
.then(() => {
connection.manager.query(projectQuery)
})
.catch((error) => console.log(error))
try {
await Promise.all([
connection.manager.query(projectSocialQuery),
connection.manager.query(partnershipQuery),
connection.manager.query(projectTagQuery),
connection.manager.query(projectFeatureQuery),
connection.manager.query(projectGlossaryQuery),
connection.manager.query(projectDescriptionQuery),
])
await connection.manager.query(projectQuery)
} catch (error) {
logger.info('delete project error', error)
throw error
}
}
Loading

0 comments on commit bcf773e

Please sign in to comment.