Skip to content

Commit

Permalink
adds latest route to api
Browse files Browse the repository at this point in the history
in regards to #16: still needs to be documented on openapi.json
  • Loading branch information
ivoputzer committed Oct 11, 2023
1 parent 61e3a1e commit 496db32
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions bin/build-coinbase-latest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env node --experimental-modules

import { createReadStream, createWriteStream } from 'node:fs'
import { mkdir } from 'node:fs/promises'
import { EOL } from 'node:os'

import { readCache, byExchange, readLastCachedJsonLineOf } from '../lib/cache.mjs'
import { csvDateReplacer, jsonDateReviver } from '../lib/date.mjs'

console.log('bin/build-coinbase-latest load: data/* ')

try {
console.time('bin/build-coinbase-latest create: @api/latest.{format:json,csv} time')

await mkdir('www/api', { recursive: true })
console.log('bin/build-coinbase-latest exists: www/api/')

const latestJson = createWriteStream('www/api/latest.json')
latestJson.write('[')

const latestCsv = createWriteStream('www/api/latest.csv')
latestCsv.write('id,interval,date,open,high,low,close,volume' + EOL)

let isFirstLine = true
for (const file of await readCache(byExchange('coinbase'))) {
console.time(`→ load: ${file} appendLine: @api/latest.{format:json,csv} time`)
const lastCachedCandle = await readLastCachedJsonLineOf(createReadStream(file), jsonDateReviver)
const [, , id, interval] = file.split(/[/,.]/)
if (isFirstLine) {
isFirstLine = false
latestJson.write(JSON.stringify([id, interval, ...lastCachedCandle]))
} else {
latestJson.write(`,${JSON.stringify([id, interval, ...lastCachedCandle])}`)
}
latestCsv.write(`${id},${interval},${JSON.stringify(lastCachedCandle, csvDateReplacer).slice(1, -1).replaceAll('"', '')}${EOL}`)
console.timeEnd(`→ load: ${file} appendLine: @api/latest.{format:json,csv} time`)
}
latestJson.write(']' + EOL)
} catch (error) {
console.error(error)
} finally {
console.timeEnd('bin/build-coinbase-latest create: @api/latest.{format:json,csv} time')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build-sitemap": "node --experimental-modules bin/build-sitemap.mjs",
"build-coinbase": "node --experimental-modules bin/build-coinbase.mjs",
"build-coinbase-history": "node --experimental-modules bin/build-coinbase-history.mjs",
"test": "NODE_V8_COVERAGE=coverage node --experimental-modules --test --test-reporter spec --experimental-test-coverage test",
"build-coinbase-latest": "node --experimental-modules bin/build-coinbase-latest.mjs",
"watch": "serve www & node --experimental-modules --test --test-reporter dot --watch test & wait"
},
"author": "Ivo von Putzer Reibegg <[email protected]> (https://github.com/ivoputzer)",
Expand Down

0 comments on commit 496db32

Please sign in to comment.