-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcities-hotspots.js
292 lines (262 loc) · 13.8 KB
/
cities-hotspots.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Ryan Patridge
* 2021-08-08
*
* Exports Helium hotspot data for specified cityIds.
*
*/
const httplib = require("@helium/http");
const { Console } = require("console");
const fs = require('fs');
// North Carolina cityIds for Apex, Cary, Chapel Hill, Durham, Knightdale, Morrisville, Raleigh
const cityIds = ['bG9zIGFuZ2VsZXNjYWxpZm9ybmlhdW5pdGVkIHN0YXRlcw']; // Los Angeles, 2300+ hotspots
/*
const cityIds = ['YXBleG5vcnRoIGNhcm9saW5hdW5pdGVkIHN0YXRlcw',
'Y2FyeW5vcnRoIGNhcm9saW5hdW5pdGVkIHN0YXRlcw',
'Y2hhcGVsIGhpbGxub3J0aCBjYXJvbGluYXVuaXRlZCBzdGF0ZXM',
'ZHVyaGFtbm9ydGggY2Fyb2xpbmF1bml0ZWQgc3RhdGVz',
'a25pZ2h0ZGFsZW5vcnRoIGNhcm9saW5hdW5pdGVkIHN0YXRlcw',
'bW9ycmlzdmlsbGVub3J0aCBjYXJvbGluYXVuaXRlZCBzdGF0ZXM',
'cmFsZWlnaG5vcnRoIGNhcm9saW5hdW5pdGVkIHN0YXRlcw'];
*/
const MAX_HOTSPOTS = 3000;
const MAX_HOTSPOTS_PER_CITY = 2000;
const MAX_HOTSPOTS_PAGES_PER_CITY = 200;
const HOTSPOTS_PAGE_SIZE = 10;
const TRANSACTIONS_PAGE_SIZE = 1000;
const MAX_TRANSACTIONS_PAGES = 100000;
const SCRIPT_START_TIME = Date.now();
const ONE_DAY_AGO = new Date().setDate(SCRIPT_START_TIME) - 1;
const SEVEN_DAYS_AGO = new Date().setDate(SCRIPT_START_TIME) - 7;
const THIRTY_DAYS_AGO = new Date().setDate(SCRIPT_START_TIME) - 30;
(async function main() {
let iHotspots = 0;
let fileStream;
const OUTPUT_FILE_NAME = "Cities_Hotspots_" + SCRIPT_START_TIME + ".csv";
console.log("Exporting hotspots to: " + OUTPUT_FILE_NAME)
try {
fileStream = fs.openSync(OUTPUT_FILE_NAME, 'a'); // Create a stream for appending, to avoid overwriting an existing file.
// Write column headers to the output file.
fs.appendFileSync(fileStream, "Name,Rewards1,Rewards7,Rewards30,Online,Height,Gps,Block,LastPocChallenge,LastChangeBlock,RewardScale,Location,Lat,Lng,Country,State,City,Street,Gain,Elevation,TimeStampAdded,Address,Owner\n");
const client = new httplib.Client();
//console.log(client.network.endpoint); //= https://api.helium.io/v1
// List all hotspots in each specified cityId.
for (const cityId of cityIds) {
console.log("cityId: " + cityId)
// https://github.com/helium/helium-js/blob/master/packages/http/src/resources/Hotspots.ts
// https://github.com/helium/helium-js/blob/master/packages/http/src/models/Hotspot.ts
// https://github.com/helium/helium-js/blob/master/packages/http/src/ResourceList.ts
// Note: client.city.hotspots.list() returns up to 1000 results all at once, in one page.
// If there are more than 1000, results the returned data has a "cursor" property at the end.
// Note: this does not retrive a full city object, just a stub pointing to the cityId, used by Hotspots.ts to search for hotspots.
const hotspots = await client.city(cityId).hotspots.list();
//console.log(JSON.stringify(hotspots));
/*
{
"data": [{Hotspot},{Hotspot},...,{Hotspot}],
"cursor": "eyJoZWlnaHQiOjk1OTA3NiwiZmlsdGVyX21vZGVzIjpbImZ1bGwiLCJsaWdodCIsImRhdGFvbmx5Il0sImJlZm9yZV9ibG9jayI6ODkxNzkyLCJiZWZvcmVfYWRkcmVzcyI6IjExMlFVSzNBYWU5V1VMVzNRTFBNVG5LWEhGcW02NllFNVhKZ2s2NGVxNjh2dGhYZk1IclcifQ"
}
*/
break;
let iCityHotspots = 0;
let iCityHotspotsPages = 0;
do {
const hotspotsPage = await hotspots.take(HOTSPOTS_PAGE_SIZE);
//console.log(JSON.stringify(hotspotsPage));
for (const hotspot of hotspotsPage) {
/*
* Example hotspot JSON.
*/
/*
{
"client": {
"network": {
"baseURL": "https://api.helium.io",
"version": 1
},
"retry": 5
},
"rewardScale": 0.80645751953125,
"owner": "14b3zTWdeiYueJx6vuyC9UVJBrvx2FX3Y5sGqJTc44yJFCi7JTo",
"name": "trendy-grey-worm",
"location": "8c2ad6d2c0f15ff",
"lng": -78.85130915556415,
"lat": 35.83589399242602,
"block": 904783,
"status": {
"gps": "",
"height": 904694,
"online": "online",
"listenAddrs": ["/ip4/136.56.6.56/tcp/44158"]
},
"nonce": 4,
"blockAdded": 465868,
"timestampAdded": "2020-08-23T16:40:36.000000Z",
"lastPocChallenge": 904401,
"lastChangeBlock": 904772,
"gain": 12,
"elevation": 0,
"geocode": {
"shortStreet": "Point Comfort Ln",
"shortState": "NC",
"shortCountry": "US",
"shortCity": "Cary",
"longStreet": "Point Comfort Lane",
"longState": "North Carolina",
"longCountry": "United States",
"longCity": "Cary",
"cityId": "Y2FyeW5vcnRoIGNhcm9saW5hdW5pdGVkIHN0YXRlcw"
},
"address": "112VNJtZVeDjEMp3NN6msjzq3Tqb7BbZ4rVLKzHtK8PKn23bofUM"
}
*/
// https://github.com/helium/helium-js/blob/master/packages/http/src/resources/Transactions.ts
// https://github.com/helium/helium-js/blob/master/packages/http/src/models/Transaction.ts
// https://github.com/helium/helium-js/blob/master/packages/http/src/ResourceList.ts
//const transactions = await hotspot.activity.list({
// filterTypes: ['payment_v1','payment_v2','rewards_v1','rewards_v2','poc_receipts_v1' ]
// });
//const transactions = await hotspot.activity.list();
const transactions = await hotspot.activity.list({
filterTypes: ['rewards_v1', 'rewards_v2']
});
//console.log(JSON.stringify(transactions));
/*
* Example transactions ResourceList JSON.
*/
/*
{
"type": "rewards_v2",
"time": 1625266597,
"startEpoch": 904785,
"rewards": [{
"type": "data_credits",
"gateway": "112EkjffyXdNdLFQMcghKaK5TtwsajSaFG9rE3KSMjaH1RStjAyS",
"amount": {
"type": {
"ticker": "HNT",
"decimalPlaces": "8",
"coefficient": "0.00000001"
},
"integerBalance": 164,
"bigInteger": "164",
"bigBalance": "0.00000164",
"floatBalance": 0.00000164
},
"account": "14b3zTWdeiYueJx6vuyC9UVJBrvx2FX3Y5sGqJTc44yJFCi7JTo"
}],
"height": 904822,
"hash": "RlfgVMzpn6JQ5WAxf56FtsiWjTKNWbfASXnA4BotcEA",
"endEpoch": 904821,
"totalAmount": {
"type": {
"ticker": "HNT",
"decimalPlaces": "8",
"coefficient": "0.00000001"
},
"integerBalance": 164,
"bigInteger": "164",
"bigBalance": "0.00000164",
"floatBalance": 0.00000164
}
}
*/
let iTransactionPages = 0;
let floatRewards1 = 0;
let floatRewards7 = 0;
let floatRewards30 = 0;
try {
do {
const transactionsPage = await transactions.take(TRANSACTIONS_PAGE_SIZE);
//console.log(JSON.stringify(transactionsPage));
for (const transaction of transactionsPage) {
let transactionTime = new Date(transaction.time * 1000); // Transaction time is in seconds, not milliseconds expected by Date.
// Remember to add floatRewards1 to floatRewards7 and floatRewards30, and floatRewards7 to floatRewards30, afterward.
if (transactionTime < THIRTY_DAYS_AGO) {
break;
} else if (transactionTime < SEVEN_DAYS_AGO) {
floatRewards30 += transaction.totalAmount.floatBalance;
} else if (transactionTime < ONE_DAY_AGO) {
floatRewards7 += transaction.totalAmount.floatBalance;
} else {
floatRewards1 += transaction.totalAmount.floatBalance;
}
} // for each transaction
iTransactionPages++;
if (iTransactionPages >= MAX_TRANSACTIONS_PAGES) {
console.log("WARNING: Reached configurable limit (MAX_TRANSACTIONS_PAGES): " + iTransactionPages);
floatRewards7 = -1;
floatRewards30 = -1;
}
} while (transactions.hasMore && iTransactionPages < MAX_TRANSACTIONS_PAGES)
floatRewards7 += floatRewards1
floatRewards30 += floatRewards7
} catch (ex) {
console.log("ERROR: Exporting rewards for hotspot: " + hotspot.name);
console.log(ex);
floatRewards1 = -1;
floatRewards7 = -1;
floatRewards30 = -1;
}
let outCSV = "";
outCSV = hotspot.name + "," +
floatRewards1 + "," +
floatRewards7 + "," +
floatRewards30 + "," +
hotspot.status.online + "," +
hotspot.status.height + "," +
JSON.stringify(hotspot.status.gps) + "," +
//hotspot.status.timestamp + "," +
hotspot.block + "," +
hotspot.lastPocChallenge + "," +
hotspot.lastChangeBlock + "," +
//hotspot.score + "," +
//hotspot.scoreUpdateHeight + "," +
hotspot.rewardScale + "," +
hotspot.location + "," +
//hotspot.locationHex + "," +
hotspot.lat + "," +
hotspot.lng + "," +
hotspot.geocode.shortCountry + "," +
hotspot.geocode.shortState + "," +
hotspot.geocode.shortCity + "," +
hotspot.geocode.shortStreet + "," +
hotspot.gain + "," +
hotspot.elevation + "," +
//hotspot.mode + "," +
hotspot.timestampAdded + "," +
hotspot.address + "," +
hotspot.owner + "\n";
// Append a record to the output file.
fs.appendFileSync(fileStream, outCSV);
iCityHotspots++;
iHotspots++;
if (iHotspots >= MAX_HOTSPOTS) {
console.log("WARNING: Reached configurable limit (MAX_HOTSPOTS): " + iHotspots);
break;
}
if (iCityHotspots >= MAX_HOTSPOTS_PER_CITY) {
console.log("WARNING: Reached configurable limit (MAX_HOTSPOTS_PER_CITY): " + iCityHotspots);
break;
}
} // for each hotspot
console.log("Hotspots: " + iCityHotspots);
iCityHotspotsPages++;
if (iCityHotspotsPages >= MAX_HOTSPOTS_PAGES_PER_CITY) {
console.log("WARNING: Reached configurable limit (MAX_HOTSPOTS_PAGES): " + iCityHotspotsPages);
break;
}
} while (hotspots.hasMore && iHotspots < MAX_HOTSPOTS && iCityHotspots < MAX_HOTSPOTS_PER_CITY && iCityHotspotsPages < MAX_HOTSPOTS_PAGES_PER_CITY)
if (iHotspots >= MAX_HOTSPOTS) {
break;
}
} // For each cityId
} catch (ex) {
console.log("ERROR: Exporting hotspots to file: " + OUTPUT_FILE_NAME);
console.log(ex);
} finally {
// Close the file stream.
if (fileStream) fs.closeSync(fileStream);
} // try file append
})().catch(ex => { console.error(ex) });