Skip to content

Commit

Permalink
chore: refactor InfluxDB repository for better injection (#214)
Browse files Browse the repository at this point in the history
Because

- We're using 2 separate packages to initialize an InfluxDB repository.

This commit

- Make configuration part of the InfluxDB repo initialization.
  • Loading branch information
jvallesm committed Jun 14, 2024
1 parent 0992b62 commit 3d529fa
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 141 deletions.
5 changes: 2 additions & 3 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,9 @@ func main() {
defer pipelinePublicServiceClientConn.Close()
}

influxDBClient, influxDBQueryAPI := external.InitInfluxDBServiceClientV2(ctx, &config.Config)
defer influxDBClient.Close()
influxDB := repository.MustNewInfluxDB(ctx)
defer influxDB.Close()

influxDB := repository.NewInfluxDB(influxDBQueryAPI, config.Config.InfluxDB.Bucket)
repository := repository.NewRepository(db, redisClient)
service := service.NewService(repository, redisClient, influxDB, pipelinePublicServiceClient, &aclClient, config.Config.Server.InstillCoreHost)

Expand Down
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ type DatabaseConfig struct {

// InfluxDBConfig related to influxDB database
type InfluxDBConfig struct {
URL string `koanf:"url"`
Token string `koanf:"token"`
Org string `koanf:"org"`
Bucket string `koanf:"bucket"`
FlushInterval int `koanf:"flushinterval"`
URL string `koanf:"url"`
Token string `koanf:"token"`
Org string `koanf:"org"`
Bucket string `koanf:"bucket"`
FlushInterval time.Duration `koanf:"flushinterval"`
HTTPS struct {
Cert string `koanf:"cert"`
Key string `koanf:"key"`
Expand Down
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ influxdb:
token: i-love-instill-ai
org: instill-ai
bucket: instill-ai
flushinterval: 10 # In seconds for non-blocking batch mode
flushinterval: 10s
https:
cert:
key:
Expand Down
61 changes: 0 additions & 61 deletions pkg/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ import (
"context"
"crypto/tls"
"fmt"
"time"

"github.com/influxdata/influxdb-client-go/v2/api"
"github.com/influxdata/influxdb-client-go/v2/log"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"

influxdb3 "github.com/InfluxCommunity/influxdb3-go/influx"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"

"github.com/instill-ai/mgmt-backend/config"
"github.com/instill-ai/mgmt-backend/pkg/constant"
"github.com/instill-ai/mgmt-backend/pkg/logger"
Expand Down Expand Up @@ -67,58 +61,3 @@ func InitUsageServiceClient(ctx context.Context, serverConfig *config.ServerConf

return usagePB.NewUsageServiceClient(clientConn), clientConn
}

// InitInfluxDBServiceClientV2 initialises a InfluxDBServiceClientV2 instance
func InitInfluxDBServiceClientV2(ctx context.Context, appConfig *config.AppConfig) (influxdb2.Client, api.QueryAPI) {

logger, _ := logger.GetZapLogger(ctx)

var creds credentials.TransportCredentials
var err error

influxOptions := influxdb2.DefaultOptions()
if appConfig.Server.Debug {
influxOptions = influxOptions.SetLogLevel(log.DebugLevel)
}
influxOptions = influxOptions.SetFlushInterval(uint(time.Duration(appConfig.InfluxDB.FlushInterval * int(time.Second)).Milliseconds()))

if appConfig.InfluxDB.HTTPS.Cert != "" && appConfig.InfluxDB.HTTPS.Key != "" {
// TODO: support TLS
creds, err = credentials.NewServerTLSFromFile(appConfig.InfluxDB.HTTPS.Cert, appConfig.InfluxDB.HTTPS.Key)
if err != nil {
logger.Fatal(err.Error())
}
logger.Info(creds.Info().ServerName)
}

client := influxdb2.NewClientWithOptions(
appConfig.InfluxDB.URL,
appConfig.InfluxDB.Token,
influxOptions,
)

if _, err := client.Ping(ctx); err != nil {
logger.Warn(err.Error())
}

queryAPI := client.QueryAPI(appConfig.InfluxDB.Org)

return client, queryAPI
}

// InitInfluxDBServiceClientV3 initialises a InfluxDBServiceClientV3 instance
func InitInfluxDBServiceClientV3(ctx context.Context, appConfig *config.AppConfig) *influxdb3.Client {

logger, _ := logger.GetZapLogger(ctx)

client, err := influxdb3.New(influxdb3.Configs{
HostURL: appConfig.InfluxDB.URL,
AuthToken: appConfig.InfluxDB.Token,
})

if err != nil {
logger.Error(err.Error())
}

return client
}
Loading

0 comments on commit 3d529fa

Please sign in to comment.