-
Notifications
You must be signed in to change notification settings - Fork 1
/
scan-miner-regions-locations.js
101 lines (93 loc) · 3.52 KB
/
scan-miner-regions-locations.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
const fs = require('fs')
const { formatWithOptions } = require('util')
const { load } = require('@alex.garcia/observable-prerender')
const workDir = process.env.WORK_DIR || '.'
const tmpDir = `${workDir}/tmp`
fs.mkdirSync(`${workDir}/input/miner-regions`, { recursive: true })
fs.mkdirSync(`${workDir}/input/miner-locations`, { recursive: true })
async function run () {
const notebook = await load(
'@jimpick/miner-report-miner-to-region-mapper',
['minerRegionsTable', 'minerLocationsTable', 'currentEpoch', 'regionHierarchy']
// { headless: false }
)
const epoch = await notebook.value('currentEpoch')
const minerRegions = await notebook.value('minerRegionsTable')
const minerLocations = await notebook.value('minerLocationsTable')
const regionHierarchy = await notebook.value('regionHierarchy')
await notebook.browser.close()
async function writeMinerRegions () {
fs.mkdirSync(`${workDir}/input/miner-regions/${epoch}`, { recursive: true })
const jsonFilename = `miner-regions-${epoch}.json`
const dest =`${workDir}/input/miner-regions/${epoch}/${jsonFilename}`
if (fs.existsSync(dest)) {
console.log(`File already exists, skipping. ${jsonFilename}`)
} else {
const jsonFile = fs.createWriteStream(`${tmpDir}/${jsonFilename}`)
for (const record of minerRegions) {
await jsonFile.write(JSON.stringify(record) + '\n')
}
jsonFile.on('finish', () => {
fs.rename(`${tmpDir}/${jsonFilename}`, dest, err => {
if (err) {
console.error('Error', err)
process.exit(1)
}
})
})
jsonFile.end()
console.log('Filename:', jsonFilename)
console.log('Epoch:', epoch)
console.log('Records:', minerRegions.length)
}
}
async function writeMinerLocations () {
fs.mkdirSync(`${workDir}/input/miner-locations/${epoch}`, { recursive: true })
const jsonFilename = `miner-locations-${epoch}.json`
const dest =`${workDir}/input/miner-locations/${epoch}/${jsonFilename}`
if (fs.existsSync(dest)) {
console.log(`File already exists, skipping. ${jsonFilename}`)
} else {
const jsonFile = fs.createWriteStream(`${tmpDir}/${jsonFilename}`)
for (const record of minerLocations) {
await jsonFile.write(JSON.stringify(record) + '\n')
}
jsonFile.on('finish', () => {
fs.rename(`${tmpDir}/${jsonFilename}`, dest, err => {
if (err) {
console.error('Error', err)
process.exit(1)
}
})
})
jsonFile.end()
console.log('Filename:', jsonFilename)
console.log('Epoch:', epoch)
console.log('Records:', minerLocations.length)
}
}
async function writeRegionHierarchy () {
fs.mkdirSync(`${workDir}/input/region-hierarchy/${epoch}`, { recursive: true })
const jsonFilename = `region-hierarchy-${epoch}.json`
const dest =`${workDir}/input/region-hierarchy/${epoch}/${jsonFilename}`
if (fs.existsSync(dest)) {
console.log(`File already exists, skipping. ${jsonFilename}`)
} else {
fs.writeFileSync(`${tmpDir}/${jsonFilename}`,
JSON.stringify(regionHierarchy, null, 2))
fs.rename(`${tmpDir}/${jsonFilename}`, dest, err => {
if (err) {
console.error('Error', err)
process.exit(1)
}
})
console.log('Filename:', jsonFilename)
console.log('Epoch:', epoch)
}
}
await writeMinerRegions()
await writeMinerLocations()
await writeRegionHierarchy()
await notebook.browser.close()
}
run()