common/repository/statistics/click_statistics.go

216 lines
5.2 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
event_time, ctxsession, ctxquizid, ctxquestionid, ctxidint, message, keyos,
ctxuserip, ctxuserport, keydomain, keypath, ctxquiz, ctxreferrer
FROM (SELECT
event_time, ctxsession, ctxquizid, ctxquestionid, ctxidint, message, keyos,
ctxuserip, ctxuserport, keydomain, keypath, ctxquiz, ctxreferrer,
2024-06-14 11:22:18 +00:00
row_number() OVER (PARTITION BY ctxsession ORDER BY event_time DESC) as row_num
FROM statistics
WHERE message IN ('InfoQuizOpen', 'InfoAnswer', 'InfoResult') AND event_level = 'info') AS sorted
WHERE row_num = 1;
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-13 19:03:41 +00:00
type PipeLineStatsResp [][]Statistic
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
FROM ( SELECT groupArray(ctxquestionid) AS reversed, arraySlice(arrayReverse(groupArray(ctxquestionid)), 1, 1)[1] AS last_que
FROM statistics 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]
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) {
var pipelines PipeLineStatsResp
// получили id вопросов воронок где массив состоит из 1 элемента
funnel, err := s.getFunnel(ctx, quizID, from, to)
if err != nil {
return nil, err
}
2024-06-19 14:29:56 +00:00
fmt.Println(funnel)
var idS []int64
for queID := range funnel {
idS = append(idS, queID)
}
2024-06-14 13:51:47 +00:00
if len(idS) == 0 {
return nil, nil
}
// тут считаем количество ответов на эти вопросы по уникальным сессиям
sesCount, err := s.countSession(ctx, quizID, from, to, idS)
2024-06-14 13:51:47 +00:00
if err != nil {
return nil, err
2024-06-13 15:37:58 +00:00
}
for questionID := range funnel {
if sessionCount, ok := sesCount[questionID]; ok {
pipeline := []Statistic{
{
QuestionID: questionID,
Count: sessionCount,
},
}
pipelines = append(pipelines, 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
}