srvc

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: AGPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CacheDir is the standard Linux cache directory for proglv
	CacheDir = "/var/cache/proglv/testfiles"

	// MaxCacheSize is the maximum cache size in bytes (10GB)
	MaxCacheSize = 10 * 1024 * 1024 * 1024

	// MaxConcurrentDownloads limits parallel downloads
	MaxConcurrentDownloads = 10
)
View Source
const ErrCodeFailedToGetTaskFromDb = "failed_to_get_task_from_db"
View Source
const ErrCodeGetImageWidthAndHeight = "image_width_and_height"
View Source
const ErrCodeImageAlreadyExists = "image_already_exists"
View Source
const ErrCodeImageFileExtFromMimeType = "image_file_ext_from_mime_type"
View Source
const ErrCodeImageInadequateDimensions = "image_inadequate_dimensions"
View Source
const ErrCodeImageNotFound = "image_not_found"
View Source
const ErrCodeInternalServerError = "internal_server_error"
View Source
const ErrCodeInvalidTaskZip = "invalid_task_zip"
View Source
const ErrCodeTaskAlreadyExists = "task_already_exists"
View Source
const ErrCodeTaskNotFound = "task_not_found"
View Source
const ErrCodeUnsupportedTaskZip = "unsupported_task_zip"

Variables

View Source
var ErrSomeTaskNotFound = srvcerror.New(
	"some_task_not_found",
	"kāds no uzdevumiem netika atrasts",
).SetHttpStatusCode(http.StatusNotFound)

Functions

func CompressWithZstd

func CompressWithZstd(data []byte) ([]byte, error)

CompressWithZstd compresses the given data using Zstandard compression. It returns the compressed data or an error if the compression fails.

func DecompressWithZstd

func DecompressWithZstd(compressedData []byte) ([]byte, error)

DecompressWithZstd decompresses data that was compressed with Zstandard. It returns the decompressed data or an error if the decompression fails.

func GetTestfileS3Key

func GetTestfileS3Key(body []byte) string

func MimeFromFname

func MimeFromFname(fname string) (string, error)

func MimeToFileExt

func MimeToFileExt(mimeType string) (string, error)

func NewErrorFailedToGetTaskFromDb

func NewErrorFailedToGetTaskFromDb(taskId string) srvcerror.E

func NewErrorGetImageWidthAndHeight

func NewErrorGetImageWidthAndHeight() srvcerror.E

func NewErrorImageAlreadyExists

func NewErrorImageAlreadyExists(filename string) srvcerror.E

func NewErrorImageFileExtFromMimeType

func NewErrorImageFileExtFromMimeType(mime string) srvcerror.E

func NewErrorImageInadequateDimensions

func NewErrorImageInadequateDimensions() srvcerror.E

func NewErrorImageNotFound

func NewErrorImageNotFound(filename string) srvcerror.E

func NewErrorInternalServerError

func NewErrorInternalServerError() srvcerror.E

func NewErrorInvalidTaskZip

func NewErrorInvalidTaskZip(reason string) srvcerror.E

func NewErrorTaskAlreadyExists

func NewErrorTaskAlreadyExists(taskId string) srvcerror.E

func NewErrorTaskNotFound

func NewErrorTaskNotFound(taskId string) srvcerror.E

func NewErrorUnsupportedTaskZip

func NewErrorUnsupportedTaskZip(reason string) srvcerror.E

func NewTaskSrvc

func NewTaskSrvc(
	repo TaskPgRepo,
	publicStore, testfileStore ObjectStore,
	opts ...TaskSrvcOption,
) *taskSrvc

func Sha2Hex

func Sha2Hex(body []byte) (sha2 string)

Types

type CacheEntry

type CacheEntry struct {
	Path     string
	Size     int64
	LastUsed time.Time
}

CacheEntry represents a cached file with metadata

type Example

type Example struct {
	Input  string
	Output string
	MdNote map[string]string
}

type IllustrationImage

type IllustrationImage struct {
	S3Key     string
	WidthPx   int
	HeightPx  int
	SzInBytes int
}

type MarkdownStatement

type MarkdownStatement struct {
	LangIso639 string // primary key of the statement of task

	Story   string
	Input   string
	Output  string
	Notes   string
	Scoring string
	Talk    string // communication in interactive tasks
	Example string // example in interactive tasks
}

type ObjectStore

type ObjectStore interface {
	Upload(content []byte, key string, mediaType string) (string, error)
	Download(key string) ([]byte, error)
	Exists(key string) (bool, error)
	Delete(key string) error
}

type OriginNote

type OriginNote struct {
	Lang string
	Info string
}

OriginNote represents origin notes with language and information.

type Solution

type Solution struct {
	Fname    string
	Content  string
	Subtasks []int // which subtasks should it solve
}

type StatementImage

type StatementImage struct {
	S3Key     string // e.g. <task_id>/<sha256-prefix>.png; md-images/ is implicit
	Filename  string // filename of the image, e.g., nekoks.png
	WidthPx   int    // og width [px] stored in s3
	HeightPx  int    // og height [px] stored in s3
	SzInBytes int    // size in bytes
}

type Subtask

type Subtask struct {
	Score   int
	TestIDs []int

	Descriptions map[string]string
}

type Task

type Task struct {
	// url slug friendly identifier
	ShortId string

	// full name of the task in multiple languages (key: ISO 639 code)
	FullName map[string]string

	// original language of the task (ISO 639 code), may be empty
	OrigLang string

	// markdown with todos, notes, etc.
	Readme string

	// illustration image
	IllustrImg *IllustrationImage

	// constraints
	MemLimMegabytes int
	CpuTimeLimSecs  float64

	// metadata (origin, difficulty)
	OriginOlympiad   string
	OriginNotes      []OriginNote
	DifficultyRating int
	OriginOrg        string
	OriginYear       string
	OlympStage       string
	OriginDivisions  []string

	// statement
	MdStatements   []MarkdownStatement
	MdImages       []StatementImage
	VisInpSubtasks []VisibleInputSubtask
	Examples       []Example
	Subtasks       []Subtask

	// testing
	Tests      []Test
	Checker    string
	Interactor string

	// scoring
	TestGroups []TestGroup

	// metadata: authors (free-form names)
	Authors []string

	// metadata: problem tags (free-form short labels)
	ProblemTags []string

	// original full archive S3 key (optional)
	OgFilesZipS3Key string

	Solutions []Solution
}

func (*Task) CheckerPtr

func (t *Task) CheckerPtr() *string

func (*Task) CpuMillis

func (t *Task) CpuMillis() int

func (*Task) DefaultFullName

func (t *Task) DefaultFullName() string

DefaultFullName returns the preferred full name, prioritizing OrigLang, then 'lv', then any available name.

func (*Task) FindTestgroupSubtasks

func (t *Task) FindTestgroupSubtasks(testGroupId int) []int

func (*Task) InteractorPtr

func (t *Task) InteractorPtr() *string

func (*Task) MemoryKiB

func (t *Task) MemoryKiB() int

type TaskEvalTestGroupInformation

type TaskEvalTestGroupInformation struct {
	TestGroupID int
	Score       int
	Subtask     int
}

type TaskPgRepo

type TaskPgRepo interface {
	GetTask(ctx context.Context, shortId string) (Task, error)
	GetTaskPreview(ctx context.Context, shortId string) (TaskPreview, error)
	SearchTasksByName(ctx context.Context, name string) ([]string, error)
	ListTasks(ctx context.Context, limit int, offset int) ([]Task, error)
	ListTaskPreviews(ctx context.Context, limit int, offset int) ([]TaskPreview, error)
	ResolveNames(ctx context.Context, shortIds []string) ([]string, error)
	Exists(ctx context.Context, shortId string) (bool, error)
	CreateTask(ctx context.Context, task Task) error
	DeleteTask(ctx context.Context, shortId string) error
	UpdateStatement(ctx context.Context, taskId string, statement MarkdownStatement) error
	AddStatementImg(ctx context.Context, taskId string, img StatementImage) error
	DeleteStatementImg(ctx context.Context, taskId string, filename string) error
	UpdateIllustrationImg(ctx context.Context, taskId string, img IllustrationImage) error
}

type TaskPreview

type TaskPreview struct {
	ShortId string

	FullName map[string]string
	OrigLang string

	IllustrImg *IllustrationImage

	DifficultyRating int

	OriginOlympiad  string
	OriginNote      string
	OriginNoteShort string
	OriginOrg       string
	OriginYear      string
	OlympStage      string
	OriginDivisions []string

	MdStatementStory string
}

func (*TaskPreview) DefaultFullName

func (t *TaskPreview) DefaultFullName() string

DefaultFullName returns the preferred full name for TaskPreview.

type TaskService

type TaskService interface {
	GetTask(ctx context.Context, shortId string) (Task, srvcerror.E)
	CreateTask(ctx context.Context, task Task) srvcerror.E
	DeleteTask(ctx context.Context, shortId string) srvcerror.E

	// test files
	UploadTestFile(ctx context.Context, body []byte) srvcerror.E
	GetTestDownlUrl(ctx context.Context, testFileSha256 string) (string, srvcerror.E)
	DownloadTestFile(ctx context.Context, testFileSha256 string) ([]byte, srvcerror.E)

	// illustration image
	UploadIllustrationImg(ctx context.Context, mimeType string, body []byte) (string, srvcerror.E)
	DeleteIllustrationImg(ctx context.Context, taskId string) srvcerror.E
	UpdateIllustrationImg(ctx context.Context, taskId string, img IllustrationImage) srvcerror.E
	GetHttpUrlForIllustrImg(ctx context.Context, illstrImgS3Key string) (string, srvcerror.E)

	// markdown statement
	UpdateStatementMd(ctx context.Context, taskId string, statement MarkdownStatement) srvcerror.E

	// markdown statement image
	UploadStatementImage(ctx context.Context, taskId string, filename string, mimeType string, body []byte) (string, srvcerror.E)
	DeleteStatementImage(ctx context.Context, taskId string, filename string) srvcerror.E
	GetHttpUrlForStatementImage(ctx context.Context, statementImageS3Key string) (string, srvcerror.E)

	// website
	GetTaskPreview(ctx context.Context, shortId string) (TaskPreview, srvcerror.E)
	ListTaskPreviews(ctx context.Context) ([]TaskPreview, srvcerror.E)

	// taskzip archive format
	ImportTaskFromZip(ctx context.Context, zipBytes []byte, overrideId string) (string, srvcerror.E)
	ExportTaskAsZip(ctx context.Context, taskId string) ([]byte, srvcerror.E)

	// unorganized
	GetTaskFullNames(ctx context.Context, shortIds []string) ([]string, srvcerror.E)
	ResolveNames(ctx context.Context, shortIds []string) ([]string, srvcerror.E)
	SearchTasksByName(ctx context.Context, name string) ([]string, srvcerror.E)
}

type TaskSrvcOption

type TaskSrvcOption func(*taskSrvc)

func WithPublicAPIBaseURL

func WithPublicAPIBaseURL(baseURL string) TaskSrvcOption

func WithTestfileDownloadSigningKey

func WithTestfileDownloadSigningKey(key []byte) TaskSrvcOption

type Test

type Test struct {
	InpSha2 string
	AnsSha2 string
}

type TestFileCache

type TestFileCache struct {
	// contains filtered or unexported fields
}

TestFileCache manages cached test files with size limits and LRU eviction

func NewTestFileCache

func NewTestFileCache() *TestFileCache

NewTestFileCache creates a new test file cache

func (*TestFileCache) GetCacheStats

func (c *TestFileCache) GetCacheStats() (totalSize int64, fileCount int, err error)

GetCacheStats returns cache statistics

func (*TestFileCache) GetTestFile

func (c *TestFileCache) GetTestFile(ctx context.Context, sha256Hash string, downloadURL string, logger *slog.Logger) ([]byte, error)

GetTestFile returns decompressed test file content from cache or downloads it

type TestGroup

type TestGroup struct {
	Points int
	Public bool
	// Subtask int
	TestIDs []int
}

TestGroup represents a group of tests within a task.

type VisInpSubtaskTest

type VisInpSubtaskTest struct {
	TestId int
	Input  string
}

type VisibleInputSubtask

type VisibleInputSubtask struct {
	SubtaskId int
	Tests     []VisInpSubtaskTest
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL