From ca30554eb929da0794ccaba051550dc314196909 Mon Sep 17 00:00:00 2001 From: Oliver Boehlk Date: Sun, 19 Apr 2020 17:30:30 +0200 Subject: [PATCH 1/3] use new day api --- backend/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/backend/index.js b/backend/index.js index 8b1707d..4b89d90 100644 --- a/backend/index.js +++ b/backend/index.js @@ -42,20 +42,20 @@ app.all("/*", function (req, res, next) { }) app.get('/API/day', function (req, res) { - var date = new Date(req.query.date); const kind = parseInt(req.query.kind); - if (date instanceof Date && Number.isInteger(kind)) { + if (Number.isInteger(kind)) { //Mock Data: if (kind === -1) return res.send(mockDataDay); - if (!isNaN(date.getTime()) && kind >= 1 && kind <= 113) { - const daybegin = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; - date.setDate(date.getDate() + 1); - const dayend = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; - const querystring = `SELECT time, price FROM market WHERE kind = ${kind} AND time > "${daybegin}" AND time < "${dayend}"` + if (kind >= 1 && kind <= 113) { + var dayend = new Date().toISOString().slice(0, 19).replace('T', ' '); + var daybegin = new Date(); + daybegin.setDate(daybegin.getDate() - 1); + daybegin = daybegin.toISOString().slice(0, 19).replace('T', ' '); + const querystring = `SELECT time, price, quality FROM marketv2 WHERE kind = ${kind} AND time > "${daybegin}" AND time < "${dayend}" ORDER BY quality, time`; connection.query(querystring, function (error, results, fields) { if (error) { - throw error; + res.status(status.INTERNAL_SERVER_ERROR).send("database connection failed"); } res.send(results); }); From 47cd744fe2d676502db6cff5cb04069a90b0e1fe Mon Sep 17 00:00:00 2001 From: Oliver Boehlk Date: Sun, 19 Apr 2020 17:32:00 +0200 Subject: [PATCH 2/3] remove date dependency --- frontend/src/components/content.js | 2 +- frontend/src/components/resourcechart/resourcechart.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/content.js b/frontend/src/components/content.js index 2369ca6..d145e2b 100644 --- a/frontend/src/components/content.js +++ b/frontend/src/components/content.js @@ -23,7 +23,7 @@ export default function Content() {
- } /> + } />
); diff --git a/frontend/src/components/resourcechart/resourcechart.js b/frontend/src/components/resourcechart/resourcechart.js index 66e9d22..63e2249 100644 --- a/frontend/src/components/resourcechart/resourcechart.js +++ b/frontend/src/components/resourcechart/resourcechart.js @@ -11,7 +11,7 @@ export default function ResourceChart(props) { const [data, setData] = React.useState(null); let { id } = useParams(); const loadData = async () => { - let nextData = await fetch(`/simcompanies/API/day?date=${props["day"]}&kind=${id}`); + let nextData = await fetch(`/simcompanies/API/day?kind=${id}`); nextData = await nextData.json(); for (let i = 0; i < nextData.length; i++) { nextData[i]["time"] = new Date(nextData[i]["time"]); From 66d837f3879cc6f05d59d4f701a30ab2d4916e49 Mon Sep 17 00:00:00 2001 From: Oliver Boehlk Date: Sun, 19 Apr 2020 18:40:03 +0200 Subject: [PATCH 3/3] render all quality price charts --- backend/index.js | 15 ++++- .../components/resourcechart/resourcechart.js | 57 ++++++++++++++++--- 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/backend/index.js b/backend/index.js index 4b89d90..1dae020 100644 --- a/backend/index.js +++ b/backend/index.js @@ -16,6 +16,17 @@ var resourceList; var serverStartupComplete = false; + +function twoDigits(d) { + if (0 <= d && d < 10) return "0" + d.toString(); + if (-10 < d && d < 0) return "-0" + (-1 * d).toString(); + return d.toString(); +} +Date.prototype.toMysqlFormat = function () { + return this.getFullYear() + "-" + twoDigits(1 + this.getMonth()) + "-" + twoDigits(this.getDate()) + " " + twoDigits(this.getHours()) + ":" + twoDigits(this.getMinutes()) + ":" + twoDigits(this.getSeconds()); +}; + + async function loadData() { var rL = await fetch("https://www.simcompanies.com/api/v3/en/encyclopedia/resources/"); resourceList = await rL.json(); @@ -48,11 +59,11 @@ app.get('/API/day', function (req, res) { if (kind === -1) return res.send(mockDataDay); if (kind >= 1 && kind <= 113) { - var dayend = new Date().toISOString().slice(0, 19).replace('T', ' '); + var dayend = new Date().toMysqlFormat(); var daybegin = new Date(); daybegin.setDate(daybegin.getDate() - 1); daybegin = daybegin.toISOString().slice(0, 19).replace('T', ' '); - const querystring = `SELECT time, price, quality FROM marketv2 WHERE kind = ${kind} AND time > "${daybegin}" AND time < "${dayend}" ORDER BY quality, time`; + const querystring = `SELECT time, price, quality FROM marketv2 WHERE kind = ${kind} AND time > "${daybegin}" AND time < "${dayend}" ORDER BY time, quality`; connection.query(querystring, function (error, results, fields) { if (error) { res.status(status.INTERNAL_SERVER_ERROR).send("database connection failed"); diff --git a/frontend/src/components/resourcechart/resourcechart.js b/frontend/src/components/resourcechart/resourcechart.js index 63e2249..3b74533 100644 --- a/frontend/src/components/resourcechart/resourcechart.js +++ b/frontend/src/components/resourcechart/resourcechart.js @@ -11,13 +11,49 @@ export default function ResourceChart(props) { const [data, setData] = React.useState(null); let { id } = useParams(); const loadData = async () => { - let nextData = await fetch(`/simcompanies/API/day?kind=${id}`); - nextData = await nextData.json(); - for (let i = 0; i < nextData.length; i++) { - nextData[i]["time"] = new Date(nextData[i]["time"]); - nextData[i]["time"] = nextData[i]["time"].toLocaleTimeString(); + let dayData = await fetch(`/simcompanies/API/day?kind=${id}`); + let dataWithDate = await dayData.json(); + let qualitySortedData = []; + for (let i = 0; i < dataWithDate.length; i++) { + if (i === 0) qualitySortedData.push(dataWithDate[i]); + else { + if (dataWithDate[i]["time"] === qualitySortedData[qualitySortedData.length - 1]["time"]) { + switch (dataWithDate[i].quality) { + case 0: break; + case 1: + qualitySortedData[qualitySortedData.length - 1]["q1"] = dataWithDate[i]["price"]; + break; + case 2: + qualitySortedData[qualitySortedData.length - 1]["q2"] = dataWithDate[i]["price"]; + break; + case 3: + qualitySortedData[qualitySortedData.length - 1]["q3"] = dataWithDate[i]["price"]; + break; + case 4: + qualitySortedData[qualitySortedData.length - 1]["q4"] = dataWithDate[i]["price"]; + break; + case 5: + qualitySortedData[qualitySortedData.length - 1]["q5"] = dataWithDate[i]["price"]; + break; + case 6: + qualitySortedData[qualitySortedData.length - 1]["q6"] = dataWithDate[i]["price"]; + break; + case 7: + qualitySortedData[qualitySortedData.length - 1]["q7"] = dataWithDate[i]["price"]; + break; + } + } else { + qualitySortedData.push(dataWithDate[i]); + qualitySortedData[qualitySortedData.length - 1]["q0"] = qualitySortedData[qualitySortedData.length - 1]["price"]; + } + } } - setData(nextData) + for (let i = 0; i < qualitySortedData.length; i++) { + qualitySortedData[i]["time"] = new Date(qualitySortedData[i]["time"]); + qualitySortedData[i]["time"] = qualitySortedData[i]["time"].toLocaleTimeString(); + } + console.log(qualitySortedData); + setData(qualitySortedData) } if (data === null) { @@ -38,7 +74,14 @@ export default function ResourceChart(props) { - + + + + + + + + ); }