-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestController.js
127 lines (109 loc) · 3.53 KB
/
RequestController.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
var index = require( './index' );
var dataController = require( index.handle.sink );
var dataSource = require( index.handle.source );
var querystring = require( 'querystring' );
var mustache = require( 'mustache' );
var util = require( 'util' );
var fs = require( 'fs' );
var path = require( 'path' );
function route( segments, response, postData ) {
switch( segments[ 1 ] ) {
case "approach":
singleApproachView( segments, response, postData );
break;
case "static" :
loadStaticView( segments, response, postData );
break;
case "fetch_trends" :
fetchTrends( segments, response, postData );
break;
default:
mainView( segments, response, postData );
break;
}
}
function mainView( segments, response, postData ) {
var dataSources = dataSource.getDataSourceList();
var approachList = dataController.getApproachList();
response.writeHead(200, { "content-type" : "text/html" } );
var data = fs.readFile( index.templateDirectory + 'index.html', 'utf8', function( err, data ) {
if( err ) {
}
else {
response.write( mustache.to_html( data.toString(), {
dataSources : dataSources,
approachList : approachList
} ) );
response.end();
}
} );
}
function singleApproachView( segments, response, postData ) {
if ( postData ) {
var parsedData = querystring.parse( postData );
var approachList = JSON.parse( parsedData.approachList ).list;
dataSource.initDataSource( {
'sourceName' : parsedData.sourceName,
'dataRate' : parsedData.dataRate,
'approachList' : approachList
} );
dataController.setCron( {
'approachList' : approachList,
'scoreRate' : parsedData.scoreRate
} );
response.writeHead( 200, { "content-type" : "text/html" } );
var data = fs.readFile( index.templateDirectory + 'singleApproachView.html', 'utf8', function( err, data ) {
if( err ) {
}
else {
response.write( mustache.to_html( data.toString(), {} ) );
response.end();
}
} );
}
else {
}
}
function loadStaticView( segments, response, postData ) {
var filePath = index.staticDirectory + segments.slice( 2 ).join( '/' );
var data = fs.readFileSync( filePath, 'utf8' );
switch( path.extname( filePath ) ){
case '.css':
response.writeHead( 200, { "content-type" : "text/css" } );
break;
case '.js':
response.writeHead( 200, { "content-type" : "text/javascript" } );
break;
default:
response.writeHead( 200, { "content-type" : "text" } );
}
response.write( data );
response.end();
}
function fetchTrends( segments, response, postData ) {
if ( postData ) {
var parsedData = querystring.parse( postData );
var trends = {};
dataSource.fetchGroundTruth( {
'sourceName' : parsedData.sourceName,
'trends' : trends,
'requestTime' : new Date().getTime(),
'callback' : function( options ) {
options.trends.approaches = [];
dataController.fetchTrends( {
'approachList' : JSON.parse( parsedData.approachList ).list,
'trends' : options[ 'trends' ],
'callback' : function( options ) {
options.trends.memoryUsage = process.memoryUsage();
response.writeHead( 200, { "content-type" : "application/json" } );
response.write( JSON.stringify( options ['trends' ] ) );
response.end();
}
} );
}
} );
}
else {
}
}
exports.route = route;