mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2026-06-21 00:09:04 +02:00
Release catalyst
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/blevesearch/bleve/v2"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/generated/models"
|
||||
)
|
||||
|
||||
type Index struct {
|
||||
name string
|
||||
internal bleve.Index
|
||||
}
|
||||
|
||||
func New(name string) (*Index, error) {
|
||||
var err error
|
||||
var bleveIndex bleve.Index
|
||||
if _, oerr := os.Stat(name); os.IsNotExist(oerr) {
|
||||
bleveIndex, err = bleve.New(name, bleve.NewIndexMapping())
|
||||
} else {
|
||||
bleveIndex, err = bleve.Open(name)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Index{name: name, internal: bleveIndex}, nil
|
||||
}
|
||||
|
||||
func (i *Index) Index(incidents []*models.TicketSimpleResponse) {
|
||||
b := i.internal.NewBatch()
|
||||
for _, incident := range incidents {
|
||||
if incident.ID == 0 {
|
||||
log.Println(errors.New("no ID"), incident)
|
||||
continue
|
||||
}
|
||||
|
||||
err := b.Index(fmt.Sprint(incident.ID), incident)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
err := i.internal.Batch(b)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Index) Search(term string) (ids []string, err error) {
|
||||
query := bleve.NewQueryStringQuery(term)
|
||||
result, err := i.internal.Search(bleve.NewSearchRequestOptions(query, 10000, 0, false))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, match := range result.Hits {
|
||||
ids = append(ids, match.ID)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func (i *Index) Truncate() error {
|
||||
err := i.internal.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.RemoveAll(i.name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
index, err := bleve.New(i.name, bleve.NewIndexMapping())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.internal = index
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package index_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/generated/models"
|
||||
"github.com/SecurityBrewery/catalyst/test"
|
||||
)
|
||||
|
||||
func TestIndex(t *testing.T) {
|
||||
type args struct {
|
||||
term string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantIds []string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "Exists", args: args{"foo"}, wantIds: []string{"1"}},
|
||||
{name: "Not exists", args: args{"bar"}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
i, cleanup, err := test.Index(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
i.Index([]*models.TicketSimpleResponse{
|
||||
{ID: 0, Name: "bar"},
|
||||
{ID: 1, Name: "foo"},
|
||||
})
|
||||
|
||||
gotIds, err := i.Search(tt.args.term)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Search() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(gotIds, tt.wantIds) {
|
||||
t.Errorf("Search() gotIds = %v, want %v", gotIds, tt.wantIds)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndex_Truncate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "Truncate"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
i, cleanup, err := test.Index(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
i.Index([]*models.TicketSimpleResponse{
|
||||
{ID: 0, Name: "bar"},
|
||||
{ID: 1, Name: "foo"},
|
||||
})
|
||||
|
||||
if err := i.Truncate(); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Truncate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
|
||||
ids, err := i.Search("foo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if ids != nil {
|
||||
t.Fatal("should return no results")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user