* Optimize performance of update history metriclib.

This commit is contained in:
zhouxin
2024-03-25 15:10:16 +08:00
parent b544148c9d
commit 9ecd5db435
3 changed files with 26 additions and 3 deletions
+3 -2
View File
@@ -180,10 +180,11 @@ class metric extends control
{
$calcObj = $calcList[$code];
$calcObj->result = json_decode(json_encode($calc->result), true);
$records[$code] = $this->metricZen->getRecordByCodeAndDate($code, $calcObj, $date);
$inferenceRecord = $this->metricZen->getRecordByCodeAndDate($code, $calcObj, $date);
if(!empty($inferenceRecord)) $records[$code] = $inferenceRecord;
}
}
$this->metric->insertMetricLib($records);
$this->metric->insertMetricLib($records, 'inference');
}
/**
+20 -1
View File
@@ -837,10 +837,11 @@ class metricModel extends model
* Insert into metric lib.
*
* @param array $recordWithCode
* @param string $calcType
* @access public
* @return void
*/
public function insertMetricLib($recordWithCode)
public function insertMetricLib($recordWithCode, $calcType = 'cron')
{
$this->dao->begin();
foreach($recordWithCode as $code => $records)
@@ -848,6 +849,8 @@ class metricModel extends model
foreach($records as $record)
{
if(empty($record)) continue;
$record->calcType = $calcType;
$this->dao->insert(TABLE_METRICLIB)
->data($record)
->exec();
@@ -2377,4 +2380,20 @@ class metricModel extends model
return $this->lang->metric->noData;
}
public function checkHasInferenceOfDate($code, $dateType, $date)
{
if($dateType == 'day' || $dateType == 'nodate') return false;
$date = $this->parseDateStr($date, $dateType);
$records = $this->dao->select('id')->from(TABLE_METRICLIB)
->where('metricCode')->eq($code)
->andWhere('calcType')->eq('inference')
->beginIF($dateType == 'year')->andWhere('year')->eq($date['year'])->fi()
->beginIF($dateType == 'month')->andWhere('year')->eq($date['year'])->andWhere('month')->eq($date['month'])->fi()
->beginIF($dateType == 'week')->andWhere('year')->eq($date['year'])->andWhere('week')->eq($date['week'])->fi()
->fetch();
return !empty($records);
}
}
+3
View File
@@ -234,6 +234,9 @@ class metricZen extends metric
$metric = $this->metric->getByCode($code);
$dateType = $this->metric->getDateTypeByCode($code);
if($dateType == 'nodate') return array();
if($this->metric->checkHasInferenceOfDate($code, $dateType, $date)) return array();
$dateConfig = $this->metric->parseDateStr($date, $dateType);
$recordCommon = $this->buildRecordCommonFields($metric->id, $code, $now, $dateConfig);
$initRecords = $this->initMetricRecords($recordCommon, $metric->scope, "{$date} 23:59:59");