common/repository/statistics/statistics.go

76 lines
2.4 KiB
Go
Raw Normal View History

2024-03-15 12:31:12 +00:00
package statistics
import (
2024-03-15 13:02:09 +00:00
"context"
2024-03-15 12:31:12 +00:00
"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,
}
}
2024-03-15 13:02:09 +00:00
type DeviceStatReq struct {
2024-03-15 13:36:41 +00:00
QuizId int64
2024-03-15 13:02:09 +00:00
From uint64
To uint64
}
type DeviceStatResp struct {
2024-03-15 13:36:41 +00:00
//ключ DeviceType значение процент
Device map[string]float64 // процентное соотношение DeviceType по всем ответам на опроc c res==true
// тоже самое тут только по OS и BROWSER
2024-03-15 13:02:09 +00:00
OS map[string]float64
Browser map[string]float64
}
2024-03-15 13:36:41 +00:00
func (r *StatisticsRepository) GetDeviceStatistics(ctx context.Context, req DeviceStatReq) (DeviceStatResp, error) {
2024-03-15 13:02:09 +00:00
resp := DeviceStatResp{
Device: make(map[string]float64),
OS: make(map[string]float64),
Browser: make(map[string]float64),
}
2024-03-15 13:36:41 +00:00
//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
2024-03-15 13:02:09 +00:00
}
2024-03-15 16:28:22 +00:00
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) {
}