Skip to content

Commit

Permalink
Merge pull request #44 from rsevilla87/empty-document-list
Browse files Browse the repository at this point in the history
Don't create file when document list is empty
  • Loading branch information
vishnuchalla authored Mar 9, 2024
2 parents 0140b10 + 30b5e1e commit 879de04
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
6 changes: 2 additions & 4 deletions indexers/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
"github.com/elastic/go-elasticsearch/v7/esutil"
)

const elastic = "elastic"

// Elastic ElasticSearch instance
type Elastic struct {
index string
Expand All @@ -44,7 +42,7 @@ var ESClient *elasticsearch.Client

// Init function
func init() {
indexerMap[elastic] = &Elastic{}
indexerMap[ElasticIndexer] = &Elastic{}
}

// Returns new indexer for elastic search
Expand Down Expand Up @@ -140,7 +138,7 @@ func (esIndexer *Elastic) Index(documents []interface{}, opts IndexingOpts) (str
for stat, val := range indexerStats {
statString += fmt.Sprintf(" %s=%d", stat, val)
}
if(redundantSkipped > 0){
if redundantSkipped > 0 {
statString += fmt.Sprintf(" redundantskipped=%d", redundantSkipped)
}
return fmt.Sprintf("Indexing finished in %v:%v", dur.Truncate(time.Millisecond), statString), nil
Expand Down
7 changes: 4 additions & 3 deletions indexers/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ import (
"path"
)

const local = "local"

// Local indexer instance
type Local struct {
metricsDirectory string
}

// Init function
func init() {
indexerMap[local] = &Local{}
indexerMap[LocalIndexer] = &Local{}
}

// Prepares local indexing directory
Expand All @@ -45,6 +43,9 @@ func (l *Local) new(indexerConfig IndexerConfig) error {

// Index uses generates a local file with the given name and metrics
func (l *Local) Index(documents []interface{}, opts IndexingOpts) (string, error) {
if len(documents) == 0 {
return "", fmt.Errorf("Empty document list in %v", opts.MetricName)
}
if opts.MetricName == "" {
return "", fmt.Errorf("MetricName shouldn't be empty")
}
Expand Down
24 changes: 16 additions & 8 deletions indexers/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package indexers

import (
"errors"
"fmt"
"log"
"os"
"path"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -19,9 +21,8 @@ var _ = Describe("Tests for local.go", func() {
var localIndexer Local
BeforeEach(func() {
testcase = newtestcase{
indexerconfig: IndexerConfig{Type: "elastic",
indexerconfig: IndexerConfig{Type: "local",
Servers: []string{""},
Index: "go-commons-test",
InsecureSkipVerify: true,
MetricsDirectory: "",
},
Expand All @@ -41,7 +42,7 @@ var _ = Describe("Tests for local.go", func() {
})

Context("Default behaviour of local.go, Index()", func() {
var testcase indexMethodTestcase
var testcase, emtpyTestCase indexMethodTestcase
var indexer Local
indexer.metricsDirectory = "placeholder"
BeforeEach(func() {
Expand Down Expand Up @@ -71,6 +72,12 @@ var _ = Describe("Tests for local.go", func() {
MetricName: "placeholder",
},
}
emtpyTestCase = indexMethodTestcase{
documents: []interface{}{},
opts: IndexingOpts{
MetricName: "empty",
},
}
})
AfterEach(func() {
err := os.RemoveAll(indexer.metricsDirectory)
Expand All @@ -79,13 +86,10 @@ var _ = Describe("Tests for local.go", func() {
}
})

It("No err is returned", func() {
It("Metric file is created", func() {
_, err := indexer.Index(testcase.documents, testcase.opts)
Expect(err).To(BeNil())
})

It("No err is returned", func() {
_, err := indexer.Index(testcase.documents, testcase.opts)
_, err = os.Stat(path.Join(indexer.metricsDirectory, testcase.opts.MetricName+".json"))
Expect(err).To(BeNil())
})

Expand All @@ -100,5 +104,9 @@ var _ = Describe("Tests for local.go", func() {
_, err := indexer.Index(testcase.documents, testcase.opts)
Expect(err).To(BeEquivalentTo(errors.New("JSON encoding error: json: unsupported type: chan string")))
})
It("returns err no empty document list", func() {
_, err := indexer.Index(emtpyTestCase.documents, emtpyTestCase.opts)
Expect(err).To(BeEquivalentTo(fmt.Errorf("Empty document list in %v", emtpyTestCase.opts.MetricName)))
})
})
})
6 changes: 2 additions & 4 deletions indexers/opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
opensearchutil "github.com/opensearch-project/opensearch-go/opensearchutil"
)

const indexer = "opensearch"

// OSClient OpenSearch client instance
var OSClient *opensearch.Client

Expand All @@ -44,7 +42,7 @@ type OpenSearch struct {

// Init function
func init() {
indexerMap[indexer] = &OpenSearch{}
indexerMap[OpenSearchIndexer] = &OpenSearch{}
}

// Returns new indexer for OpenSearch
Expand Down Expand Up @@ -140,7 +138,7 @@ func (OpenSearchIndexer *OpenSearch) Index(documents []interface{}, opts Indexin
for stat, val := range indexerStats {
statString += fmt.Sprintf(" %s=%d", stat, val)
}
if(redundantSkipped > 0){
if redundantSkipped > 0 {
statString += fmt.Sprintf(" redundantskipped=%d", redundantSkipped)
}
return fmt.Sprintf("Indexing finished in %v:%v", dur.Truncate(time.Millisecond), statString), nil
Expand Down

0 comments on commit 879de04

Please sign in to comment.