From c1b2330f2a0d682589eec01d96b9eadbd918a7a1 Mon Sep 17 00:00:00 2001 From: Zaven Arra Date: Wed, 19 May 2021 16:15:16 -0700 Subject: [PATCH] feat: implement tree_name feature --- .env | 3 --- package.json | 2 +- src/app.js | 5 +++++ src/cron/assign-new-trees-to-clusters.js | 16 +++++++++++++--- src/models/Map.js | 5 ++++- src/models/Tree.js | 1 + src/models/sqls/SQLCase2.js | 8 ++++++++ 7 files changed, 32 insertions(+), 8 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index 493b0e64..00000000 --- a/.env +++ /dev/null @@ -1,3 +0,0 @@ -IMAGE=treetracker-web-map -VERSION=latest -CONTAINER_NAME=web-map diff --git a/package.json b/package.json index ea8b18a3..64f9978c 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "eslint": "eslint --report-unused-disable-directives .", "eslint:fix": "npm run eslint -- --fix", "test": "jest --watchAll --runInBand", - "dev": "NODE_PORT=3001 nodemon src/server.js" + "dev": "NODE_PORT=3001 NODE_ENV=dev nodemon src/server.js" }, "dependencies": { "@sentry/node": "^5.4.3", diff --git a/src/app.js b/src/app.js index c9838132..3fe5bfdb 100644 --- a/src/app.js +++ b/src/app.js @@ -29,6 +29,7 @@ const allowCrossDomain = (req, res, next) => { } if(process.env.NODE_ENV == 'dev'){ + console.log('disable cors'); app.use(allowCrossDomain); } @@ -61,12 +62,16 @@ app.get("/tree", async function (req, res){ const tree = new Tree(); const treeId = req.query.tree_id; const uuid = req.query.uuid; + const treeName = req.query.tree_name; if(treeId){ const treeDetail = await tree.getTreeById(treeId); res.status(200).json(treeDetail); } else if(uuid){ const treeDetail = await tree.getTreeByUUID(uuid); res.status(200).json(treeDetail); + } else if(treeName){ + const treeDetail = await tree.getTreeByName(treeName); + res.status(200).json(treeDetail); } else { console.warn("no tree id", treeId); res.status(400).json({message:"no tree id"}); diff --git a/src/cron/assign-new-trees-to-clusters.js b/src/cron/assign-new-trees-to-clusters.js index b157ecb5..73631071 100644 --- a/src/cron/assign-new-trees-to-clusters.js +++ b/src/cron/assign-new-trees-to-clusters.js @@ -14,12 +14,17 @@ const pool = new Pool({ const client = await pool.connect(); const select = { - text: `SELECT count(id) + text: `SELECT count(trees.id) FROM trees + JOIN region + ON ST_Contains( region.geom, trees.estimated_geometric_location) + JOIN region_zoom + ON region_zoom.region_id = region.id WHERE trees.active = true AND trees.cluster_regions_assigned = false` }; const rval = await client.query(select); + console.log(rval.rows[0].count) /* if(rval.rows[0].count == 0){ console.log("no new trees"); client.release(); @@ -63,13 +68,18 @@ const pool = new Pool({ AND cluster_regions_assigned = false` }; console.log(update); - await client.query(update); + const rval2 = await client.query(update); + console.log(rval2); await client.query('COMMIT'); const select = { - text: `SELECT count(id) + text: `SELECT count(trees.id) FROM trees + JOIN region + ON ST_Contains( region.geom, trees.estimated_geometric_location) + JOIN region_zoom + ON region_zoom.region_id = region.id WHERE trees.active = true AND trees.cluster_regions_assigned = false` }; diff --git a/src/models/Map.js b/src/models/Map.js index d41eb862..ccf325a2 100644 --- a/src/models/Map.js +++ b/src/models/Map.js @@ -18,6 +18,7 @@ class Map{ async init(settings){ console.debug("init map with settings:", settings); this.treeid = settings.treeid; + this.tree_name = settings.tree_name; this.zoomLevel = parseInt(settings.zoom_level); this.userid = settings.userid; this.clusterRadius = settings.clusterRadius; @@ -34,7 +35,9 @@ class Map{ */ this.sql = new SQLCase2(); this.sql.addTreeFilter(this.treeid); - + }else if(this.tree_name){ + this.sql = new SQLCase2(); + this.sql.addTreeNameFilter(this.tree_name); }else if(this.capture_id){ this.sql = new SQLCase2(); this.sql.addUUIDFilter(this.capture_id); diff --git a/src/models/Tree.js b/src/models/Tree.js index 985718c5..76b4a04e 100644 --- a/src/models/Tree.js +++ b/src/models/Tree.js @@ -56,6 +56,7 @@ class Tree{ } return treeObject; } + } diff --git a/src/models/sqls/SQLCase2.js b/src/models/sqls/SQLCase2.js index 89382bf5..e24b0524 100644 --- a/src/models/sqls/SQLCase2.js +++ b/src/models/sqls/SQLCase2.js @@ -12,6 +12,11 @@ class SQLCase2{ this.treeid = treeid; } + addTreeNameFilter(tree_name){ + this.tree_name = tree_name; + } + + addUUIDFilter(uuid){ this.uuid = uuid; } @@ -46,6 +51,9 @@ class SQLCase2{ if(this.treeid){ result += 'AND trees.id = ' + this.treeid + ' \n'; } + if(this.tree_name){ + result += "AND trees.name = '" + this.tree_name + "' \n"; + } if(this.uuid){ result += 'AND trees.uuid = ' + this.uuid + ' \n'; }