add more style

This commit is contained in:
Aleksandr Trushkin
2024-01-05 23:03:15 +03:00
parent fbe9927ac3
commit 48f5d80f7a
10 changed files with 415 additions and 134 deletions

View File

@ -59,7 +59,7 @@ type YDBConnection struct {
}
func NewYDBConnection(ctx context.Context, cfg config.YDB, log *slog.Logger) (*YDBConnection, error) {
opts := make([]ydb.Option, 0, 2)
opts := make([]ydb.Option, 0, 3)
switch auth := cfg.Auth.(type) {
case config.YCAuthIAMToken:
opts = append(opts, ydb.WithAccessTokenCredentials(auth.Token))
@ -69,6 +69,10 @@ func NewYDBConnection(ctx context.Context, cfg config.YDB, log *slog.Logger) (*Y
yc.WithServiceAccountKeyFileCredentials(auth.Path),
)
}
opts = append(opts,
ydb.WithDialTimeout(time.Second*3),
)
db, err := ydb.Open(
ctx,
cfg.DSN,
@ -117,7 +121,7 @@ func (r *ydbCourseRepository) List(
qtParams := queryTemplateParams{
Fields: coursesFieldsStr,
Table: "courses",
Suffix: "ORDER BY id\nLIMIT $limit",
Suffix: "ORDER BY learning_type,course_thematic,id\nLIMIT $limit",
Declares: []queryTemplateDeclaration{
{
Name: "limit",
@ -235,10 +239,36 @@ func (r *ydbCourseRepository) List(
return result, err
}
func (r *ydbCourseRepository) Get(ctx context.Context, id string) (course domain.Course, err error) {
func (r *ydbCourseRepository) Get(
ctx context.Context,
id string,
) (course domain.Course, err error) {
const queryName = "get"
const querySelect = `DECLARE $id AS Text;
SELECT
id,
external_id,
source_type,
source_name,
course_thematic,
learning_type,
organization_id,
origin_link,
image_link,
name,
description,
full_price,
discount,
duration,
starts_at,
created_at,
updated_at,
deleted_at
FROM
courses
WHERE
id = $id;`
courses := make([]domain.Course, 0, 1)
readTx := table.TxControl(
table.BeginTx(
table.WithOnlineReadOnly(),
@ -262,63 +292,48 @@ func (r *ydbCourseRepository) Get(ctx context.Context, id string) (course domain
_, res, err := s.Execute(
ctx,
readTx,
`
DECLARE $id AS Text;
SELECT
id,
external_id,
source_type,
source_name,
course_thematic,
learning_type,
organization_id,
origin_link,
image_link,
name,
description,
full_price,
discount,
duration,
starts_at,
created_at,
updated_at,
deleted_at
FROM
courses
WHERE
id = $id;
`,
querySelect,
table.NewQueryParameters(
table.ValueParam("$id", types.TextValue(id)),
),
options.WithCollectStatsModeBasic(),
)
if err != nil {
return fmt.Errorf("executing: %w", err)
return fmt.Errorf("executing query: %w", err)
}
for res.NextResultSet(ctx) {
for res.NextRow() {
var cdb courseDB
_ = res.ScanNamed(cdb.getNamedValues()...)
courses = append(courses, mapCourseDB(cdb))
if !res.NextResultSet(ctx) || !res.HasNextRow() {
return errors.ErrNotFound
}
for res.NextRow() {
var cdb courseDB
err = res.ScanNamed(cdb.getNamedValues()...)
if err != nil {
return fmt.Errorf("scanning row: %w", err)
}
course = mapCourseDB(cdb)
}
if err = res.Err(); err != nil {
return err
}
stats := res.Stats()
xcontext.LogInfo(
ctx, r.log, "query stats",
slog.String("ast", stats.QueryAST()),
slog.String("plan", stats.QueryPlan()),
slog.Duration("total_cpu_time", stats.TotalCPUTime()),
slog.Duration("total_duration", stats.TotalDuration()),
slog.Duration("process_cpu_time", stats.ProcessCPUTime()),
)
return nil
},
table.WithIdempotent())
if err != nil {
return domain.Course{}, err
}
if len(courses) == 0 {
return course, errors.ErrNotFound
}
return courses[0], err
table.WithIdempotent(),
)
return course, err
}
func (r *ydbCourseRepository) GetByExternalID(ctx context.Context, id string) (domain.Course, error) {