Skip to content

Commit

Permalink
"Update version and refine scripts, logging, comments
Browse files Browse the repository at this point in the history
Incremented the version for 3d-inventory-mongo-api in package and lock files. Simplified the npm scripts, removing redundant or domain-specific actions and sorting for more logical order. Revised and streamlined comments in various files for consistency and clarity.

Enhancements in logging, especially in models' router, now include information about the quantity of models returned or warning when none are found. Changes in logger.ts structure the log messages and console output for better readability.

No major consequences are expected from these changes, they aim at improving code organization, understanding, and logging efficiency."
  • Loading branch information
karol-preiskorn committed Jul 28, 2024
1 parent 0318a6d commit cc39b35
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 52 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 2 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "3d-inventory-mongo-api",
"version": "0.42.5",
"version": "0.42.6",
"description": "Mongo API for 3d-inventory project",
"keywords": [
"3d-inventory",
Expand All @@ -19,28 +19,19 @@
"author": "Karol Preiskorn",
"main": "index.js",
"scripts": {
"prebuild": "npm --no-git-tag-version version patch",
"build": "npm run versionPatch && npx tsc",
"clean:cache": "npm cache clean --force && rm -rf ./dist",
"clean:cleanup": "rm package-lock.json && rm -rf ./node_modules",
"cp": "cp -f README.md src/assets",
"predebug": "npm --no-git-tag-version version patch",
"debug": "npm run versionPatch && npm --no-git-tag-version version patch && nodemon",
"dev": "tsx watch src/index.ts",
"doc": "npx typedoc --readme ./README.md --name '3d-inventory-mongo-apo' --out docs src",
"gts:browse": "gcloud app browse",
"gts:clean": "gts clean",
"gts:deploy": "gcloud app deploy",
"gts:fix": "gts fix",
"gts:gcp-build": "",
"gts:lint": "gts lint",
"kill-port-usage-4200": "kill -s 9 `lsof -nP -iTCP:4200 -sTCP:LISTEN | sed -n 2p | awk '{print $2}'`",
"kill-port-usage-8080": "kill -s 9 `lsof -nP -iTCP:8080 -sTCP:LISTEN | sed -n 2p | awk '{print $2}'`",
"lint": "clear && /usr/bin/figlet -f ./src/assets/font.flf 'Eslint' && npx eslint \"src/**/*.{ts,tsx}\" --ignore-path .gitignore --config .eslintrc --fix",
"maven:latest": "cd api && mvn versions:use-latest-versions",
"maven:updates": "cd api && mvn versions:display-plugin-updates",
"nvm": "nvm ls-remote && nvm install-latest-npm",
"openapi:format": "npx openapi-format api/openapi.yaml --output api/openapi-formatted.yaml",
"prebuild": "npm --no-git-tag-version version patch",
"prepare": "node .husky/install.mjs && husky install",
"prepare:husky": "cd ../ && husky install server/.husky",
"prettier": "prettier --single-quote --check .",
Expand All @@ -54,7 +45,6 @@
"test:jest": "node --trace-deprecation node_modules/jest/bin/jest.js ",
"test:watch": "npm run test --watchAll",
"unused": "./unused.sh",
"update": "npm update",
"upgrade": "npm-check-updates -u && npm update",
"vscode:cache": "rm -rf '$HOME/.config/Code/Cache/*'"
},
Expand Down
4 changes: 1 addition & 3 deletions src/routers/devices.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* @file /routers/devices.js
* @module /routers
* @description devices router
* @version 2024-01-25 C2RLO - add new way to connect to DB
*/
**/

import '../utils/loadEnvironment'

Expand Down
16 changes: 11 additions & 5 deletions src/routers/models.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @file: /routers/models.js
* @description: This file contains the router for handling model-related API endpoints.
* @version: 2023-12-29 C2RLO Initial
*/
* @description: This file contains the router for handling model-related API endpoints.
* @version: 2023-12-29 C2RLO Initial
**/

import '../utils/loadEnvironment'

import express, { RequestHandler } from 'express'
import { Collection, Db, DeleteResult, InsertOneResult, ObjectId, OptionalId, UpdateFilter } from 'mongodb'

import { connectionClose, connectToCluster, connectToDb } from '../db/conn'
import { logger } from '../utils/logger'

export type Dimension = {
width: number
Expand Down Expand Up @@ -37,7 +37,13 @@ router.get('/', (async (req, res) => {
const db: Db = connectToDb(client)
const collection: Collection = db.collection(collectionName)
const results: object[] = await collection.find({}).limit(50).toArray()
res.status(200).send(results)
if (!results) {
logger.warn('GET /models - not found')
res.status(404).send('Not found')
} else {
logger.info('GET /models - oki return ' + results.length + ' models')
res.status(200).send(results)
}
await connectionClose(client)
}) as RequestHandler)

Expand Down
19 changes: 8 additions & 11 deletions src/utils/banner.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/**
* File: /utils/banner.js
* Description: Print banner log.
*
* Date By Comments
* ---------- ----- ------------------------------
* 2024-01-07 C2RLO Initial
* @description: Print banner log.
* @version: 2024-01-07 C2RLO Initial
*/

import { promises as fs } from "fs";
import figlet, { Fonts } from "figlet";
import { logger } from "./logger.js";
import figlet, { Fonts } from 'figlet'
import { promises as fs } from 'fs'

import { logger } from './logger.js'

let fontLoaded = false

Expand All @@ -31,6 +28,6 @@ async function loadFont(): Promise<void> {
* @returns {Promise<void>} A promise that resolves when the banner is displayed.
*/
export async function banner(): Promise<void> {
await loadFont();
logger.warn('\n' + figlet.textSync('3d-inv API', 'myfont' as Fonts));
await loadFont()
logger.warn('\n' + figlet.textSync('3d-inv API', 'myfont' as Fonts))
}
32 changes: 13 additions & 19 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* @file: /src/util/logger.ts
* @description: log information to console and files
* @version 2024-01-30 C2RLO - Add rotate files
* @version 2023-12-22 C2RLO - Add parent-module as label
Expand All @@ -15,38 +14,33 @@ const myFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message} ${process.env.PARENT_MODULE ? `parent-module: ${process.env.PARENT_MODULE}` : ''}`
})

const transport = new transports.DailyRotateFile({
filename: 'logs/%DATE%.log',
datePattern: 'YYYYMMDD',
maxFiles: '3d',
level: 'debug',
})

// transport.on('rotate', function (_oldFilename, _newFilename) {
// // do something fun
// });

export const logger = createLogger({
transports: [
transport,
new transports.Console({
new transports.DailyRotateFile({
level: 'debug',
handleExceptions: true,
filename: 'logs/%DATE%.log',
datePattern: 'YYYYMMDD',
maxFiles: '1d',
format: combine(
format.splat(),
colorize(),
timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
myFormat,
),
}),
new transports.File({
new transports.Console({
level: 'debug',
filename: 'logs/api.log',
handleExceptions: true,
maxsize: 1024, // 1MB
maxFiles: 3,
format: combine(
format.splat(),
colorize(),
timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
myFormat,
),
}),
],
})
Expand Down

0 comments on commit cc39b35

Please sign in to comment.