diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index e97769b..c9a6e3e 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -319,7 +319,7 @@ WHERE a.id = ANY($1::bigint[]) AND a.deleted = FALSE AND q.accountid = $2 AND a. -- name: CheckResultOwner :one SELECT q.accountid FROM answer a JOIN quiz q ON a.quiz_id = q.id WHERE a.id = $1 AND a.deleted = FALSE AND a.start = false; --- name:DeviceStats :many +-- name: DeviceStatistics :many WITH DeviceStats AS ( SELECT device_type, @@ -327,7 +327,7 @@ WITH DeviceStats AS ( FROM answer WHERE - quiz_id = $1 + answer.quiz_id = $1 AND created_at >= to_timestamp($2) AND created_at <= to_timestamp($3) AND result = TRUE @@ -341,7 +341,7 @@ WITH DeviceStats AS ( FROM answer WHERE - quiz_id = $1 + answer.quiz_id = $1 AND created_at >= to_timestamp($2) AND created_at <= to_timestamp($3) AND result = TRUE @@ -355,7 +355,7 @@ WITH DeviceStats AS ( FROM answer WHERE - quiz_id = $1 + answer.quiz_id = $1 AND created_at >= to_timestamp($2) AND created_at <= to_timestamp($3) AND result = TRUE @@ -368,20 +368,20 @@ WITH DeviceStats AS ( FROM answer WHERE - quiz_id = $1 + answer.quiz_id = $1 AND created_at >= to_timestamp($2) AND created_at <= to_timestamp($3) AND result = TRUE ) SELECT - device_type, - (device_count::FLOAT / total_count) * 100 AS device_percentage, - os, - (os_count::FLOAT / total_count) * 100 AS os_percentage, - browser, - (browser_count::FLOAT / total_count) * 100 AS browser_percentage + DeviceStats.device_type, + (DeviceStats.device_count::FLOAT / TotalStats.total_count) * 100 AS device_percentage, + OSStats.os, + (OSStats.os_count::FLOAT / TotalStats.total_count) * 100 AS os_percentage, + BrowserStats.browser, + (BrowserStats.browser_count::FLOAT / TotalStats.total_count) * 100 AS browser_percentage FROM DeviceStats, OSStats, BrowserStats, - TotalStats; \ No newline at end of file + TotalStats; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 1066e9f..aebc5fd 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -345,6 +345,119 @@ func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) return i, err } +const deviceStatistics = `-- name: DeviceStatistics :many +WITH DeviceStats AS ( + SELECT + device_type, + COUNT(*) AS device_count + FROM + answer + WHERE + answer.quiz_id = $1 + AND created_at >= to_timestamp($2) + AND created_at <= to_timestamp($3) + AND result = TRUE + GROUP BY + device_type +), + OSStats AS ( + SELECT + os, + COUNT(*) AS os_count + FROM + answer + WHERE + answer.quiz_id = $1 + AND created_at >= to_timestamp($2) + AND created_at <= to_timestamp($3) + AND result = TRUE + GROUP BY + os + ), + BrowserStats AS ( + SELECT + browser, + COUNT(*) AS browser_count + FROM + answer + WHERE + answer.quiz_id = $1 + AND created_at >= to_timestamp($2) + AND created_at <= to_timestamp($3) + AND result = TRUE + GROUP BY + browser + ), + TotalStats AS ( + SELECT + COUNT(*) AS total_count + FROM + answer + WHERE + answer.quiz_id = $1 + AND created_at >= to_timestamp($2) + AND created_at <= to_timestamp($3) + AND result = TRUE + ) +SELECT + DeviceStats.device_type, + (DeviceStats.device_count::FLOAT / TotalStats.total_count) * 100 AS device_percentage, + OSStats.os, + (OSStats.os_count::FLOAT / TotalStats.total_count) * 100 AS os_percentage, + BrowserStats.browser, + (BrowserStats.browser_count::FLOAT / TotalStats.total_count) * 100 AS browser_percentage +FROM + DeviceStats, + OSStats, + BrowserStats, + TotalStats +` + +type DeviceStatisticsParams struct { + QuizID int64 `db:"quiz_id" json:"quiz_id"` + ToTimestamp float64 `db:"to_timestamp" json:"to_timestamp"` + ToTimestamp_2 float64 `db:"to_timestamp_2" json:"to_timestamp_2"` +} + +type DeviceStatisticsRow struct { + DeviceType string `db:"device_type" json:"device_type"` + DevicePercentage int32 `db:"device_percentage" json:"device_percentage"` + Os string `db:"os" json:"os"` + OsPercentage int32 `db:"os_percentage" json:"os_percentage"` + Browser string `db:"browser" json:"browser"` + BrowserPercentage int32 `db:"browser_percentage" json:"browser_percentage"` +} + +func (q *Queries) DeviceStatistics(ctx context.Context, arg DeviceStatisticsParams) ([]DeviceStatisticsRow, error) { + rows, err := q.db.QueryContext(ctx, deviceStatistics, arg.QuizID, arg.ToTimestamp, arg.ToTimestamp_2) + if err != nil { + return nil, err + } + defer rows.Close() + var items []DeviceStatisticsRow + for rows.Next() { + var i DeviceStatisticsRow + if err := rows.Scan( + &i.DeviceType, + &i.DevicePercentage, + &i.Os, + &i.OsPercentage, + &i.Browser, + &i.BrowserPercentage, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const duplicateQuestion = `-- name: DuplicateQuestion :one INSERT INTO question( quiz_id, title, description, questiontype, required,