utils

package
v0.0.0-...-98367f4 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// MutexDebugEnabled enables mutex debugging features globally
	MutexDebugEnabled = false

	// WarnAfterLockDelaySeconds is the time to wait before warning about potential deadlocks
	WarnAfterLockDelaySeconds = 30
)

Functions

func CleanupString

func CleanupString(s string) string

func DownloadWithProgress

func DownloadWithProgress(sourceURL, destination string, callback func(progress float32), resume bool) error

func ExtractCode

func ExtractCode(code string, re *regexp.Regexp) string

func ExtractGoCode

func ExtractGoCode(code string) string

func ExtractPythonCode

func ExtractPythonCode(code string) string

func ExtractPythonException

func ExtractPythonException(output string) string

func FileExists

func FileExists(path string) bool

func GenerateNestedSchemaForGET

func GenerateNestedSchemaForGET(v interface{}) *huma.Schema

GenerateNestedSchemaForGET generates a schema for a given struct excluding writeOnly fields. In contrast of GenerateSchemaForGET it serializes nested structures as well

func GenerateNestedSchemaForGETString

func GenerateNestedSchemaForGETString(v interface{}) string

The same as GenerateNestedSchemaForGET but returns string representation of JSON

func GenerateNestedSchemaForPOSTPUT

func GenerateNestedSchemaForPOSTPUT(v interface{}) *huma.Schema

GenerateNestedSchemaForPOSTPUT generates a schema for a given struct excluding readOnly fields. In contrast of GenerateSchemaForPOSTPUT it serializes nested structures as well

func GenerateNestedSchemaForPOSTPUTString

func GenerateNestedSchemaForPOSTPUTString(v interface{}) string

The same as GenerateNestedSchemaForPOSTPUT but returns string representation of JSON

func GenerateSchemaForGET

func GenerateSchemaForGET(v interface{}) *huma.Schema

GenerateSchemaForGET generates a schema for a given struct excluding writeOnly fields. It uses huma registry to possibly create references for nested structs. The schema that is returned can then be passed to `huma.Validate` to efficiently validate incoming requests.

func GenerateSchemaForGETString

func GenerateSchemaForGETString(v interface{}) string

The same as GenerateSchemaForGET but returns string representation of JSON

func GenerateSchemaForPOSTPUT

func GenerateSchemaForPOSTPUT(v interface{}) *huma.Schema

GenerateSchemaForPOSTPUT generates a schema for a given struct excluding readOnly fields. It uses huma registry to possibly create references for nested structs. The schema that is returned can then be passed to `huma.Validate` to efficiently validate incoming requests.

func GenerateSchemaForPOSTPUTString

func GenerateSchemaForPOSTPUTString(v interface{}) string

The same as GenerateSchemaForPOSTPUT but returns string representation of JSON

func GetFileChecksum

func GetFileChecksum(filename string) (string, error)

func GetFileSize

func GetFileSize(filename string) (int64, error)

func GetJsonFieldName

func GetJsonFieldName(v interface{}, fieldKey string) string

func InitStructWithDefaults

func InitStructWithDefaults(v interface{})

InitStructWithDefaults initializes a struct with default values from struct tags

func IsPipe

func IsPipe(file *os.File) bool

func MergeJSONWithDefaults

func MergeJSONWithDefaults(defaultPtr interface{}, inputJSON string) (string, error)

MergeJSONWithDefaults merges a given JSON string into the struct pointed to by defaultPtr, preserving existing default values for any missing fields. It also validates the merged JSON against the schema defined by defaultPtr using ValidateJSONAgainstSchemaOnPost. If inputJSON is empty, it returns the default struct as JSON. This function updates the value pointed to by defaultPtr with the merged result. It supports deep embedded structs and embedded structs within other embedded structs.

func NestedSchemaFromType

func NestedSchemaFromType(r huma.Registry, t reflect.Type) *huma.Schema

Similar to numa.SchemaFromType, but instead of referncing nested structs they are serialized.

func PrepareFilePath

func PrepareFilePath(filename string) error

func SaveUploadedTempFile

func SaveUploadedTempFile(file multipart.File, tempdir string, filename string) (string, error)

SaveUploadedTempFile saves the uploaded file to a temporary location and returns the path If tempdir is provided, it uses that directory; otherwise uses the system default temp directory

func SetMutexDeadlockWarningDelay

func SetMutexDeadlockWarningDelay(seconds int)

SetMutexDeadlockWarningDelay sets the delay before warning about potential deadlocks

func SetMutexDebug

func SetMutexDebug(enabled bool)

SetMutexDebug enables or disables mutex debugging globally

func StringToPrettyJSON

func StringToPrettyJSON(data string) string

StringToPrettyJSON converts a string to a formatted JSON object

func StructToJSONString

func StructToJSONString(v interface{}) (string, error)

func TemplateToText

func TemplateToText(tmpl string) (string, error)

func UnmarshalParquetFile

func UnmarshalParquetFile(filePath string, v interface{}) error

func ValidateJSONAgainstSchemaOnGet

func ValidateJSONAgainstSchemaOnGet(paramsBytes []byte, params interface{}) error

ValidateJSONAgainstSchemaOnGet validates the JSON against the schema for GET requests

func ValidateJSONAgainstSchemaOnPost

func ValidateJSONAgainstSchemaOnPost(paramsBytes []byte, params interface{}) error

Types

type DebugMutex

type DebugMutex struct {
	sync.Mutex
	// contains filtered or unexported fields
}

DebugMutex is a drop-in replacement for sync.Mutex with debugging capabilities

func (*DebugMutex) GetOwner

func (m *DebugMutex) GetOwner() int64

GetOwner returns the ID of the goroutine that currently holds the lock

func (*DebugMutex) IsLocked

func (m *DebugMutex) IsLocked() bool

IsLocked returns whether the mutex is currently locked

func (*DebugMutex) Lock

func (m *DebugMutex) Lock()

Lock acquires the mutex with debug capabilities

func (*DebugMutex) LockedAt

func (m *DebugMutex) LockedAt() time.Time

LockedAt returns when the mutex was locked

func (*DebugMutex) TryLock

func (m *DebugMutex) TryLock() bool

func (*DebugMutex) Unlock

func (m *DebugMutex) Unlock()

Unlock releases the mutex with debug capabilities

type ExecResult

type ExecResult struct {
	Stdout   []string
	Stderr   []string
	ExitCode int
	Duration time.Duration
}

func Exec

func Exec(ctx context.Context, args []string, cwd string) ExecResult

Exec runs a command with args in the given working directory, capturing stdout, stderr, exit code, and duration. It respects context for cancellation/timeouts.

type LineBuffer

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

func NewLineBuffer

func NewLineBuffer(ctx context.Context) *LineBuffer

NewLineBuffer creates a LineBuffer and starts a goroutine to scan written data.

func (*LineBuffer) Close

func (lb *LineBuffer) Close() error

func (*LineBuffer) CloseAndGetLines

func (lb *LineBuffer) CloseAndGetLines() []string

func (*LineBuffer) Write

func (lb *LineBuffer) Write(p []byte) (int, error)

Write writes data to the internal pipe for scanning.

type ProgressTracker

type ProgressTracker struct {
	Total                 int64
	Current               int64
	LastLog               int
	URL                   string
	Dest                  string
	Callback              func(progress float32)
	LastLogTime           time.Time
	ProgressThresholdMsec int64
	// contains filtered or unexported fields
}

func (*ProgressTracker) Close

func (pt *ProgressTracker) Close() error

ProgressTracker has to implement ReadCloser interface

func (*ProgressTracker) Read

func (pt *ProgressTracker) Read(p []byte) (int, error)

ProgressTracker has to implement ReadCloser interface

func (*ProgressTracker) TrackProgress

func (pt *ProgressTracker) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser

type SystemInfo

type SystemInfo struct {
	ID          uuid.UUID `json:"id" gorm:"primaryKey"`
	CreatedAtMs int64     `json:"created_at" gorm:"index;type:bigint" doc:"unix timestamp in milliseconds"`

	// OS information
	OS struct {
		Name    string `json:"name" gorm:"column:os_name"`
		Version string `json:"version" gorm:"column:os_version"`
		Arch    string `json:"arch" gorm:"column:os_arch"`
	} `json:"os" gorm:"embedded"`

	// CPU information
	CPU struct {
		Model     string  `json:"model" gorm:"column:cpu_model"`
		NumCPUs   int     `json:"num_cpus" gorm:"column:cpu_num_cpus"`
		Cores     int     `json:"cores" gorm:"column:cpu_cores"`
		Frequency float64 `json:"frequency_mhz" gorm:"column:cpu_frequency_mhz"`
	} `json:"cpu" gorm:"embedded"`

	// GPU information
	GPUs []struct {
		Model         string  `json:"model" gorm:"column:gpu_model"`
		Cores         int     `json:"cores" gorm:"column:gpu_cores"`
		TotalMemoryMB float64 `json:"total_memory_mb" gorm:"column:gpu_total_memory_mb"`
		UsedMemoryMB  float64 `json:"used_memory_mb" gorm:"column:gpu_used_memory_mb"`
	} `json:"gpus" gorm:"serializer:json"`

	// Memory information
	Memory struct {
		Total     uint64  `json:"total_bytes" gorm:"column:memory_total_bytes"`
		Available uint64  `json:"available_bytes" gorm:"column:memory_available_bytes"`
		Used      uint64  `json:"used_bytes" gorm:"column:memory_used_bytes"`
		UsedPerc  float64 `json:"used_percent" gorm:"column:memory_used_percent"`
	} `json:"memory" gorm:"embedded"`

	// Host information
	Host struct {
		Hostname string `json:"hostname" gorm:"column:host_hostname"`
		Uptime   uint64 `json:"uptime_sec" gorm:"column:host_uptime_sec"`
	} `json:"host" gorm:"embedded"`

	// Battery information
	Battery struct {
		OnBattery  bool `json:"on_battery" gorm:"column:on_battery"`
		Percentage int  `json:"percentage" gorm:"column:battery_percentage"`
	} `json:"battery" gorm:"embedded"`
}

SystemInfo represents system information

func CollectSysInfo

func CollectSysInfo() (*SystemInfo, error)

Collect gathers system information

func (*SystemInfo) FormatString

func (si *SystemInfo) FormatString() string

FormatString returns a human-readable string representation of system information

func (*SystemInfo) ToMap

func (si *SystemInfo) ToMap() map[string]string

ToMap converts system information to a map[string]string

Jump to

Keyboard shortcuts

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