Code generalization: request db info from other source: remove unused constants

This commit is contained in:
Michael Hohn
2024-10-28 18:45:21 -07:00
committed by =Michael Hohn
parent 52aafd6fc9
commit e7d32861e5
5 changed files with 20 additions and 33 deletions

View File

@@ -5,22 +5,16 @@ import (
"mrvacommander/pkg/common" "mrvacommander/pkg/common"
) )
// xx: afl use
// XX: static types: split by type? // XX: static types: split by type?
// Restrict the keys / values for ArtifactLocation and centralize the common ones // Restrict the keys / values for ArtifactLocation and centralize the common ones
// here // here
const ( const (
AF_VAL_BUCKET_RESULTS = "results" AF_BUCKETNAME_RESULTS = "results"
AF_VAL_BUCKET_PACKS = "packs" AF_BUCKETNAME_PACKS = "packs"
AF_KEY_BUCKET = "bucket"
AF_KEY_KEY = "key"
) )
type ArtifactLocation struct { type ArtifactLocation struct {
// Data is a map of key-value pairs that describe the location of the artifact.
// For example, a simple key-value pair could be "path" -> "/path/to/artifact.tgz".
// Alternatively, a more complex example could be "bucket" -> "example", "key" -> "UNIQUE_ARTIFACT_IDENTIFIER".
// XX: static types
// Data map[string]string `json:"data"`
Key string // location in bucket Key string // location in bucket
Bucket string // which bucket: packs or results Bucket string // which bucket: packs or results
} }

View File

@@ -25,6 +25,7 @@ func (store *InMemoryArtifactStore) GetQueryPack(location ArtifactLocation) ([]b
store.mu.Lock() store.mu.Lock()
defer store.mu.Unlock() defer store.mu.Unlock()
// xx: afl use
// XX: static types // XX: static types
// key := location.Data[AF_KEY_KEY] // key := location.Data[AF_KEY_KEY]
key := location.Key key := location.Key
@@ -44,13 +45,10 @@ func (store *InMemoryArtifactStore) SaveQueryPack(sessionId int, data []byte) (A
store.packs[key] = data store.packs[key] = data
// XX: static types // XX: static types
// xx: afl use
location := ArtifactLocation{ location := ArtifactLocation{
Bucket: AF_VAL_BUCKET_PACKS, Bucket: AF_BUCKETNAME_PACKS,
Key: key, Key: key,
// Data: map[string]string{
// AF_KEY_BUCKET: AF_VAL_BUCKET_PACKS,
// AF_KEY_KEY: key,
// },
} }
return location, nil return location, nil
} }
@@ -60,6 +58,7 @@ func (store *InMemoryArtifactStore) GetResult(location ArtifactLocation) ([]byte
store.mu.Lock() store.mu.Lock()
defer store.mu.Unlock() defer store.mu.Unlock()
// xx: afl use
// key := location.Data[AF_KEY_KEY] // key := location.Data[AF_KEY_KEY]
key := location.Key key := location.Key
data, exists := store.results[key] data, exists := store.results[key]
@@ -75,6 +74,7 @@ func (store *InMemoryArtifactStore) GetResultSize(location ArtifactLocation) (in
defer store.mu.Unlock() defer store.mu.Unlock()
// key := location.Data[AF_KEY_KEY] // key := location.Data[AF_KEY_KEY]
// xx: afl use
key := location.Key key := location.Key
data, exists := store.results[key] data, exists := store.results[key]
if !exists { if !exists {
@@ -92,13 +92,10 @@ func (store *InMemoryArtifactStore) SaveResult(jobSpec common.JobSpec, data []by
store.results[key] = data store.results[key] = data
// XX: static types // XX: static types
// xx: afl use
location := ArtifactLocation{ location := ArtifactLocation{
Bucket: AF_VAL_BUCKET_RESULTS, Bucket: AF_BUCKETNAME_RESULTS,
Key: key, Key: key,
// Data: map[string]string{
// AF_KEY_BUCKET: AF_VAL_BUCKET_RESULTS,
// AF_KEY_KEY: key,
// },
} }
return location, nil return location, nil
} }

View File

@@ -29,12 +29,12 @@ func NewMinIOArtifactStore(endpoint, id, secret string) (*MinIOArtifactStore, er
slog.Info("Connected to MinIO artifact store server") slog.Info("Connected to MinIO artifact store server")
// Create "results" bucket // Create "results" bucket
if err := common.CreateMinIOBucketIfNotExists(minioClient, AF_VAL_BUCKET_RESULTS); err != nil { if err := common.CreateMinIOBucketIfNotExists(minioClient, AF_BUCKETNAME_RESULTS); err != nil {
return nil, fmt.Errorf("could not create results bucket: %v", err) return nil, fmt.Errorf("could not create results bucket: %v", err)
} }
// Create "packs" bucket // Create "packs" bucket
if err := common.CreateMinIOBucketIfNotExists(minioClient, AF_VAL_BUCKET_PACKS); err != nil { if err := common.CreateMinIOBucketIfNotExists(minioClient, AF_BUCKETNAME_PACKS); err != nil {
return nil, fmt.Errorf("could not create packs bucket: %v", err) return nil, fmt.Errorf("could not create packs bucket: %v", err)
} }
@@ -48,7 +48,8 @@ func (store *MinIOArtifactStore) GetQueryPack(location ArtifactLocation) ([]byte
} }
func (store *MinIOArtifactStore) SaveQueryPack(jobId int, data []byte) (ArtifactLocation, error) { func (store *MinIOArtifactStore) SaveQueryPack(jobId int, data []byte) (ArtifactLocation, error) {
return store.saveArtifact(AF_VAL_BUCKET_PACKS, deriveKeyFromSessionId(jobId), data, "application/gzip") // xx: afl use
return store.saveArtifact(AF_BUCKETNAME_PACKS, deriveKeyFromSessionId(jobId), data, "application/gzip")
} }
func (store *MinIOArtifactStore) GetResult(location ArtifactLocation) ([]byte, error) { func (store *MinIOArtifactStore) GetResult(location ArtifactLocation) ([]byte, error) {
@@ -56,8 +57,7 @@ func (store *MinIOArtifactStore) GetResult(location ArtifactLocation) ([]byte, e
} }
func (store *MinIOArtifactStore) GetResultSize(location ArtifactLocation) (int, error) { func (store *MinIOArtifactStore) GetResultSize(location ArtifactLocation) (int, error) {
// bucket := location.Data[AF_KEY_BUCKET] // xx: afl use
// key := location.Data[AF_KEY_KEY]
bucket := location.Bucket bucket := location.Bucket
key := location.Key key := location.Key
@@ -74,14 +74,14 @@ func (store *MinIOArtifactStore) GetResultSize(location ArtifactLocation) (int,
} }
func (store *MinIOArtifactStore) SaveResult(jobSpec common.JobSpec, data []byte) (ArtifactLocation, error) { func (store *MinIOArtifactStore) SaveResult(jobSpec common.JobSpec, data []byte) (ArtifactLocation, error) {
return store.saveArtifact(AF_VAL_BUCKET_RESULTS, deriveKeyFromJobSpec(jobSpec), data, "application/zip") // xx: afl use
return store.saveArtifact(AF_BUCKETNAME_RESULTS, deriveKeyFromJobSpec(jobSpec), data, "application/zip")
} }
func (store *MinIOArtifactStore) getArtifact(location ArtifactLocation) ([]byte, error) { func (store *MinIOArtifactStore) getArtifact(location ArtifactLocation) ([]byte, error) {
// xx: afl use
bucket := location.Bucket bucket := location.Bucket
key := location.Key key := location.Key
// bucket := location.Data[AF_KEY_BUCKET]
// key := location.Data[AF_KEY_KEY]
object, err := store.client.GetObject(context.Background(), bucket, key, minio.GetObjectOptions{}) object, err := store.client.GetObject(context.Background(), bucket, key, minio.GetObjectOptions{})
if err != nil { if err != nil {
@@ -107,14 +107,11 @@ func (store *MinIOArtifactStore) saveArtifact(bucket, key string, data []byte,
return ArtifactLocation{}, err return ArtifactLocation{}, err
} }
// XX: afl use
// XX: static types // XX: static types
location := ArtifactLocation{ location := ArtifactLocation{
Bucket: bucket, Bucket: bucket,
Key: key, Key: key,
// Data: map[string]string{
// AF_KEY_BUCKET: bucket,
// AF_KEY_KEY: key,
// },
} }
return location, nil return location, nil

View File

@@ -15,8 +15,6 @@ import (
// Restrict the keys / values and centralize the common ones here // Restrict the keys / values and centralize the common ones here
const ( const (
QL_DB_BUCKETNAME = "qldb" QL_DB_BUCKETNAME = "qldb"
QL_KEY_BUCKET = "bucket"
QL_KEY_KEY = "key"
) )
type MinIOCodeQLDatabaseStore struct { type MinIOCodeQLDatabaseStore struct {

View File

@@ -791,6 +791,7 @@ func (c *CommanderSingle) decodeAndSaveBase64QueryPack(qp string, sessionId int)
return artifactstore.ArtifactLocation{}, err return artifactstore.ArtifactLocation{}, err
} }
// XX: afl use
artifactLocation, err := c.v.Artifacts.SaveQueryPack(sessionId, tgz) artifactLocation, err := c.v.Artifacts.SaveQueryPack(sessionId, tgz)
if err != nil { if err != nil {
slog.Error("Failed to save query pack", "err", err) slog.Error("Failed to save query pack", "err", err)