UI and better date generation finished
This commit is contained in:
+8
-57
@@ -1,59 +1,10 @@
|
||||
import { fileURLToPath } from "url";
|
||||
import { connection } from "./index.js";
|
||||
// Create the connection to database
|
||||
import mysql from "mysql2/promise";
|
||||
|
||||
// get data for users between 2 dates
|
||||
async function getTopTen() {
|
||||
let filteredResults;
|
||||
let startDate = "2021-01-01";
|
||||
let endDate = "2029-01-01";
|
||||
if (req.query.to) startDate = req.query.to;
|
||||
if (req.query.from) startDate = req.query.from;
|
||||
try {
|
||||
const [results, fields] = await connection.query(
|
||||
"SELECT t.user,t.time,t.date,t.project \
|
||||
FROM Timelog t\
|
||||
WHERE t.date BETWEEN ? AND ? ;",
|
||||
[startDate, endDate],
|
||||
);
|
||||
filteredResults = results;
|
||||
console.log(filteredResults);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
const connection = await mysql.createConnection({
|
||||
host: "localhost",
|
||||
user: "monty",
|
||||
password: "some_pass",
|
||||
database: "timelog",
|
||||
});
|
||||
|
||||
if (req.query.filterBy) filterBy = req.query.filterBy;
|
||||
// how much time each user has logged during that time period
|
||||
if (filterBy === "user") {
|
||||
let users = {};
|
||||
for (let i = 0; i < filteredResults.length; i++) {
|
||||
if (filteredResults[i].user in users) {
|
||||
users[filteredResults[i].user] += filteredResults[i].time;
|
||||
} else {
|
||||
users[filteredResults[i].user] = filteredResults[i].time;
|
||||
}
|
||||
}
|
||||
// Create items array
|
||||
let items = Object.keys(users).map(function (key) {
|
||||
return [key, users[key]];
|
||||
});
|
||||
|
||||
// Sort the array based on the second element
|
||||
items.sort(function (first, second) {
|
||||
return second[1] - first[1];
|
||||
});
|
||||
|
||||
// Create a new array with only the first 10 items
|
||||
return items.slice(0, 10);
|
||||
}
|
||||
}
|
||||
|
||||
export { getTopTen };
|
||||
|
||||
// SELECT u.f_name,u.l_name,u.mail,p.name,t.time,t.date
|
||||
// FROM Timelog t
|
||||
// INNER JOIN Project p ON p.id=t.project
|
||||
// INNER JOIN User u ON u.id=t.user
|
||||
// WHERE User = "100";
|
||||
// ORDER BY time
|
||||
// LIMIT 10 OFFSET 10
|
||||
export default connection;
|
||||
|
||||
+10
-170
@@ -1,179 +1,19 @@
|
||||
import express from "express";
|
||||
import mysql from "mysql2/promise";
|
||||
import { getTopTen } from "./db.js";
|
||||
import { resourceUsage } from "process";
|
||||
|
||||
export const connection = await mysql.createConnection({
|
||||
host: "localhost",
|
||||
user: "monty",
|
||||
password: "some_pass",
|
||||
database: "timelog",
|
||||
});
|
||||
connection.config.namedPlaceholders = true;
|
||||
import getAll from "./routers/getAll.js";
|
||||
import getUser from "./routers/getUser.js";
|
||||
import getTopTen from "./routers/getTopTen.js";
|
||||
import reset from "./routers/reset.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/getall", async (req, res) => {
|
||||
let sql =
|
||||
"SELECT u.f_name,u.l_name,u.mail,p.name,t.time,t.date,t.user \
|
||||
FROM Timelog t \
|
||||
INNER JOIN Project p ON p.id=t.project \
|
||||
INNER JOIN User u ON u.id=t.user ";
|
||||
let where = "WHERE t.date BETWEEN ? AND ? ";
|
||||
let order = "ORDER BY ?? ";
|
||||
let offsetSql = "LIMIT 10 OFFSET ?;";
|
||||
let flag = 0;
|
||||
if (req.query.from || req.query.to) {
|
||||
sql = sql + where;
|
||||
flag += 1;
|
||||
}
|
||||
if (req.query.sortby) {
|
||||
sql = sql + order;
|
||||
flag += 2;
|
||||
}
|
||||
sql = sql + offsetSql;
|
||||
let results;
|
||||
let fields;
|
||||
if (flag == 0) {
|
||||
[results, fields] = await connection.query(sql, [
|
||||
parseInt(req.query.offset),
|
||||
]);
|
||||
} else if (flag === 1) {
|
||||
[results, fields] = await connection.query(sql, [
|
||||
req.query.from,
|
||||
req.query.to,
|
||||
parseInt(req.query.offset),
|
||||
]);
|
||||
} else if (flag === 2) {
|
||||
[results, fields] = await connection.query(sql, [
|
||||
req.query.sortby,
|
||||
parseInt(req.query.offset),
|
||||
]);
|
||||
} else if (flag === 3) {
|
||||
[results, fields] = await connection.query(sql, [
|
||||
req.query.from,
|
||||
req.query.to,
|
||||
req.query.sortby,
|
||||
parseInt(req.query.offset),
|
||||
]);
|
||||
}
|
||||
res.json(results);
|
||||
});
|
||||
router.get("/gettopten", async (req, res) => {
|
||||
let filteredResults;
|
||||
let results2;
|
||||
let from = "2021-01-01";
|
||||
let to = "2029-01-01";
|
||||
let filterBy = "user";
|
||||
if (req.query.from) from = req.query.from;
|
||||
if (req.query.to) to = req.query.to;
|
||||
|
||||
try {
|
||||
const [results, fields] = await connection.query(
|
||||
"SELECT t.user,t.time,t.date,t.project \
|
||||
FROM Timelog t\
|
||||
WHERE t.date BETWEEN ? AND ? ;",
|
||||
[from, to],
|
||||
);
|
||||
filteredResults = results;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
if (req.query.filterBy) filterBy = req.query.filterBy;
|
||||
// how much time each user has logged during that time period
|
||||
if (filterBy === "user") {
|
||||
let users = {};
|
||||
for (let i = 0; i < filteredResults.length; i++) {
|
||||
if (filteredResults[i].user in users) {
|
||||
users[filteredResults[i].user] += filteredResults[i].time;
|
||||
} else {
|
||||
users[filteredResults[i].user] = filteredResults[i].time;
|
||||
}
|
||||
}
|
||||
// Create items array
|
||||
let items = Object.keys(users).map(function (key) {
|
||||
return [key, users[key]];
|
||||
});
|
||||
|
||||
// Sort the array based on the second element
|
||||
items.sort(function (first, second) {
|
||||
return second[1] - first[1];
|
||||
});
|
||||
|
||||
// Create a new array with only the first 10 items
|
||||
results2 = items.slice(0, 10);
|
||||
} else if (filterBy === "project") {
|
||||
let projects = {};
|
||||
for (let i = 0; i < filteredResults.length; i++) {
|
||||
if (filteredResults[i].project in projects) {
|
||||
projects[filteredResults[i].project] += filteredResults[i].time;
|
||||
} else {
|
||||
projects[filteredResults[i].project] = filteredResults[i].time;
|
||||
}
|
||||
}
|
||||
// Create items array
|
||||
let items = Object.keys(projects).map(function (key) {
|
||||
return [key, projects[key]];
|
||||
});
|
||||
|
||||
// Sort the array based on the second element
|
||||
items.sort(function (first, second) {
|
||||
return second[1] - first[1];
|
||||
});
|
||||
|
||||
// Create a new array with only the first 10 items
|
||||
results2 = items.slice(0, 10);
|
||||
}
|
||||
|
||||
res.json(results2);
|
||||
});
|
||||
router.get("/reset", async (req, res) => {
|
||||
try {
|
||||
const [results, fields] = await connection.query("CALL InitDb;", []);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
res.status(200).json({ message: "Success" });
|
||||
});
|
||||
router.get("/getUser", async (req, res) => {
|
||||
const userId = req.query.userid;
|
||||
let results, fields;
|
||||
console.log(userId);
|
||||
try {
|
||||
[results, fields] = await connection.query(
|
||||
"SELECT p.name,t.time,t.project \
|
||||
FROM Timelog t \
|
||||
INNER JOIN Project p ON p.id=t.project \
|
||||
INNER JOIN User u ON u.id=t.user \
|
||||
WHERE USER = ? ;",
|
||||
[userId],
|
||||
);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
let projects = {};
|
||||
let projectIdtoName = {};
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
if (results[i].project in projects) {
|
||||
projects[results[i].project] += results[i].time;
|
||||
} else {
|
||||
projects[results[i].project] = results[i].time;
|
||||
projectIdtoName[results[i].project] = results[i].name;
|
||||
}
|
||||
}
|
||||
// map projectIds to project names before sending the data
|
||||
let respData = {};
|
||||
for (let key in projects) {
|
||||
if (projects.hasOwnProperty(key)) {
|
||||
respData[projectIdtoName[key]] = projects[key];
|
||||
}
|
||||
}
|
||||
res.json(respData);
|
||||
});
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
app.use("/api", getAll);
|
||||
app.use("/api", getTopTen);
|
||||
app.use("/api", getUser);
|
||||
app.use("/api", reset);
|
||||
|
||||
app.use("/api", router);
|
||||
const PORT = process.env.PORT || 5000;
|
||||
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import express from "express";
|
||||
import connection from "../db.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/getall", async (req, res) => {
|
||||
let sql =
|
||||
"SELECT u.f_name,u.l_name,u.mail,p.name,t.time,t.date,t.user \
|
||||
FROM Timelog t \
|
||||
INNER JOIN Project p ON p.id=t.project \
|
||||
INNER JOIN User u ON u.id=t.user ";
|
||||
const where = "WHERE t.date BETWEEN ? AND ? ";
|
||||
const order = "ORDER BY ?? ";
|
||||
const offsetSql = "LIMIT 10 OFFSET ?;";
|
||||
let flag = 0;
|
||||
// construct the sql statement based on incoming request
|
||||
if (req.query.from || req.query.to) {
|
||||
sql = sql + where;
|
||||
flag += 1;
|
||||
}
|
||||
if (req.query.sortby) {
|
||||
sql = sql + order;
|
||||
flag += 2;
|
||||
}
|
||||
sql = sql + offsetSql;
|
||||
|
||||
let results;
|
||||
let fields;
|
||||
switch (flag) {
|
||||
case 0:
|
||||
[results, fields] = await connection.query(sql, [
|
||||
parseInt(req.query.offset),
|
||||
]);
|
||||
break;
|
||||
case 1:
|
||||
[results, fields] = await connection.query(sql, [
|
||||
req.query.from,
|
||||
req.query.to,
|
||||
parseInt(req.query.offset),
|
||||
]);
|
||||
break;
|
||||
case 2:
|
||||
[results, fields] = await connection.query(sql, [
|
||||
req.query.sortby,
|
||||
parseInt(req.query.offset),
|
||||
]);
|
||||
break;
|
||||
case 3:
|
||||
[results, fields] = await connection.query(sql, [
|
||||
req.query.from,
|
||||
req.query.to,
|
||||
req.query.sortby,
|
||||
parseInt(req.query.offset),
|
||||
]);
|
||||
break;
|
||||
}
|
||||
res.json(results);
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,69 @@
|
||||
import express from "express";
|
||||
import connection from "../db.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/gettopten", async (req, res) => {
|
||||
let from = "2020-01-01";
|
||||
let to = "2029-01-01";
|
||||
let filterBy = "user";
|
||||
if (req.query.from) from = req.query.from;
|
||||
if (req.query.to) to = req.query.to;
|
||||
if (req.query.filterBy) filterBy = req.query.filterBy;
|
||||
let results, fields;
|
||||
let filterBySql = filterBy === "user" ? "t.user" : "t.project";
|
||||
try {
|
||||
[results, fields] = await connection.query(
|
||||
"SELECT t.user,t.date,t.project,u.f_name,u.l_name,p.name,SUM(t.time) as total_time \
|
||||
FROM Timelog t \
|
||||
INNER JOIN Project p ON p.id=t.project \
|
||||
INNER JOIN User u ON u.id=t.user \
|
||||
WHERE t.date BETWEEN ? AND ? \
|
||||
GROUP BY ?? \
|
||||
ORDER BY total_time DESC\
|
||||
LIMIT 10;",
|
||||
[from, to, filterBySql],
|
||||
);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(400).json({ message: "Error" });
|
||||
}
|
||||
// how much time each user has logged during that time period
|
||||
// let items = {};
|
||||
// let itemIdtoName = {};
|
||||
// for (let i = 0; i < results.length; i++) {
|
||||
// if (results[i][filterBy] in items) {
|
||||
// items[results[i][filterBy]] += results[i].time;
|
||||
// } else {
|
||||
// items[results[i][filterBy]] = results[i].time;
|
||||
// if (filterBy === "user")
|
||||
// itemIdtoName[results[i][filterBy]] =
|
||||
// results[i].f_name + " " + results[i].l_name;
|
||||
// else itemIdtoName[results[i][filterBy]] = results[i].name;
|
||||
// }
|
||||
// }
|
||||
// let respData = {};
|
||||
// for (let key in items) {
|
||||
// if (items.hasOwnProperty(key)) {
|
||||
// respData[itemIdtoName[key]] = items[key];
|
||||
// }
|
||||
// }
|
||||
// // transform obj to array
|
||||
// respData = Object.keys(items).map(function (key) {
|
||||
// return [key, items[key]];
|
||||
// });
|
||||
//
|
||||
// // sort the array based on the second element
|
||||
// respData.sort(function (first, second) {
|
||||
// return second[1] - first[1];
|
||||
// });
|
||||
//
|
||||
// // console.log(itemIdtoName);
|
||||
// // console.log(respData);
|
||||
// // Create a new array with only the first 10 items
|
||||
// respData = respData.slice(0, 10);
|
||||
|
||||
res.json(results);
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,42 @@
|
||||
import express from "express";
|
||||
import connection from "../db.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/getUser", async (req, res) => {
|
||||
const userId = req.query.userid;
|
||||
let results, fields;
|
||||
try {
|
||||
[results, fields] = await connection.query(
|
||||
"SELECT p.name,t.time,t.project \
|
||||
FROM Timelog t \
|
||||
INNER JOIN Project p ON p.id=t.project \
|
||||
INNER JOIN User u ON u.id=t.user \
|
||||
WHERE USER = ? ;",
|
||||
[userId],
|
||||
);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(400).json("ERROR");
|
||||
}
|
||||
let projects = {};
|
||||
let projectIdtoName = {};
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
if (results[i].project in projects) {
|
||||
projects[results[i].project] += results[i].time;
|
||||
} else {
|
||||
projects[results[i].project] = results[i].time;
|
||||
projectIdtoName[results[i].project] = results[i].name;
|
||||
}
|
||||
}
|
||||
// map projectIds to project names before sending the data
|
||||
let respData = {};
|
||||
for (let key in projects) {
|
||||
if (projects.hasOwnProperty(key)) {
|
||||
respData[projectIdtoName[key]] = projects[key];
|
||||
}
|
||||
}
|
||||
res.json(respData);
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,16 @@
|
||||
import express from "express";
|
||||
import connection from "../db.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get("/reset", async (req, res) => {
|
||||
try {
|
||||
await connection.query("CALL InitDb;", []);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(400).json({ message: "Error" });
|
||||
}
|
||||
res.status(200).json({ message: "Success" });
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -127,3 +127,11 @@ END$$
|
||||
DELIMITER ;
|
||||
|
||||
|
||||
SELECT t.user,t.date,t.project,u.f_name,u.l_name,p.name,SUM(t.time) as total_time
|
||||
FROM Timelog t
|
||||
INNER JOIN Project p ON p.id=t.project
|
||||
INNER JOIN User u ON u.id=t.user
|
||||
GROUP BY t.user
|
||||
ORDER BY total_time DESC
|
||||
LIMIT 10;
|
||||
|
||||
|
||||
+41
-2
@@ -84,14 +84,28 @@ BEGIN
|
||||
DECLARE hours FLOAT;
|
||||
DECLARE project INT;
|
||||
DECLARE curDate DATE DEFAULT "2024-11-18";
|
||||
DECLARE h2 INT;
|
||||
|
||||
WHILE users <= 100 DO
|
||||
SET logs = FLOOR(1+(RAND()*20));
|
||||
SET j=1;
|
||||
WHILE j <= logs DO
|
||||
SET hours = (RAND() * (8 - 0.25)) + 0.25;
|
||||
SET curDate = DATE_ADD(curDate, INTERVAL 1 DAY);
|
||||
SET project = FLOOR(1+(RAND()*3));
|
||||
SET curDate = DATE_ADD('2020-01-01', INTERVAL FLOOR(RAND() * DATEDIFF('2020-02-01', '2020-01-01')) DAY);
|
||||
SET hours = (RAND() * (8 - 0.25)) + 0.25;
|
||||
|
||||
SELECT SUM(time) INTO h2
|
||||
FROM Timelog
|
||||
WHERE date = curdate && user = users ;
|
||||
|
||||
WHILE (h2+hours) > 8 DO
|
||||
SET curDate = DATE_ADD('2020-01-01', INTERVAL FLOOR(RAND() * DATEDIFF('2020-02-01', '2020-01-01')) DAY);
|
||||
|
||||
SELECT SUM(time)INTO h2
|
||||
FROM Timelog
|
||||
WHERE date = curdate && user = users ;
|
||||
|
||||
END WHILE;
|
||||
INSERT INTO Timelog (user, project, date,time ) VALUES (users,project,curDate,hours);
|
||||
SET j=j+1;
|
||||
END WHILE;
|
||||
@@ -104,3 +118,28 @@ DELIMITER ;
|
||||
##
|
||||
-- get data
|
||||
SELECT t.time,t.date,p.name,u.f_name,u.l_name,u.mail FROM Timelog t INNER JOIN Project p ON p.id=t.project INNER JOIN User u ON u.id=t.user;
|
||||
|
||||
CREATE PROCEDURE fill_timelog ()
|
||||
BEGIN
|
||||
DECLARE j INT DEFAULT 1;
|
||||
DECLARE users INT DEFAULT 1;
|
||||
DECLARE logs INT;
|
||||
DECLARE hours FLOAT;
|
||||
DECLARE project INT;
|
||||
DECLARE curDate DATE DEFAULT "2024-11-18";
|
||||
|
||||
WHILE users <= 100 DO
|
||||
SET logs = FLOOR(1+(RAND()*20));
|
||||
SET j=1;
|
||||
WHILE j <= logs DO
|
||||
SET hours = (RAND() * (8 - 0.25)) + 0.25;
|
||||
SET project = FLOOR(1+(RAND()*3));
|
||||
SET curDate = DATE_ADD(curDate, INTERVAL 1 DAY);
|
||||
INSERT INTO Timelog (user, project, date,time ) VALUES (users,project,curDate,hours);
|
||||
SET j=j+1;
|
||||
END WHILE;
|
||||
SET users=users+1;
|
||||
END WHILE;
|
||||
END$$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
Reference in New Issue
Block a user