common/repository/statistics/statistics.go

99 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package statistics
import (
"context"
"database/sql"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen"
)
type Deps struct {
Queries *sqlcgen.Queries
Pool *sql.DB
}
type StatisticsRepository struct {
queries *sqlcgen.Queries
pool *sql.DB
}
func NewStatisticsRepo(deps Deps) *StatisticsRepository {
return &StatisticsRepository{
queries: deps.Queries,
pool: deps.Pool,
}
}
type DeviceStatReq struct {
QuizId int64
From uint64
To uint64
}
type DeviceStatResp struct {
//ключ DeviceType значение процент
Device map[string]float64 // процентное соотношение DeviceType по всем ответам на опроc c res==true
// тоже самое тут только по OS и BROWSER
OS map[string]float64
Browser map[string]float64
}
func (r *StatisticsRepository) GetDeviceStatistics(ctx context.Context, req DeviceStatReq) (DeviceStatResp, error) {
resp := DeviceStatResp{
Device: make(map[string]float64),
OS: make(map[string]float64),
Browser: make(map[string]float64),
}
//todo подумать как в sqlc сделать не int32 а float64
allStatistics, err := r.queries.DeviceStatistics(ctx, sqlcgen.DeviceStatisticsParams{
QuizID: req.QuizId,
ToTimestamp: float64(req.From),
ToTimestamp_2: float64(req.To),
})
if err != nil {
return resp, err
}
for _, stat := range allStatistics {
resp.Device[stat.DeviceType] = float64(stat.DevicePercentage)
resp.OS[stat.Os] = float64(stat.OsPercentage)
resp.Browser[stat.Browser] = float64(stat.BrowserPercentage)
}
return resp, nil
}
type GeneralStatsResp struct {
Open map[uint64]uint64 // количество ответов с полем start == true за период от одного пункта разбиения и до другого
Result map[uint64]uint64 // количество ответов с полем result == true за период от одного пункта разбиения и до другого
AvTime map[uint64]uint64 // среднее время между ответом с полем result == true и start == true. в рамках сессии
Conversion map[uint64]uint64 // Result/Open за период от одного пункта разбиения и до другого
}
func (r *StatisticsRepository) GetGeneralStatistics(ctx context.Context, req DeviceStatReq) (GeneralStatsResp, error) {
resp := GeneralStatsResp{
Open: make(map[uint64]uint64),
Result: make(map[uint64]uint64),
AvTime: make(map[uint64]uint64),
Conversion: make(map[uint64]uint64),
}
allStatistics, err := r.queries.GeneralStatistics(ctx, sqlcgen.GeneralStatisticsParams{
QuizID: req.QuizId,
ToTimestamp: float64(req.From),
ToTimestamp_2: float64(req.To),
})
if err != nil {
return resp, err
}
for _, stat := range allStatistics {
resp.Open[stat.TimeBucket] = stat.OpenCount
resp.Result[stat.TimeBucket] = stat.ResultCount
resp.AvTime[stat.TimeBucket] = stat.AvgTime
resp.Conversion[stat.TimeBucket] = stat.Conversion
}
return resp, nil
}