-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerUtils.js
98 lines (79 loc) · 2.9 KB
/
ServerUtils.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
import GraphsCache from '../cryptodash-server/src/graphs/graphs-cache'
import Utils from './Utils'
import AuthService from '../cryptodash-server/src/auth/auth-service'
import WalletsService from '../cryptodash-server/src/wallets/wallets-service'
var fs = require('fs')
var xml2js = require('xml2js')
//import db from '../cryptodash-server/src/db'
const ONE_DAY = 1000 * 60 * 60 * 24
const timeFrames = {
"1d": ONE_DAY,
"1w": ONE_DAY * 7,
"1m": ONE_DAY * 30,
"1y": ONE_DAY * 365,
}
const knex = require('knex')
import { DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_DB } from '../cryptodash-server/src/config'
const db = knex({
client: 'pg',
connection: {
host: DATABASE_HOST,
user: DATABASE_USER,
password: DATABASE_PASSWORD,
database: DATABASE_DB,
},
})
import CoinIdMap from "./static_data/coin_id_map.json"
const ServerUtils = {
getCoinGraph(coin, timeFrame) {
const timeEnd = Date.now()
const timeStart = timeFrames[timeFrame] ? timeEnd - timeFrames[timeFrame] : 0
let [ok, graph] = GraphsCache.checkCache(coin, timeStart, timeEnd)
return ok ? graph : null
},
getUserWallets(authToken) {
const payload = AuthService.verifyJwt(authToken)
return AuthService.getUserWithUserName(
db,
payload.sub,
).then(user => {
if (!user || !user.id)
return null
return WalletsService.getWalletsForUser(db, user.id)
})
},
getRssData() {
var parser = new xml2js.Parser()
return parser.parseStringPromise(fs.readFileSync('static_data/crypto_rss.xml', 'utf8'));
},
getMarketData() {
return JSON.parse(fs.readFileSync('static_data/coins_markets_list.json', 'utf8'));
},
tryGetPredictionData(coin) {
let preloadedPrediction = GraphsCache.getPredictionCacheJSON(CoinIdMap[coin])
let lastRealData = preloadedPrediction[0].slice().pop()
const ONE_HOUR = 1000 * 60 * 60
// Make sure up to date within 24 h
if (!lastRealData || Date.now() - lastRealData[0] > ONE_HOUR * 24) {
// Micro optimization to already start preloading prediction before client makes 2nd request if no data ready now
try {
fetch(`http://localhost:8000/api/predictions/${CoinIdMap[coin]}`)
} catch { }
return [[], []]
}
return preloadedPrediction
}
}
function withServerDir(f) {
return (...args) => {
const saveCWD = process.cwd()
process.chdir("../cryptodash-server")
try {
return f(...args)
} finally {
process.chdir(saveCWD)
}
}
}
const MappedServerUtils = Utils.mapDict(ServerUtils, (k, v) => withServerDir(v))
export default MappedServerUtils