From 0e88a64624be5667ff832d8b55547b3b1ad2de1c Mon Sep 17 00:00:00 2001 From: Ally Date: Fri, 17 Nov 2023 17:51:15 -0600 Subject: [PATCH 1/4] Use primarily manual for schedule. And port configuration --- src/admin/routes/api.js | 2 +- src/analysis/routes/api.js | 52 ++++++++++++++++++++++---------------- src/schedule/schedule.js | 38 +++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/src/admin/routes/api.js b/src/admin/routes/api.js index ebb1a1b5..7c3a4053 100644 --- a/src/admin/routes/api.js +++ b/src/admin/routes/api.js @@ -90,7 +90,7 @@ router.get("/matches", async (req,res) => { //1 check if there is a manual schedu,e //2 if there is a manual schedule, send it instead of the TBA one //3 if there isnt send the TBA one - let manualSchedule = await axios.get('http://localhost:8080/schedule/matches').then(res=>res.data) // temp fix + let manualSchedule = await axios.get(`http://localhost:${process.env.PORT || 8080}/schedule/matches`).then(res=>res.data) // temp fix if(Object.keys(manualSchedule).length != 0){ // find a better way to check if its empty res.json({ "allMatches": manualSchedule, diff --git a/src/analysis/routes/api.js b/src/analysis/routes/api.js index 90a7671b..2812a0e7 100644 --- a/src/analysis/routes/api.js +++ b/src/analysis/routes/api.js @@ -17,12 +17,20 @@ router.get("/teams", async (req, res) => { if (!config.secrets.TBA_API_KEY) { return res.json([]); //no key, no teams } - let tbaTeams = (await axios.get(`https://www.thebluealliance.com/api/v3/event/${config.TBA_EVENT_KEY}/teams`, { - headers: { - "X-TBA-Auth-Key": config.secrets.TBA_API_KEY - } - }).catch(e => console.log(e,chalk.bold.red("\nError fetching teams from Blue Alliance API!")))).data; - res.json(tbaTeams) + + var teams = []; + + teams = await axios.get(`http://localhost:${process.env.PORT || 8080}/schedule/tempteams`).then(res=> res.data); + if (teams.length == 0) { + teams = (await axios.get(`https://www.thebluealliance.com/api/v3/event/${config.TBA_EVENT_KEY}/teams`, { + headers: { + "X-TBA-Auth-Key": config.secrets.TBA_API_KEY + } + }).catch(e => console.log(e,chalk.bold.red("\nError fetching teams from Blue Alliance API!")))).data; + } + + console.log(teams); + res.json(teams); }) router.get("/csv", async (req,res) => { @@ -32,30 +40,30 @@ router.get("/csv", async (req,res) => { let rows = []; let headerRow = true; let checkData = function(team){ - if(Object.entries(team).filter(([key,value])=>key!="manual").length == 0){ - return false - } - return true + if(Object.entries(team).filter(([key,value])=>key!="manual").length == 0){ + return false + } + return true } for (let [teamNumber,team] of Object.entries(dataset.teams).filter(([num,team])=>checkData(team)) ) { - if(headerRow){ - headerRow = false; - rows.push(["Team #", - ...Object.entries(team.averages).filter(([key,value])=>!isNaN(value)&&value).map(([i,x]) => i+" Average"), //all averages - ...Object.entries(team.averageScores).filter(item=>!isNaN(item)).map(([i,x]) => i+" Score Average"), //all averages - "Average Cycle", - "Average Completed Cycle" - ]) - } - rows.push([teamNumber, + if(headerRow){ + headerRow = false; + rows.push(["Team #", + ...Object.entries(team.averages).filter(([key,value])=>!isNaN(value)&&value).map(([i,x]) => i+" Average"), //all averages + ...Object.entries(team.averageScores).filter(item=>!isNaN(item)).map(([i,x]) => i+" Score Average"), //all averages + "Average Cycle", + "Average Completed Cycle" + ]) + } + rows.push([teamNumber, ...Object.entries(team.averages).filter(([key,value])=>!isNaN(value)&&value).map(([i,x]) => x), //all averages ...Object.entries(team.averageScores).filter(item=>!isNaN(item)).map(([i,x]) => x), //all averages team.cycle.averageTime, team.cycle.averageTimeComplete - ]) + ]) - } + } //make into csv let csv = rows.map(row => row.reduce((acc,value) => acc+`,${value}`)).reduce((acc,row) => acc+`${row}\n`, ""); diff --git a/src/schedule/schedule.js b/src/schedule/schedule.js index 388ab219..e9e0b439 100644 --- a/src/schedule/schedule.js +++ b/src/schedule/schedule.js @@ -1,6 +1,10 @@ +const { unwatchFile } = require("fs"); + express = require("express"); var schedule = {}; +var tempTeams = []; + let router = express.Router(); router.use(express.static(__dirname + "/public")); @@ -10,16 +14,42 @@ router.get("/", (req,res) => { }) router.post('/matches',(req,res)=>{ - console.log("req body on /schedule/matches") - console.log(req.body) schedule = req.body; + addTeam(req.body); + res.send(200) }) router.get('/matches',(req,res)=>{ - res.send(schedule) + res.send(schedule); }) router.use("/api", require("./routes/api.js")); -module.exports = router; \ No newline at end of file +router.get("/tempteams", async (req, res) => { + res.send(tempTeams); +}); + +const addTeam = (matchLists) => { + var teams = []; + + for (let i = 0; i < matchLists.length; i++) { + const element = matchLists[i]; + + element.robots.blue.forEach(teamNumber => { + if (!teams.includes(teamNumber)) { + teams.push({team_number: teamNumber, nickname: "temp team"}); + } + }); + + element.robots.red.forEach(teamNumber => { + if (!teams.includes(teamNumber)) { + teams.push({team_number: teamNumber, nickname: "temp team"}); + } + }); + } + + tempTeams = teams; +} + +module.exports = router; From f91639c155833f7f18ed98305dd74f293df33316 Mon Sep 17 00:00:00 2001 From: Ally Date: Tue, 5 Dec 2023 17:32:02 -0600 Subject: [PATCH 2/4] Preventing empty schedules --- src/schedule/public/js/form.js | 1 + src/schedule/public/js/script.js | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/schedule/public/js/form.js b/src/schedule/public/js/form.js index 972302a4..e5c972d7 100644 --- a/src/schedule/public/js/form.js +++ b/src/schedule/public/js/form.js @@ -34,5 +34,6 @@ function updateForm() { } } catch (e) { //keep going even if this errors, we need them to be able to input data + console.log(e); } } \ No newline at end of file diff --git a/src/schedule/public/js/script.js b/src/schedule/public/js/script.js index 48d35134..9b891350 100644 --- a/src/schedule/public/js/script.js +++ b/src/schedule/public/js/script.js @@ -165,13 +165,19 @@ function makeMatchSchedule(matchTotalNum){ async function processTeams(matchNum, teams) { - // inserts data into the correct spot let data = await teams; console.log(data); + let redTeams = []; let blueTeams = []; + for(let i = 0; i < 3; i++) { + if (data[i] === null || data[i+3] === null) { + new Popup("error", "You can't leave team numbers empty. Match wasn't uploaded."); + return; + } + redTeams.push(data[i]); blueTeams.push(data[i+3]); } From 556991819bb2bc4d0b6f2513c442189d11567bf1 Mon Sep 17 00:00:00 2001 From: Ally Date: Tue, 5 Dec 2023 17:35:30 -0600 Subject: [PATCH 3/4] Preventing empty schedules --- src/schedule/public/js/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schedule/public/js/script.js b/src/schedule/public/js/script.js index 9b891350..990b2ae2 100644 --- a/src/schedule/public/js/script.js +++ b/src/schedule/public/js/script.js @@ -173,7 +173,7 @@ async function processTeams(matchNum, teams) { for(let i = 0; i < 3; i++) { - if (data[i] === null || data[i+3] === null) { + if (data[i] === "" || data[i+3] === "") { new Popup("error", "You can't leave team numbers empty. Match wasn't uploaded."); return; } From 641c340254b861c0cb4ab90b241753904a3cf72b Mon Sep 17 00:00:00 2001 From: ally Date: Tue, 9 Jan 2024 09:25:35 -0600 Subject: [PATCH 4/4] Remove server sided get requests to routes --- src/admin/routes/api.js | 9 ++++----- src/analysis/routes/api.js | 11 ++++++----- src/schedule/schedule.js | 16 +++++++++------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/admin/routes/api.js b/src/admin/routes/api.js index 7c3a4053..90bcca79 100644 --- a/src/admin/routes/api.js +++ b/src/admin/routes/api.js @@ -4,6 +4,7 @@ let router = Router(); const config = require("../../../config/config.json"); const { TeamMatchPerformance } = require("../../lib/db"); let axios = require("axios") +const {getManualMatches} = require("../../schedule/schedule"); const DEMO = false; router.use((req,res,next) => { @@ -87,11 +88,9 @@ router.post("/setMatch", (req,res) => { }); router.get("/matches", async (req,res) => { -//1 check if there is a manual schedu,e -//2 if there is a manual schedule, send it instead of the TBA one -//3 if there isnt send the TBA one - let manualSchedule = await axios.get(`http://localhost:${process.env.PORT || 8080}/schedule/matches`).then(res=>res.data) // temp fix - if(Object.keys(manualSchedule).length != 0){ // find a better way to check if its empty + let manualSchedule = getManualMatches() + + if(manualSchedule === {}){ res.json({ "allMatches": manualSchedule, "currentMatch": ScoutingSync.match diff --git a/src/analysis/routes/api.js b/src/analysis/routes/api.js index 2812a0e7..95342875 100644 --- a/src/analysis/routes/api.js +++ b/src/analysis/routes/api.js @@ -2,6 +2,7 @@ const { Router } = require("express"); const executeAnalysisPipeline = require("../analysisPipeline.js") const axios = require("axios") const config = require("../../../config/config.json"); +const {getTempTeams} = require("../../schedule/schedule"); let router = Router(); @@ -18,18 +19,18 @@ router.get("/teams", async (req, res) => { return res.json([]); //no key, no teams } - var teams = []; + let teams = []; - teams = await axios.get(`http://localhost:${process.env.PORT || 8080}/schedule/tempteams`).then(res=> res.data); - if (teams.length == 0) { + teams = getTempTeams(); + + if (teams.length === 0) { teams = (await axios.get(`https://www.thebluealliance.com/api/v3/event/${config.TBA_EVENT_KEY}/teams`, { headers: { "X-TBA-Auth-Key": config.secrets.TBA_API_KEY } - }).catch(e => console.log(e,chalk.bold.red("\nError fetching teams from Blue Alliance API!")))).data; + }).catch(e => console.error(e, chalk.bold.red("\nError fetching teams from Blue Alliance API!")))).data; } - console.log(teams); res.json(teams); }) diff --git a/src/schedule/schedule.js b/src/schedule/schedule.js index e9e0b439..8ebb1f96 100644 --- a/src/schedule/schedule.js +++ b/src/schedule/schedule.js @@ -20,15 +20,17 @@ router.post('/matches',(req,res)=>{ res.send(200) }) -router.get('/matches',(req,res)=>{ - res.send(schedule); -}) router.use("/api", require("./routes/api.js")); -router.get("/tempteams", async (req, res) => { - res.send(tempTeams); -}); +function getManualMatches() { + return schedule; +} + +function getTempTeams() { + return tempTeams; +} + const addTeam = (matchLists) => { var teams = []; @@ -52,4 +54,4 @@ const addTeam = (matchLists) => { tempTeams = teams; } -module.exports = router; +module.exports = {router, getTempTeams, getManualMatches};