-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadRedis.js
214 lines (164 loc) · 5.64 KB
/
readRedis.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var express = require('express');
var app = require('express')();
var server = require('http').Server(app);
var redis = require('redis');
app.use(express.static("public"));
app.set("view engine", "ejs");
var redisClient = redis.createClient();
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const { MongoClient } = require('mongodb');
var bigml = require('bigml');
var source = new bigml.Source();
const getDataRedis = function(){
//Store and get Hash i.e. object( as keyvalue pairs)
redisClient.LRANGE("packages",0,-1,function(err, data) {
if(data.length>0){
console.log("reply:")
console.log(data)
let itemsarr =[];
data.forEach((itemsPack)=>{
var items=JSON.parse(itemsPack);
console.log(items)
itemsarr.push({items});
})
const uri = "mongodb+srv://<userName>:<password>@cluster0.4e5rp.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect((err) => {
const db = client.db("arieldb");
const collection = db.collection("items");
// perform actions on the collection object
// the following code examples can be pasted here...
collection.insertMany(itemsarr, function(err, res) {
if (err) throw err;
console.log("Number of documents inserted: " + res.insertedCount);
let size = data.length;
redisClient.LTRIM("packages",size,-1);
getDataMongo(db,collection);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error("Not Found");
err.status = 404;
next(err);
});
});
});
client.close();
}
});
}
function getDataMongo(db,collection) {
// Use connect method to connect to the server
// the following code examples can be pasted here...
collection.find({}, { projection: { _id: 0} }).toArray(function (err, result) {
if (err) throw err;
console.log(result);
console.log("send to createor");
createCSVfile(result);
});
//console.log('documents =>', findResult);
return 'done.';
}
function createCSVfile(packages){
const csvWriter = createCsvWriter({
path: './CSVfiles/packages.csv',
header: [
{id: 'items', title: 'items'},
]
});
let records = [];
for(let i=0;i<packages.length;i++){
let record = packages[i].items[0];
for(let j=1; j<packages[i].items.length;j++){
record+=";"+packages[i].items[j];
}
records.push({items: record});
}
csvWriter.writeRecords(records) // returns a promise
.then(() => {
console.log("csv is ready")
});
}
let test = [{leftSide: '-', rightSide:'-',support: '-',confidence: '-', lift:'-'}];
function bigML(){
return new Promise(async (resolve,reject) => {
source.create('./CSVfiles/packages.csv', function(error, sourceInfo) {
if (!error && sourceInfo) {
var dataset = new bigml.Dataset();
dataset.create(sourceInfo, function(error, datasetInfo) {
if (!error && datasetInfo) {
var association = new bigml.Association();
association.create(datasetInfo, async (error, modelInfo)=> {
if (!error && modelInfo) {
console.log(modelInfo)
var rules = await getRules(modelInfo.resource)
resolve(rules)
}
else{
console.log("problem")
}
});
}
else{
console.error(error)
}
});
}
else{
console.error(error)
}
});
});
}
function getRules(associationId){
return new Promise(async (resolve,reject) => {
let rules = [];
var connection = new bigml.BigML('oriya2011', '5d493bfc7f0074769217b774e40e22d6cc15179c');
var association = new bigml.Association(connection)
association.get(associationId, true,
'only_model=true;limit=-1',
async (error, resource) =>{
if (!error && resource) {
try{
var localAssociation = new bigml.LocalAssociation(resource);
}catch{return [{leftSide: '-', rightSide:'-',support: '-',confidence: '-', lift:'-'}]}
//console.log(localAssociation.items);
let itemsMap ={};
console.log(localAssociation.items)
localAssociation.items.forEach((item)=>{
itemsMap[item.index]=item.name;
})
console.log("asos")
localAssociation.getRules().forEach((rule)=>{
let lhs = rule.lhs;
let leftSide="";
lhs.forEach((index)=>{
leftSide+=itemsMap[index]+" "
})
let rhs = rule.rhs;
let rightSide="";
rhs.forEach((index)=>{
rightSide+=itemsMap[index]+" "
})
let conf = rule.confidence;
let support = rule.support;
let lift = rule.lift;
rules.push({leftSide: leftSide, rightSide: rightSide,support: support,confidence: conf, lift:lift})
})
resolve(rules)
console.log("done with rules")
console.log(rules);
}
})
})
}
function buildAssociationRules(){
redisClient.on('connect', function() {
console.log('Reciver connected to Redis');
});
return new Promise(async (resolve,reject) => {
await getDataRedis();
var bigmlrules= await bigML();
resolve(bigmlrules);
})
}
module.exports = {bigML,buildAssociationRules};