added click house migrate files
This commit is contained in:
parent
a39660f6e1
commit
7d03e23b4e
5
dal/ch_schema/000001_init.down.sql
Normal file
5
dal/ch_schema/000001_init.down.sql
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
DROP VIEW IF EXISTS view_respondent_paths;
|
||||||
|
DROP VIEW IF EXISTS view_pipelines_signs;
|
||||||
|
DROP VIEW IF EXISTS view_last_answers;
|
||||||
|
DROP MATERIALIZED VIEW IF EXISTS mv_answers;
|
||||||
|
DROP MATERIALIZED VIEW if exists mv_last_answers_events;
|
42
dal/ch_schema/000001_init.up.sql
Normal file
42
dal/ch_schema/000001_init.up.sql
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
CREATE MATERIALIZED VIEW IF NOT EXISTS mv_answers
|
||||||
|
ENGINE = MergeTree() ORDER BY event_time
|
||||||
|
POPULATE AS
|
||||||
|
SELECT ctxquizid AS quizid,
|
||||||
|
ctxquestionid AS questionid,
|
||||||
|
ctxsession AS session,
|
||||||
|
event_time
|
||||||
|
FROM default.statistics
|
||||||
|
WHERE (message IN ('InfoQuizOpen', 'InfoAnswer', 'InfoResult'))
|
||||||
|
AND (event_level = 'info')
|
||||||
|
LIMIT 1 BY ctxquestionid, ctxsession;
|
||||||
|
|
||||||
|
CREATE VIEW IF NOT EXISTS view_last_answers AS
|
||||||
|
SELECT last_quesion,
|
||||||
|
any(last_quiz) AS quiz,
|
||||||
|
argMax(session, length(questions)) AS long_session
|
||||||
|
FROM default.view_respondent_paths
|
||||||
|
GROUP BY last_quesion;
|
||||||
|
|
||||||
|
CREATE VIEW IF NOT EXISTS view_pipelines_signs AS
|
||||||
|
SELECT target_quiz,
|
||||||
|
questionid,
|
||||||
|
long_sess
|
||||||
|
FROM (
|
||||||
|
SELECT questionid,
|
||||||
|
any(quiz) AS target_quiz,
|
||||||
|
any(long_session) AS long_sess,
|
||||||
|
groupArray(last_quesion) AS footsteps
|
||||||
|
FROM default.view_last_answers
|
||||||
|
INNER JOIN default.mv_answers ON long_session = session
|
||||||
|
GROUP BY questionid
|
||||||
|
)
|
||||||
|
WHERE length(footsteps) = 1;
|
||||||
|
|
||||||
|
CREATE VIEW IF NOT EXISTS view_respondent_paths AS
|
||||||
|
SELECT session,
|
||||||
|
groupArray(questionid) AS questions,
|
||||||
|
anyLast(questionid) AS last_quesion,
|
||||||
|
anyLast(quizid) AS last_quiz
|
||||||
|
FROM default.mv_answers
|
||||||
|
GROUP BY session;
|
||||||
|
|
@ -217,12 +217,9 @@ func NewClickHouseDAL(ctx context.Context, cred string) (*ClickHouseDAL, error)
|
|||||||
return nil, fmt.Errorf("error ping database: %w", err)
|
return nil, fmt.Errorf("error ping database: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
statsClickRepo, err := statistics.NewClickStatistic(ctx, statistics.DepsClick{
|
statsClickRepo := statistics.NewClickStatistic(statistics.DepsClick{
|
||||||
Conn: conn,
|
Conn: conn,
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ClickHouseDAL{
|
return &ClickHouseDAL{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
@ -3,7 +3,6 @@ package statistics
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type DepsClick struct {
|
type DepsClick struct {
|
||||||
@ -14,35 +13,10 @@ type StatisticClick struct {
|
|||||||
conn *sql.DB
|
conn *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClickStatistic(ctx context.Context, deps DepsClick) (*StatisticClick, error) {
|
func NewClickStatistic(deps DepsClick) *StatisticClick {
|
||||||
s := &StatisticClick{
|
return &StatisticClick{
|
||||||
conn: deps.Conn,
|
conn: deps.Conn,
|
||||||
}
|
}
|
||||||
|
|
||||||
err := s.checkMW(ctx)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("error check material view existing", err)
|
|
||||||
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()
|
|
||||||
ORDER BY (ctxsession, event_time) POPULATE AS
|
|
||||||
SELECT
|
|
||||||
event_time, ctxsession, ctxquizid, ctxquestionid, ctxidint, message,ctxquiz
|
|
||||||
FROM statistics
|
|
||||||
WHERE message IN ('InfoQuizOpen', 'InfoAnswer', 'InfoResult') AND event_level = 'info';
|
|
||||||
`
|
|
||||||
_, err := s.conn.ExecContext(ctx, query)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Statistic struct {
|
type Statistic struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user