Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/report dashboard weekly monthly #82

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
232 changes: 232 additions & 0 deletions appinterface/rdbreportdashboard/rdbreportdashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Loading