-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
172 lines (165 loc) · 4.94 KB
/
app.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
/* eslint-env node */
/* eslint-disable no-console */
const http = require( "http" ),
url = require( "url" ),
path = require( "path" ),
mime = require( "mime" ),
fs = require( "fs" ),
SocketIOFileUploadServer = require( "./socketio-file-upload" ),
socketio = require( "socket.io" ),
express = require( "express" ),
redis = require( "redis" ),
dl = require( 'datalib' );
const redisOptions = {
port: 6379,
host: 'redis',
detect_buffers: true,
socket_keepalive: true
};
const redisClient = redis.createClient( redisOptions );
redisClient.on( "error", function ( error ) {
console.error( error );
} );
let app,
io;
// Simple Static File Server. Used under the terms of the BSD license.
// http://classes.engineering.wustl.edu/cse330/index.php/Node.JS
app = http.createServer( function ( req, resp ) {
var filename = path.join( __dirname, "public", url.parse( req.url ).pathname );
( fs.exists || path.exists )( filename, function ( exists ) {
if ( exists ) {
fs.readFile( filename, function ( err, data ) {
if ( err ) {
// File exists but is not readable (permissions issue?)
resp.writeHead( 500, { "Content-Type": "text/plain" } );
resp.write( "Internal server error: could not read file" );
resp.end();
return;
}
// File exists and is readable
var mimetype = mime.lookup( filename );
resp.writeHead( 200, { "Content-Type": mimetype } );
resp.write( data );
resp.end();
return;
} );
}
} );
} );
//app.listen(3456);
//io = socketio.listen(app);
//SocketIOFileUploadServer.listen(app);
app = express()
.use( SocketIOFileUploadServer.router )
.use( express.static( __dirname + "/out" ) )
.use( express.static( __dirname + "/public" ) )
.listen( 8089 );
io = socketio( app );
io.on( "connection", function ( socket ) {
var siofuServer = new SocketIOFileUploadServer();
socket.on( 'redisCodes', function ( data ) {
let code_list = data[ 'data' ];
for ( code in code_list ) {
readRedisData( socket, redisClient, code_list[code], data['freq'], data[ 'intervalSec' ] );
}
} )
siofuServer.on( "saved", function ( event ) {
console.log( event.file );
event.file.clientDetail.base = event.file.base;
readCSVData( socket );
} );
siofuServer.on( "error", function ( data ) {
console.log( "Error: " + data.memo );
console.log( data.error );
} );
siofuServer.on( "start", function ( event ) {
if ( /\.exe$/.test( event.file.name ) ) {
console.log( "Aborting: " + event.file.id );
siofuServer.abort( event.file.id, socket );
}
} );
siofuServer.dir = "uploads";
siofuServer.maxFileSize = 200000000;
siofuServer.listen( socket );
} );
function readCSVData( socket ) {
let data = dl.csv( __dirname + '/uploads' + '/temp.csv', {
parse: {
'datetime': 'string',
'code': 'string',
'name': 'string',
'open': 'number',
'high': 'number',
'low': 'number',
'close': 'number',
'vol': 'number'
}
} );
let groupdata = dl
.groupby( 'code' )
.execute( data );
let code_list = [],
intervalSec = 5000;
for ( let i = 0; i < groupdata.length; i++ ) {
code_list.push( groupdata[ i ].code )
};
socket.emit( 'code_list', { data: code_list } );
socket.on( 'intervalSec', function ( data ) {
intervalSec = data[ 'data' ];
for ( code in code_list ) {
send_data( socket, code_list[code], groupdata[ code ].values, intervalSec );
}
} );
}
function readRedisData( socket, redisClient, code, freq, intervalSec ) {
redisClient.get( 'history_' + code + '_' + freq, function ( err, reply ) {
if ( reply === null ) {
console.log( `${ code } did not has any data` )
} else {
reply = JSON.parse( reply )
let name = Object
.keys( reply )
.includes( 'name' )
? code + ' ' + reply.name[ 0 ]
: code;
let i = 0;
let intervalID = setInterval( function () {
if ( i === [...Object.keys( reply.datetime )].length ) {
clearInterval( intervalID );
} else {
socket.emit( code, {
data: {
datetime: reply.datetime[i],
open: reply.open[i],
high: reply.high[i],
low: reply.low[i],
close: reply.close[i],
vol: reply.vol[ i ]
},
code: name //reply.code[i] + ' ' + reply.name[i]
} );
i += 1;
}
}, intervalSec )
}
} )
}
function send_data( socket, code, data, intervalSec ) {
let name = Object
.keys( data[ 0 ] )
.includes( 'name' )
? code + ' ' + data[ 0 ].name
: code;
let i = 0;
let intervalID = setInterval( function () {
if ( i === data.length ) { //data[ i ].datetime === null ||
clearInterval( intervalID );
} else {
socket.emit( code, {
data: data[i],
code: name
} );
i += 1;
}
}, intervalSec );
}