56 lines
2.1 KiB
Go
56 lines
2.1 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
func (p *Projector) writeRealtimeRedis(ctx context.Context, contributions []Contribution) error {
|
|
if p.redisClient == nil || len(contributions) == 0 {
|
|
return nil
|
|
}
|
|
pipe := p.redisClient.Pipeline()
|
|
for _, c := range contributions {
|
|
if c.Empty() || c.DeltaSign == 0 {
|
|
continue
|
|
}
|
|
key := fmt.Sprintf("%s:today:%s:%s:%s",
|
|
p.cfg.Dashboard.RedisKeyPrefix,
|
|
DailyDate(c.EventTime, p.storageZone).Format("2006-01-02"),
|
|
c.SysOrigin,
|
|
c.Country.Code,
|
|
)
|
|
sign := decimal.NewFromInt(int64(c.DeltaSign))
|
|
newDealerUserRecharge := decimal.Zero
|
|
if SamePeriod(c.UserCreatedAt, c.EventTime, PeriodWindow{Type: PeriodDay}, p.storageZone) {
|
|
newDealerUserRecharge = c.NewDealerUserRecharge
|
|
}
|
|
hincrDecimal(pipe, ctx, key, "official_recharge", c.OfficialRecharge.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "mifapay_recharge", c.MifapayRecharge.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "google_recharge", c.GoogleRecharge.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "dealer_recharge", c.DealerRecharge.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "user_recharge", c.UserRecharge.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "new_dealer_user_recharge", newDealerUserRecharge.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "salary_exchange", c.SalaryExchange.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "salary_transfer", c.SalaryTransfer.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "lucky_gift_total_flow", c.LuckyGiftTotalFlow.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "lucky_gift_payout", c.LuckyGiftPayout.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "game_total_flow", c.GameTotalFlow.Mul(sign))
|
|
hincrDecimal(pipe, ctx, key, "game_payout", c.GamePayout.Mul(sign))
|
|
pipe.Expire(ctx, key, p.cfg.Dashboard.RealtimeRedisTTL)
|
|
}
|
|
_, err := pipe.Exec(ctx)
|
|
return err
|
|
}
|
|
|
|
func hincrDecimal(pipe redis.Pipeliner, ctx context.Context, key string, field string, value decimal.Decimal) {
|
|
if value.IsZero() {
|
|
return
|
|
}
|
|
floatValue, _ := value.Float64()
|
|
pipe.HIncrByFloat(ctx, key, field, floatValue)
|
|
}
|