Skip to content

Commit

Permalink
日志展示优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Bitxx committed Feb 28, 2024
1 parent a991133 commit 94a1360
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (a *App) ScanAllTransactions() {
tx := appModel.Transaction{}
err := a.db.Last(&tx).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
log.Errorf("txScan => get latest local block error: %s", err.Error())
log.Errorf("[txScan] => get latest local block error: %s", err.Error())
continue
}
// 此时如果err不为空,则说明ErrRecordNotFound,也就是 blockNumStart = 1
Expand All @@ -54,13 +54,13 @@ func (a *App) ScanAllTransactions() {
//获取块结束
blockNumEnd, err := a.client.LatestBlockNumber()
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
log.Errorf("txScan => get latest chain block error: %s", err.Error())
log.Errorf("[txScan] => get latest chain block error: %s", err.Error())
continue
}

//不断追最新块
if blockNumStart < blockNumEnd {
log.Infof("txScan => new scanning, block from %d to %d", blockNumStart, blockNumEnd)
log.Infof("[txScan] => new scanning, block from %d to %d", blockNumStart, blockNumEnd)
a.ScanTransactionsByNumber(blockNumStart, blockNumEnd)
blockNumStart = blockNumEnd + 1
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func (a *App) ScanTransactionsByNumber(blockNumStart, blockNumEnd uint64) {
for {
transactions, err = a.client.TxReceiptByBlockNumber(i)
if err != nil {
log.Errorf("txScan => block: %d,transactions error: %s", i, err.Error())
log.Errorf("[txScan] => block: %d,transactions error: %s", i, err.Error())
time.Sleep(constant.TimeSleep)
continue
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (a *App) saveTransactions(txsCh chan chan []appModel.Transaction, blockNumS

//展示扫描情况
for _, tx := range txs {
log.Infof("txScan => scanning ... block number: %d, tx count: %d", tx.BlockNumber, len(txs))
log.Infof("[txScan] => scanning ... block number: %d, tx count: %d", tx.BlockNumber, len(txs))
break
}

Expand All @@ -156,11 +156,11 @@ func (a *App) saveTransactions(txsCh chan chan []appModel.Transaction, blockNumS
for {
err := a.db.Create(a.txCache).Error
if err != nil {
log.Errorf("txScan => batch insert error, begin block: %d, end block: %d, error info: %s", a.txCache[0].BlockNumber, a.txCache[len(a.txCache)-1].BlockNumber, err)
log.Errorf("[txScan] => batch insert error, begin block: %d, end block: %d, error info: %s", a.txCache[0].BlockNumber, a.txCache[len(a.txCache)-1].BlockNumber, err)
time.Sleep(constant.TimeSleep)
continue
}
log.Infof("txScan => batch insert success, begin block: %d, end block: %d", a.txCache[0].BlockNumber, a.txCache[len(a.txCache)-1].BlockNumber)
log.Infof("[txScan] => batch insert success, begin block: %d, end block: %d", a.txCache[0].BlockNumber, a.txCache[len(a.txCache)-1].BlockNumber)
break
}
a.txCache = []appModel.Transaction{}
Expand Down
20 changes: 10 additions & 10 deletions app/zkf/zkf.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (z *ZKF) StatGas() {
// @receiver z
func (z *ZKF) statGasByTableName(tableName string) {
if tableName != zkfModel.HourTable && tableName != zkfModel.DailyTable && tableName != zkfModel.WeeklyTable {
log.Error("zkf => table name %s is error", tableName)
log.Errorf("[zkf] => table name %s is error", tableName)
return
}
//UTC启动时间
dateStart, err := dateutil.ParseStrToTime("2024-01-14 00:00:00", "UTC", -1)
if err != nil {
log.Error("zkf => get default start date error: ", err)
log.Errorf("[zkf] => get default start date error: %s", err.Error())
return
}
dateEnd := dateStart
Expand All @@ -69,7 +69,7 @@ func (z *ZKF) statGasByTableName(tableName string) {
zkfStat := zkfModel.ZkfStatGas{}
err := z.db.Table(tableName).Last(&zkfStat).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
log.Errorf("zkf => [%s] get latest gas stat info error: %s", tableName, err.Error())
log.Errorf("[zkf] => [%s] get latest gas stat info error: %s", tableName, err.Error())
continue
}

Expand Down Expand Up @@ -101,19 +101,19 @@ func (z *ZKF) statGasByTableName(tableName string) {
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()
err = row.Scan(&totalGasFee, &totalGasPrice, &minBlockNumber, &maxBlockNumber, &maxBlockNumberDate, &count)
if err != nil {
log.Error("zkf => [%s] stat gas data err: %s", flag, err.Error())
log.Error("[zkf] => [%s] stat gas data err: %s", flag, err.Error())
continue
}
if count <= 0 {
log.Warnf("zkf => [%s] tx data is empty", flag)
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())
log.Error("[zkf] => [%s] get latest tx err: %s", flag, err.Error())
continue
}
if tx.CreatedAt.Compare(dateEnd) > 0 {
Expand All @@ -135,20 +135,20 @@ func (z *ZKF) statGasByTableName(tableName string) {
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()
err = row.Scan(&totalGasFee, &totalGasPrice, &minBlockNumber, &maxBlockNumber, &maxBlockNumberDate, &count)
if err != nil {
log.Error("zkf => [%s] stat gas data err: %s", flag, err.Error())
log.Error("[zkf] => [%s] stat gas data err: %s", flag, err.Error())
continue
}

if count <= 0 {
log.Warnf("zkf => [%s] %s data is empty", flag, tbName)
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())
log.Errorf("[zkf] => [%s] get latest tx err: %s", flag, err.Error())
continue
}
if zkfStatGas.DateEnd.Compare(dateEnd) > 0 {
Expand Down Expand Up @@ -186,6 +186,6 @@ func (z *ZKF) statGasByTableName(tableName string) {
}
z.db.Table(tableName).Save(&result)

log.Infof("zkf => calc over [%s] start block %d, end block %d", flag, minBlockNumber, maxBlockNumber)
log.Infof("[zkf] => [%s] calc over. start block %d, end block %d", flag, minBlockNumber, maxBlockNumber)
}
}

0 comments on commit 94a1360

Please sign in to comment.