Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // create max datetime for given day
- function max_date(d) {
- return new Date(d.setHours(23,59,59));
- }
- // create min datetime for given day
- function min_date(d) {
- return new Date(d.setHours(0,0,0));
- }
- // add to given object proper type2 field
- function add_type2(one) {
- if (one.type == 'no_picked') {
- one.type2 = 'no_picked';
- } else {
- if (one.status == 'confirmed' || one.status == 'shift') {
- one.type2 = 'answered';
- } else {
- one.type2 = 'not_answered';
- }
- }
- return one;
- }
- // return list of records from mongodb
- function return_records(day,consultant) {
- var t1 = min_date(day);
- var t2 = max_date(day);
- var pom = new Date(1970,1,1,0,0,0,0);
- // pipeline for call_history collection
- var pipeline_1 = [
- {'$match': { // choose all calls for chosen consultant in chosen time period
- 'consultant': 'ObjectId('+consultant+')',
- 'started_at': {
- '$gte': 'Date('+t1.toMongo()+')',
- '$lte': 'Date('+t2.toMongo()+')'}
- }
- },
- {'$project': { // create m_seconds field which is duration field changed into milliseconds
- 'status': 1,
- 'started_at': 1,
- 'm_seconds': {'$multiply': ['$duration', 1000]}}
- },
- {'$project': { // create end field and auxiliary pom_date field
- 'pom_date': 'Date('+pom.toMongo()+')',
- 'started_at': 1,
- 'end': {
- '$add': ['$started_at','$m_seconds']
- },
- 'status': 1}
- },
- {'$project': { // creates ts_ fields which holds timestamp
- 'status': 1,
- 'started_at': 1,
- 'end': 1,
- 'ts_started_at': {
- '$subtract': ['$started_at','$pom_date']
- },
- 'ts_end': {
- '$subtract': ['$end','$pom_date']}
- }
- },
- {'$sort': {'ts_started_at': 1}}, // sort
- {'$addFields': {'type': 'call'}} // add type field
- ];
- var records_1 = VM.mongo.aggregate('call_history',pipeline_1);
- var pipeline_2 = [ // pipeline for call_queue_pick_log collection
- {'$match': {
- 'created_at': { // choose no_picked attemps in chosen time by chosen consultant
- '$gte': 'Date('+t1.toMongo()+')',
- '$lte': 'Date('+t2.toMongo()+')'
- },
- 'user': 'ObjectId('+consultant+')',
- 'contact_picked': false,
- 'created_at': {'$exists': true}} // just in case
- },
- {'$project': { // create pom_date secondary field
- 'pom_date': 'Date('+pom.toMongo()+')',
- 'created_at': 1}
- },
- {'$project': { // create ts_started_at field which is created_at changed into timestamp
- 'created_at': 1,
- 'ts_started_at': {
- '$subtract': ['$created_at','$pom_date']}
- }
- },
- {'$sort': {'ts_started_at': 1}}, // sort
- {'$addFields': { // add the rest of required fields
- 'type': 'no_picked',
- 'started_at': '$created_at',
- 'end': '$created_at',
- 'ts_end': '$ts_started_at'}
- }
- ];
- var records_2 = VM.mongo.aggregate('call_queue_pick_log',pipeline_2);
- if (records_2.length == 0) { // if list with no_picked attemps is empty
- return records_1; // return list with calls only
- } else { // else join lists and sort them
- var records = new Array();
- var index = 0;
- var len_limit = records_2.length;
- for (x=0; x<records_1.length; x++) {
- while (records_2[index].ts_started_at < records_1[x].ts_started_at && index < len_limit) {
- records.push(add_type2(records_2[index])); // before put into target list add type2 property
- index ++;
- }
- records.push(add_type2(records_1[x])); // before put into target list add type2 property
- }
- return records; // return joined list
- }
- }
- // return the difference between two values in milliseconds expressed in seconds
- function dif_sec(a,b) {
- return (b - a) / 1000;
- }
- // run the test
- function main_2b(day, consultant, limit_a, limit_b, limit_c, limit_d) {
- var work_time = 0;
- var empty_time = 0;
- var rest_time = 0;
- var T = new Array(); // [start, period_type]
- var Long_periods = new Array(); // [work_time, empty_time, rest_time, T]
- var records = return_records(day,consultant);
- if (records.length == 0) {
- return 0;
- }
- var period_start = null;
- var period_type = null; // 1 - work_time; 2 - empty_time; 3 - rest_time
- var prev_stop = null;
- var prev_type2 = null;
- // checking first record
- period_start = [records[0].started_at,records[0].ts_started_at];
- if (records[0].type == 'call') {
- period_type = 'call';
- T.push([period_start[0],1]);
- } else if (records[0].type = 'no_picked') {
- period_type = 'no_picked';
- T.push([period_start[0],2]);
- }
- prev_stop = records[0].ts_end;
- prev_type2 = records[0].type2;
- // checking all other records
- for (x = 1; x<records.length; x++) {
- var diff = dif_sec(prev_stop,records[x].ts_started_at);
- if (diff >= limit_b) { // if this is the end of period
- if (period_type == 'call') {
- work_time += dif_sec(period_start[1],prev_stop);
- } else {
- empty_time += dif_sec(period_start[1],prev_stop);
- }
- T.push([prev_stop, 0]);
- Long_periods.push(T);
- T = new Array();
- period_start = [records[x].started_at,records[x].ts_started_at];
- if (records[x].type == 'call') {
- T.push([records[x].started_at,1]);
- period_type = 'call';
- } else {
- T.push([records[x].started_at,2]);
- period_type = 'no_picked';
- }
- // else if the break is too long
- } else if ((prev_type2 == 'not_answered' && diff >= limit_a) || (prev_type2 == 'answered' && diff >= limit_d) || (prev_type2 == 'no_picked' && diff >= limit_c)) {
- if (period_type == 'call') {
- work_time += dif_sec(period_start[1],prev_stop);
- } else {
- empty_time += dif_sec(period_start[1],prev_stop);
- }
- T.push([prev_stop,3]);
- rest_time += dif_sec(prev_stop,records[x].ts_started_at);
- period_start = [records[x].started_at,records[x].ts_started_at];
- if (records[x].type == 'call') {
- T.push([records[x].started_at,1]);
- period_type = 'call';
- } else {
- T.push([records[x].started_at,2]);
- period_type = 'no_picked';
- }
- } else { // if break is not too long
- if (records[x].type != period_type) {
- if (records[x].type == 'call') {
- empty_time += dif_sec(period_start[1],records[x].ts_started_at);
- T.push([records[x].started_at,1]);
- period_start = [records[x].started_at,records[x].ts_started_at];
- period_type = 'call';
- } else {
- work_time += dif_sec(period_start[1],records[x].ts_started_at);
- T.push([records[x].started_at,2]);
- period_start = [records[x].started_at,records[x].ts_started_at];
- period_type = 'no_picked';
- }
- }
- }
- prev_stop = records[x].ts_end;
- prev_type2 = records[2].type2;
- }
- T.push([prev_stop, 0]);
- Long_periods.push(T);
- return [Long_periods, work_time, empty_time, rest_time];
- }
- function main_2() {
- var limit_a = 4 * 60; // the limit after contact that was neither confirmed nor shifted
- var limit_b = 5 * 60 * 60; // the limit of continuity of one period
- var limit_c = 2 * 60; // the limit after failed attempt to get a contact from the queue
- var limit_d = 10 * 60; // the limit after confirmed / shift contact
- var day = new Date(2018,6,10,0,0,0,0); // selected day
- var consultant = "5aa10ace68a2366db728b936"; // selected consultant
- // run test
- var response = main_2b(day, consultant, limit_a, limit_b, limit_c, limit_d);
- var List = response[0]; // list in format: [period,period...]
- // when period = [[start,type],[start,type]]
- // types: working time: 1, empty time: 2, rest time: 3, end of period: 0
- var work_time = response[1]; // amount of seconds when consultant was working
- var empty_time = response[2]; // amount of seconds when the queue was empty
- var rest_time = response[3]; // amount of seconds when consultant was not working
- /*
- VM.log('\n\n');
- VM.log('work_time: '+ work_time);
- VM.log('empty_time: '+ empty_time);
- VM.log('rest_time: '+ rest_time);
- VM.log('List:\n\n');
- for (x = 0; x<List.length; x++) {
- VM.log(List[x]);
- VM.log('\n');
- }
- */
- }
- main_2();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement