reader.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. require("./date.js")
  2. var rwc = require('random-weighted-choice');
  3. _ = require("./underscore.min.js")
  4. fs = require('fs')
  5. complete = 0;
  6. fs.readFile('shakespeare.json', 'utf8', function (err,data) {
  7. i = 0;
  8. if (err) {
  9. return console.log(err);
  10. }
  11. var obj = JSON.parse(data);
  12. var lines = obj.length
  13. console.error("Generating " + lines + " events");
  14. _.each(obj, function (o) {
  15. setTimeout(print_obj(o), 10000) //wait ten seconds before continuing
  16. });
  17. function getRandomInRange(from, to, fixed) {
  18. return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
  19. }
  20. function print_obj(o) {
  21. var randomnumber=Math.floor(Math.random()*137600000)
  22. var command = {index:{_index: "shakespeare", _type: "line", _id: i}};
  23. o['@timestamp'] = new Date((new Date()).getTime() -18000000 + randomnumber);
  24. o.geo = {
  25. geojson : [getRandomInRange(-90, 90, 3),getRandomInRange(-180, 180, 3)],
  26. country : get_country()
  27. }
  28. if(o.geo.country == 'US')
  29. o.geo.state = get_state();
  30. console.log(JSON.stringify(command))
  31. console.log(JSON.stringify(o));
  32. var percent = Math.floor((i/lines)*100)
  33. if (percent%10 == 0 && percent > complete) {
  34. console.error(percent+"% complete")
  35. complete = percent;
  36. }
  37. i = i + 1;
  38. }
  39. function get_state() {
  40. var states = ['AL','AK','AZ','AR','CA','CO','CT','DE',
  41. 'FL','GA','HI','ID','IL','IN','IA','KS',
  42. 'KY','LA','ME','MD','MA','MI','MN','MS',
  43. 'MO','MT','NE','NV','NH','NJ','NM','NY',
  44. 'NC','ND','OH','OK','OR','PA','RI','SC',
  45. 'SD','TN','TX','UT','VT','VA','WA','WV',
  46. 'WI','WY'];
  47. return states[Math.floor(Math.random()*50)];
  48. }
  49. function get_country() {
  50. var countries = [
  51. {id:"CN",weight:1330044000},{id:"IN",weight:1173108018},
  52. {id:"US",weight:610232863},{id:"ID",weight:242968342},
  53. {id:"BR",weight:201103330},{id:"PK",weight:184404791},
  54. {id:"BD",weight:156118464},{id:"NG",weight:154000000},
  55. {id:"RU",weight:140702000},{id:"JP",weight:127288000},
  56. {id:"MX",weight:112468855},{id:"PH",weight:99900177},
  57. {id:"VN",weight:89571130},{id:"ET",weight:88013491},
  58. {id:"DE",weight:81802257},{id:"EG",weight:80471869},
  59. {id:"TR",weight:77804122},{id:"IR",weight:76923300},
  60. {id:"CD",weight:70916439},{id:"TH",weight:67089500},
  61. {id:"FR",weight:64768389},{id:"GB",weight:62348447},
  62. {id:"IT",weight:60340328},{id:"MM",weight:53414374},
  63. {id:"ZA",weight:49000000},{id:"KR",weight:48422644},
  64. {id:"ES",weight:46505963},{id:"UA",weight:45415596},
  65. {id:"CO",weight:44205293},{id:"TZ",weight:41892895},
  66. {id:"AR",weight:41343201},{id:"KE",weight:40046566},
  67. {id:"PL",weight:38500000},{id:"SD",weight:35000000},
  68. {id:"DZ",weight:34586184},{id:"CA",weight:33679000},
  69. {id:"UG",weight:33398682},{id:"MA",weight:31627428},
  70. {id:"PE",weight:29907003},{id:"IQ",weight:29671605},
  71. {id:"AF",weight:29121286},{id:"NP",weight:28951852},
  72. {id:"MY",weight:28274729},{id:"UZ",weight:27865738},
  73. {id:"VE",weight:27223228},{id:"SA",weight:25731776},
  74. {id:"GH",weight:24339838},{id:"YE",weight:23495361},
  75. {id:"KP",weight:22912177},{id:"TW",weight:22894384},
  76. {id:"SY",weight:22198110},{id:"MZ",weight:22061451},
  77. {id:"RO",weight:21959278},{id:"AU",weight:21515754},
  78. {id:"LK",weight:21513990},{id:"MG",weight:21281844},
  79. {id:"CI",weight:21058798},{id:"CM",weight:19294149},
  80. {id:"CL",weight:16746491},{id:"NL",weight:16645000},
  81. {id:"BF",weight:16241811},{id:"NE",weight:15878271},
  82. {id:"MW",weight:15447500},{id:"KZ",weight:15340000},
  83. {id:"EC",weight:14790608},{id:"KH",weight:14453680},
  84. {id:"ML",weight:13796354},{id:"GT",weight:13550440},
  85. {id:"ZM",weight:13460305},{id:"AO",weight:13068161},
  86. {id:"SN",weight:12323252},{id:"ZW",weight:11651858},
  87. {id:"CU",weight:11423000},{id:"RW",weight:11055976},
  88. {id:"GR",weight:11000000},{id:"CS",weight:10829175},
  89. {id:"PT",weight:10676000},{id:"TN",weight:10589025},
  90. {id:"TD",weight:10543464},{id:"CZ",weight:10476000},
  91. {id:"BE",weight:10403000},{id:"GN",weight:10324025},
  92. {id:"SO",weight:10112453},{id:"BO",weight:9947418},
  93. {id:"HU",weight:9930000},{id:"BI",weight:9863117},
  94. {id:"DO",weight:9823821},{id:"BY",weight:9685000},
  95. {id:"HT",weight:9648924},{id:"BJ",weight:9056010},
  96. {id:"SE",weight:9045000},{id:"AZ",weight:8303512},
  97. {id:"SS",weight:8260490},{id:"AT",weight:8205000},
  98. {id:"HN",weight:7989415},{id:"CH",weight:7581000},
  99. {id:"TJ",weight:7487489},{id:"IL",weight:7353985},
  100. {id:"RS",weight:7344847},{id:"BG",weight:7148785},
  101. {id:"HK",weight:6898686},{id:"TG",weight:6587239},
  102. {id:"LY",weight:6461454},{id:"JO",weight:6407085},
  103. {id:"PY",weight:6375830},{id:"LA",weight:6368162},
  104. {id:"PG",weight:6064515},{id:"SV",weight:6052064},
  105. {id:"NI",weight:5995928},{id:"ER",weight:5792984},
  106. {id:"KG",weight:5508626},{id:"DK",weight:5484000},
  107. {id:"SK",weight:5455000},{id:"SL",weight:5245695},
  108. {id:"FI",weight:5244000},{id:"NO",weight:5009150},
  109. {id:"AE",weight:4975593},{id:"TM",weight:4940916},
  110. {id:"CF",weight:4844927},{id:"SG",weight:4701069},
  111. {id:"GE",weight:4630000},{id:"IE",weight:4622917},
  112. {id:"BA",weight:4590000},{id:"CR",weight:4516220},
  113. {id:"HR",weight:4491000},{id:"MD",weight:4324000},
  114. {id:"NZ",weight:4252277},{id:"LB",weight:4125247},
  115. {id:"PR",weight:3916632},{id:"PS",weight:3800000},
  116. {id:"LR",weight:3685076},{id:"LT",weight:3565000},
  117. {id:"UY",weight:3477000},{id:"PA",weight:3410676},
  118. {id:"MR",weight:3205060},{id:"MN",weight:3086918},
  119. {id:"CG",weight:3039126},{id:"AL",weight:2986952},
  120. {id:"AM",weight:2968000},{id:"OM",weight:2967717},
  121. {id:"JM",weight:2847232},{id:"KW",weight:2789132},
  122. {id:"LV",weight:2217969},{id:"NA",weight:2128471},
  123. {id:"MK",weight:2061000},{id:"BW",weight:2029307},
  124. {id:"SI",weight:2007000},{id:"LS",weight:1919552},
  125. {id:"XK",weight:1800000},{id:"GM",weight:1593256},
  126. {id:"GW",weight:1565126},{id:"GA",weight:1545255},
  127. {id:"SZ",weight:1354051},{id:"MU",weight:1294104},
  128. {id:"EE",weight:1291170},{id:"TT",weight:1228691},
  129. {id:"TL",weight:1154625},{id:"CY",weight:1102677},
  130. {id:"GQ",weight:1014999},{id:"FJ",weight:875983},
  131. {id:"QA",weight:840926},{id:"RE",weight:776948},
  132. {id:"KM",weight:773407},{id:"GY",weight:748486},
  133. {id:"DJ",weight:740528},{id:"BH",weight:738004},
  134. {id:"BT",weight:699847},{id:"ME",weight:666730},
  135. {id:"SB",weight:559198},{id:"CV",weight:508659},
  136. {id:"LU",weight:497538},{id:"SR",weight:492829},
  137. {id:"MO",weight:449198},{id:"GP",weight:443000},
  138. {id:"MQ",weight:432900},{id:"MT",weight:403000},
  139. {id:"MV",weight:395650},{id:"BN",weight:395027},
  140. {id:"BZ",weight:314522},{id:"IS",weight:308910},
  141. {id:"BS",weight:301790},{id:"BB",weight:285653},
  142. {id:"EH",weight:273008},{id:"PF",weight:270485},
  143. {id:"VU",weight:221552},{id:"NC",weight:216494},
  144. {id:"GF",weight:195506},{id:"WS",weight:192001},
  145. {id:"ST",weight:175808},{id:"LC",weight:160922},
  146. {id:"GU",weight:159358},{id:"YT",weight:159042},
  147. {id:"CW",weight:141766},{id:"AN",weight:136197},
  148. {id:"TO",weight:122580},{id:"VI",weight:108708},
  149. {id:"GD",weight:107818},{id:"FM",weight:107708},
  150. {id:"VC",weight:104217},{id:"KI",weight:92533},
  151. {id:"JE",weight:90812},{id:"SC",weight:88340},
  152. {id:"AG",weight:86754},{id:"AD",weight:84000},
  153. {id:"IM",weight:75049},{id:"DM",weight:72813},
  154. {id:"AW",weight:71566},{id:"MH",weight:65859},
  155. {id:"BM",weight:65365},{id:"GG",weight:65228},
  156. {id:"AS",weight:57881},{id:"GL",weight:56375},
  157. {id:"MP",weight:53883},{id:"KN",weight:49898},
  158. {id:"FO",weight:48228},{id:"KY",weight:44270},
  159. {id:"SX",weight:37429},{id:"MF",weight:35925},
  160. {id:"LI",weight:35000},{id:"MC",weight:32965},
  161. {id:"SM",weight:31477},{id:"GI",weight:27884},
  162. {id:"AX",weight:26711},{id:"VG",weight:21730},
  163. {id:"CK",weight:21388},{id:"TC",weight:20556},
  164. {id:"PW",weight:19907},{id:"BQ",weight:18012},
  165. {id:"WF",weight:16025},{id:"AI",weight:13254},
  166. {id:"TV",weight:10472},{id:"NR",weight:10065},
  167. {id:"MS",weight:9341},{id:"BL",weight:8450},
  168. {id:"SH",weight:7460},{id:"PM",weight:7012},
  169. {id:"IO",weight:4000},{id:"FK",weight:2638},
  170. {id:"SJ",weight:2550},{id:"NU",weight:2166},
  171. {id:"NF",weight:1828},{id:"CX",weight:1500},
  172. {id:"TK",weight:1466},{id:"VA",weight:921},
  173. {id:"CC",weight:628},{id:"TF",weight:140},
  174. {id:"PN",weight:46},{id:"GS",weight:30}
  175. ];
  176. return rwc(countries);
  177. }
  178. });