From d3cae6fbddc088cb7f7fb430ed8a20730631489c Mon Sep 17 00:00:00 2001 From: patrick Date: Fri, 19 Jul 2019 00:25:52 +1000 Subject: [PATCH] batch load jobs performance tuning --- Xero.php | 14 ++++++++++---- js/bts_timesheet.js | 43 ++++++++++++++++++++++++++----------------- ts.php | 4 +++- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Xero.php b/Xero.php index be8fe46..21e0d5b 100644 --- a/Xero.php +++ b/Xero.php @@ -72,15 +72,21 @@ class Xero { // //sync users to wordpress system //does not work for too many users or employees - public function sync_users($mininterval){ + public function sync_users($mininterval, $employeeonly, $clientsonly){ $this->usage(); $this->minimum_sync_interval_in_seconds = $mininterval; $msg="Sync users with minimum interval set to $mininterval \n"; $this->logConsole($msg); try{ - $this->sync_clients(); - $this->sync_employees(); + if ($clientsonly){ + $this->sync_clients(); + }else if ($employeeonly){ + $this->sync_employees(); + }else{ + $this->sync_clients(); + $this->sync_employees(); + } }catch(RateLimitExceededException $e){ $msg= "Xero API rate limit exceeded, please try again later, existing sync within 600 seconds will by passed automatically\n"; $this->logConsole($msg); @@ -285,7 +291,7 @@ class Xero { $msg .= "wp sync_users --mininterval=6000 \n"; $msg .= "6000 means those users synced within 6000 seconds will be bypassed \n"; $msg .= "to sync everything \n"; - $msg .= "wp sync_users --mininterval=0 \n"; + $msg .= "wp sync_users --mininterval=0 [--clientsonly] [--employeeonly]\n"; $msg .= "but it may hit XERO rate limit, 60call/sec, 5000/day \n"; $msg .= "---------------------------------------------\n"; $this->logConsole($msg); diff --git a/js/bts_timesheet.js b/js/bts_timesheet.js index 59df7a9..860c157 100644 --- a/js/bts_timesheet.js +++ b/js/bts_timesheet.js @@ -314,7 +314,10 @@ return; var table = $(this).closest('div.divTable'); var job = table.data().job; - clone_data_create_new_job(job.get_record_from_ui()); + var newj = clone_data_create_new_job(job.get_record_from_ui()); + $('div.workspace').append(newj.el); + newj.el.get(0).scrollIntoView();//make sure it's visible; + newj.mark_highlight_me(1000);//for 1 second; dtp_init(); }); @@ -907,6 +910,7 @@ if (!confirm ('copy entire week to next week? ')) return; var jobs = []; + var job_els =[]; $('div.week1 >div').each(function(i,e){ var date = new Date($(e).find('span.weekday').data().date); var strDate = format_date(date); //yyyy-mm-dd @@ -917,16 +921,13 @@ var el = $(e).closest('div.divTable'); if (el.is(":visible")){ var j = el.data().job; - jobs.push(j); + var newj = clone_data_create_new_job(j.get_record_from_ui(),7);//add 7 days + job_els.push(newj.el); } } }); }); - - jobs.forEach(function(e){ - clone_data_create_new_job(e.get_record_from_ui(),7);//add 7 days - }); - dtp_init(); + show_jobs(job_els); debounced_calculate(); }); @@ -942,9 +943,13 @@ } if (!confirm ('copy to next week')) return; + var jobs_el = []; $('div.bstart input.blink_me').each(function(i,e){ - copy_single_day_to_next_week(e); + var r = copy_single_day_to_next_week(e); + if (r != false) + jobs_el.push(r.el); }); + show_jobs(jobs_el); unblink_all_date(); }); @@ -958,9 +963,10 @@ var tb = $(el).closest('div.divTable'); if (tb.is(':visible')){ var j = $(tb).data().job; - clone_data_create_new_job(j.get_record_from_ui() , 7); // +7 days + var newj = clone_data_create_new_job(j.get_record_from_ui() , 7); // +7 days + return newj; } - dtp_init(); + return false; } function clone_data_create_new_job(val, num_of_shifted_days){ @@ -984,7 +990,7 @@ data.finish = format_date_time(f); } var newj = new Job(data); - newj.mark_highlight_me(1000);//for 1 second; + return newj; } function is_valid_date_str(val){ @@ -1043,13 +1049,13 @@ if (response.status =='success'){ var job_els = []; response.jobs.forEach(function(job){ - console.log('loading job... %o', job); + //console.log('loading job... %o', job); var o = new Job(job); job_els.push(o.el); }); - show_jobs(job_els); + show_jobs(job_els, 'in-ajax=true'); //filter it if reqired - do_filter_workspace(); + debounced_filter_workspace(); }else{ alert('error loading job'); } @@ -1058,11 +1064,14 @@ } - function show_jobs(job_els){ + function show_jobs(job_els, in_ajax){ if (job_els.length >0){ $('div.workspace').append(job_els); job_els[0].get(0).scrollIntoView(); + console.log('loading ... %d jobs', job_els.length); } + if (typeof in_ajax =='undefined') + dtp_init(); } function format_date(date){ @@ -1106,8 +1115,8 @@ }); - - $(document).on('click','div.userlist', debounce(do_filter_workspace)); + var debounced_filter_workspace = debounce(do_filter_workspace, 1000); + $(document).on('click','div.userlist', debounced_filter_workspace); function do_filter_workspace(){ var staffs =[]; diff --git a/ts.php b/ts.php index 5e52d29..d82ac57 100644 --- a/ts.php +++ b/ts.php @@ -215,8 +215,10 @@ class AcareOffice{ public function sync_user_cli($args = array(), $assoc_args = array()){ $arguments = wp_parse_args( $assoc_args, array( 'mininterval' => 600, + 'employeeonly' => false, + 'clientsonly' => false, ) ); - $this->xero->sync_users($arguments['mininterval']); + $this->xero->sync_users($arguments['mininterval'], $arguments['employeeonly'], $arguments['clientsonly']); return; }