-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeplaystocksTask3.js
128 lines (108 loc) · 3.77 KB
/
nodeplaystocksTask3.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
/*
* PURPOSE: Retrieve stock data directly from InterSystems IRIS Data Platform.
*
* NOTES: When running,
* 1. Choose option 2 to store stock data natively.
* 2. Choose option 3 to retrieve stock data natively.
*/
const irisnative = require('intersystems-iris-native')
const readline = require('readline-sync');
const fs = require('fs');
// Helper method: Get connection details from config file
function GetConnections(filename){
var connections = {};
var array = fs.readFileSync(filename).toString().split("\n");
for(i in array) {
// Remove all spaces and split line based on colon
var details = array[i].replace(/\s/g, '').split(":")
connections[details[0]] = details[1]
}
return connections;
}
function main()
{
// Get connection details from connections.config
var connections = GetConnections("connections.config");
// Retrieve connection information from configuration file
var ip = connections["ip"];
var port = connections["port"];
var namespace = connections["namespace"];
var username = connections["username"];
var password = connections["password"];
// Create connection to InterSystems IRIS
const connection = irisnative.createConnection({host: ip, port: port, ns: namespace, user: username, pwd: password})
// Create InterSystems IRIS native object
const iris = connection.createIris()
console.log("Connected to InterSystems IRIS")
// Starting interactive prompt
while(true)
{
console.log("1. Test");
console.log("2. Store Stock Data");
console.log("3. View stock data");
console.log("4. Generate Trades");
console.log("5. Call routines");
console.log("6. Quit");
var selection = readline.question("What would you like to do? ")
switch(selection){
case "1":
SetTestGlobal(iris)
break;
case "2":
StoreStockData(iris);
break;
case "3":
ViewStockData(iris);
break;
case "4":
console.log("TO DO: Generate trades")
break;
case "5":
console.log("TO DO: Call routines");
break;
case "6":
console.log("Exited");
return;
default:
console.log("Invalid option. Try again!");
break;
}
}
}
// Write to a test global
function SetTestGlobal(irisNative)
{
irisNative.set(8888, "^testglobal", "1");
globalValue = irisNative.get("^testglobal", "1");
console.log("The value of ^testglobal(1) is " + globalValue);
}
// Store stock data directly into InterSystems IRIS
function StoreStockData(irisNative){
// Clear global from previous runs
irisNative.kill("^nyse");
console.log("Storing stock data using Native API...");
// Get all stock data from all_stocks.csv file into a list
var array = fs.readFileSync('all_stocks.csv').toString().split("\n");
// Get start time
var start = Date.now()/1000;
// Loop through list of stock and store natively
for (var j = 1; j < array.length; j++){
irisNative.set(array[j], "^nyse", j);
}
// Get time consuming
var end = Date.now()/1000;
var totalConsume = (end - start).toFixed(4);
console.log("Stored natively successfully. Execution time: " + totalConsume + "ms");
}
// Iterate over all nodes and print
function ViewStockData(irisNative)
{
// Create iterator
var iter = irisNative.iterator("^nyse");
console.log("Walk forwards");
// Iterate over all nodes
for ([key, value] of iter){
console.log("subscript = " + key + ", value = " + value);
}
}
main()