openreplay/backend/pkg/db/postgres/connector.go
Alexander 45c956c489
Json logs format (#1952)
* feat(backend): try a new approach for logs formatting (http)

* feat(backend): added logger module

* feat(backend): added project/session info to /i endpoint

* feat(backend): found a solution for correct caller information

* feat(backend): finished logs for http handlers

* feat(backend): finished logs for mobile http handlers

* feat(backend): finished ender

* feat(backend): finished assets

* feat(backend): finished heuristics

* feat(backend): finished image-storage

* feat(backend): finished sink

* feat(backend): finished storage

* feat(backend): formatted logs in all services

* feat(backend): finished foss part

* feat(backend): added missed foss part

* feat(backend): fixed panic in memory manager and sink service

* feat(backend): connectors
2024-03-14 12:51:14 +01:00

70 lines
1.7 KiB
Go

package postgres
import (
"context"
"openreplay/backend/pkg/db/postgres/batch"
"openreplay/backend/pkg/db/postgres/pool"
"openreplay/backend/pkg/logger"
"openreplay/backend/pkg/sessions"
)
type CH interface {
InsertAutocomplete(session *sessions.Session, msgType, msgValue string) error
}
// Conn contains batches, bulks and cache for all sessions
type Conn struct {
log logger.Logger
Pool pool.Pool
batches *batch.BatchSet
bulks *BulkSet
chConn CH // hack for autocomplete inserts, TODO: rewrite
}
func (conn *Conn) SetClickHouse(ch CH) {
conn.chConn = ch
}
func NewConn(log logger.Logger, pool pool.Pool) *Conn {
if pool == nil {
log.Fatal(context.Background(), "pg pool is empty")
}
return &Conn{
log: log,
Pool: pool,
bulks: NewBulkSet(log, pool),
batches: batch.NewBatchSet(log, pool),
}
}
func (conn *Conn) Close() error {
conn.bulks.Stop()
conn.batches.Stop()
return nil
}
func (conn *Conn) InsertAutocompleteValue(sessionID uint64, projectID uint32, tp string, value string) {
if len(value) == 0 {
return
}
if err := conn.bulks.Get("autocompletes").Append(value, tp, projectID); err != nil {
conn.log.Error(context.Background(), "can't add autocomplete to PG, err: %s", err)
}
if conn.chConn == nil {
return
}
// Send autocomplete data to clickhouse
if err := conn.chConn.InsertAutocomplete(&sessions.Session{SessionID: sessionID, ProjectID: projectID}, tp, value); err != nil {
conn.log.Error(context.Background(), "can't add autocomplete to CH, err: %s", err)
}
}
func (conn *Conn) BatchQueue(sessionID uint64, sql string, args ...interface{}) {
conn.batches.BatchQueue(sessionID, sql, args...)
}
func (conn *Conn) Commit() {
conn.bulks.Send()
conn.batches.Commit()
}