|
| 1 | +const log = require("loglevel"); |
| 2 | +const { Pool} = require('pg'); |
| 3 | +const SQLCase2 = require("./sqls/SQLCase2"); |
| 4 | +const SQLCase2Timeline = require("./sqls/SQLCase2Timeline"); |
| 5 | +const SQLCase1 = require("./sqls/SQLCase1"); |
| 6 | +const SQLCase1WithZoomTarget = require("./sqls/SQLCase1WithZoomTarget"); |
| 7 | +const SQLCase1Timeline = require("./sqls/SQLCase1Timeline"); |
| 8 | +const SQLCase3Timeline = require("./sqls/SQLCase3Timeline"); |
| 9 | +const SQLCase3 = require("./sqls/SQLCase3"); |
| 10 | +const SQLCase4 = require("./sqls/SQLCase4"); |
| 11 | +const SQLZoomTargetCase1V2 = require("./sqls/SQLZoomTargetCase1V2"); |
| 12 | + |
| 13 | + |
| 14 | +class Map{ |
| 15 | + constructor(){ |
| 16 | + this.pool = new Pool({ connectionString: process.env.DB_URL }); |
| 17 | + } |
| 18 | + |
| 19 | + async init(settings){ |
| 20 | + console.debug("init map with settings:", settings); |
| 21 | + this.treeid = settings.treeid; |
| 22 | + this.zoomLevel = parseInt(settings.zoom_level); |
| 23 | + this.userid = settings.userid; |
| 24 | + this.clusterRadius = settings.clusterRadius; |
| 25 | + this.mapName = settings.map_name; |
| 26 | + this.bounds = settings.bounds; |
| 27 | + this.wallet = settings.wallet; |
| 28 | + this.flavor = settings.flavor; |
| 29 | + this.token = settings.token; |
| 30 | + this.treeIds = []; |
| 31 | + this.timeline = settings.timeline; |
| 32 | + if(this.treeid){ |
| 33 | + /* |
| 34 | + * Single tree map mode |
| 35 | + */ |
| 36 | + this.sql = new SQLCase2(); |
| 37 | + this.sql.addTreeFilter(this.treeid); |
| 38 | + |
| 39 | + }else if(this.capture_id){ |
| 40 | + this.sql = new SQLCase2(); |
| 41 | + this.sql.addUUIDFilter(this.capture_id); |
| 42 | + }else if(this.userid){ |
| 43 | + /* |
| 44 | + * User map mode |
| 45 | + */ |
| 46 | + //count the trees amount first |
| 47 | + const result = await this.pool.query({ |
| 48 | + text: `select count(*) as count from trees where planter_id = ${this.userid}`, |
| 49 | + values:[] |
| 50 | + }); |
| 51 | + const treeCount = result.rows[0].count; |
| 52 | + parseInt(treeCount); |
| 53 | + log.warn("count by userId %d, get %s", this.userid, treeCount); |
| 54 | + if(this.zoomLevel > 15){ |
| 55 | + this.sql = new SQLCase2(); |
| 56 | + this.sql.setBounds(this.bounds); |
| 57 | + this.sql.addFilterByUserId(this.userid); |
| 58 | + }else{ |
| 59 | + if(treeCount > 2000){ |
| 60 | + this.sql = new SQLCase1WithZoomTarget(); |
| 61 | + this.sql.addFilterByUserId(this.userid); |
| 62 | + this.sql.setZoomLevel(this.zoomLevel); |
| 63 | + this.sql.setBounds(this.bounds); |
| 64 | + }else{ |
| 65 | + this.sql = new SQLCase3(); |
| 66 | + this.sql.setZoomLevel(this.zoomLevel); |
| 67 | + this.sql.addFilterByUserid(this.userid); |
| 68 | + this.sql.setBounds(this.bounds); |
| 69 | + } |
| 70 | + } |
| 71 | + if(this.zoomLevel <= 9){ |
| 72 | + this.sqlZoomTarget = new SQLZoomTargetCase1V2(); |
| 73 | + this.sqlZoomTarget.setBounds(this.bounds); |
| 74 | + this.sqlZoomTarget.setZoomLevel(this.zoomLevel); |
| 75 | + } |
| 76 | + |
| 77 | + }else if(this.wallet){ |
| 78 | + /* |
| 79 | + * wallet map mode |
| 80 | + */ |
| 81 | + if(this.zoomLevel > 15){ |
| 82 | + this.sql = new SQLCase2(); |
| 83 | + this.sql.setBounds(this.bounds); |
| 84 | + this.sql.addFilterByWallet(this.wallet); |
| 85 | + }else{ |
| 86 | + this.sql = new SQLCase3(); |
| 87 | + this.sql.setZoomLevel(this.zoomLevel); |
| 88 | + this.sql.addFilterByWallet(this.wallet); |
| 89 | + this.sql.setBounds(this.bounds); |
| 90 | + } |
| 91 | + if(this.zoomLevel <= 9){ |
| 92 | + this.sqlZoomTarget = new SQLZoomTargetCase1V2(); |
| 93 | + this.sqlZoomTarget.setBounds(this.bounds); |
| 94 | + this.sqlZoomTarget.setZoomLevel(this.zoomLevel); |
| 95 | + } |
| 96 | + |
| 97 | + }else if(this.flavor){ |
| 98 | + /* |
| 99 | + * flavor map mode |
| 100 | + */ |
| 101 | + if(this.zoomLevel > 15){ |
| 102 | + this.sql = new SQLCase2(); |
| 103 | + this.sql.setBounds(this.bounds); |
| 104 | + this.sql.addFilterByFlavor(this.flavor); |
| 105 | + }else{ |
| 106 | + this.sql = new SQLCase3(); |
| 107 | + this.sql.setZoomLevel(this.zoomLevel); |
| 108 | + this.sql.addFilterByFlavor(this.flavor); |
| 109 | + this.sql.setBounds(this.bounds); |
| 110 | + } |
| 111 | + if(this.zoomLevel <= 9){ |
| 112 | + this.sqlZoomTarget = new SQLZoomTargetCase1V2(); |
| 113 | + this.sqlZoomTarget.setBounds(this.bounds); |
| 114 | + this.sqlZoomTarget.setZoomLevel(this.zoomLevel); |
| 115 | + } |
| 116 | + |
| 117 | + }else if(this.token){ |
| 118 | + /* |
| 119 | + * Token map mode |
| 120 | + */ |
| 121 | + if(this.zoomLevel > 15){ |
| 122 | + this.sql = new SQLCase2(); |
| 123 | + this.sql.setBounds(this.bounds); |
| 124 | + this.sql.addFilterByToken(this.token); |
| 125 | + }else{ |
| 126 | + this.sql = new SQLCase3(); |
| 127 | + this.sql.setZoomLevel(this.zoomLevel); |
| 128 | + this.sql.addFilterByToken(this.token); |
| 129 | + this.sql.setBounds(this.bounds); |
| 130 | + } |
| 131 | + if(this.zoomLevel <= 9){ |
| 132 | + this.sqlZoomTarget = new SQLZoomTargetCase1V2(); |
| 133 | + this.sqlZoomTarget.setBounds(this.bounds); |
| 134 | + this.sqlZoomTarget.setZoomLevel(this.zoomLevel); |
| 135 | + } |
| 136 | + |
| 137 | + }else if(this.mapName){ |
| 138 | + /* |
| 139 | + * org map mode |
| 140 | + */ |
| 141 | + if(this.zoomLevel > 15){ |
| 142 | + this.sql = new SQLCase2(); |
| 143 | + this.sql.addFilterByMapName(this.mapName); |
| 144 | + this.sql.setBounds(this.bounds); |
| 145 | + } else if ([12, 13, 14, 15].includes(this.zoomLevel) && this.mapName != 'freetown') { |
| 146 | + this.sql = new SQLCase3(); |
| 147 | + this.sql.setZoomLevel(this.zoomLevel); |
| 148 | + this.sql.addFilterByMapName(this.mapName); |
| 149 | + this.sql.setBounds(this.bounds); |
| 150 | + }else{ |
| 151 | + this.sql = new SQLCase1WithZoomTarget(); |
| 152 | + this.sql.addMapNameFilter(this.mapName); |
| 153 | + this.sql.setBounds(this.bounds); |
| 154 | + this.sql.setZoomLevel(this.zoomLevel); |
| 155 | + } |
| 156 | + |
| 157 | + }else if(this.timeline){ |
| 158 | + if(this.zoomLevel > 15){ |
| 159 | + this.sql = new SQLCase2Timeline(); |
| 160 | + this.sql.addTimeline(this.timeline); |
| 161 | + this.sql.setBounds(this.bounds); |
| 162 | + } else if ([12, 13, 14, 15].includes(this.zoomLevel) ) { |
| 163 | + this.sql = new SQLCase3Timeline(); |
| 164 | + this.sql.setZoomLevel(this.zoomLevel); |
| 165 | + this.sql.setBounds(this.bounds); |
| 166 | + this.sql.addTimeline(this.timeline); |
| 167 | + }else{ |
| 168 | + this.sql = new SQLCase1Timeline(); |
| 169 | + this.sql.addTimeline(this.timeline); |
| 170 | + this.sql.setBounds(this.bounds); |
| 171 | + this.sql.setZoomLevel(this.zoomLevel); |
| 172 | + } |
| 173 | + }else{ |
| 174 | + /* |
| 175 | + * Normal map mode |
| 176 | + */ |
| 177 | + if(this.zoomLevel > 15){ |
| 178 | + this.sql = new SQLCase2(); |
| 179 | + this.sql.setBounds(this.bounds); |
| 180 | + } else if ([12, 13, 14, 15].includes(this.zoomLevel)) { |
| 181 | + this.sql = new SQLCase4(); |
| 182 | + this.sql.setBounds(this.bounds) |
| 183 | + }else{ |
| 184 | + this.sql = new SQLCase1WithZoomTarget(); |
| 185 | + this.sql.setBounds(this.bounds) |
| 186 | + this.sql.setZoomLevel(this.zoomLevel); |
| 187 | + } |
| 188 | + } |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + async getQuery(){ |
| 193 | + return this.sql.getQuery(); |
| 194 | + } |
| 195 | + |
| 196 | + async getZoomTargetQuery(){ |
| 197 | + if(this.sqlZoomTarget){ |
| 198 | + return this.sqlZoomTarget.getQuery(); |
| 199 | + }else{ |
| 200 | + return undefined; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + async getPoints(){ |
| 205 | + const query = await this.getQuery(); |
| 206 | + console.log(query); |
| 207 | + const beginTime = Date.now(); |
| 208 | + const data = await this.pool.query(query); |
| 209 | + console.log("get points took time:%d ms", Date.now() - beginTime); |
| 210 | + log.warn("get point:", data.rows.length); |
| 211 | + console.log(data.rows.slice(0,2)) |
| 212 | + return data.rows; |
| 213 | + } |
| 214 | + |
| 215 | + async getZoomTargets(){ |
| 216 | + const zoomTargetsQuery = await this.getZoomTargetQuery(); |
| 217 | + let zoomTargets; |
| 218 | + if(zoomTargetsQuery){ |
| 219 | + const beginTime = Date.now(); |
| 220 | + const result = await this.pool.query(zoomTargetsQuery); |
| 221 | + console.log("get zoom target took time:%d ms", Date.now() - beginTime); |
| 222 | + console.log('got zoom targets data'); |
| 223 | + zoomTargets = result.rows; |
| 224 | + } |
| 225 | + return zoomTargets; |
| 226 | + } |
| 227 | + |
| 228 | +} |
| 229 | + |
| 230 | +module.exports = Map; |
0 commit comments