-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add variable query for distinct event property values
- Loading branch information
Showing
17 changed files
with
924 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/factrylabs/factry-historian-datasource.git/pkg/schemas" | ||
"github.com/factrylabs/factry-historian-datasource.git/pkg/util" | ||
"github.com/google/uuid" | ||
) | ||
|
||
// GetFilteredAssets returns a map of assets that match the given asset strings | ||
func (api *API) GetFilteredAssets(ctx context.Context, assetStrings []string, historianInfo *schemas.HistorianInfo) (map[uuid.UUID]schemas.Asset, error) { | ||
assetUUIDSet := map[uuid.UUID]schemas.Asset{} | ||
|
||
if util.CheckMinimumVersion(historianInfo, "6.4.0") { | ||
for _, assetString := range assetStrings { | ||
assetQuery := url.Values{} | ||
assetQuery.Add("Keyword", assetString) | ||
assets, err := api.GetAssets(ctx, assetQuery.Encode()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
assetUUIDSet = util.ByUUID(assets) | ||
} | ||
} else { | ||
// Deprecated | ||
assets, err := api.GetAssets(ctx, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, assetString := range assetStrings { | ||
if filteredAssets := filterAssetUUIDs(assets, assetString); len(filteredAssets) > 0 { | ||
assetUUIDSet = util.ByUUID(filteredAssets) | ||
} | ||
} | ||
} | ||
|
||
return assetUUIDSet, nil | ||
} | ||
|
||
// GetFilteredEventTypes returns a map of event types that match the given event type strings | ||
func (api *API) GetFilteredEventTypes(ctx context.Context, eventTypeStrings []string, historianInfo *schemas.HistorianInfo) (map[uuid.UUID]schemas.EventType, error) { | ||
eventTypeUUIDSet := map[uuid.UUID]schemas.EventType{} | ||
|
||
if util.CheckMinimumVersion(historianInfo, "6.4.0") { | ||
for _, eventTypeString := range eventTypeStrings { | ||
eventTypeQuery := url.Values{} | ||
eventTypeQuery.Add("Keyword", eventTypeString) | ||
eventTypes, err := api.GetEventTypes(ctx, eventTypeQuery.Encode()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
eventTypeUUIDSet = util.ByUUID(eventTypes) | ||
} | ||
} else { | ||
// Deprecated | ||
eventTypes, err := api.GetEventTypes(ctx, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, eventTypeString := range eventTypeStrings { | ||
if filteredEventTypes := filterEventTypeUUIDs(eventTypes, eventTypeString); len(filteredEventTypes) > 0 { | ||
eventTypeUUIDSet = util.ByUUID(filteredEventTypes) | ||
} | ||
} | ||
} | ||
|
||
return eventTypeUUIDSet, nil | ||
} | ||
|
||
func filterItems[T any](items []T, searchValue string, matchFuncs ...func(T) string) []T { | ||
filteredItems := make([]T, 0, len(items)) | ||
if len(searchValue) == 0 { | ||
return filteredItems // Early exit for empty search | ||
} | ||
var re *regexp.Regexp | ||
if strings.HasPrefix(searchValue, "/") && strings.HasSuffix(searchValue, "/") { | ||
if len(searchValue) > 2 { | ||
pattern := searchValue[1 : len(searchValue)-1] | ||
var err error | ||
re, err = regexp.Compile(pattern) | ||
if err != nil { | ||
return filteredItems | ||
} | ||
} | ||
} | ||
|
||
for _, item := range items { | ||
for _, matchFunc := range matchFuncs { | ||
if (re != nil && re.MatchString(matchFunc(item))) || (re == nil && matchFunc(item) == searchValue) { | ||
filteredItems = append(filteredItems, item) | ||
break | ||
} | ||
} | ||
} | ||
return filteredItems | ||
} | ||
func filterAssetUUIDs(assets []schemas.Asset, searchValue string) []schemas.Asset { | ||
return filterItems(assets, searchValue, | ||
func(asset schemas.Asset) string { return asset.AssetPath }, | ||
func(asset schemas.Asset) string { return asset.UUID.String() }) | ||
} | ||
func filterEventTypeUUIDs(eventTypes []schemas.EventType, searchValue string) []schemas.EventType { | ||
return filterItems(eventTypes, searchValue, | ||
func(eventType schemas.EventType) string { return eventType.Name }, | ||
func(eventType schemas.EventType) string { return eventType.UUID.String() }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.