Commit 6e61fe82 by 彭芳

[minor]: format calc data

parent 8531d67b
......@@ -18,6 +18,7 @@ require (
github.com/gin-contrib/pprof v1.4.0
github.com/gin-gonic/gin v1.8.2
github.com/google/uuid v1.3.0
github.com/shopspring/decimal v1.3.1
)
require (
......
......@@ -132,6 +132,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/shirou/gopsutil/v3 v3.21.8/go.mod h1:YWp/H8Qs5fVmf17v7JNZzA0mPJ+mS2e9JdiUF9LlKzQ=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
......
......@@ -20,7 +20,7 @@ const (
type ListReportRequest struct {
common.PageRequest
TimeGranularity TimeGranularity `form:"time_granularity" json:"time_granularity"` // 时间粒度(0:按天维度;1:按小时维度)
TimeGranularity TimeGranularity `form:"time_granularity" json:"time_granularity"` // 时间粒度(允许值:STAT_TIME_GRANULARITY_DAILY (按天维度),STAT_TIME_GRANULARITY_HOURLY (按小时维度))
StartDate string `form:"start_date" json:"start_date"` // 开始时间(格式YYYY-MM-DD)
EndDate string `form:"end_date" json:"end_date"` // 结束时间(格式YYYY-MM-DD)
StartDateLong util.Second `json:"-"` // 开始时间
......@@ -28,20 +28,25 @@ type ListReportRequest struct {
}
func (p *ListReportRequest) Validate() error {
if p.StartDate != "" {
parse, err := time.Parse("2006-01-02", p.StartDate)
if err != nil {
return errs.ErrorArgs.WithMessage("开始时间格式错误")
}
p.StartDateLong = util.NewSecond(parse)
// set default data today
today := time.Now().Format("2006-01-02")
if p.StartDate == "" {
p.StartDate = today
}
if p.EndDate != "" {
parse, err := time.Parse("2006-01-02", p.EndDate)
if err != nil {
return errs.ErrorArgs.WithMessage("结束时间格式错误")
}
p.EndDateLong = util.NewSecond(parse)
if p.EndDate == "" {
p.EndDate = today
}
parse, err := time.Parse("2006-01-02", p.StartDate)
if err != nil {
return errs.ErrorArgs.WithMessage("开始时间格式错误")
}
p.StartDateLong = util.NewSecond(parse)
parse, err = time.Parse("2006-01-02", p.EndDate)
if err != nil {
return errs.ErrorArgs.WithMessage("结束时间格式错误")
}
p.EndDateLong = util.NewSecond(parse)
return nil
}
......@@ -100,35 +105,35 @@ type Campaign struct {
AttributionActivePayIntraOneDayAmount float64 `json:"attribution_active_pay_intra_one_day_amount"` // 游戏行业-激活后24h付费金额
Timestamp util.CivilTime `json:"timestamp"` // 创建时间(yyyy-MM-dd HH:mm:ss)
// 计算字段
AvgShowCost float64 `json:"avg_show_cost"` // 千次展示成本
AvgClickCost float64 `json:"avg_click_cost"` // 点击成本
Ctr float64 `json:"ctr"` // 点击率
ConvertCost float64 `json:"convert_cost"` // 转化成本
ConvertRate float64 `json:"convert_rate"` // 转化率
DeepConvertCost float64 `json:"deep_convert_cost"` // 深度转化成本
DeepConvertRate float64 `json:"deep_convert_rate"` // 深度转化率
AttributionConvertCost float64 `json:"attribution_convert_cost"` // 转化成本(计费时间)
AttributionDeepConvertCost float64 `json:"attribution_deep_convert_cost"` // 深度转化成本(计费时间)
DownloadStartCost float64 `json:"download_start_cost"` // 安卓下载开始成本
DownloadStartRate float64 `json:"download_start_rate"` // 安卓下载开始率
DownloadFinishCost float64 `json:"download_finish_cost"` // 安卓下载完成成本
DownloadFinishRate float64 `json:"download_finish_rate"` // 安卓下载完成率
InstallFinishCost float64 `json:"install_finish_cost"` // 安卓安装完成成本
InstallFinishRate float64 `json:"install_finish_rate"` // 安卓安装完成率
ActiveCost float64 `json:"active_cost"` // 激活成本
ActiveRate float64 `json:"active_rate"` // 激活率
ActiveRegisterCost float64 `json:"active_register_cost"` // 注册成本
ActiveRegisterRate float64 `json:"active_register_rate"` // 注册率
AttributionNextDayOpenCost float64 `json:"attribution_next_day_open_cost"` // 次留成本
AttributionNextDayOpenRate float64 `json:"attribution_next_day_open_rate"` // 次留率
GameAddictionCost float64 `json:"game_addiction_cost"` // 关键行为成本
GameAddictionRate float64 `json:"game_addiction_rate"` // 关键行为率
ActivePayCost float64 `json:"active_pay_cost"` // 首次付费成本
ActivePayRate float64 `json:"active_pay_rate"` // 首次付费率
ValidPlayCost float64 `json:"valid_play_cost"` // 有效播放成本
ValidPlayRate float64 `json:"valid_play_rate"` // 有效播放率
AveragePlayTimePerPlay float64 `json:"average_play_time_per_play"` // 视频数据-平均单次播放时长,单位:秒
PlayOverRate float64 `json:"play_over_rate"` // 播完率
AvgShowCost util.Float64 `json:"avg_show_cost"` // 千次展示成本
AvgClickCost util.Float64 `json:"avg_click_cost"` // 点击成本
Ctr util.Float64 `json:"ctr"` // 点击率
ConvertCost util.Float64 `json:"convert_cost"` // 转化成本
ConvertRate util.Float64 `json:"convert_rate"` // 转化率
DeepConvertCost util.Float64 `json:"deep_convert_cost"` // 深度转化成本
DeepConvertRate util.Float64 `json:"deep_convert_rate"` // 深度转化率
AttributionConvertCost util.Float64 `json:"attribution_convert_cost"` // 转化成本(计费时间)
AttributionDeepConvertCost util.Float64 `json:"attribution_deep_convert_cost"` // 深度转化成本(计费时间)
DownloadStartCost util.Float64 `json:"download_start_cost"` // 安卓下载开始成本
DownloadStartRate util.Float64 `json:"download_start_rate"` // 安卓下载开始率
DownloadFinishCost util.Float64 `json:"download_finish_cost"` // 安卓下载完成成本
DownloadFinishRate util.Float64 `json:"download_finish_rate"` // 安卓下载完成率
InstallFinishCost util.Float64 `json:"install_finish_cost"` // 安卓安装完成成本
InstallFinishRate util.Float64 `json:"install_finish_rate"` // 安卓安装完成率
ActiveCost util.Float64 `json:"active_cost"` // 激活成本
ActiveRate util.Float64 `json:"active_rate"` // 激活率
ActiveRegisterCost util.Float64 `json:"active_register_cost"` // 注册成本
ActiveRegisterRate util.Float64 `json:"active_register_rate"` // 注册率
AttributionNextDayOpenCost util.Float64 `json:"attribution_next_day_open_cost"` // 次留成本
AttributionNextDayOpenRate util.Float64 `json:"attribution_next_day_open_rate"` // 次留率
GameAddictionCost util.Float64 `json:"game_addiction_cost"` // 关键行为成本
GameAddictionRate util.Float64 `json:"game_addiction_rate"` // 关键行为率
ActivePayCost util.Float64 `json:"active_pay_cost"` // 首次付费成本
ActivePayRate util.Float64 `json:"active_pay_rate"` // 首次付费率
ValidPlayCost util.Float64 `json:"valid_play_cost"` // 有效播放成本
ValidPlayRate util.Float64 `json:"valid_play_rate"` // 有效播放率
AveragePlayTimePerPlay util.Float64 `json:"average_play_time_per_play"` // 视频数据-平均单次播放时长,单位:秒
PlayOverRate util.Float64 `json:"play_over_rate"` // 播完率
}
func (p *Campaign) PrepareRateAndCost() {
......@@ -163,127 +168,127 @@ func (p *Campaign) PrepareRateAndCost() {
p.PlayOverRate = p.getPlayOverRate()
}
func (p *Campaign) getAvgShowCost() float64 {
return calcRate(p.Cost, float64(p.Show))
func (p *Campaign) getAvgShowCost() util.Float64 {
return calcRate(p.Cost, float64(p.Show)) * 1000
}
func (p *Campaign) getAvgClickCost() float64 {
func (p *Campaign) getAvgClickCost() util.Float64 {
return calcRate(p.Cost, float64(p.Click))
}
func (p *Campaign) getCtr() float64 {
return calcRate(float64(p.Click), float64(p.Show))
func (p *Campaign) getCtr() util.Float64 {
return calcRate(float64(p.Click), float64(p.Show)) * 100
}
func (p *Campaign) getConvertCost() float64 {
func (p *Campaign) getConvertCost() util.Float64 {
return calcRate(p.Cost, float64(p.Convert))
}
func (p *Campaign) getConvertRate() float64 {
return calcRate(float64(p.Convert), float64(p.Click))
func (p *Campaign) getConvertRate() util.Float64 {
return calcRate(float64(p.Convert), float64(p.Click)) * 100
}
func (p *Campaign) getDeepConvertCost() float64 {
func (p *Campaign) getDeepConvertCost() util.Float64 {
return calcRate(p.Cost, float64(p.DeepConvert))
}
func (p *Campaign) getDeepConvertRate() float64 {
return calcRate(float64(p.DeepConvert), float64(p.Convert))
func (p *Campaign) getDeepConvertRate() util.Float64 {
return calcRate(float64(p.DeepConvert), float64(p.Convert)) * 100
}
func (p *Campaign) getAttributionConvertCost() float64 {
func (p *Campaign) getAttributionConvertCost() util.Float64 {
return calcRate(p.Cost, float64(p.AttributionConvert))
}
func (p *Campaign) getAttributionDeepConvertCost() float64 {
func (p *Campaign) getAttributionDeepConvertCost() util.Float64 {
return calcRate(p.Cost, float64(p.AttributionDeepConvert))
}
func (p *Campaign) getDownloadStartCost() float64 {
func (p *Campaign) getDownloadStartCost() util.Float64 {
return calcRate(p.Cost, float64(p.DownloadStart))
}
func (p *Campaign) getDownloadStartRate() float64 {
return calcRate(float64(p.DownloadStart), float64(p.Click))
func (p *Campaign) getDownloadStartRate() util.Float64 {
return calcRate(float64(p.DownloadStart), float64(p.Click)) * 100
}
func (p *Campaign) getDownloadFinishCost() float64 {
func (p *Campaign) getDownloadFinishCost() util.Float64 {
return calcRate(p.Cost, float64(p.DownloadFinish))
}
func (p *Campaign) getDownloadFinishRate() float64 {
return calcRate(float64(p.DownloadFinish), float64(p.DownloadStart))
func (p *Campaign) getDownloadFinishRate() util.Float64 {
return calcRate(float64(p.DownloadFinish), float64(p.DownloadStart)) * 100
}
func (p *Campaign) getInstallFinishCost() float64 {
func (p *Campaign) getInstallFinishCost() util.Float64 {
return calcRate(p.Cost, float64(p.InstallFinish))
}
func (p *Campaign) getInstallFinishRate() float64 {
return calcRate(float64(p.InstallFinish), float64(p.DownloadFinish))
func (p *Campaign) getInstallFinishRate() util.Float64 {
return calcRate(float64(p.InstallFinish), float64(p.DownloadFinish)) * 100
}
func (p *Campaign) getActiveCost() float64 {
func (p *Campaign) getActiveCost() util.Float64 {
return calcRate(p.Cost, float64(p.Active))
}
func (p *Campaign) getActiveRate() float64 {
return calcRate(float64(p.Active), float64(p.Click))
func (p *Campaign) getActiveRate() util.Float64 {
return calcRate(float64(p.Active), float64(p.Click)) * 100
}
func (p *Campaign) getActiveRegisterCost() float64 {
func (p *Campaign) getActiveRegisterCost() util.Float64 {
return calcRate(p.Cost, float64(p.Register))
}
func (p *Campaign) getActiveRegisterRate() float64 {
return calcRate(float64(p.Register), float64(p.Active))
func (p *Campaign) getActiveRegisterRate() util.Float64 {
return calcRate(float64(p.Register), float64(p.Active)) * 100
}
func (p *Campaign) getAttributionNextDayOpenCost() float64 {
func (p *Campaign) getAttributionNextDayOpenCost() util.Float64 {
return calcRate(p.Cost, float64(p.AttributionNextDayOpenCnt))
}
func (p *Campaign) getAttributionNextDayOpenRate() float64 {
return calcRate(float64(p.AttributionNextDayOpenCnt), float64(p.Active))
func (p *Campaign) getAttributionNextDayOpenRate() util.Float64 {
return calcRate(float64(p.AttributionNextDayOpenCnt), float64(p.Active)) * 100
}
func (p *Campaign) getGameAddictionCost() float64 {
func (p *Campaign) getGameAddictionCost() util.Float64 {
return calcRate(p.Cost, float64(p.GameAddiction))
}
func (p *Campaign) getGameAddictionRate() float64 {
return calcRate(float64(p.GameAddiction), float64(p.Active))
func (p *Campaign) getGameAddictionRate() util.Float64 {
return calcRate(float64(p.GameAddiction), float64(p.Active)) * 100
}
func (p *Campaign) getActivePayCost() float64 {
func (p *Campaign) getActivePayCost() util.Float64 {
return calcRate(p.Cost, float64(p.PayCount))
}
func (p *Campaign) getActivePayRate() float64 {
return calcRate(float64(p.PayCount), float64(p.Active))
func (p *Campaign) getActivePayRate() util.Float64 {
return calcRate(float64(p.PayCount), float64(p.Active)) * 100
}
func (p *Campaign) getValidPlayCost() float64 {
func (p *Campaign) getValidPlayCost() util.Float64 {
return calcRate(p.Cost, float64(p.ValidPlay))
}
func (p *Campaign) getValidPlayRate() float64 {
return calcRate(float64(p.ValidPlay), float64(p.Show))
func (p *Campaign) getValidPlayRate() util.Float64 {
return calcRate(float64(p.ValidPlay), float64(p.Show)) * 100
}
func (p *Campaign) getAveragePlayTimePerPlay() float64 {
func (p *Campaign) getAveragePlayTimePerPlay() util.Float64 {
return calcRate(float64(p.PlayDurationSum), float64(p.TotalPlay))
}
func (p *Campaign) getPlayOverRate() float64 {
return calcRate(float64(p.Play100FeedBreak), float64(p.TotalPlay))
func (p *Campaign) getPlayOverRate() util.Float64 {
return calcRate(float64(p.Play100FeedBreak), float64(p.TotalPlay)) * 100
}
func calcRate(x float64, y float64) float64 {
func calcRate(x float64, y float64) util.Float64 {
if y == 0 {
return 0
}
return x / y
return util.Float64(x / y)
}
// CampaignRepo is a Report camRepo.
......
package util
import (
"fmt"
"github.com/shopspring/decimal"
)
type Float64 float64
func (f Float64) MarshalJSON() ([]byte, error) {
out, _ := decimal.NewFromFloat(float64(f)).Round(2).Float64()
return []byte(fmt.Sprint(out)), nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment