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-17 11:37:03 +00:00
|
|
|
|
"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 {
|
2024-06-17 11:37:03 +00:00
|
|
|
|
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-14 10:58:29 +00:00
|
|
|
|
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
|
2024-06-14 10:58:29 +00:00
|
|
|
|
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-17 11:37:03 +00:00
|
|
|
|
func (s *StatisticClick) getFunnel(ctx context.Context, quizID int64, from uint64, to uint64) (map[int64][]string, error) {
|
|
|
|
|
// берем из матвью все что принадлежит quizID в указанном интервале времени
|
|
|
|
|
// выбираем самыые поздние по роу набер - 1
|
|
|
|
|
// группируем по ид вопроса и для каждого ид вопроса формируем массив сессий которые были последнимим для этого вопроса
|
|
|
|
|
// выбираем только те где длина массива = 1
|
|
|
|
|
query := `
|
|
|
|
|
SELECT ctxquestionid, arrayJoin(endsession) AS session
|
|
|
|
|
FROM (SELECT ctxquestionid, groupArray(ctxsession) AS endsession
|
|
|
|
|
FROM (SELECT ctxsession,ctxquestionid,row_number() OVER (PARTITION BY ctxsession ORDER BY event_time DESC) AS row_num
|
|
|
|
|
FROM mv_last_answers_events WHERE ctxquizid = ? AND event_time BETWEEN ? AND ?
|
|
|
|
|
) AS rows
|
|
|
|
|
WHERE row_num = 1 GROUP BY ctxquestionid
|
|
|
|
|
) AS group_sessions WHERE length(endsession) = 1;
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
rows, err := s.conn.QueryContext(ctx, query, quizID, from, to)
|
2024-06-13 15:37:58 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-06-17 11:37:03 +00:00
|
|
|
|
defer rows.Close()
|
2024-06-14 18:06:01 +00:00
|
|
|
|
|
2024-06-17 11:37:03 +00:00
|
|
|
|
funnel := make(map[int64][]string)
|
2024-06-14 18:06:01 +00:00
|
|
|
|
|
2024-06-17 11:37:03 +00:00
|
|
|
|
for rows.Next() {
|
|
|
|
|
var questionID int64
|
|
|
|
|
var sessionID string
|
|
|
|
|
err := rows.Scan(&questionID, &sessionID)
|
|
|
|
|
if err != nil {
|
2024-06-14 18:06:01 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-06-17 11:37:03 +00:00
|
|
|
|
funnel[questionID] = append(funnel[questionID], sessionID)
|
2024-06-14 13:51:47 +00:00
|
|
|
|
}
|
2024-06-13 15:37:58 +00:00
|
|
|
|
|
2024-06-17 11:37:03 +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-17 11:37:03 +00:00
|
|
|
|
return funnel, nil
|
2024-06-14 13:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 11:37:03 +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
|
|
|
|
|
}
|
|
|
|
|
var idS []int64
|
|
|
|
|
for queID := range funnel {
|
|
|
|
|
idS = append(idS, queID)
|
|
|
|
|
}
|
2024-06-14 13:51:47 +00:00
|
|
|
|
|
2024-06-17 11:37:03 +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
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 11:37:03 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 11:37:03 +00:00
|
|
|
|
return pipelines, nil
|
2024-06-14 13:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 11:37:03 +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
|
|
|
|
|
2024-06-17 11:37:03 +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
|
|
|
|
|
2024-06-17 11:37:03 +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
|
|
|
|
|
}
|
2024-06-17 11:37:03 +00:00
|
|
|
|
defer rows.Close()
|
2024-06-13 15:37:58 +00:00
|
|
|
|
|
2024-06-17 11:37:03 +00:00
|
|
|
|
counts := make(map[int64]int64)
|
2024-06-14 13:51:47 +00:00
|
|
|
|
|
2024-06-17 11:37:03 +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
|
|
|
|
|
}
|
2024-06-17 11:37:03 +00:00
|
|
|
|
counts[questionID] = count
|
|
|
|
|
}
|
2024-06-14 13:51:47 +00:00
|
|
|
|
|
2024-06-17 11:37:03 +00:00
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, err
|
2024-06-14 13:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 11:37:03 +00:00
|
|
|
|
return counts, nil
|
2024-06-13 12:39:18 +00:00
|
|
|
|
}
|