65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
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
|
||
}
|