-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
191 lines (159 loc) · 5.59 KB
/
server.js
File metadata and controls
191 lines (159 loc) · 5.59 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
////////////////////////////////////////////////////
//////// RedisLab and HTTP SERVER ///////
/////// version 0.5.0 ///////
//////////////////////////////////////////////////
'use strict';
const config = require('./config');
const order = require('./models/orders');
const express = require('express');
const path = require('path');
const banterfile = require('./texts');
const countries = require('./countries/country')
const Redis = require('ioredis');
const fs = require("fs");
const XLSX = require("xls-to-json");
const app = express();
const server = require('http').Server(app);
const port = process.env.PORT || 3002;
////////////////////////////////////////////////
let redisPort = config.redis.port;
let redisHost = config.redis.host;
let redisPassword = config.redis.password;
let redis = new Redis({
port: redisPort,
host: redisHost
});
let pub = new Redis({
port: redisPort,
host: redisHost
});
// cli command line
let action = process.argv[2];
/////////////////////// arrays with additional data to decorate the text messages ////////////////
let img = ['1', '2', '3', '4', '5', '6', '7', '8', 'chatbot', 'helpbot', 'smartbot']
let orders = []
let requests = [
"i am looking for ",
"please ship me 12 ",
"how much for ",
"what is the price of ",
"are you carrying ",
"what is the cost to ship ",
"i would like to order ",
"please replenish my prior order ",
"how do i place an order for ",
"ship 6 of ",
"email catalogue at joe@gmail.com for ",
" send to pat@xiollc.com the pricing for ",
" you can reach me at bot@hotmail.com for status on ",
"urgently need status at me@geo.org for "
]
// if the order.json file does not exist import the test spreadsheet with 8000+ order items
// to save json file set output to products/products.json -- else set to null
// Delete the order collection in session db on monglab if json file removed here
if (!fs.existsSync("products/orders.json")) {
XLSX({
input: "products/samplesales.xls",
output: "products/orders.json",
sheet: "Orders"
}, function(err, result) {
if(err) {
console.error(err);
} else {
console.log(result[0]['customername'])
console.log(result[0]['salesamt'])
// this creates the order collection in session on mongodb
// used by microplex microservices for ai interactions
result.filter((item) => {
order.save((item), () => {
// do nothing on callback
})
})
}
})
}
switch (action) {
case "banter":
banter()
break;
case "product":
product();
break;
default:
console.log('Error ' + '' + action + ' not recognized')
break;
}
// subscribe to a channel
redis.subscribe(action, function (err, count) {
console.log("Subscribed to " + count + " channel")
});
// listen for orders
redis.subscribe('orders', function (err, count) {
console.log("Subscribed to " + count + " channel")
});
// log message when detected on redis channel
redis.on('message', function (channel, message) {
console.log("Channel> " + channel + " Message> " + message);
});
// publish messages randomly -- test runner for chaotic platform
// add a country name and avatar chosen randomly from arrays
// Note that the target in the config file refers to the host and port where the
// static assets reside which is chaotic dash
function streambanter() {
let msgObj = banterfile[Math.floor(Math.random() * banterfile.length)];
msgObj.flagURL = config.target + "/img/flags/" + countries[Math.floor(Math.random() * countries.length)].name + ".png"
msgObj.avatarURL = config.target + "/img/avatars/" + img[Math.floor(Math.random() * img.length)] + ".jpg"
let sendMsg = JSON.stringify(msgObj)
pub.publish(action, sendMsg);
}
function banter() {
setInterval(function() {
console.log('bantering');
streambanter()
}, 5000)};
// create a test file using banter file as base, randomly updated with requests
// This serves as the basis for deeper testing on the chaotic platform
function prepproducts(cb) {
let id = 0
let msgObj = {}
let productObj = {}
let productarray = banterfile.filter((msg) => {
id++
//productObj = productfile[Math.floor(Math.random() * productfile.length)];
// msgObj.text = productObj.text
msg.id = id
msg.name = orders[Math.floor(Math.random() * orders.length)].customername
msg.text = requests[Math.floor(Math.random() * requests.length)] + orders[Math.floor(Math.random() * orders.length)].productname
return msg
})
cb(productarray)
}
const streamrequests = (arr) => {
let sendObj = arr[Math.floor(Math.random() * arr.length)];
sendObj.flagURL = config.target + "/img/flags/" + countries[Math.floor(Math.random() * countries.length)].name + ".png"
sendObj.avatarURL = config.target + "/img/avatars/" + img[Math.floor(Math.random() * img.length)] + ".jpg"
var sendMsg = JSON.stringify(sendObj)
pub.publish(action, sendMsg);
}
const streamorders = (arr) => {
let sendObj = arr[Math.floor(Math.random() * arr.length)];
var sendMsg = JSON.stringify(sendObj)
pub.publish('orders', sendMsg);
}
function product() {
if (fs.existsSync("products/orders.json")) {
orders = JSON.parse(fs.readFileSync("products/orders.json", {encoding: 'utf8'}))
prepproducts (function(arr) {
setInterval(function() {
streamrequests(arr)
}, 5000)
// setInterval(function() {
// streamorders(orders)
// }, 10000)
})
}
};
// server spins up and listed for the cli command
server.listen(port, function() {
console.log("Listening on port " + port)
})