-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add interface to support different types of databases
- Loading branch information
Showing
40 changed files
with
645 additions
and
538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package empty | ||
|
||
import ( | ||
"context" | ||
"github.com/kava-labs/kava-proxy-service/clients/database" | ||
) | ||
|
||
type Empty struct{} | ||
|
||
func New() *Empty { | ||
return &Empty{} | ||
} | ||
|
||
func (e *Empty) SaveProxiedRequestMetric(ctx context.Context, metric *database.ProxiedRequestMetric) error { | ||
return nil | ||
} | ||
|
||
func (e *Empty) ListProxiedRequestMetricsWithPagination(ctx context.Context, cursor int64, limit int) ([]*database.ProxiedRequestMetric, int64, error) { | ||
return []*database.ProxiedRequestMetric{}, 0, nil | ||
} | ||
|
||
func (e *Empty) CountAttachedProxiedRequestMetricPartitions(ctx context.Context) (int64, error) { | ||
return 0, nil | ||
} | ||
|
||
func (e *Empty) GetLastCreatedAttachedProxiedRequestMetricsPartitionName(ctx context.Context) (string, error) { | ||
return "", nil | ||
} | ||
|
||
func (e *Empty) DeleteProxiedRequestMetricsOlderThanNDays(ctx context.Context, n int64) error { | ||
return nil | ||
} | ||
|
||
func (e *Empty) HealthCheck() error { | ||
return nil | ||
} | ||
|
||
func (e *Empty) Partition(prefillPeriodDays int) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
package database | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type MetricsDatabase interface { | ||
SaveProxiedRequestMetric(ctx context.Context, prm *ProxiedRequestMetric) error | ||
ListProxiedRequestMetricsWithPagination(ctx context.Context, cursor int64, limit int) ([]ProxiedRequestMetric, int64, error) | ||
SaveProxiedRequestMetric(ctx context.Context, metric *ProxiedRequestMetric) error | ||
ListProxiedRequestMetricsWithPagination(ctx context.Context, cursor int64, limit int) ([]*ProxiedRequestMetric, int64, error) | ||
CountAttachedProxiedRequestMetricPartitions(ctx context.Context) (int64, error) | ||
GetLastCreatedAttachedProxiedRequestMetricsPartitionName(ctx context.Context) (string, error) | ||
DeleteProxiedRequestMetricsOlderThanNDays(ctx context.Context, days int) error | ||
DeleteProxiedRequestMetricsOlderThanNDays(ctx context.Context, n int64) error | ||
|
||
HealthCheck() error | ||
Partition(prefillPeriodDays int) error | ||
} | ||
|
||
type ProxiedRequestMetric struct { | ||
ID int64 | ||
MethodName string | ||
BlockNumber *int64 | ||
ResponseLatencyMilliseconds int64 | ||
Hostname string | ||
RequestIP string | ||
RequestTime time.Time | ||
UserAgent *string | ||
Referer *string | ||
Origin *string | ||
ResponseBackend string | ||
ResponseBackendRoute string | ||
CacheHit bool | ||
PartOfBatch bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package postgres | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestDisabledDBCreation(t *testing.T) { | ||
config := DatabaseConfig{} | ||
_, err := NewClient(config) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestHealthcheckNoDatabase(t *testing.T) { | ||
config := DatabaseConfig{} | ||
_, err := NewClient(config) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/require" | ||
"github.com/uptrace/bun/migrate" | ||
"testing" | ||
) | ||
|
||
func TestMigrateNoDatabase(t *testing.T) { | ||
db := &Client{} | ||
|
||
_, err := db.Migrate(context.Background(), migrate.Migrations{}, nil) | ||
require.Error(t, err) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package postgres | ||
|
||
import ( | ||
"github.com/kava-labs/kava-proxy-service/config" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestUnitTestPartitionsForPeriodReturnsExpectedNumPartitionsWhenPrefillPeriodIsNotContainedInCurrentMonth(t *testing.T) { | ||
// prepare | ||
|
||
// pick a date in the middle of a month | ||
startFrom := time.Date(1989, 5, 20, 12, 0, 0, 0, time.UTC) | ||
|
||
// set prefill period to more then days remaining in month | ||
// from above date | ||
daysToPrefill := 21 | ||
|
||
// execute | ||
actualPartitionsForPeriod, err := PartitionsForPeriod(startFrom, daysToPrefill) | ||
|
||
// assert | ||
assert.Nil(t, err) | ||
assert.Equal(t, daysToPrefill, len(actualPartitionsForPeriod)) | ||
} | ||
|
||
func TestUnitTestPartitionsForPeriodReturnsErrWhenTooManyPrefillDays(t *testing.T) { | ||
// prepare | ||
daysToPrefill := config.MaxMetricPartitioningPrefillPeriodDays + 1 | ||
|
||
// execute | ||
_, err := PartitionsForPeriod(time.Now(), daysToPrefill) | ||
|
||
// assert | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestUnitTestPartitionsForPeriodReturnsExpectedNumPartitionsWhenPrefillPeriodIsContainedInCurrentMonth(t *testing.T) { | ||
// prepare | ||
|
||
// pick a date in the middle of a month | ||
startFrom := time.Date(1989, 5, 11, 12, 0, 0, 0, time.UTC) | ||
|
||
// set prefill period to less then days remaining in month | ||
// from above date | ||
daysToPrefill := 3 | ||
|
||
// execute | ||
actualPartitionsForPeriod, err := PartitionsForPeriod(startFrom, daysToPrefill) | ||
|
||
// assert | ||
assert.Nil(t, err) | ||
assert.Equal(t, daysToPrefill, len(actualPartitionsForPeriod)) | ||
} |
Oops, something went wrong.