reworked
This commit is contained in:
parent
7b56647a86
commit
4b23ef4598
@ -4,6 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/utils"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -59,7 +61,6 @@ type Statistic struct {
|
|||||||
|
|
||||||
type PipeLineStatsResp [][]Statistic
|
type PipeLineStatsResp [][]Statistic
|
||||||
|
|
||||||
// todo нужно исключить множества из подмножеств, пока что получаются все уникальные векторы респондентов по опросу
|
|
||||||
// пример:
|
// пример:
|
||||||
//"[0, 116783, 116810]"
|
//"[0, 116783, 116810]"
|
||||||
//"[0, 116783, 116798]"
|
//"[0, 116783, 116798]"
|
||||||
@ -69,27 +70,15 @@ type PipeLineStatsResp [][]Statistic
|
|||||||
//"[0, 116783]"
|
//"[0, 116783]"
|
||||||
//"[0, 116783, 116810, 116843]"
|
//"[0, 116783, 116810, 116843]"
|
||||||
|
|
||||||
//SELECT DISTINCT final FROM (
|
//SELECT DISTINCT last_que, reversed
|
||||||
//SELECT groupArray(ctxquestionid) AS final FROM (SELECT DISTINCT f.ctxsession, a.ctxquestionid
|
//FROM ( SELECT groupArray(ctxquestionid) AS reversed, arraySlice(arrayReverse(groupArray(ctxquestionid)), 1, 1)[1] AS last_que
|
||||||
//FROM (SELECT ctxsession, max(event_time) AS max_time
|
//FROM statistics WHERE ctxquizid = 26276 GROUP BY ctxsession ) AS sub;
|
||||||
//FROM statistics WHERE ctxquizid = 26276 GROUP BY ctxsession ) f
|
|
||||||
//JOIN ( SELECT ctxsession, ctxquestionid, event_time
|
|
||||||
//FROM statistics WHERE ctxquizid = 26276 ) a ON f.ctxsession = a.ctxsession)
|
|
||||||
//GROUP BY ctxsession);
|
|
||||||
|
|
||||||
func (s *StatisticClick) getFunnel(ctx context.Context, quizID int64, from uint64, to uint64) (map[int64][]string, error) {
|
func (s *StatisticClick) getFunnel(ctx context.Context, quizID int64, from uint64, to uint64) (map[int64][]int64, error) {
|
||||||
// берем из матвью все что принадлежит quizID в указанном интервале времени
|
|
||||||
// выбираем самыые поздние по роу набер - 1
|
|
||||||
// группируем по ид вопроса и для каждого ид вопроса формируем массив сессий которые были последнимим для этого вопроса
|
|
||||||
// выбираем только те где длина массива = 1
|
|
||||||
query := `
|
query := `
|
||||||
SELECT ctxquestionid, arrayJoin(endsession) AS session
|
SELECT DISTINCT last_que, reversed
|
||||||
FROM (SELECT ctxquestionid, groupArray(ctxsession) AS endsession
|
FROM ( SELECT groupArray(ctxquestionid) AS reversed, arraySlice(arrayReverse(groupArray(ctxquestionid)), 1, 1)[1] AS last_que
|
||||||
FROM (SELECT ctxsession,ctxquestionid,row_number() OVER (PARTITION BY ctxsession ORDER BY event_time DESC) AS row_num
|
FROM statistics WHERE ctxquizid = ? AND event_time BETWEEN ? AND ? GROUP BY ctxsession ) AS sub;
|
||||||
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)
|
rows, err := s.conn.QueryContext(ctx, query, quizID, from, to)
|
||||||
@ -98,23 +87,50 @@ func (s *StatisticClick) getFunnel(ctx context.Context, quizID int64, from uint6
|
|||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
funnel := make(map[int64][]string)
|
funnel := make(map[int64][]int64)
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var questionID int64
|
var lastQue int64
|
||||||
var sessionID string
|
var reversed []int64
|
||||||
err := rows.Scan(&questionID, &sessionID)
|
if err := rows.Scan(&lastQue, &reversed); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
funnel[questionID] = append(funnel[questionID], sessionID)
|
funnel[lastQue] = reversed
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := rows.Err(); err != nil {
|
if err := rows.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return funnel, nil
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatisticClick) GetPipelinesStatistics(ctx context.Context, quizID int64, from uint64, to uint64) (PipeLineStatsResp, error) {
|
func (s *StatisticClick) GetPipelinesStatistics(ctx context.Context, quizID int64, from uint64, to uint64) (PipeLineStatsResp, error) {
|
||||||
|
20
utils/binary_search.go
Normal file
20
utils/binary_search.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
func BinarySearch(target int64, array []int64) bool {
|
||||||
|
left := 0
|
||||||
|
right := len(array) - 1
|
||||||
|
|
||||||
|
for left <= right {
|
||||||
|
mid := (left + right) / 2
|
||||||
|
|
||||||
|
if array[mid] == target {
|
||||||
|
return true
|
||||||
|
} else if array[mid] < target {
|
||||||
|
left = mid + 1
|
||||||
|
} else if array[mid] > target {
|
||||||
|
right = mid - 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user