database

package
v1.0.2 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: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTokenExpired  = errors.New("token is expired")
	ErrTokenNotFound = errors.New("token not found")
)

Functions

func NewConnection

func NewConnection(config *gormpg.Conf) (*gorm.DB, error)

NewConnection creates a new database connection

func Seed

func Seed(db *gorm.DB) error

Types

type AuthzFilterApplier

type AuthzFilterApplier func(scope *auth.IdentityScope, db *gorm.DB) *gorm.DB

type FilterFieldApplier

type FilterFieldApplier func(db *gorm.DB, vv []string) (*gorm.DB, error)

func ParserInFilterFieldApplier

func ParserInFilterFieldApplier[T any](f string, t func(string) (T, error)) FilterFieldApplier

func StringInFilterFieldApplier

func StringInFilterFieldApplier(f string) FilterFieldApplier

type GormAgentRepository

type GormAgentRepository struct {
	*GormRepository[domain.Agent]
}

func NewAgentRepository

func NewAgentRepository(db *gorm.DB) *GormAgentRepository

NewAgentRepository creates a new instance of AgentRepository

func (*GormAgentRepository) AuthScope

AuthScope returns the auth scope for the agent

func (*GormAgentRepository) CountByProvider

func (r *GormAgentRepository) CountByProvider(ctx context.Context, providerID properties.UUID) (int64, error)

func (*GormAgentRepository) FindByServiceTypeAndTags

func (r *GormAgentRepository) FindByServiceTypeAndTags(ctx context.Context, serviceTypeID properties.UUID, tags []string) ([]*domain.Agent, error)

func (*GormAgentRepository) MarkInactiveAgentsAsDisconnected

func (r *GormAgentRepository) MarkInactiveAgentsAsDisconnected(ctx context.Context, inactiveDuration time.Duration) (int64, error)

type GormAgentTypeRepository

type GormAgentTypeRepository struct {
	*GormRepository[domain.AgentType]
}

func NewAgentTypeRepository

func NewAgentTypeRepository(db *gorm.DB) *GormAgentTypeRepository

NewAgentTypeRepository creates a new instance of AgentTypeRepository

func (*GormAgentTypeRepository) AuthScope

AuthScope returns the auth scope for the agent type

func (*GormAgentTypeRepository) Count

Count returns the total number of agent types

type GormEventRepository

type GormEventRepository struct {
	*GormRepository[domain.Event]
}

func NewEventRepository

func NewEventRepository(db *gorm.DB) *GormEventRepository

NewEventRepository creates a new instance of EventRepository

func (*GormEventRepository) AuthScope

func (*GormEventRepository) ListFromSequence

func (r *GormEventRepository) ListFromSequence(ctx context.Context, fromSequenceNumber int64, limit int) ([]*domain.Event, error)

ListFromSequence retrieves events starting from a specific sequence number

func (*GormEventRepository) ServiceUptime

func (r *GormEventRepository) ServiceUptime(ctx context.Context, serviceID properties.UUID, start time.Time, end time.Time) (uptimeSeconds uint64, downtimeSeconds uint64, err error)

ServiceUptime returns the uptime and downtime in seconds of a service in a given time range It streams service transition events and calculates uptime progressively using a result set

type GormEventSubscriptionRepository

type GormEventSubscriptionRepository struct {
	*GormRepository[domain.EventSubscription]
}

func NewEventSubscriptionRepository

func NewEventSubscriptionRepository(db *gorm.DB) *GormEventSubscriptionRepository

NewEventSubscriptionRepository creates a new instance of EventSubscriptionRepository

func (*GormEventSubscriptionRepository) AuthScope

AuthScope returns the auth scope for the event subscription

func (*GormEventSubscriptionRepository) DeleteBySubscriberID

func (r *GormEventSubscriptionRepository) DeleteBySubscriberID(ctx context.Context, subscriberID string) error

DeleteBySubscriberID removes an event subscription by subscriber ID

func (*GormEventSubscriptionRepository) ExistsBySubscriberID

func (r *GormEventSubscriptionRepository) ExistsBySubscriberID(ctx context.Context, subscriberID string) (bool, error)

ExistsBySubscriberID checks if an event subscription exists by subscriber ID

func (*GormEventSubscriptionRepository) FindBySubscriberID

func (r *GormEventSubscriptionRepository) FindBySubscriberID(ctx context.Context, subscriberID string) (*domain.EventSubscription, error)

FindBySubscriberID retrieves an event subscription by subscriber ID

func (*GormEventSubscriptionRepository) ListExpiredLeases

ListExpiredLeases retrieves subscriptions with expired leases

type GormJobRepository

type GormJobRepository struct {
	*GormRepository[domain.Job]
}

func NewJobRepository

func NewJobRepository(db *gorm.DB) *GormJobRepository

NewJobRepository creates a new instance of JobRepository

func (*GormJobRepository) AuthScope

func (*GormJobRepository) DeleteOldCompletedJobs

func (r *GormJobRepository) DeleteOldCompletedJobs(ctx context.Context, olderThan time.Duration) (int, error)

DeleteOldCompletedJobs removes completed or failed jobs older than the specified days

func (*GormJobRepository) GetPendingJobsForAgent

func (r *GormJobRepository) GetPendingJobsForAgent(ctx context.Context, agentID properties.UUID, limit int) ([]*domain.Job, error)

GetPendingJobsForAgent retrieves pending jobs targeted for a specific agent Returns only one pending job per service group with the highest priority Excludes service groups that have any jobs currently in processing status

func (*GormJobRepository) GetTimeOutJobs

func (r *GormJobRepository) GetTimeOutJobs(ctx context.Context, olderThan time.Duration) ([]*domain.Job, error)

GetTimeOutJobs retrieves jobs that have been processing for too long and returns them

type GormMetricEntryRepository

type GormMetricEntryRepository struct {
	*GormRepository[domain.MetricEntry]
}

func NewMetricEntryRepository

func NewMetricEntryRepository(db *gorm.DB) *GormMetricEntryRepository

NewMetricEntryRepository creates a new instance of MetricEntryRepository

func (*GormMetricEntryRepository) Aggregate

func (r *GormMetricEntryRepository) Aggregate(ctx context.Context, aggregateType domain.AggregateType, serviceID properties.UUID, typeID properties.UUID, start time.Time, end time.Time) (float64, error)

Aggregate performs aggregation operations on metric entries for a specific metric type and service within a time range

func (*GormMetricEntryRepository) AuthScope

func (*GormMetricEntryRepository) CountByMetricType

func (r *GormMetricEntryRepository) CountByMetricType(ctx context.Context, typeID properties.UUID) (int64, error)

CountByMetricType counts the number of entries for a specific metric type

type GormMetricTypeRepository

type GormMetricTypeRepository struct {
	*GormRepository[domain.MetricType]
}

func NewMetricTypeRepository

func NewMetricTypeRepository(db *gorm.DB) *GormMetricTypeRepository

NewMetricTypeRepository creates a new instance of MetricTypeRepository

func (*GormMetricTypeRepository) AuthScope

func (*GormMetricTypeRepository) FindByName

func (r *GormMetricTypeRepository) FindByName(ctx context.Context, name string) (*domain.MetricType, error)

FindByName retrieves a metric type by name

type GormParticipantRepository

type GormParticipantRepository struct {
	*GormRepository[domain.Participant]
}

func NewParticipantRepository

func NewParticipantRepository(db *gorm.DB) *GormParticipantRepository

NewParticipantRepository creates a new instance of ParticipantRepository

func (*GormParticipantRepository) AuthScope

AuthScope returns the auth scope for the participant

type GormRepository

type GormRepository[T Tabler] struct {
	// contains filtered or unexported fields
}

GormRepository provides a base implementation of Repository using GORM

func NewGormRepository

func NewGormRepository[T Tabler](
	db *gorm.DB,
	filterApplier PageFilterApplier,
	sortApplier PageFilterApplier,
	authzFilterApplier AuthzFilterApplier,
	findPreloadPaths []string,
	listPreloadPaths []string,
) *GormRepository[T]

NewGormRepository creates a new instance of GormRepository

func (*GormRepository[T]) AuthScopeByFields

func (r *GormRepository[T]) AuthScopeByFields(ctx context.Context, id properties.UUID, scopeFields ...string) (auth.ObjectScope, error)

AuthScopeByFields retrieves auth scope for an entity with specified scope fields

func (*GormRepository[T]) Count

func (r *GormRepository[T]) Count(ctx context.Context) (int64, error)

func (*GormRepository[T]) Create

func (r *GormRepository[T]) Create(ctx context.Context, entity *T) error

func (*GormRepository[T]) Delete

func (r *GormRepository[T]) Delete(ctx context.Context, id properties.UUID) error

func (*GormRepository[T]) Exists

func (r *GormRepository[T]) Exists(ctx context.Context, id properties.UUID) (bool, error)

func (*GormRepository[T]) Get

func (r *GormRepository[T]) Get(ctx context.Context, id properties.UUID) (*T, error)

func (*GormRepository[T]) List

func (r *GormRepository[T]) List(ctx context.Context, authIdentityScope *auth.IdentityScope, page *domain.PageReq) (*domain.PageRes[T], error)

func (*GormRepository[T]) Save

func (r *GormRepository[T]) Save(ctx context.Context, entity *T) error

type GormServiceGroupRepository

type GormServiceGroupRepository struct {
	*GormRepository[domain.ServiceGroup]
}

func NewServiceGroupRepository

func NewServiceGroupRepository(db *gorm.DB) *GormServiceGroupRepository

NewServiceGroupRepository creates a new instance of ServiceGroupRepository

func (*GormServiceGroupRepository) AuthScope

func (*GormServiceGroupRepository) CountByService

func (r *GormServiceGroupRepository) CountByService(ctx context.Context, serviceID properties.UUID) (int64, error)

CountByService returns the number of service groups associated with a specific service

type GormServiceRepository

type GormServiceRepository struct {
	*GormRepository[domain.Service]
}

func NewServiceRepository

func NewServiceRepository(db *gorm.DB) *GormServiceRepository

NewServiceRepository creates a new instance of ServiceRepository

func (*GormServiceRepository) AuthScope

func (*GormServiceRepository) CountByAgent

func (r *GormServiceRepository) CountByAgent(ctx context.Context, agentID properties.UUID) (int64, error)

func (*GormServiceRepository) CountByGroup

func (r *GormServiceRepository) CountByGroup(ctx context.Context, groupID properties.UUID) (int64, error)

func (*GormServiceRepository) FindByExternalID

func (r *GormServiceRepository) FindByExternalID(ctx context.Context, agentID properties.UUID, externalID string) (*domain.Service, error)

FindByExternalID retrieves a service by its external ID and agent ID

type GormServiceTypeRepository

type GormServiceTypeRepository struct {
	*GormRepository[domain.ServiceType]
}

func NewServiceTypeRepository

func NewServiceTypeRepository(db *gorm.DB) *GormServiceTypeRepository

NewServiceTypeRepository creates a new instance of ServiceTypeRepository

func (*GormServiceTypeRepository) AuthScope

AuthScope returns the auth scope for the service type

func (*GormServiceTypeRepository) Count

Count returns the total number of service types

type GormStore

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

GormStore implements the domain.Store interface using GORM

func NewGormStore

func NewGormStore(db *gorm.DB) *GormStore

NewGormStore creates a new GormStore instance

func (*GormStore) AgentRepo

func (s *GormStore) AgentRepo() domain.AgentRepository

func (*GormStore) AgentTypeRepo

func (s *GormStore) AgentTypeRepo() domain.AgentTypeRepository

func (*GormStore) Atomic

func (s *GormStore) Atomic(ctx context.Context, fn func(domain.Store) error) error

Atomic executes the given function within a transaction

func (*GormStore) EventRepo

func (s *GormStore) EventRepo() domain.EventRepository

func (*GormStore) EventSubscriptionRepo

func (s *GormStore) EventSubscriptionRepo() domain.EventSubscriptionRepository

func (*GormStore) JobRepo

func (s *GormStore) JobRepo() domain.JobRepository

func (*GormStore) MetricEntryRepo

func (s *GormStore) MetricEntryRepo() domain.MetricEntryRepository

func (*GormStore) MetricTypeRepo

func (s *GormStore) MetricTypeRepo() domain.MetricTypeRepository

func (*GormStore) ParticipantRepo

func (s *GormStore) ParticipantRepo() domain.ParticipantRepository

func (*GormStore) ServiceGroupRepo

func (s *GormStore) ServiceGroupRepo() domain.ServiceGroupRepository

func (*GormStore) ServiceRepo

func (s *GormStore) ServiceRepo() domain.ServiceRepository

func (*GormStore) ServiceTypeRepo

func (s *GormStore) ServiceTypeRepo() domain.ServiceTypeRepository

func (*GormStore) TokenRepo

func (s *GormStore) TokenRepo() domain.TokenRepository

type GormTokenAuthenticator

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

GormTokenAuthenticator implements domain.Authenticator using GORM database

func NewTokenAuthenticator

func NewTokenAuthenticator(store domain.Store) *GormTokenAuthenticator

NewTokenAuthenticator creates a new token authenticator

func (*GormTokenAuthenticator) Authenticate

func (a *GormTokenAuthenticator) Authenticate(ctx context.Context, tokenValue string) (*auth.Identity, error)

Authenticate extracts and validates the token from the HTTP request Returns nil if authentication fails

func (*GormTokenAuthenticator) Health

Health checks if the token authenticator dependencies are healthy

type GormTokenRepository

type GormTokenRepository struct {
	*GormRepository[domain.Token]
}

func NewTokenRepository

func NewTokenRepository(db *gorm.DB) *GormTokenRepository

NewTokenRepository creates a new instance of TokenRepository

func (*GormTokenRepository) AuthScope

AuthScope returns the auth scope for the token

func (*GormTokenRepository) DeleteByAgentID

func (r *GormTokenRepository) DeleteByAgentID(ctx context.Context, agentID properties.UUID) error

DeleteByAgentID removes all tokens associated with an agent ID

func (*GormTokenRepository) DeleteByParticipantID

func (r *GormTokenRepository) DeleteByParticipantID(ctx context.Context, participantID properties.UUID) error

DeleteByParticipantID removes all tokens associated with a participant ID

func (*GormTokenRepository) FindByHashedValue

func (r *GormTokenRepository) FindByHashedValue(ctx context.Context, hashedValue string) (*domain.Token, error)

FindByHashedValue finds a token by its hashed value

type PageFilterApplier

type PageFilterApplier func(db *gorm.DB, r *domain.PageReq) (*gorm.DB, error)

func MapFilterApplier

func MapFilterApplier(fields map[string]FilterFieldApplier) PageFilterApplier

func MapSortApplier

func MapSortApplier(fields map[string]string) PageFilterApplier

type Tabler

type Tabler interface {
	TableName() string
}

type TestDB

type TestDB struct {
	DB     *gorm.DB
	DBName string
}

TestDB contains the database connection and utility functions for tests

func NewTestDB

func NewTestDB(t *testing.T) *TestDB

NewTestDB creates a new instance of TestDB

func (*TestDB) Cleanup

func (tdb *TestDB) Cleanup(t *testing.T)

Cleanup removes the test database

Jump to

Keyboard shortcuts

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