diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 1fd03ce..518963d 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -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 diff --git a/repository/statistics/statistics.go b/repository/statistics/statistics.go index 05cfba3..0bf9088 100644 --- a/repository/statistics/statistics.go +++ b/repository/statistics/statistics.go @@ -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 }