add show course and map names

This commit is contained in:
Aleksandr Trushkin
2024-01-08 18:31:35 +03:00
parent 48f5d80f7a
commit 2c0564f68c
22 changed files with 166 additions and 90 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"os"
"path"
"strings"
"text/template"
@ -17,10 +18,12 @@ import (
"git.loyso.art/frx/kurious/pkg/xdefault"
"github.com/ydb-platform/ydb-go-sdk/v3"
ydblog "github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
yc "github.com/ydb-platform/ydb-go-yc"
)
@ -69,9 +72,11 @@ func NewYDBConnection(ctx context.Context, cfg config.YDB, log *slog.Logger) (*Y
yc.WithServiceAccountKeyFileCredentials(auth.Path),
)
}
opts = append(opts,
ydb.WithDialTimeout(time.Second*3),
)
if cfg.DebugYDB {
opts = append(opts,
ydb.WithLogger(ydblog.Default(os.Stdout, ydblog.WithMinLevel(ydblog.DEBUG)), trace.DetailsAll),
)
}
db, err := ydb.Open(
ctx,
@ -81,6 +86,14 @@ func NewYDBConnection(ctx context.Context, cfg config.YDB, log *slog.Logger) (*Y
if err != nil {
return nil, fmt.Errorf("opening connection: %w", err)
}
endpoints, err := db.Discovery().Discover(ctx)
if err != nil {
return nil, fmt.Errorf("discovering endpoints: %w", err)
}
for _, endpoint := range endpoints {
xcontext.LogInfo(ctx, log, "discovered endpoint", slog.String("value", endpoint.Address()))
}
return &YDBConnection{
Driver: db,
@ -163,11 +176,9 @@ func (r *ydbCourseRepository) List(
query, err := qtParams.render()
if err != nil {
return result, fmt.Errorf("rendering: %w", err)
return result, fmt.Errorf("rendering query params: %w", err)
}
xcontext.LogInfo(ctx, r.log, "planning to run query", slog.String("query", query), slog.String("opts", tableParamOptsToString(opts...)))
courses := make([]domain.Course, 0, 1_000)
readTx := table.TxControl(
table.BeginTx(
@ -176,11 +187,9 @@ func (r *ydbCourseRepository) List(
table.CommitTx(),
)
xcontext.LogInfo(ctx, r.log, "executing do")
err = r.db.Table().Do(
ctx,
func(ctx context.Context, s table.Session) error {
xcontext.LogInfo(ctx, r.log, "inside do")
start := time.Now()
defer func() {
since := time.Since(start)
@ -194,8 +203,6 @@ func (r *ydbCourseRepository) List(
queryParams := table.NewQueryParameters(opts...)
xcontext.LogDebug(ctx, r.log, "executing")
_, res, err := s.Execute(
ctx, readTx, query, queryParams,
options.WithCollectStatsModeBasic(),
@ -204,14 +211,10 @@ func (r *ydbCourseRepository) List(
return fmt.Errorf("executing: %w", err)
}
xcontext.LogDebug(ctx, r.log, "checking")
if !res.NextResultSet(ctx) || !res.HasNextRow() {
return nil
}
xcontext.LogDebug(ctx, r.log, "scanning")
for res.NextRow() {
var cdb courseDB
err = res.ScanNamed(cdb.getNamedValues()...)
@ -393,7 +396,6 @@ func createCourseParamsAsStruct(params domain.CreateCourseParams) types.Value {
}
func (r *ydbCourseRepository) CreateBatch(ctx context.Context, params ...domain.CreateCourseParams) error {
// -- PRAGMA TablePathPrefix("courses");
const upsertQuery = `DECLARE $courseData AS List<Struct<
id: Text,
external_id: Optional<Text>,
@ -680,8 +682,8 @@ func mapCourseDB(cdb courseDB) domain.Course {
Name: cdb.Name,
SourceType: st,
SourceName: nullable.NewValuePtr(cdb.SourceName),
Thematic: cdb.CourseThematic,
LearningType: cdb.LearningType,
ThematicID: cdb.CourseThematic,
LearningTypeID: cdb.LearningType,
OrganizationID: cdb.OrganizationID,
OriginLink: cdb.OriginLink,
ImageLink: cdb.ImageLink,
@ -738,11 +740,10 @@ WHERE {{ range .Conditions }}{{.}}{{end}}
var querySelect = template.Must(template.New("").Parse(queryTemplateSelect))
func tableParamOptsToString(in ...table.ParameterOption) string {
var sb strings.Builder
for _, opt := range in {
sb.WriteString(opt.Name() + "(" + opt.Value().Type().String() + ");")
}
return sb.String()
}
// func tableParamOptsToString(in ...table.ParameterOption) string {
// var sb strings.Builder
// for _, opt := range in {
// sb.WriteString(opt.Name() + "(" + opt.Value().Type().String() + ");")
// }
// return sb.String()
// }