stats

package module
v0.0.0-...-ff0031d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

README

使用redis进行各类统计信息的方法汇总

PV UV 统计
  • 使用 redis HyperLogLog 结构实现 约 0.81% 的误差率
  • 支持任何事件级统计 例如
    • 页面PV UV 或 按钮点击次数
  • 默认自动以当前时间作为 key 格式为 [prefix]:[event]:[daytime]
    • 也可以主动变更为最后的日期为任意字符串 格式为 [prefix]:[event]:[string]
  • 可汇聚任意时间段内进行统计
    • 也可以进行任意 key 的合并或统计
  • must 开头的函数 均没有返回值
work 作品统计 封装常见 转发 观看 点赞 评论 质量
  • 转发分享 使用 redis set 结构
    • 细看连头条类最大转发也不过5位数 set 预计最大占用 100mb 也不算大KEY 碰到了再说
  • 观看 使用上面得PV UV统计
  • 点赞 使用 redis set 结构
    • 这个就特别要注意了 可能存在 大KEY 得可能性 但是在 时间 空间 和成本中选择 我选成本 碰到了再说
  • 评论 使用 redis set 结构
    • 上W评论可能性太低 碰到了再说
    • 用户是可以多条评论的
  • 质量 需要业务判断作品质量分 这边只是存储
  • 定时同步上述信息结构 汇聚到 作品ID key下 使用 redis hash 结构进行计数 仅计数
  • 建议业务上层进行一次拦截 对于作品超过N个时间后 就不要进行统计了 根据通用经验 N ~=15 甚至可以 N ~=7
  • workId 不宜过长 暂时还没有考虑优化到key长度的地步 所以先以基本的语义化为主

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TimeRangerParamsError  = errors.New("params error : endTime <= startTime ")
	ParamsLengthEmptyError = errors.New("params error : input params is empty")
	ParamsKeyEmptyError    = errors.New("params error : default key is empty please use newStats or NewStatsKey init struct")
)
View Source
var (
	DefaultPrefix = "st"
)

Functions

func GenRedisClient

func GenRedisClient(addr string, password string, db int) *redis.Client

Types

type HyperStats

type HyperStats struct {
	Prefix string
	Event  string

	Rdb *redis.Client
	// contains filtered or unexported fields
}

HyperStats HyperLog redis的统计

func NewStats

func NewStats(event string, rdb *redis.Client) *HyperStats

NewStats 默认统计方法 以今日时间为维度

func NewStatsKey

func NewStatsKey(event string, rdb *redis.Client, key string) *HyperStats

NewStatsKey 可选统计方法 指定key后缀 适用于文章等以ID为主的维度

func (*HyperStats) Add

func (c *HyperStats) Add(ctx context.Context, elements ...string) error

Add 已存在的重复元素不会计数

func (*HyperStats) AddAny

func (c *HyperStats) AddAny(ctx context.Context, key string, elements ...string) error

AddAny 任何key赋值

func (*HyperStats) ChangePrefix

func (c *HyperStats) ChangePrefix(newPrefix string)

ChangePrefix 变更前缀 请在生成时就变更 最好别变更

func (*HyperStats) Counts

func (c *HyperStats) Counts(ctx context.Context, hold bool, keys ...string) (saveKey string, allCount int64, err error)

Counts 统计多个keys 先合并 再计数 hold为统计之后是否保留

func (*HyperStats) DayTimeRangerCount

func (c *HyperStats) DayTimeRangerCount(ctx context.Context, start time.Time, end time.Time) (int64, error)

DayTimeRangerCount 时间范围统计 包含开始和结束当天

func (*HyperStats) Del

func (c *HyperStats) Del(ctx context.Context, keys ...string) error

Del 删除key

func (*HyperStats) GenerateKey

func (c *HyperStats) GenerateKey(k string) string

GenerateKey 通用生成规则的k [prefix]:[event]:[string]

func (*HyperStats) GetAnyMonthCount

func (c *HyperStats) GetAnyMonthCount(ctx context.Context, monthNumber time.Month) (int64, error)

GetAnyMonthCount 获取任何月份整月汇总 请输入1-12的月份

func (*HyperStats) GetNowMonthCount

func (c *HyperStats) GetNowMonthCount(ctx context.Context) (int64, error)

GetNowMonthCount 获取本月汇总 从1号到今天

func (*HyperStats) GetNowWeekCount

func (c *HyperStats) GetNowWeekCount(ctx context.Context) (int64, error)

GetNowWeekCount 获取本周汇总 周一到今天

func (*HyperStats) Merges

func (c *HyperStats) Merges(ctx context.Context, keys ...string) (saveKey string, err error)

Merges 合并多个keys 一定要注意 是合并 如果A存在于 key1 和 key2中 只会计数1次

func (*HyperStats) MustAdd

func (c *HyperStats) MustAdd(ctx context.Context, elements ...string)

MustAdd 必然新增

func (*HyperStats) MustAddAny

func (c *HyperStats) MustAddAny(ctx context.Context, key string, elements ...string)

MustAddAny 必然新增任何any

func (*HyperStats) NowCount

func (c *HyperStats) NowCount(ctx context.Context) (int64, error)

NowCount 统计当前

func (*HyperStats) NowCountVal

func (c *HyperStats) NowCountVal(ctx context.Context) int64

NowCountVal 统计当前 仅返回数量

func (*HyperStats) SummaryKeys

func (c *HyperStats) SummaryKeys(ctx context.Context, keys ...string) (int64, error)

SummaryKeys 汇总统计多个KEY 对每个key进行计数 仅计数不合并不生成额外key

func (*HyperStats) SummaryKeysUseRule

func (c *HyperStats) SummaryKeysUseRule(ctx context.Context, ids ...string) (int64, error)

SummaryKeysUseRule 汇总统计多个KEY 使用规则生成最终的key eg: [prefix]:[event]:[id]

type WorkStats

type WorkStats struct {
	Prefix string
	WorkId string
	Rdb    *redis.Client
	// contains filtered or unexported fields
}

WorkStats 作品统计

func NewWorkStats

func NewWorkStats(workId string, rdb *redis.Client) *WorkStats

func (*WorkStats) AddComment

func (c *WorkStats) AddComment(ctx context.Context, userId string, mid string) error

AddComment 新增评论 用户是可以多条评论的

func (*WorkStats) AddLike

func (c *WorkStats) AddLike(ctx context.Context, userId string) error

AddLike 新增喜欢

func (*WorkStats) AddPv

func (c *WorkStats) AddPv(ctx context.Context, ip string, ua string) error

AddPv 新增pv ip+ua

func (*WorkStats) AddPvHash

func (c *WorkStats) AddPvHash(ctx context.Context, ip string, ua string) error

AddPvHash 新增pv ip+ua 做hash

func (*WorkStats) AddShare

func (c *WorkStats) AddShare(ctx context.Context, userId string) error

AddShare 新增转发分享

func (*WorkStats) AddUv

func (c *WorkStats) AddUv(ctx context.Context, userId string) error

AddUv 新增uv 观看者userId

func (*WorkStats) GenerateKey

func (c *WorkStats) GenerateKey(event string) string

func (*WorkStats) GetSummary

func (c *WorkStats) GetSummary(ctx context.Context) (*WorkStatsResp, error)

GetSummary 获取汇总信息

func (*WorkStats) InComment

func (c *WorkStats) InComment(ctx context.Context, userId string, mid string) (bool, error)

InComment 判断是否在评论列表中

func (*WorkStats) InLike

func (c *WorkStats) InLike(ctx context.Context, userId string) (bool, error)

InLike 判断是否在喜欢列表中

func (*WorkStats) InShare

func (c *WorkStats) InShare(ctx context.Context, userId string) (bool, error)

InShare 判断是否在分享列表中

func (*WorkStats) SummarySync

func (c *WorkStats) SummarySync(ctx context.Context) error

SummarySync 汇总同步保存

func (*WorkStats) UnComment

func (c *WorkStats) UnComment(ctx context.Context, userId string, mid string) error

UnComment 删除评论

func (*WorkStats) UnLike

func (c *WorkStats) UnLike(ctx context.Context, userId string) error

UnLike 取消喜欢

type WorkStatsResp

type WorkStatsResp struct {
	Id      string `json:"id"`
	Share   int    `json:"share"`
	Like    int    `json:"like"`
	Comment int    `json:"comment"`
	Pv      int    `json:"pv"`
	Uv      int    `json:"uv"`
}

func BatchWorkGetSummary

func BatchWorkGetSummary(ctx context.Context, rdb *redis.Client, wordIds ...string) ([]*WorkStatsResp, error)

BatchWorkGetSummary 自动按批次并发获取作品汇总信息

func ManyWorkGetSummary

func ManyWorkGetSummary(ctx context.Context, rdb *redis.Client, batchLimit int, wordIds ...string) ([]*WorkStatsResp, error)

ManyWorkGetSummary 多个作品获取汇总信息

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL