Flare-k

[Add] Collect Executing Time

/* eslint-disable no-console */
import routes from "../routes";
import File from "../models/File";
import performance from "perf_hooks";
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvInsertWriter = createCsvWriter({
path: 'insertOutput.csv',
header: [
{id: 'ms', title: 'time'}
]
});
const csvSelectWriter = createCsvWriter({
path: 'selectOutput.csv',
header: [
{id: 'ms', title: 'time'}
]
});
export const home = async (req, res) => {
try {
const files = await File.find({}).sort({ _id: -1 }); // 모든 비디오를 가져온다.
res.render("home", { pageTitle: "Home", files }); // render DB에 저장된 video의 내용을 보여준다
} catch (error) {
console.log(error);
res.render("home", { pageTitle: "Home", files: [] });
}
try {
const files = await File.find({}).sort({ _id: -1 }); // 모든 파일을 가져온다.
res.render("home", { pageTitle: "Home", files }); // render DB에 저장된 file의 내용을 보여준다
} catch (error) {
console.log(error);
res.render("home", { pageTitle: "Home", files: [] });
}
};
const searchTime = new Array();
export const search = async (req, res) => {
console.time("SearchFunction");
const startTime = new Date().getTime();
const {
query: { term: searchingBy },
} = req; // == const searchingBy = req.query.term;
......@@ -23,7 +34,13 @@ export const search = async (req, res) => {
files = await File.find({
title: { $regex: searchingBy, $options: "i" }, // i를 옵션으로 추가하면 insensitive.. 대소문자 구분 안함.
});
console.timeEnd("SearchFunction");
const endTime = new Date().getTime(); // SELECT의 경우 파일 시간 측정
searchTime.push({ms: endTime - startTime});
if (searchTime.length === 3){
csvSelectWriter
.writeRecords(searchTime)
.then(() => console.log("The CSV file was written successfully~"));
}
} catch (error) {
console.log(error);
}
......@@ -33,57 +50,63 @@ export const search = async (req, res) => {
// upload 또한 upload를 준비하기 위한 get 페이지와 실제 데이터를 보내는 post 페이지가 필요하다.
export const getUpload = (req, res) =>
res.render("upload", { pageTitle: "Upload" });
res.render("upload", { pageTitle: "Upload" });
const insertTime = new Array();
export const postUpload = async (req, res) => {
// const {} 를 통해 body를 받아와 요청하는 정보들을 확인한다.
// 이는 pug와 db.js를 확인해야하는 듯 하다.
console.time("UploadFunction");
const {
body: { title },
file: { path }, // path로 할때는 로컬의 경로. S3는 location
} = req; // file에 path라는 요소가 있다.
const startTime = new Date().getTime();
// multer를 해야 파일이 넘어가는 것이 나타난다.
const {
body: { title },
file: { path },
} = req;
const newFile = await File.create({
fileUrl: path,
title,
const newFile = await File.create({
fileUrl: path,
title,
// 여기있는 fileUrl, title은 fileDB의 속성이다.
});
});
console.log(newFile);
console.timeEnd("UploadFunction");
const endTime = new Date().getTime(); // INSERT의 경우 파일 시간 측정
insertTime.push({ms: endTime - startTime});
if (insertTime.length === 3){
csvInsertWriter
.writeRecords(insertTime)
.then(() => console.log("The CSV file was written successfully~"));
}
res.redirect(routes.home);
};
export const fileDetail = async (req, res) => {
// console.log(req.params); params에 id가 있다는걸 알게 됨
const {
params: { id },
} = req;
try {
const file = await File.findById(id);
res.render("fileDetail", { pageTitle: file.title, file });
} catch (error) {
res.redirect(routes.home);
}
// console.log(req.params); params에 id가 있다는걸 알게 됨
const {
params: { id },
} = req;
try {
const file = await File.findById(id);
res.render("fileDetail", { pageTitle: file.title, file });
} catch (error) {
res.redirect(routes.home);
}
};
export const deleteFile = async (req, res) => {
const {
params: { id },
} = req;
try {
const file = await File.findById(id);
// video를 받아서 render로 통해 템플릿으로 던져준다,
if (String(file.creator) !== req.user.id) {
throw Error();
} else {
await File.findOneAndRemove({ _id: id });
const {
params: { id },
} = req;
try {
const file = await File.findById(id);
// file을 받아서 render로 통해 템플릿으로 던져준다,
if (String(file.creator) !== req.user.id) {
throw Error();
} else {
await File.findOneAndRemove({ _id: id });
}
} catch (error) {
console.log(error);
}
} catch (error) {
console.log(error);
}
// 삭제를 실패하던 성공하던 home으로 redirect한다.
res.redirect(routes.home);
// 삭제를 실패하던 성공하던 home으로 redirect한다.
res.redirect(routes.home);
};
......
import dotenv from "dotenv";
import multer from "multer";
import multerS3 from "multer-s3";
import aws from "aws-sdk";
import routes from "./routes";
dotenv.config();
const s3 = new aws.S3({
accessKeyId: process.env.AWS_KEY,
secretAccessKey: process.env.AWS_PRIVATEE_KEY,
region: "ap-northeast-2",
});
const multerFile = multer({ dest: "uploads/files/" });
export const localsMiddleware = (req, res, next) => {
......
......@@ -1797,6 +1797,11 @@
"resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz",
"integrity": "sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ=="
},
"bignumber.js": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A=="
},
"binary": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/binary/-/binary-0.3.0.tgz",
......@@ -2917,6 +2922,11 @@
"resolved": "https://registry.npmjs.org/cssesc/-/cssesc-3.0.0.tgz",
"integrity": "sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg=="
},
"csv-writer": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/csv-writer/-/csv-writer-1.6.0.tgz",
"integrity": "sha512-NOx7YDFWEsM/fTRAJjRpPp8t+MKRVvniAg9wQlUKx20MFrPs73WLJhFf5iteqrxNYnsy924K3Iroh3yNHeYd2g=="
},
"currently-unhandled": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz",
......@@ -5970,6 +5980,17 @@
"integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==",
"dev": true
},
"mysql": {
"version": "2.18.1",
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
"integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==",
"requires": {
"bignumber.js": "9.0.0",
"readable-stream": "2.3.7",
"safe-buffer": "5.1.2",
"sqlstring": "2.3.1"
}
},
"nan": {
"version": "2.14.1",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.14.1.tgz",
......@@ -8207,6 +8228,11 @@
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
},
"sqlstring": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
"integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A="
},
"sshpk": {
"version": "1.16.1",
"resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz",
......
......@@ -29,6 +29,7 @@
"connect-mongo": "^3.2.0",
"cookie-parser": "^1.4.5",
"css-loader": "^3.5.3",
"csv-writer": "^1.6.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-flash": "0.0.2",
......@@ -40,6 +41,7 @@
"morgan": "^1.10.0",
"multer": "^1.4.2",
"multer-s3": "^2.9.0",
"mysql": "^2.18.1",
"ngrok": "^3.2.7",
"node-sass": "^4.14.1",
"passport": "^0.4.1",
......