-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
1244 lines (1085 loc) · 45 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var http = require('http'),
express = require('express'),
bodyParser = require('body-parser'),
path = require('path'),
mongoose = require('mongoose'),
util = require('util'),
nconf = require('nconf'),
https = require('https'),
Botkit = require('botkit'),
fs = require('fs'),
math = require('mathjs'),
request = require('request'),
CrowdCnsusModule = require(__dirname + '/src/db/CrowdCnsusModule.js'),
CrowdConsensus = require(__dirname + "/src/db/CrowdConsensus.js"),
QuesInquirer = require(__dirname + "/src/slack/QuesInquirer.js"),
QuesScheduler = require(__dirname + "/src/slack/QuesScheduler.js"),
ExhaustiveScheduler = require(__dirname + "/src/slack/ExhaustiveScheduler.js"),
async = require('async'),
GreedyApproach = require(__dirname + "/src/algo/GreedyApproach.js"),
GreedyArrayFile = require(__dirname + "/src/algo/GreedyArrayFile.js"),
GreedyArray = require(__dirname + "/src/algo/GreedyArray.js");
var schema = require(__dirname + "/src/db/Schema.js"),
CCReply = schema.CCReply,
CCModel = schema.CCModel;
nconf.argv()
.env()
.file({ file: __dirname + '/config.json' });
var port = process.env.PORT || 3000,
mongodb_url = nconf.get('CROWD_CONSENSUS_MONGO_URL'),
crowdconsensus_token = nconf.get('UNIVERSAL_TOKEN');
// Connect mongodb
mongoose.Promise = global.Promise;
mongoose.connect(mongodb_url, function (error) {
if (error) console.error(error);
else console.log('mongo connected');
});
var greedy;
var startTime, endTime;
var sampleSize = nconf.get("NUMBER_OF_USERS_TO_ASK");
var timeoutInterval = nconf.get("TIMEOUT_INTERVAL");
var EXHAUSTIVE_APPROACH = 0, TIMEOUT_METHOD = 1, DATA_COLLECTION_METHOD = EXHAUSTIVE_APPROACH;
var app = express();
// body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// at the base level
app.get('/', function (req, res) {
console.log('Welcome to CrowdConsensus-II with Probability');
res.status(200).send('Welcome to CrowdConsensus-II with Probability!\n');
});
/******************************************************************************************************
* Save the aggregated result into CrowdConsensus module
**************************************************************************************************/
function crowdCollect(cb_id, callback){
CCReply.aggregate([
{
$group : {
_id : {
parent_id : '$parent_id',
criterion : '$criterion',
object1 : '$object1',
object2 : '$object2',
reply : '$reply'
},
count : {$sum : 1}
}
},
{
$group : {
_id : {
criterion : '$_id.criterion',
parent_id : '$_id.parent_id',
object1 : '$_id.object1',
object2 : '$_id.object2'
},
total_count : { $sum : '$count' },
result : {
$push : {
'reply' : '$_id.reply',
'count' : '$count'
}
}
}
},
{
$unwind : '$result'
},
{
$project : {
_id : {
criterion : '$_id.criterion',
parent_id : '$_id.parent_id',
object1 : '$_id.object1',
object2 : '$_id.object2'
},
gt : {$cond : {if : {'$eq' : ['$result.reply', '>']}, then : {'$divide' : ['$result.count', '$total_count']}, else : '0.0'}},
lt : {$cond : {if : {'$eq' : ['$result.reply', '<']}, then : {'$divide' : ['$result.count', '$total_count']}, else : '0.0'}},
indiff : {$cond : {if : {'$eq' : ['$result.reply', '~']}, then : {'$divide' : ['$result.count', '$total_count']}, else : '0.0'}}
}
},
{
$group : {
_id : {
parent_id : '$_id.parent_id',
criterion : '$_id.criterion',
object1 : '$_id.object1',
object2 : '$_id.object2'
},
gt : {$sum : '$gt'},
lt : {$sum : '$lt'},
indiff : {$sum : '$indiff'}
}
}
], function(err, result){
var retArr = [];
if(err) console.error(err);
else {
// console.log("__________replies________");
for(var i = 0; i < result.length; i++){
if(result[i]._id.parent_id != cb_id) continue;
//console.log(JSON.stringify(result[i]));
var returnVal = {};
var multi = 1;
returnVal.criterion = result[i]._id.criterion;
returnVal.parent_id = result[i]._id.parent_id;
returnVal.object1 = result[i]._id.object1;
returnVal.object2 = result[i]._id.object2;
returnVal.gt = result[i].gt;
returnVal.lt = result[i].lt;
returnVal.indiff = result[i].indiff
retArr.push(returnVal);
}
}
callback(retArr);
});
}
/******************************************************************************************************
* Update or insert into CrowdConsensus collection by aggregating 'CrowdReply' instances
********************************************************************************************************/
var upsert = function(oneReply, callback){
var ins = function(){
CCModel.update(
{
'_id' : oneReply.parent_id
},
{'$push' : {'responses' : {
'object1' : oneReply.object1,
'object2' : oneReply.object2,
'criterion' : oneReply.criterion,
'gt' : oneReply.gt,
'lt' : oneReply.lt,
'indiff' : oneReply.indiff
}}},
function(err, model){
if(err) console.error("Error at CCModel.update: " + err);
console.log('insert success ');
callback();
});
};
// try new method separating find and update or find and insert
CCModel.findOne({
'_id' : oneReply.parent_id,
'responses.object1' : oneReply.object1,
'responses.object2' : oneReply.object2,
'responses.criterion' : oneReply.criterion
},
function(err, model){
if(err) console.error("Error at CCModel.findOne: " + err);
if(model != null && model.length != 0){
// for update
CCModel.update(
{
'responses.object1' : oneReply.object1,
'responses.object2' : oneReply.object2,
'responses.criterion' : oneReply.criterion
},
{
'$pull' : {
'responses' : {
'object1' : oneReply.object1,
'object2' : oneReply.object2,
'criterion' : oneReply.criterion
}
}
},
function(err, model){
if(err) console.error("Error at CCModel.update: " + err);
console.log('remove success ' + oneReply.gt);
// after delete insert again
ins();
});
} else {
// for insert
ins();
}
});
}
/******************************************************************************************************
* Save the response from the user into the mongodb
**************************************************************************************************/
var saveInDB = function(cb_id, userId, userName, questionParam, returnedVal) {
CCReply.count({
'parent_id' : cb_id,
'object1' : questionParam.object1,
'object2' : questionParam.object2,
'criterion' : questionParam.criterion
}, function(err, count){
if(count <= sampleSize - 1) {
var crModel = new CCReply();
crModel.parent_id = cb_id;
crModel.member_id = userId;
crModel.name = userName;
crModel.object1 = questionParam.object1;
crModel.object2 = questionParam.object2;
crModel.criterion = questionParam.criterion;
crModel.reply = returnedVal;
// save the record in mongo collection
crModel.save(function(err, body){
console.error(err);
console.log('the new entry is now saved in CrowdReply');
// aggregate crowd replies into CrowdConsensus collection
crowdCollect(cb_id, function(replyCrowdCollect){
// update the response field with the replyCrowdCollect in crowdconsensus collection
// using async library
var arr1 = [];
async.each(replyCrowdCollect, function(file, callback){
arr1.push(function(callback1){
upsert(file, function(){
callback1(null, '');
});
});
console.log("---------NEW PROB VALUES_______");
console.log(JSON.stringify(file));
callback();
}, function(err){
if(err) console.error(err);
async.parallel(arr1, function(err, results){
if(err) console.error(err);
console.log(" results -> " + JSON.stringify(results));
});
});
});
});
} else if(count >= sampleSize) {
console.log("Sorry we can't update your responses for ("+ questionParam.object1 +
" " + questionParam.criterion + " "+ questionParam.object2 +") into our database because we have saturated our " + sampleSize + " limitation of responses");
}
});
};
/**************************************
* For initializing the SlackBot
**************************************/
var controller = Botkit.slackbot({
interactive_replies: true, // tells botkit to send button clicks into conversations
json_file_store: './db_slackbutton_bot/'
// rtm_receive_messages: false, // disable rtm_receive_messages if you enable events api
}).configureSlackApp(
{
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
scopes: ['bot']
}
);
/*
controller.spawn({
token: nconf.get("BOT_ID")
}).startRTM(function(err) {
if (err) {
throw new Error(err);
}
});
*/
controller.setupWebserver(port, function(err,webserver) {
controller.createWebhookEndpoints(controller.webserver);
controller.createOauthEndpoints(controller.webserver,function(err,req,res) {
if (err) {
console.log("oauth failure: " + err);
res.status(500).send('ERROR: ' + err);
} else {
console.log("oauth sucess");
res.send('Success!');
}
});
});
// just a simple way to make sure we don't
// connect to the RTM twice for the same team
var _bots = {};
function trackBot(bot) {
_bots[bot.config.token] = bot;
}
controller.storage.teams.all(function(err,teams) {
if (err) {
throw new Error(err);
}
// connect all teams with bots up to slack!
for (var t in teams) {
if (teams[t].bot) {
controller.spawn(teams[t]).startRTM(function(err, bot) {
if (err) {
console.log('Error connecting bot to Slack:',err);
} else {
console.log('connected the bot to Slack:',err);
trackBot(bot);
}
});
}
}
});
/***
* A Controller to detect the callback from the interactive buttons i.e.
* when the user clicks on one of the interactive buttons this event is triggered
* This however is not used anymore and can be deleted without much effect
***/
controller.on('interactive_message_callback', function(bot, message) {
console.log("interactive_message_callback is received baby !");
var reply = {
text: ' ',
attachments: [],
};
bot.replyInteractive(message, reply);
});
/**************************************************************************************
* controller to detect when the bot is created
**************************************************************************************/
controller.on('create_bot',function(bot,config) {
if (_bots[bot.config.token]) {
// already online! do nothing.
} else {
bot.startRTM(function(err) {
if (!err) {
trackBot(bot);
}
bot.startPrivateConversation({user: config.createdBy},function(err,convo) {
if (err) {
console.log(err);
} else {
convo.say('I am a bot that has just joined your team');
convo.say('You must now /invite me to a channel so that I can be of use!');
}
});
});
}
});
// Handle events related to the websocket connection to Slack
controller.on('rtm_open',function(bot) {
console.log('** The RTM api just connected!');
});
controller.on('rtm_close',function(bot) {
console.log('** The RTM api just closed');
// you may want to attempt to re-open
});
/**************************************************
* For requesting the poll to be created by the bot
**************************************************/
controller.on('file_share', function(bot, message) {
var id = message.file.id;
// file.info
bot.api.files.info({
file: id
}, function(err,res) {
if (err) {
console.log("Failed to read file : "+err);
bot.reply(message, 'Sorry, there has been an error: '+err);
} else {
var comment = '';
if(res.comments.length > 0) comment = res.comments[0].comment;
if(comment == "crowdconsensus"){
var ccm = CrowdCnsusModule.createCrowdCnsusModule(res.content);
var replyMsg = CrowdConsensus.createCrowdConsensus(ccm);
bot.reply(message, replyMsg, function(err,resp) {
if(err) console.error(err);
});
}
}
});
});
/******************************************************************************
* Find the name of the user from the given Id
*********************************************************************************/
var lookupUserNameFromId = function(userId) {
return userId;
var members = [];
// previous one
for(var mem in members) {
if(members[mem].id == userId)
return members[mem].name;
}
return "NAME_NOT_FOUND";
}
/************************************************************************************
* Shows the help text to the user
**************************************************************************************/
var help = function(bot, message, inChannel) {
CrowdConsensus.getList(function(lists){
var attachments = [],
attachment1 = {
title: 'If you want to create a new Pareto-Optimal Finding Problem, just upload your JSON file into '+
'any channel and write \'crowdconsensus\' as the comment in the upload',
color: '#87CEFA',
fields: []
},
attachment2 = {
title: 'You shall be asked question when someone types [/ask (ID)] in a channel or mentions the bot with text \'Help\' \n ID | Description',
color: '#FFCC99',
fields: []
},
attachment3 = {
title: 'You can set the timeout interval for a question by typing [/settimeout (time in seconds)]',
color: '#A9CB7B',
fields: []
},
replyObj = {
text: 'Hello, Seems like you need some help. Don\'t worry. I\'m here to help. Take a look into some of my suggestions below:\n'
};
for(var i = 0; i < lists.length; i++){
attachment2.fields.push({
id: lists[i].id,
title: (i + 1) + " | " + lists[i].desc
});
}
attachments.push(attachment1);
if(lists.length > 0)
attachments.push(attachment2);
attachments.push(attachment3);
replyObj.attachments = attachments;
if(inChannel) bot.replyPrivate(message, replyObj, function(err, resp){console.log(err, resp);});
else bot.reply(message, replyObj, function(err,resp) {
console.log(err,resp);
});
});
};
/************************************************************************************
* The Exhaustive Framework for Asking questions to the users
**************************************************************************************/
var exhaustiveAskFramework = function(bot, message, cb_id, members, channelId) {
CrowdConsensus.getResponses(cb_id, function(resp){
// reset the instance if previously stopped
if(ExhaustiveScheduler.getInstance() && ExhaustiveScheduler.getInstance().STOP_ASKING_QUESTION)
ExhaustiveScheduler.self = null;
ExhaustiveScheduler.create(resp);
// add members
for(var member in members) {
if(!members[member].is_bot && members[member].id !== 'USLACKBOT' && !members[member].deleted)
ExhaustiveScheduler.getInstance().activeUsers.push(members[member].id);
}
// set the total population count
ExhaustiveScheduler.getInstance().totalPopulation = ExhaustiveScheduler.getInstance().activeUsers.length;
ExhaustiveScheduler.getInstance().on('question_user_paired', function(pair, uid, index, ts){
bot.startPrivateConversation({user : uid}, function(err, convo){
if(err) {
console.log(err);
return;
}
convo.ask({
delete_original : true,
attachments:[
{
title: "In comparing these 2 : *" + pair.object1 + "* and *" + pair.object2 + "*, which one do you think is better in terms of *" + pair.criterion+"*",
fallback : 'You have a new question',
callback_id: "" + ts +":"+ index,
attachment_type: 'default',
actions: [
{
"name": ""+pair.object1 + "," + pair.object2+"," + pair.criterion,
"text": ""+pair.object1 + " > " + pair.object2,
"type": "button",
"value": "gt"
},
{
"name": ""+pair.object1 + "," + pair.object2+"," + pair.criterion,
"text": ""+pair.object1 + " < " + pair.object2,
"type": "button",
"value": "lt"
},
{
"name": ""+pair.object1 + "," + pair.object2+"," + pair.criterion,
"text": "" + pair.object1 + " ~ " + pair.object2,
"type": "button",
"value": "~",
}
]
}
]
},[
{
pattern: "gt",
callback: function(reply, convo) {
var username = lookupUserNameFromId(reply.user);
console.log("The username is : " + reply.user + ", >");
//console.log("received response with timestamp : " + reply.callback_id);
ExhaustiveScheduler.getInstance().nextQues(reply.user, parseInt(reply.callback_id.split(":")[1], 10), function(pr, askedCount) {
if(pr) {
convo.say('#' + askedCount + ' You said *' + pr.object1 + '* is better than *' + pr.object2 + '* on criteria *' + pr.criterion+'*');
saveInDB(cb_id, reply.user, reply.user, pr, '>');
} else convo.say('It looks like you already answered that one');
});
convo.next();
}
},
{
pattern: "lt",
callback: function(reply, convo) {
var username = lookupUserNameFromId(reply.user);
console.log("The username is : " + reply.user + ", <");
//console.log("received response with timestamp : " + reply.callback_id);
ExhaustiveScheduler.getInstance().nextQues(reply.user, parseInt(reply.callback_id.split(":")[1], 10), function(pr, askedCount) {
if(pr) {
convo.say('#' + askedCount + ' You said *' + pr.object2 + '* is better than *' + pr.object1 + '* on criteria *' + pr.criterion+'*');
saveInDB(cb_id, reply.user, reply.user, pr, '<');
} else convo.say('It looks like you already answered that one');
});
convo.next();
}
},
{
pattern: "~",
callback: function(reply, convo) {
var username = lookupUserNameFromId(reply.user);
console.log("The username is : " + reply.user + ", ~");
ExhaustiveScheduler.getInstance().nextQues(reply.user, parseInt(reply.callback_id.split(":")[1], 10), function(pr, askedCount) {
if(pr) {
convo.say('#' + askedCount + ' You said *' + pr.object1 + '* is indifferent to *' + pr.object2 + '* on criteria *' + pr.criterion+'*');
saveInDB(cb_id, reply.user, reply.user, pr, '~');
} else convo.say('It looks like you already answered that one');
});
convo.next();
}
},
{
default: true,
callback: function(reply, convo) {
console.log("received response with timestamp : " + reply.callback_id + " which couldnt be located");
}
}
]);
});
});
ExhaustiveScheduler.getInstance().on('problem_finish', function(a) {
// Tell the user that the task is done
bot.api.im.open({user : a}, function(err4, res4) {
if(res4.channel.id)
bot.api.chat.postMessage({channel : res4.channel.id, text : "The task is finished. Thank you for your response"}, function(err3, res2) {
if(err3) console.log(""+err3);
});
});
});
// start asking users
if(ExhaustiveScheduler.getInstance().activeUsers.length > 0) ExhaustiveScheduler.getInstance().scheduleQues(cb_id);
else console.log("The population is zero. Something wrong. Hmmm");
});
};
/************************************************************************************
* The general timeout Framework for Asking questions to the users
**************************************************************************************/
var quesAskFramework = function(bot, message, cb_id, members, channelId) {
CrowdConsensus.getResponses(cb_id, function(resp){
// reset the instance if previously stopped
if(QuesScheduler.getInstance() && QuesScheduler.getInstance().STOP_ASKING_QUESTION)
QuesScheduler.self = null;
QuesScheduler.create(resp);
console.log(".....STOP_ASKING_QUESTION.... " + QuesScheduler.getInstance().STOP_ASKING_QUESTION);
QuesScheduler.getInstance().TIMEOUT_FOR_USER_TO_RESPOND = timeoutInterval;
var popnCount = 0;
console.log("size -> " + members.length);
for(var member in members) {
if(!members[member].is_bot && members[member].id !== 'USLACKBOT' && !members[member].deleted ) {
// && (members[member].id == "U28260VFX" /*|| members[member].id == "U281R5JFJ"*/)) {
QuesScheduler.getInstance().activeUsers.push(members[member].id);
popnCount++;
console.log(members[member].name);
}
}
QuesScheduler.getInstance().totalPopulation = popnCount;
// Listener that is triggered when a question is paired with suitable candidates and ready to ask them question
var getQuesUserPairListener = function(pair, uid){
console.log('**\n The paired users will now be asked questions with uniqueTimeStamp: ' + pair.uniqueTimeStamp + '**\n');
sampleSize = QuesScheduler.getInstance().minUserThreshold;
bot.startPrivateConversation({user : uid}, function(err, convo){
if(err) {
console.log(err);
return;
}
convo.ask({
delete_original : true,
attachments:[
{
title: "Between the two objects *" + pair.object1 + "* and *" + pair.object2 + "*, which is better on criteria *" + pair.criterion+"*",
fallback : 'You have a new question',
callback_id: "" + pair.uniqueTimeStamp +":"+ uid,
attachment_type: 'default',
actions: [
{
"name": ""+pair.object1 + "," + pair.object2+"," + pair.criterion,
"text": ""+pair.object1 + " > " + pair.object2,
"type": "button",
"value": "gt"
},
{
"name": ""+pair.object1 + "," + pair.object2+"," + pair.criterion,
"text": ""+pair.object1 + " < " + pair.object2,
"type": "button",
"value": "lt"
},
{
"name": ""+pair.object1 + "," + pair.object2+"," + pair.criterion,
"text": "" + pair.object1 + " ~ " + pair.object2,
"type": "button",
"value": "~",
}
]
}
]
},[
{
pattern: "gt",
callback: function(reply, convo) {
var username = lookupUserNameFromId(reply.user);
console.log("The username is : " + reply.user + ", >");
//console.log("received response with timestamp : " + reply.callback_id);
var pr = QuesScheduler.getInstance().checkTimeStamp(reply.callback_id);
if(!pr){
convo.say("Sorry your response to this question was timed out");
} else if(QuesScheduler.getInstance().answerRecorded(reply.callback_id, reply.user)) {
convo.say('You said *' + pr.object1 +
'* is better than *' + pr.object2 + '* on criteria *' + pr.criterion+'*');
saveInDB(cb_id, reply.user, reply.user, pr, '>');
} else convo.say("Thanks but we already have your input. Sorry!");
convo.next();
}
},
{
pattern: "lt",
callback: function(reply, convo) {
var username = lookupUserNameFromId(reply.user);
console.log("The username is : " + reply.user + ", <");
//console.log("received response with timestamp : " + reply.callback_id);
var pr = QuesScheduler.getInstance().checkTimeStamp(reply.callback_id);
if(!pr){
convo.say("Sorry your response to this question was timed out");
} else if(QuesScheduler.getInstance().answerRecorded(reply.callback_id, reply.user)) {
convo.say('You said *' + pr.object2 +
'* is better than *' + pr.object1 + '* on criteria *' + pr.criterion+'*');
saveInDB(cb_id, reply.user, reply.user, pr, '<');
} else convo.say("Thanks but we already have your input. Sorry!");
convo.next();
}
},
{
pattern: "~",
callback: function(reply, convo) {
var username = lookupUserNameFromId(reply.user);
console.log("The username is : " + reply.user + ", ~");
//console.log("received response with timestamp : " + reply.callback_id);
var pr = QuesScheduler.getInstance().checkTimeStamp(reply.callback_id);
if(!pr){
convo.say("Sorry your response to this question was timed out");
} else if(QuesScheduler.getInstance().answerRecorded(reply.callback_id, reply.user)) {
convo.say('You said *' + pr.object2 +
'* is indifferent to *' + pr.object1 + '* on criteria *' + pr.criterion+'*');
saveInDB(cb_id, reply.user, reply.user, pr, '~');
} else convo.say("Thanks but we already have your input. Sorry! ");
convo.next();
}
},
{
default: true,
callback: function(reply, convo) {
console.log("default msg recorded");
console.log("received response with timestamp : " + reply.callback_id + " which couldnt be located");
// do nothing
convo.say('Oops! Your message reply duration was timed out. Sorry! ');
//convo.next();
}
}
]);
// add timer for that user to know if he has answered within the specified time limit
QuesScheduler.getInstance().startTimer(pair.uniqueTimeStamp, uid);
});
};
QuesScheduler.getInstance().on('question_user_paired', getQuesUserPairListener);
QuesScheduler.getInstance().on('min_threshold_satisfied', function(a){
});
QuesScheduler.getInstance().on('problem_finish', function(a){
console.log("__________THE PROBLEM IS FINISHED_____________");
var realID = JSON.stringify(cb_id);
realID = realID.slice(1,-1);
var totalWorld = math.pow(3, QuesScheduler.getInstance().questionList.length);
var chunkSize = totalWorld, iter = 1;
while(chunkSize > 400000) {
chunkSize = chunkSize / 10;
chunkSize = chunkSize >> 0; // convert into integer
iter *= 10;
}
// now pass the data to the algorithm through shell script
var addr = (process.env.DEST_IP_ADDR) ? process.env.DEST_IP_ADDR : nconf.get("DEST_IP_ADDR");
request({
url: addr+'/pinger', //URL to hit
method: 'POST',
//Lets post the following key/values as form
form: {totalWorld : totalWorld, chunkSize : chunkSize, iter : iter, cb_id : realID}
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, body);
}
});
setTimeout(function(){QuesScheduler.destroy();}, 10000); // This wait time is there to allow writing the final input into mongodb
// Tell the user that the task is done
if(channelId)
bot.api.chat.postMessage({channel : channelId, text : "The task is finished. Thank you for your response"}, function(err3, res2) {
if(err3) console.log(""+err3);
});
});
if(popnCount > 0)
QuesScheduler.getInstance().scheduleQues();
else console.log("The population is zero. Something wrong. Hmmm");
});
};
/*****************************************************************************
* Find the detailed Information of the user from its id
******************************************************************************/
var getUserInfo = function(bot, id, callback) {
bot.api.users.info({user : id}, function(err2, res2) {
callback(res2.user);
});
};
/*********************************************************
* Detect Slash Commands
**********************************************************/
controller.on('slash_command',function(bot,message) {
console.log("command triggerred -> " + message.command);
// Perform action for the command 'ask'
if(message.command === "/ask"){
console.log("Now performing the 'ask' operation");
if(message.text === "") {
//bot.replyPublic(message,'<@' + message.user + '> is cool!');
//bot.replyPrivate(message,'*nudge nudge wink wink*');
bot.replyPrivate(message, '<@' + message.user + "> Please enter the valid integer parameter. Type '/helpme' if you need help");
} else if(parseInt(message.text)) {
var problemId;
try{
problemId = parseInt(message.text);
} catch(ex){
console.error("Error converting text");
}
console.log("Channel name: " + message.channel);
if(problemId) {
CrowdConsensus.findId(problemId, true, function(cb_id){
var lookAtChannel = true, lookAtGroup = false;
// a function that triggers the questions. The variable 'membs' can either be the channel members or group members
var starter = function(membs){
var usersInChannel = membs;
var detailUsersInfoList = [];
for(var i = 0; i < usersInChannel.length; i++) {
getUserInfo(bot, usersInChannel[i], function(detailUsersInfo){
detailUsersInfoList.push(detailUsersInfo);
console.log("detailUsersInfo -> " + detailUsersInfo.name + " and count: " + i)
// don't ask if previously already asked or the bot details couldb't be found and ask only at the last iterat of this loop
if(detailUsersInfo && detailUsersInfoList.length == usersInChannel.length) {
if(DATA_COLLECTION_METHOD == EXHAUSTIVE_APPROACH && !ExhaustiveScheduler.getInstance())
exhaustiveAskFramework(bot, message, cb_id, detailUsersInfoList, message.channel);
else if (DATA_COLLECTION_METHOD == TIMEOUT_METHOD && !QuesScheduler.getInstance())
quesAskFramework(bot, message, cb_id, detailUsersInfoList, message.channel);
} else {
console.log("Most probably the QuesScheduler instance is not null");
//bot.replyPrivate(message, "You need to close previous session of questions. Use \'stopall [id] command");
}
});
}
};
if(lookAtChannel) {
bot.api.channels.info({channel : message.channel}, function(err, res){
if(err) {
console.log("Failed to read channels : " + err);
lookAtChannel = false; lookAtGroup = true;
}
if(lookAtGroup) {
bot.api.groups.info({channel : message.channel}, function(err1, res1){
if(err1) {
console.log("Failed to read that group :" + err1 +" with id: " + message.channel);
lookAtGroup = false, lookAtChannel = false;
bot.reply("Sorry", 'Sorry, there has been an error. We couldnot find that channel '+err);
}
if(!err1) {
console.log("Should be asking question to a group");
starter(res1.group.members);
}
});
}
if(!err) {
console.log("Should be asking question to a channel");
starter(res.channel.members);
}
});
}
});
bot.replyPrivate(message, "Please take a look at the message sent by the bot");
//bot.replyPrivate(message,'*nudge nudge wink wink*');
//bot.replyPublicDelayed(message,'Reply Delayed');
//console.log("messge channel : " + message.channel);
} else {
bot.replyPrivate(message, '<@' + message.user +"> Please enter the valid integer parameter. Type '/helpme' if you need help");
}
}
}
// Perform action for the slash command 'settimeout'
else if(message.command === "/settimeout") {
console.log("Now performing the 'settimeout' operation");
if(message.text === "") {
bot.replyPrivate(message, '<@' + message.user + '>' + " You need to pass the parameter in seconds. Type '/helpme' if you need help");
} else if(parseInt(message.text)){
var timeoutValue = parseInt(message.text);
timeoutInterval = timeoutValue * 1000;
bot.replyPrivate(message, "The timeout for question is now set to " + timeoutValue + " seconds");
} else {
bot.replyPrivate(message, '<@' + message.user +"> Please enter the valid integer parameter. Type '/helpme' if you need help");
}
}
// Perform action for the slash command 'helpme'
else if(message.command == "/helpme") {
console.log("Now performing the 'helpme' operation");
// bot.replyPrivate(message, '<@' + message.user + '>' + " Don\'t worry I\'m here to help you. ");
help(bot, message, true);
// Stop asking questions
} else if(message.command == "/stopall") {
if(message.text === "") {
bot.replyPrivate(message, '<@' + message.user + '>' + " You need to pass the id of the problem. Type '/helpme' if you need help");
} else if(parseInt(message.text)){
var num = parseInt(message.text);
var instance;
if(DATA_COLLECTION_METHOD == EXHAUSTIVE_APPROACH) instance = ExhaustiveScheduler.getInstance();
else if(DATA_COLLECTION_METHOD == TIMEOUT_METHOD) instance = QuesScheduler.getInstance();
if(instance) {
instance.STOP_ASKING_QUESTION = true,
isFirstReply = true;
CrowdConsensus.findId(num, isFirstReply, function(cb_id){
CCModel.update({'_id' : cb_id}, {'$set' : {'responses' : []}}, function(err){
if(err) console.error(err);
else {
console.log("Deleted all entries");
CCReply.remove({parent_id : cb_id}, function(err){
if(err) console.error(err);
else console.log("Deleted all associated the replies");
if(DATA_COLLECTION_METHOD == TIMEOUT_METHOD)
setTimeout(function(){QuesScheduler.destroy()}, (instance.TIMEOUT_FOR_USER_TO_RESPOND + 2000));
else if(DATA_COLLECTION_METHOD == EXHAUSTIVE_APPROACH)
ExhaustiveScheduler.destroy();
console.log("secs -> "+(instance.TIMEOUT_FOR_USER_TO_RESPOND / 1000) + 2);
bot.replyPrivate(message, 'OK all process will be stopped. Please wait for ' +
((instance.TIMEOUT_FOR_USER_TO_RESPOND / 1000) + 2) + " secs before asking any questions again");
});
}
});
});
} else console.log("QuesScheduler instance is null");
} else {
bot.replyPrivate(message, '<@' + message.user +"> Please enter the valid integer parameter. Type '/helpme' if you need help");
}
}
// get the results and show them
else if(message.command == "/getresults") {
if(message.text === "") {
bot.replyPrivate(message, '<@' + message.user + '>' + " You need to pass the id of the problem. Type '/helpme' if you need help");
} else if(parseInt(message.text)){
var num = parseInt(message.text),
isFirstReply = true;
CrowdConsensus.findId(num, isFirstReply, function(cb_id){
CCModel.findOne({"_id" : cb_id}, function(err, model){
var objects = model.objects,
results = model.subscribers;
var outputStr = "";
if(results.length > 0) {
// we know that the algorithm has run successfully
for(var i = 0; i < objects.length; i++) {
outputStr += (""+objects[i]+" : " + results[i]+" \n ");
}