diff --git a/module/metric/control.php b/module/metric/control.php index 9c18f3e234..d666a07bba 100755 --- a/module/metric/control.php +++ b/module/metric/control.php @@ -164,7 +164,6 @@ class metric extends control { // 保存当前的错误报告级别和显示错误的设置 $originalDebug = $this->config->debug; - $isFirstGenerate = $this->metric->isFirstGenerate(); // 开启调试模式 $this->config->debug = 2; @@ -184,7 +183,7 @@ class metric extends control $rows = $statement->fetchAll(); $this->metricZen->calcMetric($rows, $calcGroup->calcList); - $records = $this->metricZen->prepareMetricRecord($calcGroup->calcList, $isFirstGenerate); + $records = $this->metricZen->prepareMetricRecord($calcGroup->calcList); $this->metric->insertMetricLib($records); } catch(Exception $e) diff --git a/module/metric/model.php b/module/metric/model.php index 74d2431bee..621288d3e6 100755 --- a/module/metric/model.php +++ b/module/metric/model.php @@ -673,7 +673,9 @@ class metricModel extends model */ public function deduplication(string $code): bool { + $metric = $this->getByCode($code); $fields = $this->metricTao->getRecordFields($code); + if(!in_array($metric->scope, $fields)) $fields[] = $metric->scope; if(empty($fields)) return false; @@ -735,20 +737,6 @@ class metricModel extends model } } - /** - * 是否为第一次生成度量数据。 - * Whether it is the first time to generate metric data. - * - * @access public - * @return bool - */ - public function isFirstGenerate(): bool - { - $record = $this->dao->select('id')->from(TABLE_METRICLIB)->limit(1)->fetch(); - - return !$record; - } - /** * 插入度量库数据。 * Insert into metric lib. @@ -1916,15 +1904,12 @@ class metricModel extends model * 根据日期类型和日期,拆解出日期的年、月、周、日。 * Get date values by date type and date. * - * @param string $dateType * @param string $date * @access public - * @return array + * @return object */ - public function generateDateValues($dateType, $date) + public function generateDateValues($date) { - if($dateType == 'nodate') return array(); - $timestamp = strtotime($date); $year = date('Y', $timestamp); @@ -1932,11 +1917,13 @@ class metricModel extends model $day = date('d', $timestamp); $week = date('oW', $timestamp); - $dateValues = array('year' => $year); + $dateValues = new stdClass(); - if(in_array($dateType, array('month', 'week', 'day'))) $dateValues['month'] = $month; - if($dateType == 'week') $dateValues['week'] = $week; - if($dateType == 'day') $dateValues['day'] = $day; + $dateValues->year = array('year' => $year); + $dateValues->month = array('year' => $year, 'month' => $month); + $dateValues->week = array('year' => $year, 'week' => $week); + $dateValues->day = array('year' => $year, 'month' => $month, 'day' => $day); + $dateValues->nodate = array(); return $dateValues; } diff --git a/module/metric/tao.php b/module/metric/tao.php index 8e792b4480..1ed07e37b6 100644 --- a/module/metric/tao.php +++ b/module/metric/tao.php @@ -317,7 +317,7 @@ class metricTao extends metricModel ->limit(1) ->fetch(); - if(!$record) return false; + if(!$record) return array(); $fields = array(); foreach(array_keys((array)$record) as $field) diff --git a/module/metric/zen.php b/module/metric/zen.php index cfa6ce20cd..f37a29be52 100644 --- a/module/metric/zen.php +++ b/module/metric/zen.php @@ -174,36 +174,43 @@ class metricZen extends metric * Build measurements that can be inserted into tables based on the results of the measurements computed. * * @param array $calcList - * @param bool $isFirstGenerate * @access protected * @return array */ - protected function prepareMetricRecord($calcList, $isFirstGenerate = false) + protected function prepareMetricRecord($calcList) { - $options = $isFirstGenerate ? array() : array('year' => date('Y'), 'month' => date('n'), 'week' => date('W'), 'day' => date('j')); + $options = array('year' => date('Y'), 'month' => date('n'), 'week' => date('W'), 'day' => date('j')); + + $now = helper::now(); + $dateValues = $this->metric->generateDateValues($now); - $now = helper::now(); $records = array(); foreach($calcList as $code => $calc) { - $dateType = $this->metric->getDateTypeByCode($code); - $initRecords = $isFirstGenerate ? array() : $this->initMetricRecords($code, $dateType, $now); + $metric = $this->metric->getByCode($code); + $dateType = $this->metric->getDateTypeByCode($code); + $recordCommon = $this->buildMetricRecordCommonFields($metric->id, $code, $now, $dateValues->$dateType); + $initRecords = $this->initMetricRecords($recordCommon, $metric->scope); $results = $calc->getResult($options); - $system = empty($results) ? 0 : (int)$this->metric->isSystemMetric($results); foreach($results as $record) { $record = (object)$record; + if(empty($record->value)) continue; - if(empty($record->value)) $record->value = 0; $record->metricID = $calc->id; $record->metricCode = $code; $record->date = $now; - $record->system = $system; + $record->system = $metric->scope == 'system' ? 1 : 0; $uniqueKey = $this->getUniqueKeyByRecord($record); - if(isset($initRecords[$uniqueKey])) $initRecords[$uniqueKey]->value = $record->value; - else $initRecords[$uniqueKey] = $record; + if(!isset($initRecords[$uniqueKey])) + { + $initRecords[$uniqueKey] = $record; + continue; + } + + $initRecords[$uniqueKey]->value = $record->value; } $records = array_merge($records, array_values($initRecords)); @@ -222,17 +229,14 @@ class metricZen extends metric * @access protected * @return array */ - protected function initMetricRecords($code, $dateType, $date) + protected function initMetricRecords($recordCommon, $scope) { - $metric = $this->metric->getByCode($code); - $scope = $metric->scope; - - $dateValues = $this->metric->generateDateValues($dateType, $date); - $records = array(); if($scope == 'system') { - list($uniqueKey, $record) = $this->buildMetricRecordCommonFields($metric->id, $code, $scope, '1', $date); + $record = clone $recordCommon; + $record->system = 1; + $uniqueKey = $this->getUniqueKeyByRecord($record); $records[$uniqueKey] = $record; } @@ -241,7 +245,9 @@ class metricZen extends metric $scopeList = $this->metric->getPairsByScope($scope); foreach($scopeList as $key => $value) { - list($uniqueKey, $record) = $this->buildMetricRecordCommonFields($metric->id, $code, $scope, $key, $date); + $record = clone $recordCommon; + $record->$scope = $key; + $uniqueKey = $this->getUniqueKeyByRecord($record); $records[$uniqueKey] = $record; } @@ -262,22 +268,17 @@ class metricZen extends metric * @access protected * @return array */ - protected function buildMetricRecordCommonFields($metricID, $code, $scope, $scopeValue, $date) + protected function buildMetricRecordCommonFields($metricID, $code, $date, $dateValues) { - $dateType = $this->metric->getDateTypeByCode($code); - $dateValues = $this->metric->generateDateValues($dateType, $date); - $record = new stdclass(); - $record->$scope = $scopeValue; $record->value = 0; $record->metricID = $metricID; $record->metricCode = $code; $record->date = $date; $record = (object)array_merge((array)$record, $dateValues); - $uniqueKey = $this->getUniqueKeyByRecord($record); - return array($uniqueKey, $record); + return $record; } /**