From 09ae9a345301b56534eebdf89f4f9ab9f0d5fe1f Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 29 Feb 2024 11:32:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BB=9F=E8=AE=A1=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/zkf/zkf.go | 58 ++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/app/zkf/zkf.go b/app/zkf/zkf.go index 42b245b..85c2cef 100644 --- a/app/zkf/zkf.go +++ b/app/zkf/zkf.go @@ -95,10 +95,21 @@ func (z *ZKF) statGasByTableName(tableName string) { flag := "hour" if tableName == zkfModel.HourTable { flag = "hour" - dateEnd = dateStart.Add(3599 * time.Second) + dateEnd = dateStart.Add(3600 * time.Second) + + //判断这个阶段的数据是否结束,一定要先判断,以防时间差导致结果异常 + tx := appModel.Transaction{} + err = z.db.Last(&tx).Error + if err != nil { + log.Error("[zkf] => [%s] get latest tx err: %s", flag, err.Error()) + continue + } + if tx.CreatedAt.Compare(dateEnd) >= 0 { + calcStatus = zkfConstant.CalcStatusStop + } //统计 - row := z.db.Model(&appModel.Transaction{}).Select("IFNULL(sum(gas_used * effective_gas_price),0),IFNULL(sum(effective_gas_price),0),IFNULL(min(block_number),0),IFNULL(max(block_number),0),IFNULL(max(created_at),now()),IFNULL(count(1),0)").Where("created_at>=? and created_at<=?", dateStart, dateEnd).Row() + row := z.db.Model(&appModel.Transaction{}).Select("IFNULL(sum(gas_used * effective_gas_price),0),IFNULL(sum(effective_gas_price),0),IFNULL(min(block_number),0),IFNULL(max(block_number),0),IFNULL(max(created_at),now()),IFNULL(count(1),0)").Where("created_at>=? and created_at [%s] stat gas data err: %s", flag, err.Error()) @@ -108,31 +119,32 @@ func (z *ZKF) statGasByTableName(tableName string) { log.Warnf("[zkf] => [%s] tx data is empty", flag) continue } - - //判断这个阶段的数据是否结束 - tx := appModel.Transaction{} - err = z.db.Last(&tx).Error - if err != nil { - log.Error("[zkf] => [%s] get latest tx err: %s", flag, err.Error()) - continue - } - if tx.CreatedAt.Compare(dateEnd) > 0 { - calcStatus = zkfConstant.CalcStatusStop - } } else { tbName := zkfModel.HourTable if tableName == zkfModel.DailyTable { flag = "daily" tbName = zkfModel.HourTable - dateEnd = dateStart.AddDate(0, 0, 1).Add(-1 * time.Second) + dateEnd = dateStart.AddDate(0, 0, 1) } if tableName == zkfModel.WeeklyTable { flag = "weekly" tbName = zkfModel.DailyTable - dateEnd = dateStart.AddDate(0, 0, 7).Add(-1 * time.Second) + dateEnd = dateStart.AddDate(0, 0, 7) } + + //判断这个阶段的数据是否结束,一定要先判断,以防时间差导致结果异常 + zkfStatGas := zkfModel.ZkfStatGas{} + err = z.db.Table(tbName).Last(&zkfStatGas).Error + if err != nil { + log.Errorf("[zkf] => [%s] get latest tx err: %s", flag, err.Error()) + continue + } + if zkfStatGas.DateEnd.Compare(dateEnd) >= 0 { + calcStatus = zkfConstant.CalcStatusStop + } + //统计 - row := z.db.Table(tbName).Select("IFNULL(sum(total_gas_fee),0),IFNULL(sum(total_gas_price),0),IFNULL(min(block_start),0),IFNULL(max(block_end),0),IFNULL(max(date_end),now()),IFNULL(sum(total_tx_count),0)").Where("date_start>=? and date_end<=?", dateStart, dateEnd).Row() + row := z.db.Table(tbName).Select("IFNULL(sum(total_gas_fee),0),IFNULL(sum(total_gas_price),0),IFNULL(min(block_start),0),IFNULL(max(block_end),0),IFNULL(max(date_end),now()),IFNULL(sum(total_tx_count),0)").Where("date_start>=? and date_end [%s] stat gas data err: %s", flag, err.Error()) @@ -143,17 +155,6 @@ func (z *ZKF) statGasByTableName(tableName string) { log.Warnf("[zkf] => [%s] %s data is empty", flag, tbName) continue } - - //判断这个阶段的数据是否结束 - zkfStatGas := zkfModel.ZkfStatGas{} - err = z.db.Table(tbName).Last(&zkfStatGas).Error - if err != nil { - log.Errorf("[zkf] => [%s] get latest tx err: %s", flag, err.Error()) - continue - } - if zkfStatGas.DateEnd.Compare(dateEnd) > 0 { - calcStatus = zkfConstant.CalcStatusStop - } } //若没有新增数据,则继续下一轮,减少数据写入频率 @@ -167,7 +168,8 @@ func (z *ZKF) statGasByTableName(tableName string) { result.DateEnd = &maxBlockNumberDate //若结束,则用截止时间 if calcStatus == zkfConstant.CalcStatusStop { - result.DateEnd = &dateEnd + end := dateEnd.Add(-1 * time.Second) + result.DateEnd = &end } result.BlockStart = minBlockNumber result.BlockEnd = maxBlockNumber