Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.06 KB | None | 0 0
  1. var express = require('express');
  2. var cassandra = require('cassandra-driver');
  3. var path = require('path');
  4. var async = require('async');
  5.  
  6. var app = express();
  7. //Connect to the cluster
  8. var cassandraClient = new cassandra.Client({contactPoints: ['localhost'], keyspace: 'demo'});
  9.  
  10. var port = 8080;
  11.  
  12. var router = express.Router();
  13.  
  14. var stateDictionary = {
  15. "AL":"alabama",
  16. "AK":"alaska",
  17. "AS":"americanSamoa",
  18. "AZ":"arizona",
  19. "AR":"arkansas",
  20. "CA":"california",
  21. "CO":"colorado",
  22. "CT":"connecticut",
  23. "DE":"delaware",
  24. "DC":"districtOfColumbia",
  25. "FM":"federatedStatesOfMicronesia",
  26. "FL":"florida",
  27. "GA":"georgia",
  28. "GU":"guam",
  29. "HI":"hawaii",
  30. "ID":"idaho",
  31. "IL":"illinois",
  32. "IN":"indiana",
  33. "IA":"iowa",
  34. "KS":"kansas",
  35. "KY":"kentucky",
  36. "LA":"louisiana",
  37. "ME":"maine",
  38. "MH":"marshallIslands",
  39. "MD":"maryland",
  40. "MA":"massachusetts",
  41. "MI":"michigan",
  42. "MN":"minnesota",
  43. "MS":"mississippi",
  44. "MO":"missouri",
  45. "MT":"montana",
  46. "NE":"nebraska",
  47. "NV":"nevada",
  48. "NH":"newHampshire",
  49. "NJ":"newJersey",
  50. "NM":"newMexico",
  51. "NY":"newYork",
  52. "NC":"northCarolina",
  53. "ND":"northDakota",
  54. "MP":"northernMarianaIslands",
  55. "OH":"ohio",
  56. "OK":"oklahoma",
  57. "OR":"oregon",
  58. "PW":"palau",
  59. "PA":"pennsylvania",
  60. "PR":"puertoRico",
  61. "RI":"rhodeIsland",
  62. "SC":"southCarolina",
  63. "SD":"southDakota",
  64. "TN":"tennessee",
  65. "TX":"texas",
  66. "UT": "utah",
  67. "VT": "vermont",
  68. "VI": "virginIslands",
  69. "VA": "virginia",
  70. "WA": "washington",
  71. "WV": "westVirginia",
  72. "WI": "wisconsin",
  73. "WY": "wyoming"
  74. };
  75.  
  76. function getNewInfoFormat(OldStateInfoFormat)
  77. {
  78. var citymap = {
  79. alabama: {
  80. center: {lat: 32.806671, lng: -86.791130},
  81. codeSt: OldStateInfoFormat["state_code"],
  82. errors: OldStateInfoFormat["error_count"],
  83. session: OldStateInfoFormat["session_count"]
  84. },
  85. alaska: {
  86. center: {lat: 61.370716, lng: -152.404419},
  87. codeSt: OldStateInfoFormat["state_code"],
  88. errors: OldStateInfoFormat["error_count"],
  89. session: OldStateInfoFormat["session_count"]
  90. },
  91. arizona: {
  92. center: {lat: 33.729759, lng: -111.431221},
  93. codeSt: OldStateInfoFormat["state_code"],
  94. errors: OldStateInfoFormat["error_count"],
  95. session: OldStateInfoFormat["session_count"]
  96. },
  97. arkansas: {
  98. center: {lat: 34.969704, lng: -92.373123},
  99. codeSt: OldStateInfoFormat["state_code"],
  100. errors: OldStateInfoFormat["error_count"],
  101. session: OldStateInfoFormat["session_count"]
  102. },
  103. california: {
  104. center: {lat: 36.116203, lng: -119.681564},
  105. codeSt: OldStateInfoFormat["state_code"],
  106. errors: OldStateInfoFormat["error_count"],
  107. session: OldStateInfoFormat["session_count"]
  108. },
  109. colorado: {
  110. center: {lat: 39.059811, lng: -105.311104},
  111. codeSt: OldStateInfoFormat["state_code"],
  112. errors: OldStateInfoFormat["error_count"],
  113. session: OldStateInfoFormat["session_count"]
  114. },
  115. connecticut: {
  116. center: {lat: 41.597782, lng: -72.755371},
  117. codeSt: OldStateInfoFormat["state_code"],
  118. errors: OldStateInfoFormat["error_count"],
  119. session: OldStateInfoFormat["session_count"]
  120. },
  121. delaware: {
  122. center: {lat: 39.318523, lng: -75.507141},
  123. codeSt: OldStateInfoFormat["state_code"],
  124. errors: OldStateInfoFormat["error_count"],
  125. session: OldStateInfoFormat["session_count"]
  126. },
  127. districtOfColumbia: {
  128. center: {lat: 38.897438, lng: -77.026817},
  129. codeSt: OldStateInfoFormat["state_code"],
  130. errors: OldStateInfoFormat["error_count"],
  131. session: OldStateInfoFormat["session_count"]
  132. },
  133. florida: {
  134. center: {lat: 27.766279, lng: -81.686783},
  135. codeSt: OldStateInfoFormat["state_code"],
  136. errors: OldStateInfoFormat["error_count"],
  137. session: OldStateInfoFormat["session_count"]
  138. },
  139. georgia: {
  140. center: {lat: 33.040619, lng: -83.643074},
  141. codeSt: OldStateInfoFormat["state_code"],
  142. errors: OldStateInfoFormat["error_count"],
  143. session: OldStateInfoFormat["session_count"]
  144. },
  145. hawaii: {
  146. center: {lat: 21.094318, lng: -157.498337},
  147. codeSt: OldStateInfoFormat["state_code"],
  148. errors: OldStateInfoFormat["error_count"],
  149. session: OldStateInfoFormat["session_count"]
  150. },
  151. idaho: {
  152. center: {lat: 44.240459, lng: -114.478828},
  153. codeSt: OldStateInfoFormat["state_code"],
  154. errors: OldStateInfoFormat["error_count"],
  155. session: OldStateInfoFormat["session_count"]
  156. },
  157. illinois: {
  158. center: {lat: 40.349457, lng: -88.986137},
  159. codeSt: OldStateInfoFormat["state_code"],
  160. errors: OldStateInfoFormat["error_count"],
  161. session: OldStateInfoFormat["session_count"]
  162. },
  163. indiana: {
  164. center: {lat: 39.849426, lng: -86.258278},
  165. codeSt: OldStateInfoFormat["state_code"],
  166. errors: OldStateInfoFormat["error_count"],
  167. session: OldStateInfoFormat["session_count"]
  168. },
  169. iowa: {
  170. center: {lat: 42.011539, lng: -93.210526},
  171. codeSt: OldStateInfoFormat["state_code"],
  172. errors: OldStateInfoFormat["error_count"],
  173. session: OldStateInfoFormat["session_count"]
  174. },
  175. kansas: {
  176. center: {lat: 38.526600, lng: -96.726486},
  177. codeSt: OldStateInfoFormat["state_code"],
  178. errors: OldStateInfoFormat["error_count"],
  179. session: OldStateInfoFormat["session_count"]
  180. },
  181. kentucky: {
  182. center: {lat: 37.668140, lng: -84.670067},
  183. codeSt: OldStateInfoFormat["state_code"],
  184. errors: OldStateInfoFormat["error_count"],
  185. session: OldStateInfoFormat["session_count"]
  186. },
  187. louisiana: {
  188. center: {lat: 31.169546, lng: -91.867805},
  189. codeSt: OldStateInfoFormat["state_code"],
  190. errors: OldStateInfoFormat["error_count"],
  191. session: OldStateInfoFormat["session_count"]
  192. },
  193. maine: {
  194. center: {lat: 44.693947, lng: -69.381927},
  195. codeSt: OldStateInfoFormat["state_code"],
  196. errors: OldStateInfoFormat["error_count"],
  197. session: OldStateInfoFormat["session_count"]
  198. },
  199. maryland: {
  200. center: {lat: 39.063946, lng: -76.802101},
  201. codeSt: OldStateInfoFormat["state_code"],
  202. errors: OldStateInfoFormat["error_count"],
  203. session: OldStateInfoFormat["session_count"]
  204. },
  205. massachusetts: {
  206. center: {lat: 42.230171, lng: -71.530106},
  207. codeSt: OldStateInfoFormat["state_code"],
  208. errors: OldStateInfoFormat["error_count"],
  209. session: OldStateInfoFormat["session_count"]
  210. },
  211. michigan: {
  212. center: {lat: 43.326618, lng: -84.536095},
  213. codeSt: OldStateInfoFormat["state_code"],
  214. errors: OldStateInfoFormat["error_count"],
  215. session: OldStateInfoFormat["session_count"]
  216. },
  217. minnesota: {
  218. center: {lat: 45.694454, lng: -93.900192},
  219. codeSt: OldStateInfoFormat["state_code"],
  220. errors: OldStateInfoFormat["error_count"],
  221. session: OldStateInfoFormat["session_count"]
  222. },
  223. mississippi: {
  224. center: {lat: 32.741646, lng: -89.678696},
  225. codeSt: OldStateInfoFormat["state_code"],
  226. errors: OldStateInfoFormat["error_count"],
  227. session: OldStateInfoFormat["session_count"]
  228. },
  229. missouri: {
  230. center: {lat: 38.456085, lng: -92.288368},
  231. codeSt: OldStateInfoFormat["state_code"],
  232. errors: OldStateInfoFormat["error_count"],
  233. session: OldStateInfoFormat["session_count"]
  234. },
  235. montana: {
  236. center: {lat: 46.921925, lng: -110.454353},
  237. codeSt: OldStateInfoFormat["state_code"],
  238. errors: OldStateInfoFormat["error_count"],
  239. session: OldStateInfoFormat["session_count"]
  240. },
  241. nebraska: {
  242. center: {lat: 41.125370, lng: -98.268082},
  243. codeSt: OldStateInfoFormat["state_code"],
  244. errors: OldStateInfoFormat["error_count"],
  245. session: OldStateInfoFormat["session_count"]
  246. },
  247. nevada: {
  248. center: {lat: 38.313515, lng: -117.055374},
  249. codeSt: OldStateInfoFormat["state_code"],
  250. errors: OldStateInfoFormat["error_count"],
  251. session: OldStateInfoFormat["session_count"]
  252. },
  253. newHampshire: {
  254. center: {lat: 43.452492, lng: -71.563896},
  255. codeSt: OldStateInfoFormat["state_code"],
  256. errors: OldStateInfoFormat["error_count"],
  257. session: OldStateInfoFormat["session_count"]
  258. },
  259. newJersey: {
  260. center: {lat: 40.298904, lng: -74.521011},
  261. codeSt: OldStateInfoFormat["state_code"],
  262. errors: OldStateInfoFormat["error_count"],
  263. session: OldStateInfoFormat["session_count"]
  264. },
  265. newMexico: {
  266. center: {lat: 34.840515, lng: -106.248482},
  267. codeSt: OldStateInfoFormat["state_code"],
  268. errors: OldStateInfoFormat["error_count"],
  269. session: OldStateInfoFormat["session_count"]
  270. },
  271. newYork: {
  272. center: {lat: 42.165726, lng: -74.948051},
  273. codeSt: OldStateInfoFormat["state_code"],
  274. errors: OldStateInfoFormat["error_count"],
  275. session: OldStateInfoFormat["session_count"]
  276. },
  277. northCarolina: {
  278. center: {lat: 35.630066, lng: -79.806419},
  279. codeSt: OldStateInfoFormat["state_code"],
  280. errors: OldStateInfoFormat["error_count"],
  281. session: OldStateInfoFormat["session_count"]
  282. },
  283. northDakota: {
  284. center: {lat: 47.528912, lng: -99.784012},
  285. codeSt: OldStateInfoFormat["state_code"],
  286. errors: OldStateInfoFormat["error_count"],
  287. session: OldStateInfoFormat["session_count"]
  288. },
  289. ohio: {
  290. center: {lat: 40.388783, lng: -82.764915},
  291. codeSt: OldStateInfoFormat["state_code"],
  292. errors: OldStateInfoFormat["error_count"],
  293. session: OldStateInfoFormat["session_count"]
  294. },
  295. oklahoma: {
  296. center: {lat: 35.565342, lng: -96.928917},
  297. codeSt: OldStateInfoFormat["state_code"],
  298. errors: OldStateInfoFormat["error_count"],
  299. session: OldStateInfoFormat["session_count"]
  300. },
  301. oregon: {
  302. center: {lat: 44.572021, lng: -122.070938},
  303. codeSt: OldStateInfoFormat["state_code"],
  304. errors: OldStateInfoFormat["error_count"],
  305. session: OldStateInfoFormat["session_count"]
  306. },
  307. pennsylvania: {
  308. center: {lat: 40.590752, lng: -77.209755},
  309. codeSt: OldStateInfoFormat["state_code"],
  310. errors: OldStateInfoFormat["error_count"],
  311. session: OldStateInfoFormat["session_count"]
  312. },
  313. rhodeIsland: {
  314. center: {lat: 41.680893, lng: -71.511780},
  315. codeSt: OldStateInfoFormat["state_code"],
  316. errors: OldStateInfoFormat["error_count"],
  317. session: OldStateInfoFormat["session_count"]
  318. },
  319. southCarolina: {
  320. center: {lat: 33.856892, lng: -80.945007},
  321. codeSt: OldStateInfoFormat["state_code"],
  322. errors: OldStateInfoFormat["error_count"],
  323. session: OldStateInfoFormat["session_count"]
  324. },
  325. southDakota: {
  326. center: {lat: 44.299782, lng: -99.438828},
  327. codeSt: OldStateInfoFormat["state_code"],
  328. errors: OldStateInfoFormat["error_count"],
  329. session: OldStateInfoFormat["session_count"]
  330. },
  331. tennessee: {
  332. center: {lat: 35.747845, lng: -86.692345},
  333. codeSt: OldStateInfoFormat["state_code"],
  334. errors: OldStateInfoFormat["error_count"],
  335. session: OldStateInfoFormat["session_count"]
  336. },
  337. texas: {
  338. center: {lat: 31.054487, lng: -97.563461},
  339. codeSt: OldStateInfoFormat["state_code"],
  340. errors: OldStateInfoFormat["error_count"],
  341. session: OldStateInfoFormat["session_count"]
  342. },
  343. utah: {
  344. center: {lat: 40.150032, lng: -111.862434},
  345. codeSt: OldStateInfoFormat["state_code"],
  346. errors: OldStateInfoFormat["error_count"],
  347. session: OldStateInfoFormat["session_count"]
  348. },
  349. vermont: {
  350. center: {lat: 44.045876, lng: -72.710686},
  351. codeSt: OldStateInfoFormat["state_code"],
  352. errors: OldStateInfoFormat["error_count"],
  353. session: OldStateInfoFormat["session_count"]
  354. },
  355. virginia: {
  356. center: {lat: 37.769337, lng: -78.169968},
  357. codeSt: OldStateInfoFormat["state_code"],
  358. errors: OldStateInfoFormat["error_count"],
  359. session: OldStateInfoFormat["session_count"]
  360. },
  361. washington: {
  362. center: {lat: 47.400902, lng: -121.490494},
  363. codeSt: OldStateInfoFormat["state_code"],
  364. errors: OldStateInfoFormat["error_count"],
  365. session: OldStateInfoFormat["session_count"]
  366. },
  367. westVirginia: {
  368. center: {lat: 38.491226, lng: -80.954453},
  369. codeSt: OldStateInfoFormat["state_code"],
  370. errors: OldStateInfoFormat["error_count"],
  371. session: OldStateInfoFormat["session_count"]
  372. },
  373. wisconsin: {
  374. center: {lat: 44.268543, lng: -89.616508},
  375. codeSt: OldStateInfoFormat["state_code"],
  376. errors: OldStateInfoFormat["error_count"],
  377. session: OldStateInfoFormat["session_count"]
  378. },
  379. wyoming: {
  380. center: {lat: 42.755966, lng: -107.302490},
  381. codeSt: OldStateInfoFormat["state_code"],
  382. errors: OldStateInfoFormat["error_count"],
  383. session: OldStateInfoFormat["session_count"]
  384. }
  385. };
  386. var stateName = stateDictionary[OldStateInfoFormat["state_code"]];
  387. return citymap[stateName];
  388. }
  389.  
  390. function getNewCityFormat(OldCityFormat)
  391. {
  392. console.log(OldCityFormat["city_name"]);
  393. console.log(OldCityFormat);
  394. console.log(OldCityFormat.city_name);
  395. console.log(OldCityFormat["state_code"]);
  396. var geocoder = new google.maps.Geocoder();
  397. var cityLat = 0;
  398. var cityLng = 0;
  399. geocoder.geocode( { 'address': OldCityFormat["city_name"] + ', us'}, function(results, status) {
  400. cityLat = results[0].geometry.location.lat();
  401. cityLng = results[0].geometry.location.lng();
  402. console.log("lat" + cityLat);
  403. console.log("lng"+ cityLng);
  404. });
  405.  
  406. return {
  407. country: OldCityFormat["city_name"],
  408. center: {lat: cityLat, lng: cityLng},
  409. errors: OldCityFormat["error_count"],
  410. session: OldCityFormat["session_count"]
  411. }
  412. }
  413.  
  414. router.get('/api/allData', function(req, res) {
  415. var query = "select * from state_network_statistic";
  416. cassandraClient.execute(query, function(err, result) {
  417. var models = {};
  418.  
  419. for(var i = 0; i<result.rows.length; i++) {
  420. var model = {};
  421. var row = result.rows[i];
  422. models[stateDictionary[row["state_code"]]] = getNewInfoFormat(row);
  423.  
  424. }
  425. res.setHeader('Access-Control-Allow-Origin','*');
  426. res.json(models);
  427. });
  428. });
  429.  
  430. router.get('/api/getCitiesForState', function(req, res) {
  431. var stCode = req.query["stCode"];
  432. cassandraClient.execute("select * from state_network_statistic where state_code = ?",[req.query["stCode"]], function(err, result) {
  433. var models = [];
  434. for(var i = 0; i < result.rows.length; i++) {
  435. var row = result.rows[i];
  436. console.log(result.rows[i]);
  437. console.log(row);
  438. models[i] = getNewCityFormat(row);
  439. }
  440.  
  441. res.setHeader('Access-Control-Allow-Origin','*');
  442. res.json(models);
  443. });
  444. });
  445.  
  446. router.get('/', function(req, res) {
  447. res.setHeader('Access-Control-Allow-Origin','*');
  448. res.sendFile(path.join(__dirname + '/index4.html'));
  449. });
  450.  
  451. app.use(router);
  452. app.listen(port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement