update time_bucket to_timestamp

This commit is contained in:
Pavel 2024-03-15 20:19:39 +03:00
parent a9d3f8b5c6
commit db99965348
2 changed files with 25 additions and 2 deletions

@ -390,8 +390,8 @@ FROM
WITH TimeBucket AS (
SELECT
CASE
WHEN to_timestamp($2) - to_timestamp($1) > 172800 THEN date_trunc('day', time_bucket)
ELSE date_trunc('hour', time_bucket)
WHEN to_timestamp($2) - to_timestamp($1) > 172800 THEN date_trunc('day', to_timestamp(time_bucket))
ELSE date_trunc('hour', to_timestamp(time_bucket))
END AS time_interval
FROM
generate_series(to_timestamp($1), to_timestamp($2), '1 hour') AS time_bucket

@ -71,5 +71,28 @@ type GeneralStatsResp struct {
}
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
}