common/repository/statistics/click_statistics.go

200 lines
4.6 KiB
Go
Raw Normal View History

2024-06-13 12:39:18 +00:00
package statistics
2024-06-13 15:37:58 +00:00
import (
"context"
"database/sql"
"fmt"
2024-06-19 14:29:20 +00:00
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/utils"
"sort"
"strings"
2024-06-13 15:37:58 +00:00
)
2024-06-13 12:39:18 +00:00
type DepsClick struct {
Conn *sql.DB
}
type StatisticClick struct {
conn *sql.DB
}
2024-06-13 15:37:58 +00:00
func NewClickStatistic(ctx context.Context, deps DepsClick) (*StatisticClick, error) {
s := &StatisticClick{
2024-06-13 12:39:18 +00:00
conn: deps.Conn,
}
2024-06-13 15:37:58 +00:00
err := s.checkMW(ctx)
if err != nil {
fmt.Println("error check material view existing", err)
2024-06-13 15:37:58 +00:00
return nil, err
}
return s, nil
}
func (s *StatisticClick) checkMW(ctx context.Context) error {
query := `
CREATE MATERIALIZED VIEW IF NOT EXISTS mv_last_answers_events
ENGINE = MergeTree()
2024-06-18 08:20:02 +00:00
ORDER BY (ctxsession, event_time) POPULATE AS
2024-06-13 15:37:58 +00:00
SELECT
2024-06-20 09:37:53 +00:00
event_time, ctxsession, ctxquizid, ctxquestionid, ctxidint, message,ctxquiz
FROM statistics
WHERE message IN ('InfoQuizOpen', 'InfoAnswer', 'InfoResult') AND event_level = 'info';
2024-06-13 15:37:58 +00:00
`
_, err := s.conn.ExecContext(ctx, query)
if err != nil {
return err
}
return nil
}
2024-06-13 19:03:41 +00:00
type Statistic struct {
2024-06-13 15:37:58 +00:00
Count int64
QuestionID int64
}
2024-06-19 14:51:34 +00:00
type PipeLineStatsResp map[int64][]Statistic
2024-06-13 19:03:41 +00:00
2024-06-18 18:19:49 +00:00
// пример:
//"[0, 116783, 116810]"
//"[0, 116783, 116798]"
//"[0, 116783, 116798, 116831]"
//"[0, 116783, 116810, 116849]"
//[0]
//"[0, 116783]"
//"[0, 116783, 116810, 116843]"
2024-06-19 14:29:20 +00:00
//SELECT DISTINCT last_que, reversed
//FROM ( SELECT groupArray(ctxquestionid) AS reversed, arraySlice(arrayReverse(groupArray(ctxquestionid)), 1, 1)[1] AS last_que
//FROM statistics WHERE ctxquizid = 26276 GROUP BY ctxsession ) AS sub;
func (s *StatisticClick) getFunnel(ctx context.Context, quizID int64, from uint64, to uint64) (map[int64][]int64, error) {
query := `
2024-06-19 14:29:20 +00:00
SELECT DISTINCT last_que, reversed
2024-06-19 14:51:34 +00:00
FROM ( SELECT groupUniqArray(ctxquestionid) AS reversed, arraySlice(arrayReverse(groupArray(ctxquestionid)), 1, 1)[1] AS last_que
2024-06-20 09:37:53 +00:00
FROM mv_last_answers_events WHERE ctxquizid = ? AND event_time BETWEEN ? AND ? GROUP BY ctxsession ) AS sub;
`
rows, err := s.conn.QueryContext(ctx, query, quizID, from, to)
2024-06-13 15:37:58 +00:00
if err != nil {
return nil, err
}
defer rows.Close()
2024-06-14 18:06:01 +00:00
2024-06-19 14:29:20 +00:00
funnel := make(map[int64][]int64)
for rows.Next() {
2024-06-19 14:29:20 +00:00
var lastQue int64
var reversed []int64
if err := rows.Scan(&lastQue, &reversed); err != nil {
2024-06-14 18:06:01 +00:00
return nil, err
}
2024-06-19 14:29:20 +00:00
funnel[lastQue] = reversed
2024-06-14 13:51:47 +00:00
}
2024-06-13 15:37:58 +00:00
if err := rows.Err(); err != nil {
2024-06-14 13:51:47 +00:00
return nil, err
2024-06-13 15:37:58 +00:00
}
2024-06-19 14:29:20 +00:00
result := make(map[int64][]int64)
keys := make([]int64, 0, len(funnel))
for key := range funnel {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for _, lastQue := range keys {
reversed := funnel[lastQue]
found := false
for _, otherLastQue := range keys {
if otherLastQue != lastQue {
otherReversed := funnel[otherLastQue]
sort.Slice(otherReversed, func(i, j int) bool {
return otherReversed[i] < otherReversed[j]
})
2024-06-19 14:29:20 +00:00
index := utils.BinarySearch(lastQue, otherReversed)
if index {
found = true
break
}
}
}
if !found {
result[lastQue] = reversed
}
}
return result, nil
2024-06-14 13:51:47 +00:00
}
func (s *StatisticClick) GetPipelinesStatistics(ctx context.Context, quizID int64, from uint64, to uint64) (PipeLineStatsResp, error) {
2024-06-19 14:51:34 +00:00
pipelines := make(PipeLineStatsResp)
funnel, err := s.getFunnel(ctx, quizID, from, to)
if err != nil {
return nil, err
}
2024-06-19 14:51:34 +00:00
for lastQue, idS := range funnel {
sesCount, err := s.countSession(ctx, quizID, from, to, idS)
if err != nil {
return nil, err
}
for _, queID := range idS {
if sessionCount, ok := sesCount[queID]; ok {
pipeline := Statistic{
QuestionID: queID,
Count: sessionCount,
2024-06-19 14:51:34 +00:00
}
pipelines[lastQue] = append(pipelines[lastQue], pipeline)
}
2024-06-14 13:51:47 +00:00
}
}
return pipelines, nil
2024-06-14 13:51:47 +00:00
}
func (s *StatisticClick) countSession(ctx context.Context, quizID int64, from uint64, to uint64, questionIDs []int64) (map[int64]int64, error) {
placeholders := make([]string, len(questionIDs))
args := make([]interface{}, len(questionIDs)+3)
args[0] = quizID
for i, id := range questionIDs {
placeholders[i] = "?"
args[i+1] = id
}
args[len(args)-2] = from
args[len(args)-1] = to
2024-06-14 18:06:01 +00:00
query := fmt.Sprintf(`
SELECT ctxquestionid, COUNT(DISTINCT ctxsession) AS session_count
FROM statistics
WHERE ctxquizid = ? AND ctxquestionid IN (%s) AND event_time BETWEEN ? AND ?
GROUP BY ctxquestionid;
`, strings.Join(placeholders, ","))
2024-06-14 18:06:01 +00:00
rows, err := s.conn.QueryContext(ctx, query, args...)
2024-06-14 13:51:47 +00:00
if err != nil {
2024-06-13 15:37:58 +00:00
return nil, err
}
defer rows.Close()
2024-06-13 15:37:58 +00:00
counts := make(map[int64]int64)
2024-06-14 13:51:47 +00:00
for rows.Next() {
var questionID int64
var count int64
err := rows.Scan(&questionID, &count)
2024-06-14 13:51:47 +00:00
if err != nil {
return nil, err
}
counts[questionID] = count
}
2024-06-14 13:51:47 +00:00
if err := rows.Err(); err != nil {
return nil, err
2024-06-14 13:51:47 +00:00
}
return counts, nil
2024-06-13 12:39:18 +00:00
}