diff --git a/appinterface/rdbreportdashboard/rdbreportdashboard.go b/appinterface/rdbreportdashboard/rdbreportdashboard.go index 556f842..cb4c454 100644 --- a/appinterface/rdbreportdashboard/rdbreportdashboard.go +++ b/appinterface/rdbreportdashboard/rdbreportdashboard.go @@ -313,6 +313,94 @@ func (impl *RDbReportDashboard) UpdateTotalAddressesOfRedeemedCouponsWithRDbHand return nil } +func (impl *RDbReportDashboard) UpdateTotalAddressesOfRedeemedCouponsWeeklyWithRDbHandle(currentDate int64, nextDate int64) error { + startTime := time.Now() + recordMethod := "UpdateTotalAddressesOfRedeemedCouponsWeeklyWithRDbHandle" + + if err := impl.init(currentDate); err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error initializing report dashboard %v", err) + } + + rawQuery := fmt.Sprintf( + "COUNT(*) "+ + "FROM (SELECT DISTINCT from_address "+ + "FROM view_transactions "+ + "WHERE "+ + "block_time >= %d AND "+ + "block_time < %d AND "+ + "tx_type = '%s') AS dt", currentDate, nextDate, "exchangeWithValue") + + addressesOfRedeemedCouponsCountSubQuery := impl.selectRDbHandle.StmtBuilder.Select(rawQuery) + sql, args, err := impl.selectRDbHandle.StmtBuilder.Update( + impl.table, + ).Set( + "total_redeemed_coupon_addresses_weekly", impl.selectRDbHandle.StmtBuilder.SubQuery(addressesOfRedeemedCouponsCountSubQuery), + ).Where( + "date_time = ?", currentDate, + ).ToSql() + if err != nil { + return fmt.Errorf("error building total addresses of redeemed coupons weekly update SQL: %v", err) + } + + execResult, err := impl.selectRDbHandle.Exec(sql, args...) + if err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error executing total addresses of redeemed coupons weekly update SQL: %v", err) + } + if execResult.RowsAffected() == 0 { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return errors.New("error executing total addresses of redeemed coupons weekly update SQL: no rows affected") + } + + prometheus.RecordApiExecTime(recordMethod, SUCCESS, "cronjob", time.Since(startTime).Milliseconds()) + return nil +} + +func (impl *RDbReportDashboard) UpdateTotalAddressesOfRedeemedCouponsMonthlyWithRDbHandle(currentDate int64, nextDate int64) error { + startTime := time.Now() + recordMethod := "UpdateTotalAddressesOfRedeemedCouponsMonthlyWithRDbHandle" + + if err := impl.init(currentDate); err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error initializing report dashboard %v", err) + } + + rawQuery := fmt.Sprintf( + "COUNT(*) "+ + "FROM (SELECT DISTINCT from_address "+ + "FROM view_transactions "+ + "WHERE "+ + "block_time >= %d AND "+ + "block_time < %d AND "+ + "tx_type = '%s') AS dt", currentDate, nextDate, "exchangeWithValue") + + addressesOfRedeemedCouponsCountSubQuery := impl.selectRDbHandle.StmtBuilder.Select(rawQuery) + sql, args, err := impl.selectRDbHandle.StmtBuilder.Update( + impl.table, + ).Set( + "total_redeemed_coupon_addresses_monthly", impl.selectRDbHandle.StmtBuilder.SubQuery(addressesOfRedeemedCouponsCountSubQuery), + ).Where( + "date_time = ?", currentDate, + ).ToSql() + if err != nil { + return fmt.Errorf("error building total addresses of redeemed coupons monthly update SQL: %v", err) + } + + execResult, err := impl.selectRDbHandle.Exec(sql, args...) + if err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error executing total addresses of redeemed coupons monthly update SQL: %v", err) + } + if execResult.RowsAffected() == 0 { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return errors.New("error executing total addresses of redeemed coupons monthly update SQL: no rows affected") + } + + prometheus.RecordApiExecTime(recordMethod, SUCCESS, "cronjob", time.Since(startTime).Milliseconds()) + return nil +} + func (impl *RDbReportDashboard) UpdateTotalAstraStakedWithRDbHandle(currentDate int64, nextDate int64) error { startTime := time.Now() recordMethod := "UpdateTotalAstraStakedWithRDbHandle" @@ -447,6 +535,150 @@ func (impl *RDbReportDashboard) UpdateTotalStakingAddressesWithRDbHandle(current return nil } +func (impl *RDbReportDashboard) UpdateTotalStakingAddressesWeeklyWithRDbHandle(currentDate int64, nextDate int64) error { + startTime := time.Now() + recordMethod := "UpdateTotalStakingAddressesWeeklyWithRDbHandle" + + if err := impl.init(currentDate); err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error initializing report dashboard %v", err) + } + + rawQuery := fmt.Sprintf( + "COUNT (*) FROM(SELECT DISTINCT CAST(value ->> 'content' AS jsonb) ->> 'delegatorAddress' "+ + "FROM "+ + "view_transactions, "+ + "jsonb_array_elements(view_transactions.messages) elems "+ + "WHERE "+ + "block_time >= %d AND "+ + "block_time < %d AND "+ + "value->>'type'='%s') AS tmp", currentDate, nextDate, "/cosmos.staking.v1beta1.MsgDelegate") + + addressesStakedCountSubQuery := impl.selectRDbHandle.StmtBuilder.Select(rawQuery) + sql, args, err := impl.selectRDbHandle.StmtBuilder.Update( + impl.table, + ).Set( + "total_staking_addresses_weekly", impl.selectRDbHandle.StmtBuilder.SubQuery(addressesStakedCountSubQuery), + ).Where( + "date_time = ?", currentDate, + ).ToSql() + if err != nil { + return fmt.Errorf("error building total staking addresses weekly update SQL: %v", err) + } + + execResult, err := impl.selectRDbHandle.Exec(sql, args...) + if err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error executing total staking addresses weekly update SQL: %v", err) + } + if execResult.RowsAffected() == 0 { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return errors.New("error executing total staking addresses weekly update SQL: no rows affected") + } + + prometheus.RecordApiExecTime(recordMethod, SUCCESS, "cronjob", time.Since(startTime).Milliseconds()) + return nil +} + +func (impl *RDbReportDashboard) UpdateTotalStakingAddressesMonthlyWithRDbHandle(currentDate int64, nextDate int64) error { + startTime := time.Now() + recordMethod := "UpdateTotalStakingAddressesMonthlyWithRDbHandle" + + if err := impl.init(currentDate); err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error initializing report dashboard %v", err) + } + + rawQuery := fmt.Sprintf( + "COUNT (*) FROM(SELECT DISTINCT CAST(value ->> 'content' AS jsonb) ->> 'delegatorAddress' "+ + "FROM "+ + "view_transactions, "+ + "jsonb_array_elements(view_transactions.messages) elems "+ + "WHERE "+ + "block_time >= %d AND "+ + "block_time < %d AND "+ + "value->>'type'='%s') AS tmp", currentDate, nextDate, "/cosmos.staking.v1beta1.MsgDelegate") + + addressesStakedCountSubQuery := impl.selectRDbHandle.StmtBuilder.Select(rawQuery) + sql, args, err := impl.selectRDbHandle.StmtBuilder.Update( + impl.table, + ).Set( + "total_staking_addresses_monthly", impl.selectRDbHandle.StmtBuilder.SubQuery(addressesStakedCountSubQuery), + ).Where( + "date_time = ?", currentDate, + ).ToSql() + if err != nil { + return fmt.Errorf("error building total staking addresses monthly update SQL: %v", err) + } + + execResult, err := impl.selectRDbHandle.Exec(sql, args...) + if err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error executing total staking addresses monthly update SQL: %v", err) + } + if execResult.RowsAffected() == 0 { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return errors.New("error executing total staking addresses monthly update SQL: no rows affected") + } + + prometheus.RecordApiExecTime(recordMethod, SUCCESS, "cronjob", time.Since(startTime).Milliseconds()) + return nil +} + +func (impl *RDbReportDashboard) UpdateTotalActiveAddressesWeeklyWithRDbHandle(currentDate int64, nextDate int64) error { + startTime := time.Now() + recordMethod := "UpdateTotalActiveAddressesWeeklyWithRDbHandle" + + if err := impl.init(currentDate); err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error initializing report dashboard %v", err) + } + + rawQuery := "UPDATE report_dashboard " + + "SET total_active_addresses_weekly = (SELECT COUNT(*) FROM (SELECT DISTINCT (from_address) FROM view_transactions WHERE block_time >= $1 AND block_time < $2) AS temp) " + + "WHERE date_time = $1" + + execResult, err := impl.selectRDbHandle.Exec(rawQuery, currentDate, nextDate) + if err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error executing total active addresses weekly update SQL: %v", err) + } + if execResult.RowsAffected() == 0 { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return errors.New("error executing total active addresses weekly update SQL: no rows affected") + } + + prometheus.RecordApiExecTime(recordMethod, SUCCESS, "cronjob", time.Since(startTime).Milliseconds()) + return nil +} + +func (impl *RDbReportDashboard) UpdateTotalActiveAddressesMonthlyWithRDbHandle(currentDate int64, nextDate int64) error { + startTime := time.Now() + recordMethod := "UpdateTotalActiveAddressesMonthlyWithRDbHandle" + + if err := impl.init(currentDate); err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error initializing report dashboard %v", err) + } + + rawQuery := "UPDATE report_dashboard " + + "SET total_active_addresses_monthly = (SELECT COUNT(*) FROM (SELECT DISTINCT (from_address) FROM view_transactions WHERE block_time >= $1 AND block_time < $2) AS temp) " + + "WHERE date_time = $1" + + execResult, err := impl.selectRDbHandle.Exec(rawQuery, currentDate, nextDate) + if err != nil { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return fmt.Errorf("error executing total active addresses monthly update SQL: %v", err) + } + if execResult.RowsAffected() == 0 { + prometheus.RecordApiExecTime(recordMethod, FAIL, "cronjob", time.Since(startTime).Milliseconds()) + return errors.New("error executing total active addresses monthly update SQL: no rows affected") + } + + prometheus.RecordApiExecTime(recordMethod, SUCCESS, "cronjob", time.Since(startTime).Milliseconds()) + return nil +} + func (impl *RDbReportDashboard) UpdateTotalNewAddressesWithRDbHandle(currentDate int64, prevDate int64) error { startTime := time.Now() recordMethod := "UpdateTotalNewAddressesWithRDbHandle" diff --git a/bootstrap/app.go b/bootstrap/app.go index 06aa82d..236f4c2 100644 --- a/bootstrap/app.go +++ b/bootstrap/app.go @@ -251,8 +251,9 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { // @every 0h0m5s // 59 59 0-23 * * * s.AddFunc("59 59 0-23 * * *", func() { - currentDate := time.Now().Truncate(24 * time.Hour).UnixNano() - nextDate := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour).UnixNano() + currentTime := time.Now().Truncate(24 * time.Hour) + currentDate := currentTime.UnixNano() + nextDate := currentTime.Add(24 * time.Hour).UnixNano() i = 0 var err error time.Sleep(1 * time.Second) @@ -268,8 +269,9 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { }) s.AddFunc("59 59 0-23 * * *", func() { - currentDate := time.Now().Truncate(24 * time.Hour).UnixNano() - nextDate := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour).UnixNano() + currentTime := time.Now().Truncate(24 * time.Hour) + currentDate := currentTime.UnixNano() + nextDate := currentTime.Add(24 * time.Hour).UnixNano() tikiAddress := a.config.CronjobReportDashboard.TikiAddress i = 0 var err error @@ -286,8 +288,9 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { }) s.AddFunc("59 59 0-23 * * *", func() { - currentDate := time.Now().Truncate(24 * time.Hour).UnixNano() - nextDate := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour).UnixNano() + currentTime := time.Now().Truncate(24 * time.Hour) + currentDate := currentTime.UnixNano() + nextDate := currentTime.Add(24 * time.Hour).UnixNano() i = 0 var err error time.Sleep(5 * time.Second) @@ -303,8 +306,9 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { }) s.AddFunc("59 59 0-23 * * *", func() { - currentDate := time.Now().Truncate(24 * time.Hour).UnixNano() - nextDate := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour).UnixNano() + currentTime := time.Now().Truncate(24 * time.Hour) + currentDate := currentTime.UnixNano() + nextDate := currentTime.Add(24 * time.Hour).UnixNano() i = 0 var err error time.Sleep(7 * time.Second) @@ -320,8 +324,9 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { }) s.AddFunc("59 59 0-23 * * *", func() { - currentDate := time.Now().Truncate(24 * time.Hour).UnixNano() - nextDate := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour).UnixNano() + currentTime := time.Now().Truncate(24 * time.Hour) + currentDate := currentTime.UnixNano() + nextDate := currentTime.Add(24 * time.Hour).UnixNano() i = 0 var err error time.Sleep(9 * time.Second) @@ -337,8 +342,9 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { }) s.AddFunc("59 59 0-23 * * *", func() { - currentDate := time.Now().Truncate(24 * time.Hour).UnixNano() - nextDate := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour).UnixNano() + currentTime := time.Now().Truncate(24 * time.Hour) + currentDate := currentTime.UnixNano() + nextDate := currentTime.Add(24 * time.Hour).UnixNano() i = 0 var err error time.Sleep(11 * time.Second) @@ -354,8 +360,9 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { }) s.AddFunc("59 59 0-23 * * *", func() { - currentDate := time.Now().Truncate(24 * time.Hour).UnixNano() - nextDate := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour).UnixNano() + currentTime := time.Now().Truncate(24 * time.Hour) + currentDate := currentTime.UnixNano() + nextDate := currentTime.Add(24 * time.Hour).UnixNano() i = 0 var err error time.Sleep(13 * time.Second) @@ -371,8 +378,9 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { }) s.AddFunc("59 59 0-23 * * *", func() { - currentDate := time.Now().Truncate(24 * time.Hour).UnixNano() - nextDate := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour).UnixNano() + currentTime := time.Now().Truncate(24 * time.Hour) + currentDate := currentTime.UnixNano() + nextDate := currentTime.Add(24 * time.Hour).UnixNano() i = 0 var err error time.Sleep(15 * time.Second) @@ -388,8 +396,9 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { }) s.AddFunc("59 59 0-23 * * *", func() { - currentDate := time.Now().Truncate(24 * time.Hour).UnixNano() - prevDate := time.Now().Truncate(24 * time.Hour).Add(-24 * time.Hour).UnixNano() + currentTime := time.Now().Truncate(24 * time.Hour) + currentDate := currentTime.UnixNano() + prevDate := currentTime.Add(-24 * time.Hour).UnixNano() i = 0 var err error time.Sleep(17 * time.Second) @@ -404,6 +413,118 @@ func (a *app) RunCronJobsReportDashboard(rdbHandle *rdb.Handle) { } }) + // Weekly update + // At 00:00 on Sunday + s.AddFunc("@weekly", func() { + endWeekTime := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour) + endWeekDate := endWeekTime.UnixNano() + startWeekDate := endWeekTime.Add(-7 * 24 * time.Hour).UnixNano() + i = 0 + var err error + time.Sleep(19 * time.Second) + for i < retry { + err = rdbReportDashboard.UpdateTotalAddressesOfRedeemedCouponsWeeklyWithRDbHandle(startWeekDate, endWeekDate) + if err == nil { + break + } + a.logger.Infof("failed to run UpdateTotalAddressesOfRedeemedCouponsWeeklyWithRDbHandle cronjob: %v", err) + time.Sleep(time.Duration(delayTime) * time.Second) + i += 1 + } + }) + + s.AddFunc("@weekly", func() { + endWeekTime := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour) + endWeekDate := endWeekTime.UnixNano() + startWeekDate := endWeekTime.Add(-7 * 24 * time.Hour).UnixNano() + i = 0 + var err error + time.Sleep(21 * time.Second) + for i < retry { + err = rdbReportDashboard.UpdateTotalStakingAddressesWeeklyWithRDbHandle(startWeekDate, endWeekDate) + if err == nil { + break + } + a.logger.Infof("failed to run UpdateTotalStakingAddressesWeeklyWithRDbHandle cronjob: %v", err) + time.Sleep(time.Duration(delayTime) * time.Second) + i += 1 + } + }) + + s.AddFunc("@weekly", func() { + endWeekTime := time.Now().Truncate(24 * time.Hour).Add(24 * time.Hour) + endWeekDate := endWeekTime.UnixNano() + startWeekDate := endWeekTime.Add(-7 * 24 * time.Hour).UnixNano() + i = 0 + var err error + time.Sleep(23 * time.Second) + for i < retry { + err = rdbReportDashboard.UpdateTotalActiveAddressesWeeklyWithRDbHandle(startWeekDate, endWeekDate) + if err == nil { + break + } + a.logger.Infof("failed to run UpdateTotalActiveAddressesWeeklyWithRDbHandle cronjob: %v", err) + time.Sleep(time.Duration(delayTime) * time.Second) + i += 1 + } + }) + + // Monthly update + // At 00:00 on the first day-of-month + s.AddFunc("@monthly", func() { + firstDayOfMonthTime := time.Now().Truncate(24 * time.Hour) + startMonthDate := firstDayOfMonthTime.UnixNano() + endMonthDate := firstDayOfMonthTime.AddDate(0, 1, 0).UnixNano() + i = 0 + var err error + time.Sleep(25 * time.Second) + for i < retry { + err = rdbReportDashboard.UpdateTotalAddressesOfRedeemedCouponsMonthlyWithRDbHandle(startMonthDate, endMonthDate) + if err == nil { + break + } + a.logger.Infof("failed to run UpdateTotalAddressesOfRedeemedCouponsMonthlyWithRDbHandle cronjob: %v", err) + time.Sleep(time.Duration(delayTime) * time.Second) + i += 1 + } + }) + + s.AddFunc("@monthly", func() { + firstDayOfMonthTime := time.Now().Truncate(24 * time.Hour) + startMonthDate := firstDayOfMonthTime.UnixNano() + endMonthDate := firstDayOfMonthTime.AddDate(0, 1, 0).UnixNano() + i = 0 + var err error + time.Sleep(27 * time.Second) + for i < retry { + err = rdbReportDashboard.UpdateTotalStakingAddressesMonthlyWithRDbHandle(startMonthDate, endMonthDate) + if err == nil { + break + } + a.logger.Infof("failed to run UpdateTotalStakingAddressesMonthlyWithRDbHandle cronjob: %v", err) + time.Sleep(time.Duration(delayTime) * time.Second) + i += 1 + } + }) + + s.AddFunc("@monthly", func() { + firstDayOfMonthTime := time.Now().Truncate(24 * time.Hour) + startMonthDate := firstDayOfMonthTime.UnixNano() + endMonthDate := firstDayOfMonthTime.AddDate(0, 1, 0).UnixNano() + i = 0 + var err error + time.Sleep(29 * time.Second) + for i < retry { + err = rdbReportDashboard.UpdateTotalActiveAddressesMonthlyWithRDbHandle(startMonthDate, endMonthDate) + if err == nil { + break + } + a.logger.Infof("failed to run UpdateTotalActiveAddressesMonthlyWithRDbHandle cronjob: %v", err) + time.Sleep(time.Duration(delayTime) * time.Second) + i += 1 + } + }) + s.Start() } } diff --git a/infrastructure/httpapi/handlers/report_dashboard.go b/infrastructure/httpapi/handlers/report_dashboard.go index 3c16652..1fae7a3 100644 --- a/infrastructure/httpapi/handlers/report_dashboard.go +++ b/infrastructure/httpapi/handlers/report_dashboard.go @@ -65,6 +65,9 @@ func (handler *ReportDashboardHandler) GetReportDashboardByTimeRange(ctx *fastht var fromDate string var toDate string + var groupBy string + var reportDashboardOverall report_dashboard_view.ReportDashboardOverall + var err error if string(ctx.QueryArgs().Peek("fromDate")) == "" { prometheus.RecordApiExecTime(recordMethod, strconv.Itoa(fasthttp.StatusBadRequest), "GET", time.Since(startTime).Milliseconds()) @@ -79,21 +82,32 @@ func (handler *ReportDashboardHandler) GetReportDashboardByTimeRange(ctx *fastht fromDate = string(ctx.QueryArgs().Peek("fromDate")) - cacheKey := fmt.Sprintf("GetReportDashboardByTimeRange%s%s", fromDate, toDate) + groupBy = string(ctx.QueryArgs().Peek("groupBy")) + if groupBy != "" { + if groupBy != "weekly" && groupBy != "monthly" { + prometheus.RecordApiExecTime(recordMethod, strconv.Itoa(fasthttp.StatusBadRequest), "GET", time.Since(startTime).Milliseconds()) + httpapi.BadRequest(ctx, fmt.Errorf("groupBy %s is not supported", groupBy)) + return + } + } + + cacheKey := fmt.Sprintf("GetReportDashboardByTimeRange%s%s%s", fromDate, toDate, groupBy) var reportDashboardOverallCache report_dashboard_view.ReportDashboardOverall - err := handler.astraCache.Get(cacheKey, &reportDashboardOverallCache) + err = handler.astraCache.Get(cacheKey, &reportDashboardOverallCache) if err == nil { prometheus.RecordApiExecTime(recordMethod, strconv.Itoa(200), "GET", time.Since(startTime).Milliseconds()) httpapi.SuccessNotWrappedResult(ctx, reportDashboardOverallCache) return } - reportDashboardOverall, err := handler.reportDashboardView.GetReportDashboardByTimeRange(fromDate, toDate) - if err != nil { - handler.logger.Errorf("error get report dashboard by time range: %v", err) - prometheus.RecordApiExecTime(recordMethod, strconv.Itoa(fasthttp.StatusBadRequest), "GET", time.Since(startTime).Milliseconds()) - httpapi.InternalServerError(ctx) - return + if groupBy == "" { + reportDashboardOverall, err = handler.reportDashboardView.GetReportDashboardByTimeRange(fromDate, toDate) + if err != nil { + handler.logger.Errorf("error get report dashboard by time range: %v", err) + prometheus.RecordApiExecTime(recordMethod, strconv.Itoa(fasthttp.StatusBadRequest), "GET", time.Since(startTime).Milliseconds()) + httpapi.InternalServerError(ctx) + return + } } totalUpToDateTransactions, err := handler.transactionsTotalView.FindBy("-") @@ -116,7 +130,7 @@ func (handler *ReportDashboardHandler) GetReportDashboardByTimeRange(ctx *fastht totalActiveAddresses, err := handler.reportDashboardView.GetActiveAddressesByTimeRangeDirectly(fromDate, toDate) if err != nil { - handler.logger.Errorf("error get active addresses by time range: %v", err) + handler.logger.Errorf("error get active addresses by time range directly: %v", err) prometheus.RecordApiExecTime(recordMethod, strconv.Itoa(fasthttp.StatusBadRequest), "GET", time.Since(startTime).Milliseconds()) httpapi.InternalServerError(ctx) return @@ -125,7 +139,7 @@ func (handler *ReportDashboardHandler) GetReportDashboardByTimeRange(ctx *fastht totalStakingAddresses, err := handler.reportDashboardView.GetStakingAddressesByTimeRangeDirectly(fromDate, toDate) if err != nil { - handler.logger.Errorf("error get staking addresses by time range: %v", err) + handler.logger.Errorf("error get staking addresses by time range directly: %v", err) prometheus.RecordApiExecTime(recordMethod, strconv.Itoa(fasthttp.StatusBadRequest), "GET", time.Since(startTime).Milliseconds()) httpapi.InternalServerError(ctx) return @@ -134,7 +148,7 @@ func (handler *ReportDashboardHandler) GetReportDashboardByTimeRange(ctx *fastht totalRedeemedCouponsAddresses, err := handler.reportDashboardView.GetAddressesOfRedeemedCouponsByTimeRangeDirectly(fromDate, toDate) if err != nil { - handler.logger.Errorf("error get redeemed coupons addresses by time range: %v", err) + handler.logger.Errorf("error get redeemed coupons addresses by time range directly: %v", err) prometheus.RecordApiExecTime(recordMethod, strconv.Itoa(fasthttp.StatusBadRequest), "GET", time.Since(startTime).Milliseconds()) httpapi.InternalServerError(ctx) return diff --git a/infrastructure/kafka/consumer/worker/internal_txs_consumer.go b/infrastructure/kafka/consumer/worker/internal_txs_consumer.go index 5307916..ce27230 100644 --- a/infrastructure/kafka/consumer/worker/internal_txs_consumer.go +++ b/infrastructure/kafka/consumer/worker/internal_txs_consumer.go @@ -12,13 +12,13 @@ import ( "github.com/AstraProtocol/astra-indexing/appinterface/rdb" "github.com/AstraProtocol/astra-indexing/bootstrap/config" "github.com/AstraProtocol/astra-indexing/infrastructure/kafka/consumer" + "github.com/AstraProtocol/astra-indexing/internal/evm" "github.com/segmentio/kafka-go" applogger "github.com/AstraProtocol/astra-indexing/external/logger" "github.com/AstraProtocol/astra-indexing/external/tmcosmosutils" "github.com/AstraProtocol/astra-indexing/external/utctime" utils "github.com/AstraProtocol/astra-indexing/infrastructure" - "github.com/AstraProtocol/astra-indexing/internal/evm" "github.com/AstraProtocol/astra-indexing/projection/account_transaction" accountTransactionView "github.com/AstraProtocol/astra-indexing/projection/account_transaction/view" "github.com/AstraProtocol/astra-indexing/usecase/coin" diff --git a/migrations/20230627092718_report_dashboard.up.sql b/migrations/20230627092718_report_dashboard.up.sql index 8ea91ea..a9aab2f 100644 --- a/migrations/20230627092718_report_dashboard.up.sql +++ b/migrations/20230627092718_report_dashboard.up.sql @@ -22,4 +22,11 @@ CREATE TABLE report_dashboard ( -- total_asa_withdrawn_from_tiki VARCHAR DEFAULT '' NOT NULL, total_asa_on_chain_rewards VARCHAR DEFAULT '' NOT NULL + -- Group by + total_active_addresses_weekly INT DEFAULT 0 NOT NULL, + total_active_addresses_monthly INT DEFAULT 0 NOT NULL, + total_staking_addresses_weekly INT DEFAULT 0 NOT NULL, + total_staking_addresses_monthly INT DEFAULT 0 NOT NULL, + total_redeemed_coupons_addresses_weekly INT DEFAULT 0 NOT NULL, + total_redeemed_coupons_addresses_monthly INT DEFAULT 0 NOT NULL; ); \ No newline at end of file