-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (80 loc) · 2.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require("express");
// get the client
const mysql = require("mysql2");
const cron = require("node-cron");
let shell = require("shelljs");
const ejs = require("ejs");
const bodyParser = require("body-parser");
const multer = require("multer"); // v1.0.5
const cors = require("cors");
const { exportJSONFile } = require("./utils/handleFile");
const { readReport } = require("./utils/readReport");
const {
AnalysisReportAndUpdateScore,
getNotScoredSubmissions,
} = require("./utils/scoring");
// create the connection to database
// connect to database
const config = {
connectionLimit: 10,
host: "127.0.0.1",
user: "root",
password: "",
database: "uet_calcifer",
};
const database = mysql.createPool(config);
const promiseDatabase = database.promise();
const app = express();
const port = 3001;
// for parsing multipart/form-data
const upload = multer();
// for parsing application/json
app.use(bodyParser.json());
// for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
const corsOptions = {
origin: "*",
methods: ["GET", "POST", "PUT"],
allowedHeaders: ["Content-Type"],
};
app.use(cors(corsOptions));
// routes-------------------------------------------------------
app.get("/", (req, res) => {
res.send("<h3>Hello this is auto scoring system!</h3>");
});
// -------------------------------------------------------------------
// handle 404 not found
app.use((req, res, next) => {
res.status(404).send("Sorry can't find that!");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
// -----------------------------------------------------------------
cron.schedule("*/5 * * * * *", function () {
console.log("Cron job==============================================");
Scoring(promiseDatabase);
});
// get data function------------------------------------
async function Scoring(promiseDatabase) {
const submissions = await getNotScoredSubmissions(promiseDatabase);
if (submissions.length == 0) {
console.log("All submissions is scored. Nothing to do!");
return;
}
console.log("[1] Getting submissions not scored...");
console.log("Number of submissions: ", submissions.length);
console.log("[2] Export data to json file");
exportJSONFile(submissions);
console.log("[3] Cypress run");
// shell.exec("npx cypress run --quiet --spec cypress/e2e/scoring/scoring.cy.js");
shell.exec("yarn run cy:scoring");
console.log("Cypress Done!!!");
console.log("[4] Reading cypress json report...");
let reports = readReport();
if (reports) {
console.log("Analysis results report...");
await AnalysisReportAndUpdateScore(promiseDatabase, reports);
console.log("\nScoring done!!! The End.");
}
}