From 6b635de55dedbfc0e2116c095dd0f1d68af31484 Mon Sep 17 00:00:00 2001 From: cluttrdev Date: Wed, 22 Nov 2023 13:37:14 +0100 Subject: [PATCH] Add ClickHouseDataStore implementing the DataStore interface --- pkg/clickhouse/store.go | 75 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 pkg/clickhouse/store.go diff --git a/pkg/clickhouse/store.go b/pkg/clickhouse/store.go new file mode 100644 index 0000000..499fc1a --- /dev/null +++ b/pkg/clickhouse/store.go @@ -0,0 +1,75 @@ +package clickhouse + +import ( + "context" + "fmt" + "time" + + "github.com/cluttrdev/gitlab-clickhouse-exporter/pkg/models" +) + +type ClickHouseDataStore struct { + client *Client +} + +func NewClickHouseDataStore(c *Client) *ClickHouseDataStore { + return &ClickHouseDataStore{ + client: c, + } +} + +func (ds *ClickHouseDataStore) Initialize(ctx context.Context) error { + if err := ds.client.CreateDatabase(ctx); err != nil { + return fmt.Errorf("error creating database: %w", err) + } + + if err := ds.client.CreateTables(ctx); err != nil { + return fmt.Errorf("error creating tables: %w", err) + } + + return nil +} + +func (ds *ClickHouseDataStore) CheckReadiness(ctx context.Context) error { + return ds.client.CheckReadiness(ctx) +} + +func (ds *ClickHouseDataStore) InsertPipelines(ctx context.Context, pipelines []*models.Pipeline) error { + return InsertPipelines(ctx, pipelines, ds.client) +} + +func (ds *ClickHouseDataStore) InsertJobs(ctx context.Context, jobs []*models.Job) error { + return InsertJobs(ctx, jobs, ds.client) +} + +func (ds *ClickHouseDataStore) InsertSections(ctx context.Context, sections []*models.Section) error { + return InsertSections(ctx, sections, ds.client) +} + +func (ds *ClickHouseDataStore) InsertBridges(ctx context.Context, bridges []*models.Bridge) error { + return InsertBridges(ctx, bridges, ds.client) +} + +func (ds *ClickHouseDataStore) InsertPipelineHierarchy(ctx context.Context, hierarchy *models.PipelineHierarchy) error { + return InsertPipelineHierarchy(ctx, hierarchy, ds.client) +} + +func (ds *ClickHouseDataStore) InsertTraces(ctx context.Context, traces []models.Trace) error { + return InsertTraces(ctx, traces, ds.client) +} + +func (ds *ClickHouseDataStore) InsertTestReports(ctx context.Context, reports []*models.PipelineTestReport) error { + return InsertTestReports(ctx, reports, ds.client) +} + +func (ds *ClickHouseDataStore) InsertTestSuites(ctx context.Context, suites []*models.PipelineTestSuite) error { + return InsertTestSuites(ctx, suites, ds.client) +} + +func (ds *ClickHouseDataStore) InsertTestCases(ctx context.Context, cases []*models.PipelineTestCase) error { + return InsertTestCases(ctx, cases, ds.client) +} + +func (ds *ClickHouseDataStore) QueryProjectPipelinesLatestUpdate(ctx context.Context, projectID int64) (map[int64]time.Time, error) { + return ds.client.QueryProjectPipelinesLatestUpdate(ctx, projectID) +}