app.get('/leads_customers_conversion',function(req, res){
		res.write('<h1 id="loading" style="margin: 10px">Loading...</h1>');
		client.getList(leads_list_id, function(err, data){
		    for (var i=0; i<data.fields.length; i++){
				if (data.fields[i].id == '61'){ //'61' is the id for lead source in leads list
					var lead_source_index = i;
					break;
				}
			}
		    //this will create an obj that contains all the lead sources and their ids from siq (leads list)
		    for (var i=0; i< data.fields[lead_source_index].listOptions.length;i++){ 
				lead_source_obj_siq[data.fields[lead_source_index].listOptions[i].id] = data.fields[lead_source_index].listOptions[i].display;
			}
			for (var i=0; i< data.fields[lead_source_index].listOptions.length;i++){ 
				obj[data.fields[lead_source_index].listOptions[i].display] = {};
			}

			continue_getting_lead_sources();
		});

		function continue_getting_lead_sources(){
			array_of_dates = [];

			arrayOfStartingNumberLeads = [];
			for (var i=0; i<num_leads; i=i+50) { //looping all starting number, for getListItems (since we can only fetch 50 items at a time)
			    arrayOfStartingNumberLeads.push(i);
			}
			arrayOfStartingNumberCustomers = [];
			for (var i=0; i<num_cus; i=i+50){
				arrayOfStartingNumberCustomers.push(i);
			}

			//push 5 months' date (beginning of each month in miliseconds)
			for (var i=4; i>=0; i--){
				var date = Date.today().addMonths(-i).toString('MM/01/yyyy');
				var date_in_mil = date_to_mil(date);
				array_of_dates.push(date_in_mil);
			}
			//push current date
			var current = new Date();
			array_of_dates.push(current.getTime());

			async_tasks = [];

			async.eachSeries(arrayOfStartingNumberLeads, function(item, next50items){
				async_tasks.push(function(callback){
					client.getListItems(leads_list_id, '_start=' + item, function(err, leads){
						for (var i=0; i<array_of_dates.length-1; i++){
							for (var j=0; j<leads.length; j++){
								created_date = leads[j].fieldValues['97'][0].raw; //'97' is id for created date for leads list
								if (created_date>=array_of_dates[i] && created_date<array_of_dates[i+1]){
									if (leads[j].fieldValues['61'] === undefined){ //'61' is id for leadsource for leads list
					                    continue;
					                } else {
					                    lead_source_id = leads[j].fieldValues['61'][0].raw;
					                    if (lead_source_id != null){
											var lead_source_siq = lead_source_obj_siq[lead_source_id];
											var date = which_date(i);
					                    	if (obj[lead_source_siq][date] == undefined){
					                    		obj[lead_source_siq][date] = [];
												obj[lead_source_siq][date].push([1]);
					                    	} else {
					                    		obj[lead_source_siq][date][0][0]++;
					                    	}
					                    }
					                }
								}
							}
						}
						callback();
					});
				});
				next50items();
			}, function(){
				async.parallel(async_tasks, function(){
					async.eachSeries(arrayOfStartingNumberCustomers, function(item, next50customers){
						client.getListItems(cus_list_id, '_start=' + item, function(err, customers){
							for (var j=0; j<customers.length; j++){
								for (var i=0; i<array_of_dates.length-1; i++){
									if (customers[j].fieldValues['60'] != undefined){ //'60' is id for created date (leads) in customers list
										created_date = customers[j].fieldValues['60'][0].raw; 
										if (created_date>=array_of_dates[i] && created_date<array_of_dates[i+1]){
											created_date_customer = customers[j].fieldValues['process_created_date'][0].raw; //'process_created_date' is id for created date for customers list

											for (var y=0; y<array_of_dates.length-1; y++){
												if (created_date_customer>=array_of_dates[y] && created_date_customer<array_of_dates[y+1]){
													if (customers[j].fieldValues['59'] === undefined){ //'59' is id for leadsource(leads) for customers list
									                    continue;
									                } else {
									                    lead_source_id = customers[j].fieldValues['59'][0].raw;
									                    if( lead_source_id != null){
															var lead_source_siq = lead_source_obj_siq[lead_source_id];
															var date = which_date(i);
									                    	if (obj[lead_source_siq] != undefined){
									                    		if (obj[lead_source_siq][date]!= undefined){
										                    		if (obj[lead_source_siq][date][y+1] == undefined){
											                    		obj[lead_source_siq][date][y+1] = [];
																		obj[lead_source_siq][date][y+1].push(1);
											                    	} else {
											                    		obj[lead_source_siq][date][y+1][0]++;
											                    	}
														        }
									           				}
									                    }
									                }  
												}
											}
											break;
										}	
									}
								}
							}
							next50customers();
						});
					}, function(){
						for (var i=0; i<array_of_dates.length; i++){
							array_of_dates[i] = mil_to_date(array_of_dates[i]);
						}
						//console.log(util.inspect(obj, false, null));
						res.write(compiled_template({
							"obj" : obj,
							"array_of_dates" : array_of_dates
						}));
						res.end();
					});
				});
			});
		}
	});
Ejemplo n.º 2
0
		function continue_getting_lead_sources(){
			//customers
			client.getList('5501be13e4b08567c6d76152', function(err, data){
				for (var i=0; i<data.fields.length; i++){
					if (data.fields[i].id == '29'){ //'29' is the id for lead source (accounts) in customers list
						var lead_source_index = i;
						break;
					}
				}
				lead_sources = data.fields[lead_source_index].listOptions;
				for (var i=0; i<lead_sources.length; i++){
					lead_source_customers_obj[lead_sources[i].display] = 0;
					prev_month_lead_source_customers_obj[lead_sources[i].display] = 0;
				}
				lead_source_customers_obj['UNK - Unknown - Unknown'] = 0;
				prev_month_lead_source_customers_obj['UNK - Unknown - Unknown'] = 0;
				for (var i=0; i< data.fields[lead_source_index].listOptions.length;i++){ //this will create an obj that contains all the lead sources and their ids from siq
					lead_source_customers_obj_siq[data.fields[lead_source_index].listOptions[i].id] = data.fields[lead_source_index].listOptions[i].display;
				}
			});

			arrayOfStartingNumberLeads = [];
			//<6000 leads as of Mar 2016
			for (var i = 0; i < 6000; i = i + 50) { //looping all starting number, for getListItems (since we can only fetch 50 items at a time)
			    arrayOfStartingNumberLeads.push(i);
			}
			//<800 customers as of Mar 2016
			arrayOfStartingNumberCustomers = [];
			for (var i = 0; i < 800; i = i + 50){
				arrayOfStartingNumberCustomers.push(i);
			}

			var date = new Date();
			var current_date_m_sec = date.getTime();
			var beg_of_month_m_sec = date_to_mil(Date.today().toString('MM/01/yyyy'));
			var beg_of_prev_month_m_sec = date_to_mil(Date.today().addMonths(-1).toString('MM/01/yyyy'));
			var this_month = Date.today().toString('MM/01/yyyy');
			var last_month = Date.today().addMonths(-1).toString('MM/01/yyyy');
			var array_of_dates = [];
			array_of_dates.push(this_month);
			array_of_dates.push(last_month);

			var async_tasks = [];
			//leads
			async.eachSeries(arrayOfStartingNumberLeads, function(item, next50items){
				async_tasks.push(function(callback){
					client.getListItems('54f14ea2e4b0c7427aeaa17f', '_start=' + item, function(err, leads){
				        for (var i=0; i<leads.length; i++){
				            created_date = leads[i].fieldValues['97'][0].raw; //'97' is id for created date for leads list
				            if (created_date>=beg_of_month_m_sec && created_date<=current_date_m_sec){
				                if (leads[i].fieldValues['61']===undefined||leads[i].fieldValues['61'][0].raw==null){ //'61' is id for leadsource for leads list
				                    lead_source_leads_obj['UNK - Unknown - Unknown']++;
				                } else {
				                    lead_source_id = leads[i].fieldValues['61'][0].raw;
				                    if(lead_source_id != null){
										var lead_source_siq = lead_source_obj_siq[lead_source_id];
										lead_source_leads_obj[lead_source_siq]++;
				                    }
				                }
				            } else if (created_date>=beg_of_prev_month_m_sec && created_date<beg_of_month_m_sec){
								if (leads[i].fieldValues['61'] === undefined){
									prev_month_lead_source_leads_obj['UNK - Unknown - Unknown']++;
								} else {
									lead_source_id = leads[i].fieldValues['61'][0].raw;
									if(lead_source_id != null){
										var lead_source_siq = lead_source_obj_siq[lead_source_id];
										prev_month_lead_source_leads_obj[lead_source_siq]++;
									}
								}
				            }
				        }
				        callback();
					});
				});
				next50items();
			}, function(err){
				if(err){
					console.log(err);
				} else {
					//customers
					async.eachSeries(arrayOfStartingNumberCustomers, function(item, next50items){
						async_tasks.push(function(callback){
							client.getListItems('5501be13e4b08567c6d76152', '_start=' + item, function(err, accounts){
								for (var i=0; i<accounts.length; i++){
									if (accounts[i].fieldValues['process_created_date'] != undefined){ //if created date is defined
										created_date = accounts[i].fieldValues['process_created_date'][0].raw; //'process_created_date' is id for created date for customers list
							            if (created_date>=beg_of_month_m_sec && created_date<=current_date_m_sec){
							                if (accounts[i].fieldValues['29']===undefined||accounts[i].fieldValues['29'][0].raw==null){ //'29' is id for leadsource for customers list
							                    lead_source_customers_obj['UNK - Unknown - Unknown']++;
							                } else {
							                    lead_source_id = accounts[i].fieldValues['29'][0].raw;
							                    if(lead_source_id != null){
													var lead_source_siq = lead_source_customers_obj_siq[lead_source_id];
													lead_source_customers_obj[lead_source_siq]++;
							                    }
							                }
							            } else if (created_date>=beg_of_prev_month_m_sec && created_date<beg_of_month_m_sec){
											if (accounts[i].fieldValues['29'] === undefined){
												prev_month_lead_source_customers_obj['UNK - Unknown - Unknown']++;
											} else {
												lead_source_id = accounts[i].fieldValues['29'][0].raw;
												if (lead_source_id != null){
													var lead_source_siq = lead_source_customers_obj_siq[lead_source_id];
													prev_month_lead_source_customers_obj[lead_source_siq]++;
												}
											}
							            }
									}
						        }
						        callback();
							});
						});
						next50items();
					}, function(){
						async.parallel(async_tasks,function(){
							res.write(compiled_template({
								"array_of_dates" : array_of_dates,
								"lead_source_leads_obj" : lead_source_leads_obj,
								"prev_month_lead_source_leads_obj" : prev_month_lead_source_leads_obj,
								"lead_source_customers_obj" : lead_source_customers_obj,
								"prev_month_lead_source_customers_obj" : prev_month_lead_source_customers_obj
							}));
							res.end();
						});
					});
				}
			});
		}