|
| 1 | +import moment from 'moment'; |
| 2 | +import { BaseController, TemplateHelpers } from './base_controller'; |
| 3 | +import { CandleExportHttp } from '../modules/system/candle_export_http'; |
| 4 | +import { CandleImporter } from '../modules/system/candle_importer'; |
| 5 | +import express from 'express'; |
| 6 | + |
| 7 | +export class CandlesController extends BaseController { |
| 8 | + private candleExportHttp: CandleExportHttp; |
| 9 | + private candleImporter: CandleImporter; |
| 10 | + |
| 11 | + constructor(templateHelpers: TemplateHelpers, candleExportHttp: CandleExportHttp, candleImporter: CandleImporter) { |
| 12 | + super(templateHelpers); |
| 13 | + this.candleExportHttp = candleExportHttp; |
| 14 | + this.candleImporter = candleImporter; |
| 15 | + } |
| 16 | + |
| 17 | + registerRoutes(router: express.Router): void { |
| 18 | + // GET - Display candle form and data |
| 19 | + router.get('/tools/candles', async (req: any, res: any) => { |
| 20 | + const options: any = { |
| 21 | + pairs: await this.candleExportHttp.getPairs(), |
| 22 | + start: moment().subtract(7, 'days').toDate(), |
| 23 | + end: new Date() |
| 24 | + }; |
| 25 | + |
| 26 | + if (req.query.pair && req.query.period && req.query.start && req.query.end) { |
| 27 | + const [exchange, symbol] = req.query.pair.split('.'); |
| 28 | + const candles = await this.candleExportHttp.getCandles(exchange, symbol, req.query.period, new Date(req.query.start), new Date(req.query.end)); |
| 29 | + |
| 30 | + if (req.query.metadata) { |
| 31 | + candles.map((c: any) => { |
| 32 | + c.exchange = exchange; |
| 33 | + c.symbol = symbol; |
| 34 | + c.period = req.query.period; |
| 35 | + return c; |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + options.start = new Date(req.query.start); |
| 40 | + options.end = new Date(req.query.end); |
| 41 | + options.exchange = exchange; |
| 42 | + options.symbol = symbol; |
| 43 | + options.period = req.query.period; |
| 44 | + options.candles = candles; |
| 45 | + options.candles_json = JSON.stringify(candles, null, 2); |
| 46 | + } |
| 47 | + |
| 48 | + res.render('candles', { |
| 49 | + activePage: 'candles', |
| 50 | + title: 'Candles | Crypto Bot', |
| 51 | + stylesheet: |
| 52 | + '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css" integrity="sha512-yVvxUQV0QESBt1SyZbNJMAwyKvFTLMyXSyBHDO4BG5t7k/Lw34tyqlSDlKIrIENIzCl+RVUNjmCPG+V/GMesRw==" crossorigin="anonymous" />', |
| 53 | + ...options |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + // POST - Import candles |
| 58 | + router.post('/tools/candles', async (req: any, res: any) => { |
| 59 | + const exchangeCandlesticks = JSON.parse(req.body.json); |
| 60 | + await this.candleImporter.insertCandles(exchangeCandlesticks); |
| 61 | + |
| 62 | + console.log(`Imported: ${exchangeCandlesticks.length} items`); |
| 63 | + |
| 64 | + res.redirect('/tools/candles'); |
| 65 | + }); |
| 66 | + } |
| 67 | +} |
0 commit comments