From f9d9be2a5c49fbabc893a4098b78733cab70b0c1 Mon Sep 17 00:00:00 2001 From: zhouxin Date: Tue, 26 Mar 2024 17:11:21 +0800 Subject: [PATCH] * Add recalculate single metric. --- module/metric/config.php | 5 ++ module/metric/config/table.php | 2 +- module/metric/control.php | 30 +++++++- module/metric/js/browse.ui.js | 4 +- module/metric/js/recalculate.ui.js | 4 +- module/metric/js/recalculateprogress.ui.js | 70 ++++++++++++++----- module/metric/model.php | 28 ++++++++ module/metric/ui/recalculate.html.php | 2 +- module/metric/ui/recalculateprogress.html.php | 2 + module/metric/zen.php | 5 +- 10 files changed, 124 insertions(+), 28 deletions(-) diff --git a/module/metric/config.php b/module/metric/config.php index c57369103a..7d669f0e71 100644 --- a/module/metric/config.php +++ b/module/metric/config.php @@ -34,6 +34,11 @@ $config->metric->actionList['delete']['url'] = helper::createLink('metr $config->metric->actionList['delete']['class'] = 'ajax-submit'; $config->metric->actionList['delete']['data-confirm'] = $lang->metric->confirmDelete; +$config->metric->actionList['recalculate']['icon'] = 'refresh'; +$config->metric->actionList['recalculate']['text'] = $lang->metric->recalculate; +$config->metric->actionList['recalculate']['hint'] = $lang->metric->recalculate; +$config->metric->actionList['recalculate']['url'] = 'javascript:confirmRecalculate("{code}")'; + $config->metric->necessaryMethodList = array('getStatement', 'calculate', 'getResult'); $config->metric->oldScopeMap = array(); diff --git a/module/metric/config/table.php b/module/metric/config/table.php index 8eed58e12b..dd65421d17 100755 --- a/module/metric/config/table.php +++ b/module/metric/config/table.php @@ -70,4 +70,4 @@ $config->metric->dtable->definition->fieldList['actions']['type'] = 'actions $config->metric->dtable->definition->fieldList['actions']['sortType'] = false; $config->metric->dtable->definition->fieldList['actions']['list'] = $config->metric->actionList; $config->metric->dtable->definition->fieldList['actions']['fixed'] = 'right'; -$config->metric->dtable->definition->fieldList['actions']['menu'] = array('edit', 'implement', 'delist', 'delete'); +$config->metric->dtable->definition->fieldList['actions']['menu'] = array('edit', 'implement', 'delist', 'delete', 'recalculate'); diff --git a/module/metric/control.php b/module/metric/control.php index 332979f41d..fbb5f7658f 100755 --- a/module/metric/control.php +++ b/module/metric/control.php @@ -158,6 +158,27 @@ class metric extends control $this->display(); } + /** + * 计算一个度量项的历史数据。 + * Update history metric lib of single metric. + * + * @param string $code + * @param string $date + * @access public + * @return void + */ + public function updateSingleMetricLib($code, $date) + { + $date = str_replace('_', '-', $date); + + $calc = $this->metric->calculateMetricByCode($code); + $record = $this->metricZen->getRecordByCodeAndDate($code, $calc, $date); + + $records = array(); + $records[$code] = $record; + $this->metric->insertMetricLib($records, 'inference'); + } + /** * 计算历史数据。 * Update metric lib of history. @@ -443,11 +464,13 @@ class metric extends control * 重算度量项历史数据。 * Recalculate metric history data. * + * @param string $code * @access public * @return string */ - public function recalculate() + public function recalculate($code = 'all') { + $this->view->code = $code; $this->display(); } @@ -456,14 +479,17 @@ class metric extends control * Show recalculate progress. * * @param string $calcType all|inference + * @param string $code * @access public * @return string */ - public function recalculateProgress($calcType) + public function recalculateProgress($calcType, $code = 'all') { $startDate = $this->metric->getInstallDate(); if($calcType == 'all') $endDate = helper::now(); + $this->view->code = $code; + $this->view->dateType = $dateType; $this->view->startDate = substr($startDate, 0, 10); $this->view->endDate = substr($endDate, 0, 10); $this->display(); diff --git a/module/metric/js/browse.ui.js b/module/metric/js/browse.ui.js index b711fa12b6..39a5b06c61 100644 --- a/module/metric/js/browse.ui.js +++ b/module/metric/js/browse.ui.js @@ -72,10 +72,10 @@ window.loadImplement = function(link) modal.render({url: link}); } -window.confirmRecalculate = function() +window.confirmRecalculate = function(code = 'all') { zui.Modal.confirm(confirmRecalculate).then((res)=> { - if(res) zui.Modal.open({url: $.createLink('metric', 'recalculate')}); + if(res) zui.Modal.open({url: $.createLink('metric', 'recalculate', 'code=' + code)}); }); } diff --git a/module/metric/js/recalculate.ui.js b/module/metric/js/recalculate.ui.js index 03bdc04409..02d91bd758 100644 --- a/module/metric/js/recalculate.ui.js +++ b/module/metric/js/recalculate.ui.js @@ -1,6 +1,6 @@ -window.showRecalculateProgress = function() +window.showRecalculateProgress = function(code = 'all') { var calcType = $('.isCalcAll').prop('checked') ? 'all' : 'inference'; - var link = $.createLink('metric', 'recalculateProgress', 'calcType=' + calcType); + var link = $.createLink('metric', 'recalculateProgress', 'calcType=' + calcType + '&code=' + code); loadPage(link); } diff --git a/module/metric/js/recalculateprogress.ui.js b/module/metric/js/recalculateprogress.ui.js index 3bcee1dc04..9a913263ca 100644 --- a/module/metric/js/recalculateprogress.ui.js +++ b/module/metric/js/recalculateprogress.ui.js @@ -1,18 +1,25 @@ $(document).ready(function() { - recalculate(startDate, endDate); + if(code == 'all') + { + recalculateAll(startDate, endDate); + } + else + { + recalculateSingle(code, startDate, endDate); + } }); -function recalculate(startDate, endDate) +function recalculateAll(startDate, endDate) { var dateRange = getDateRange(startDate, endDate); var calcLink = $.createLink('metric', 'saveClassifiedCalcGroup'); $.get(calcLink, function(result){ - updateHistory(dateRange); + updateAllHistory(dateRange); }); - function updateHistory(dateRange, index = 0) + function updateAllHistory(dateRange, index = 0) { if(index >= dateRange.length) { @@ -21,19 +28,37 @@ function recalculate(startDate, endDate) return; } - var date = dateRange[index]; - - var year = date.getFullYear(); - var month = (date.getMonth() + 1).toString().padStart(2, '0'); - var day = date.getDate().toString().padStart(2, '0'); - - var date = year + '_' + month + '_' + day; + var date = dateToString(dateRange[index]); var $html = recalculateLog(date); var link = $.createLink('metric', 'updateHistoryMetricLib', 'date=' + date); $.get(link, function(result){ $('.verify-content').append($html); - updateHistory(dateRange, index + 1); + updateAllHistory(dateRange, index + 1); + }); + } +} + +function recalculateSingle(code, startDate, endDate) +{ + var dateRange = getDateRange(startDate, endDate, dateType); + updateSingleHistory(dateRange); + function updateSingleHistory(dateRange, index = 0) + { + if(index >= dateRange.length) + { + var deduplication = $.createLink('metric', 'deduplicateRecord'); + $.get(deduplication, function(result){}); + return; + } + + var date = dateToString(dateRange[index]); + var $html = recalculateLog(date); + + var link = $.createLink('metric', 'updateSingleMetricLib', 'code=' + code + '&date=' + date); + $.get(link, function(result){ + $('.verify-content').append($html); + updateSingleHistory(dateRange, index + 1); }); } } @@ -48,17 +73,30 @@ function recalculateLog(date) return html; } -function getDateRange(startDate, endDate) +function getDateRange(startDate, endDate, dateType = 'day') { var startDate = new Date(startDate); var endDate = new Date(endDate); var dateRange = []; - while(startDate <= endDate) + + if(dateType == 'day') { - dateRange.push(new Date(startDate)); - startDate.setDate(startDate.getDate() + 1); + while(startDate <= endDate) + { + dateRange.push(new Date(startDate)); + startDate.setDate(startDate.getDate() + 1); + } } return dateRange; } + +function dateToString(date) +{ + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return year + '_' + month + '_' + day; +} diff --git a/module/metric/model.php b/module/metric/model.php index 1e3cb0ba50..ba99b5d52b 100755 --- a/module/metric/model.php +++ b/module/metric/model.php @@ -642,6 +642,34 @@ class metricModel extends model return $options; } + /** + * 根据代号计算度量项。 + * Calculate metric by code. + * + * @param string $code + * @access public + * @return void + */ + public function calculateMetricByCode($code) + { + $metric = $this->metricTao->fetchMetricByCode($code); + if(!$metric) return false; + + $calcPath = $this->getCalcRoot() . $metric->scope . DS . $metric->purpose . DS . $metric->code . '.php'; + if(!is_file($calcPath)) return false; + + include_once $this->getBaseCalcPath(); + include_once $calcPath; + $calculator = new $metric->code; + + $statement = $this->getDataStatement($calculator, 'statement', 'rnd'); + $rows = $statement->fetchAll(); + + foreach($rows as $row) $calculator->calculate($row); + + return $calculator; + } + /** * 根据代号获取计算实时度量项的结果。 * Get result of calculate metric by code. diff --git a/module/metric/ui/recalculate.html.php b/module/metric/ui/recalculate.html.php index 24177a5e2f..1a283a7c00 100644 --- a/module/metric/ui/recalculate.html.php +++ b/module/metric/ui/recalculate.html.php @@ -26,7 +26,7 @@ form btn ( setClass('btn primary-outline'), - bind::click('window.showRecalculateProgress()'), + bind::click("window.showRecalculateProgress('$code')"), $lang->metric->recalculate ) ); diff --git a/module/metric/ui/recalculateprogress.html.php b/module/metric/ui/recalculateprogress.html.php index 0f768edd1c..4ec82cf958 100644 --- a/module/metric/ui/recalculateprogress.html.php +++ b/module/metric/ui/recalculateprogress.html.php @@ -13,6 +13,8 @@ namespace zin; jsVar('startDate', $startDate); jsVar('endDate', $endDate); jsVar('recalculateLogText', $lang->metric->recalculateLog); +jsVar('code', $code); +jsVar('dateType', $dateType); detailHeader ( diff --git a/module/metric/zen.php b/module/metric/zen.php index b08d22dc37..8eb77ba509 100644 --- a/module/metric/zen.php +++ b/module/metric/zen.php @@ -54,10 +54,7 @@ class metricZen extends metric */ protected function responseAfterCreate($metricID, $afterCreate, $from, $location) { - if($afterCreate == 'back' && $location) - { - return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'load' => $location); - } + if($afterCreate == 'back' && $location) return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'load' => $location); $location = $this->createLink('metric', 'implement', "metricID=$metricID&from=$from"); $callback = array('name' => 'loadImplement', 'params' => $location);