@@ -84,6 +84,34 @@ func (c *Client) GetInviteCodeStats(ctx context.Context, inviteCode string, days
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetInviteCodeMonthlyStats 获取指定邀请码在自然月维度的下载统计。
|
||||
// currentTime 所在月份作为本月,上一自然月作为上月。
|
||||
func (c *Client) GetInviteCodeMonthlyStats(ctx context.Context, inviteCode string, currentTime time.Time) (*InviteCodeStats, error) {
|
||||
if currentTime.IsZero() {
|
||||
currentTime = time.Now()
|
||||
}
|
||||
|
||||
currentMonthStart := time.Date(currentTime.Year(), currentTime.Month(), 1, 0, 0, 0, 0, currentTime.Location())
|
||||
lastMonthStart := currentMonthStart.AddDate(0, -1, 0)
|
||||
|
||||
thisMonthStats, err := c.queryPeriodStats(ctx, inviteCode, currentMonthStart, currentTime)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询本月数据失败: %w", err)
|
||||
}
|
||||
|
||||
lastMonthStats, err := c.queryPeriodStats(ctx, inviteCode, lastMonthStart, currentMonthStart)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询上月数据失败: %w", err)
|
||||
}
|
||||
|
||||
return &InviteCodeStats{
|
||||
MacClicks: thisMonthStats.MacClicks,
|
||||
WindowsClicks: thisMonthStats.WindowsClicks,
|
||||
LastMonthMac: lastMonthStats.MacClicks,
|
||||
LastMonthWindows: lastMonthStats.WindowsClicks,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// queryPeriodStats 查询指定时间范围的统计数据
|
||||
func (c *Client) queryPeriodStats(ctx context.Context, inviteCode string, startTime, endTime time.Time) (*InviteCodeStats, error) {
|
||||
// 构建 Loki 查询
|
||||
|
||||
Reference in New Issue
Block a user