Bladeren bron

add mogran logger and fix cron error

Signed-off-by: Gitea <zizhao.chen@zilliz.com>
Gitea 3 jaren geleden
bovenliggende
commit
7b9018aa74
6 gewijzigde bestanden met toevoegingen van 745 en 111 verwijderingen
  1. 20 2
      express/package.json
  2. 9 9
      express/src/app.ts
  3. 4 3
      express/src/crons/crons.service.ts
  4. 0 90
      express/src/interceptors/index.ts
  5. 106 0
      express/src/middlewares/index.ts
  6. 606 7
      express/yarn.lock

+ 20 - 2
express/package.json

@@ -4,12 +4,16 @@
   "main": "dist/app.js",
   "license": "MIT",
   "dependencies": {
+    "@types/chalk": "^2.2.0",
+    "@types/morgan": "^1.9.3",
     "@zilliz/milvus2-sdk-node": "^1.0.19",
+    "chalk": "^4.1.2",
     "cors": "^2.8.5",
     "cross-env": "^7.0.3",
     "express": "^4.17.1",
     "glob": "^7.2.0",
     "helmet": "^4.6.0",
+    "morgan": "^1.10.0",
     "node-cron": "^3.0.0",
     "rimraf": "^3.0.2",
     "socket.io": "^4.3.1"
@@ -21,6 +25,7 @@
     "@types/node": "^16.11.6",
     "@types/node-cron": "^3.0.0",
     "@types/ws": "^8.2.0",
+    "nodemon": "^2.0.14",
     "ts-node": "^10.4.0",
     "tslint": "^6.1.3",
     "typescript": "^4.4.4"
@@ -29,10 +34,23 @@
     "prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
     "build": "yarn clean && tsc",
     "prestart": "yarn build",
-    "start": "node .",
+    "start": "nodemon ./src/app",
     "start:plugin": "yarn build && cross-env PLUGIN_DEV=1 node dist/milvus-insight/express/src/app.js",
     "start:prod": "node dist/app.js",
     "test": "echo \"Error: no test specified\" && exit 1",
     "clean": "rimraf dist"
+  },
+  "nodemonConfig": {
+    "ignore": [
+      "**/*.test.ts",
+      "**/*.spec.ts",
+      "build",
+      ".git",
+      "node_modules"
+    ],
+    "watch": [
+      "src"
+    ],
+    "ext": "ts"
   }
-}
+}

+ 9 - 9
express/src/app.ts

@@ -10,12 +10,13 @@ import { router as schemaRouter } from "./schema";
 import { router as cronsRouter } from "./crons";
 import { pubSub } from "./events";
 import {
-  TransformResInterceptor,
-  LoggingInterceptor,
-  ErrorInterceptor,
-} from "./interceptors";
+  TransformResMiddlerware,
+  LoggingMiddleware,
+  ErrorMiddleware,
+} from "./middlewares";
 import { getDirectories, generateCfgs } from "./utils";
 import * as path from "path";
+import chalk from "chalk";
 
 const PLUGIN_DEV = process.env?.PLUGIN_DEV;
 const SRC_PLUGIN_DIR = "src/plugins";
@@ -42,11 +43,10 @@ app.use(
   })
 );
 app.use(express.json({ limit: "150MB" }));
-
 // TransformResInterceptor
-app.use(TransformResInterceptor);
+app.use(TransformResMiddlerware);
 // LoggingInterceptor
-app.use(LoggingInterceptor);
+app.use(LoggingMiddleware);
 
 const router = express.Router();
 const pluginsRouter = express.Router();
@@ -118,9 +118,9 @@ getDirectories(SRC_PLUGIN_DIR, async (dirErr: Error, dirRes: [string]) => {
   });
 
   // ErrorInterceptor
-  app.use(ErrorInterceptor);
+  app.use(ErrorMiddleware);
   // start server
   server.listen(PORT, () => {
-    console.log(`Server started on port ${PORT} :)`);
+    console.log(chalk.green.bold(`Insight Server started on port ${PORT} :)`));
   });
 });

+ 4 - 3
express/src/crons/crons.service.ts

@@ -7,13 +7,14 @@ export class CronsService {
   constructor(
     private collectionService: CollectionsService,
     private schedulerRegistry: SchedulerRegistry
-  ) {
-    this.getCollections(WS_EVENTS.COLLECTION + "");
-  }
+  ) {}
 
   async toggleCronJobByName(data: { name: string; type: WS_EVENTS_TYPE }) {
     const { name, type } = data;
     const cronJobEntity = this.schedulerRegistry.getCronJob(name);
+    if (!cronJobEntity && Number(type) === WS_EVENTS_TYPE.START) {
+      return this.getCollections(WS_EVENTS.COLLECTION);
+    }
     return Number(type) === WS_EVENTS_TYPE.STOP
       ? cronJobEntity.stop()
       : cronJobEntity.start();

+ 0 - 90
express/src/interceptors/index.ts

@@ -1,90 +0,0 @@
-import { Request, Response, NextFunction, Errback } from "express";
-
-// TransformResInterceptor
-export const TransformResInterceptor = (
-  req: Request,
-  res: Response,
-  next: NextFunction
-) => {
-  const oldSend = res.json;
-  res.json = (data) => {
-    // console.log(data); // do something with the data
-    const statusCode = data?.statusCode;
-    const message = data?.message;
-    const error = data?.error;
-    res.json = oldSend; // set function back to avoid the 'double-send'
-    if (statusCode || message || error) {
-      return res.json({ statusCode, message, error });
-    }
-    return res.json({ data, statusCode: 200 }); // just call as normal with data
-  };
-  next();
-};
-
-const getDurationInMilliseconds = (start: any) => {
-  const NS_PER_SEC = 1e9;
-  const NS_TO_MS = 1e6;
-  const diff = process.hrtime(start);
-
-  return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
-};
-
-/**
- * Add spent time looger when accessing milvus.
- */
-export const LoggingInterceptor = (
-  req: Request,
-  res: Response,
-  next: NextFunction
-) => {
-  console.log(`${req.method} ${req.originalUrl} [STARTED]`);
-  const start = process.hrtime();
-  const { ip = "", method = "", originalUrl = "", headers = {} } = req;
-  const ua = headers["user-agent"] || "";
-
-  res.on("finish", () => {
-    const durationInMilliseconds = getDurationInMilliseconds(start);
-    console.log(
-      `${req.method} ${
-        req.originalUrl
-      } [FINISHED] ${durationInMilliseconds.toLocaleString()} ms`
-    );
-  });
-
-  res.on("close", () => {
-    const durationInMilliseconds = getDurationInMilliseconds(start);
-    const { statusCode = "" } = res;
-    // TODO: Need some special log instead of console.log
-    console.log(
-      `${req.method} ${
-        req.originalUrl
-      } [CLOSED] ${durationInMilliseconds.toLocaleString()} ms ip:${ip} ua:${ua} status:${statusCode}`
-    );
-  });
-
-  next();
-};
-
-/**
- * Handle error in here.
- * Normally depend on status which from milvus service.
- */
-export const ErrorInterceptor = (
-  err: Error,
-  req: Request,
-  res: Response,
-  next: NextFunction
-) => {
-  console.log("---error interceptor---\n%s", err);
-  // Boolean property that indicates if the app sent HTTP headers for the response.
-  // Here to prevent sending response after header has been sent.
-  if (res.headersSent) {
-    return next(err);
-  }
-  if (err) {
-    res
-      .status(500)
-      .json({ message: `${err}`, error: "Bad Request", statusCode: 500 });
-  }
-  next();
-};

+ 106 - 0
express/src/middlewares/index.ts

@@ -0,0 +1,106 @@
+import { Request, Response, NextFunction, Errback } from "express";
+import morgan from "morgan";
+import chalk from "chalk";
+
+export const TransformResMiddlerware = (
+  req: Request,
+  res: Response,
+  next: NextFunction
+) => {
+  const oldSend = res.json;
+  res.json = (data) => {
+    // console.log(data); // do something with the data
+    const statusCode = data?.statusCode;
+    const message = data?.message;
+    const error = data?.error;
+    res.json = oldSend; // set function back to avoid the 'double-send'
+    if (statusCode || message || error) {
+      return res.json({ statusCode, message, error });
+    }
+    return res.json({ data, statusCode: 200 }); // just call as normal with data
+  };
+  next();
+};
+
+const getDurationInMilliseconds = (start: any) => {
+  const NS_PER_SEC = 1e9;
+  const NS_TO_MS = 1e6;
+  const diff = process.hrtime(start);
+
+  return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
+};
+
+/**
+ * Add spent time looger when accessing milvus.
+ */
+// export const LoggingMiddleware = (
+//   req: Request,
+//   res: Response,
+//   next: NextFunction
+// ) => {
+//   console.log(`${req.method} ${req.originalUrl} [STARTED]`);
+//   const start = process.hrtime();
+//   const { ip = "", method = "", originalUrl = "", headers = {} } = req;
+//   const ua = headers["user-agent"] || "";
+
+//   res.on("finish", () => {
+//     const durationInMilliseconds = getDurationInMilliseconds(start);
+//     console.log(
+//       `${req.method} ${
+//         req.originalUrl
+//       } [FINISHED] ${durationInMilliseconds.toLocaleString()} ms`
+//     );
+//   });
+
+//   res.on("close", () => {
+//     const durationInMilliseconds = getDurationInMilliseconds(start);
+//     const { statusCode = "" } = res;
+//     // TODO: Need some special log instead of console.log
+//     console.log(
+//       `${req.method} ${
+//         req.originalUrl
+//       } [CLOSED] ${durationInMilliseconds.toLocaleString()} ms ip:${ip} ua:${ua} status:${statusCode}`
+//     );
+//   });
+
+//   next();
+// };
+
+/**
+ * Handle error in here.
+ * Normally depend on status which from milvus service.
+ */
+export const ErrorMiddleware = (
+  err: Error,
+  req: Request,
+  res: Response,
+  next: NextFunction
+) => {
+  console.log(chalk.blue.bold(req.method, req.url), chalk.red.bold(err));
+  // Boolean property that indicates if the app sent HTTP headers for the response.
+  // Here to prevent sending response after header has been sent.
+  if (res.headersSent) {
+    return next(err);
+  }
+  if (err) {
+    res
+      .status(500)
+      .json({ message: `${err}`, error: "Bad Request", statusCode: 500 });
+  }
+  next();
+};
+
+export const LoggingMiddleware = morgan((tokens, req, res) => {
+  return [
+    "\n",
+    chalk.blue.bold(tokens.method(req, res)),
+    chalk.magenta.bold(tokens.status(req, res)),
+    chalk.green.bold(tokens.url(req, res)),
+    chalk.green.bold(tokens["response-time"](req, res) + " ms"),
+    chalk.green.bold("@ " + tokens.date(req, res)),
+    chalk.yellow(tokens["remote-addr"](req, res)),
+    chalk.hex("#fffa65").bold("from " + tokens.referrer(req, res)),
+    chalk.hex("#1e90ff")(tokens["user-agent"](req, res)),
+    "\n",
+  ].join(" ");
+});

File diff suppressed because it is too large
+ 606 - 7
express/yarn.lock


Some files were not shown because too many files changed in this diff