Skip to content

Commit

Permalink
feat: add credit datamodel and methods (#194)
Browse files Browse the repository at this point in the history
Because

- We need to manipulate Instill credit (add, subtract, check remaining)

This commit

- Adds credit datamodel
- Completes INS-4083 add credit
- Completes INS-4082 get remaining credit
- Completes INS-4084 subtract credit
  • Loading branch information
jvallesm authored Apr 11, 2024
1 parent 2698e8b commit a220469
Show file tree
Hide file tree
Showing 16 changed files with 615 additions and 161 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ PUBLIC_SERVICE_PORT=8084
# container build
DOCKER_BUILDKIT=1
COMPOSE_DOCKER_CLI_BUILD=1

# test
TEST_DBHOST=localhost
TEST_DBNAME=mgmt_test
16 changes: 14 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ jobs:
permissions:
contents: read
pull-requests: write
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: password
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -26,11 +38,11 @@ jobs:
- name: Generate coverage report
run: |
go mod tidy
go test -race ./... -coverprofile=coverage.txt -covermode=atomic
make coverage DBTEST=true
- name: Upload coverage report
uses: codecov/codecov-action@v2
with:
file: ./coverage.txt
file: ./coverage.out
flags: unittests
name: codecov-umbrella
25 changes: 19 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
include .env
export

GOTEST_FLAGS := CFG_DATABASE_HOST=${TEST_DBHOST} CFG_DATABASE_NAME=${TEST_DBNAME}
ifeq (${DBTEST}, true)
GOTEST_TAGS := -tags=dbtest
endif

#============================================================================

.PHONY: dev
Expand Down Expand Up @@ -51,12 +56,20 @@ build: ## Build dev docker image
go-gen: ## Generate codes
go generate ./...

.PHONY: unit-test
unit-test: ## Run unit test
@go test -v -race -coverpkg=./... -coverprofile=coverage.out ./...
@go tool cover -func=coverage.out
@go tool cover -html=coverage.out
@rm coverage.out

.PHONY: dbtest-pre
dbtest-pre:
@${GOTEST_FLAGS} go run ./cmd/migration

.PHONY: coverage
coverage:
@if [ "${DBTEST}" = "true" ]; then make dbtest-pre; fi
@${GOTEST_FLAGS} go test -v -race ${GOTEST_TAGS} -coverpkg=./... -coverprofile=coverage.out -covermode=atomic ./...
@if [ "${HTML}" = "true" ]; then \
go tool cover -func=coverage.out && \
go tool cover -html=coverage.out && \
rm coverage.out; \
fi

.PHONY: integration-test
integration-test: ## Run integration test
Expand Down
2 changes: 1 addition & 1 deletion cmd/init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func createDefaultUser(ctx context.Context, db *gorm.DB) error {
}

func main() {
if err := config.Init(); err != nil {
if err := config.Init(config.ParseConfigFlag()); err != nil {
log.Fatal(err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func grpcHandlerFunc(grpcServer *grpc.Server, gwHandler http.Handler) http.Handl
}

func main() {
if err := config.Init(); err != nil {
if err := config.Init(config.ParseConfigFlag()); err != nil {
log.Fatal(err.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/migration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func checkExist(databaseConfig config.DatabaseConfig) error {
func main() {
migrateFolder, _ := os.Getwd()

if err := config.Init(); err != nil {
if err := config.Init(config.ParseConfigFlag()); err != nil {
panic(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func initTemporalNamespace(ctx context.Context, client client.Client) {

func main() {

if err := config.Init(); err != nil {
if err := config.Init(config.ParseConfigFlag()); err != nil {
log.Fatal(err.Error())
}

Expand Down
22 changes: 15 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type LogConfig struct {
}

// Init - Assign global config to decoded config struct
func Init() error {
func Init(filePath string) error {
k := koanf.New(".")
parser := yaml.Parser()

Expand All @@ -145,11 +145,7 @@ func Init() error {
log.Fatal(err.Error())
}

fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fileRelativePath := fs.String("file", "config/config.yaml", "configuration file")
flag.Parse()

if err := k.Load(file.Provider(*fileRelativePath), parser); err != nil {
if err := k.Load(file.Provider(filePath), parser); err != nil {
log.Fatal(err.Error())
}

Expand All @@ -171,6 +167,18 @@ func Init() error {
}

// ValidateConfig is for custom validation rules for the configuration
func ValidateConfig(cfg *AppConfig) error {
func ValidateConfig(_ *AppConfig) error {
return nil
}

var defaultConfigPath = "config/config.yaml"

// ParseConfigFlag allows clients to specify the relative path to the file from
// which the configuration will be loaded.
func ParseConfigFlag() string {
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
configPath := fs.String("file", defaultConfigPath, "configuration file")
flag.Parse()

return *configPath
}
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ database:
host: pg-sql
port: 5432
name: mgmt
version: 5
version: 6
timezone: Etc/UTC
pool:
idleconnections: 5
Expand Down
95 changes: 51 additions & 44 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,69 +1,74 @@
module github.com/instill-ai/mgmt-backend

go 1.21.1
go 1.21.3

toolchain go1.21.3
toolchain go1.21.5

retract v0.3.2 // Published accidentally.

require (
github.com/InfluxCommunity/influxdb3-go v0.1.0
github.com/frankban/quicktest v1.14.6
github.com/gabriel-vasile/mimetype v1.4.3
github.com/gofrs/uuid v4.3.1+incompatible
github.com/go-redis/redismock/v9 v9.2.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
github.com/iancoleman/strcase v0.2.0
github.com/influxdata/influxdb-client-go/v2 v2.12.3
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240222135530-192b4f25f624
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240308151517-4b0523c184d1
github.com/instill-ai/usage-client v0.2.4-alpha.0.20240123081026-6c78d9a5197a
github.com/instill-ai/x v0.3.0-alpha
github.com/knadh/koanf v1.4.4
github.com/mennanov/fieldmask-utils v0.5.0
github.com/instill-ai/x v0.4.0-alpha
github.com/knadh/koanf v1.5.0
github.com/mennanov/fieldmask-utils v1.0.0
github.com/openfga/go-sdk v0.2.3
github.com/redis/go-redis/v9 v9.2.0
github.com/redis/go-redis/v9 v9.5.1
go.einride.tech/aip v0.60.0
go.opentelemetry.io/contrib/propagators/b3 v1.17.0
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.39.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.39.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
go.opentelemetry.io/otel/sdk/metric v0.39.0
go.opentelemetry.io/otel/trace v1.16.0
go.opentelemetry.io/otel v1.24.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.24.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.24.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.24.0
go.opentelemetry.io/otel/sdk v1.24.0
go.opentelemetry.io/otel/sdk/metric v1.24.0
go.opentelemetry.io/otel/trace v1.24.0
go.temporal.io/api v1.16.0
go.temporal.io/sdk v1.21.0
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.17.0
golang.org/x/image v0.14.0
golang.org/x/net v0.17.0
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc
google.golang.org/grpc v1.56.3
google.golang.org/protobuf v1.30.0
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.21.0
golang.org/x/image v0.15.0
golang.org/x/net v0.22.0
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe
google.golang.org/grpc v1.61.1
google.golang.org/protobuf v1.33.0
gorm.io/datatypes v1.2.0
gorm.io/driver/postgres v1.5.0
gorm.io/gorm v1.25.2
gorm.io/plugin/dbresolver v1.5.1
)

require (
cloud.google.com/go/longrunning v0.4.1 // indirect
cloud.google.com/go/longrunning v0.5.4 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/apache/arrow/go/v12 v12.0.0 // indirect
github.com/apache/thrift v0.16.0 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/catalinc/hashcash v0.0.0-20220723060415-5e3ec3e24f67 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/docker v20.10.27+incompatible // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v25.0.5+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.9.11 // indirect
Expand All @@ -74,7 +79,8 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v2.0.8+incompatible // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
Expand All @@ -87,37 +93,38 @@ require (
github.com/klauspost/asmfmt v1.3.2 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 // indirect
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pborman/uuid v1.2.1 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.15.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.10.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.4.7 // indirect
)
Loading

0 comments on commit a220469

Please sign in to comment.