| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package entities
- /**
- * The ephemeralvariablemap is used "only" for variable substitution in config
- * titles, shell arguments, etc, in the foorm of {{ key }}, like Jinja2.
- *
- * OliveTin itself really only ever "writes" to this map, mostly by loading
- * EntityFiles, and the only form of "reading" is for the variable substitution
- * in configs.
- */
- import (
- "maps"
- "sort"
- "strconv"
- "strings"
- "sync"
- )
- type entityInstancesByKey map[string]*Entity
- type EntitiesByClass map[string]entityInstancesByKey
- var (
- rwmutex = sync.RWMutex{}
- entities EntitiesByClass
- )
- func init() {
- rwmutex.Lock()
- entities = make(EntitiesByClass, 0)
- rwmutex.Unlock()
- }
- func GetEntities() EntitiesByClass {
- rwmutex.RLock()
- copiedEntities := make(EntitiesByClass, len(entities))
- for entityName, entityInstances := range entities {
- copiedInstances := make(entityInstancesByKey, len(entityInstances))
- maps.Copy(copiedInstances, entityInstances)
- copiedEntities[entityName] = copiedInstances
- }
- rwmutex.RUnlock()
- return copiedEntities
- }
- func GetEntityInstances(entityName string) entityInstancesByKey {
- rwmutex.RLock()
- defer rwmutex.RUnlock()
- if entities, ok := entities[entityName]; ok {
- copiedInstances := make(entityInstancesByKey, len(entities))
- maps.Copy(copiedInstances, entities)
- return copiedInstances
- }
- return make(entityInstancesByKey, 0)
- }
- func GetEntityInstancesOrdered(entityName string) []*Entity {
- instances := GetEntityInstances(entityName)
- if len(instances) == 0 {
- return nil
- }
- keys := make([]string, 0, len(instances))
- for key := range instances {
- keys = append(keys, key)
- }
- sort.Slice(keys, func(i, j int) bool {
- return compareEntityKeys(keys[i], keys[j]) < 0
- })
- result := make([]*Entity, 0, len(keys))
- for _, key := range keys {
- result = append(result, instances[key])
- }
- return result
- }
- //gocyclo:ignore
- func compareEntityKeys(a, b string) int {
- ai, errA := strconv.ParseInt(a, 10, 64)
- bi, errB := strconv.ParseInt(b, 10, 64)
- if errA == nil && errB == nil {
- if ai < bi {
- return -1
- }
- if ai > bi {
- return 1
- }
- return 0
- }
- if a < b {
- return -1
- }
- if a > b {
- return 1
- }
- return 0
- }
- func AddEntity(entityName string, entityKey string, data any) {
- rwmutex.Lock()
- if _, ok := entities[entityName]; !ok {
- entities[entityName] = make(entityInstancesByKey, 0)
- }
- entities[entityName][entityKey] = &Entity{
- Data: data,
- UniqueKey: entityKey,
- Title: findEntityTitle(data),
- }
- rwmutex.Unlock()
- }
- var entityDisplayNameCandidates = []string{"title", "name", "id", "hostname", "host", "label"}
- func entityDisplayNameKeysByLower(data map[string]any) map[string]string {
- keys := make(map[string]string, len(data))
- for k := range data {
- lower := strings.ToLower(k)
- existing, exists := keys[lower]
- if exists && k >= existing {
- continue
- }
- keys[lower] = k
- }
- return keys
- }
- func entityDisplayNameCandidateValue(data map[string]any, keysByLower map[string]string, candidate string) (fieldKey string, value string, ok bool) {
- lookupKey, exists := keysByLower[strings.ToLower(candidate)]
- if !exists {
- return "", "", false
- }
- rawValue, exists := data[lookupKey]
- if !exists {
- return "", "", false
- }
- valueStr, isString := rawValue.(string)
- if !isString {
- return "", "", false
- }
- return lookupKey, valueStr, true
- }
- func entityDisplayNameLookup(data map[string]any) (fieldKey string, value string, found bool) {
- keysByLower := entityDisplayNameKeysByLower(data)
- for _, candidate := range entityDisplayNameCandidates {
- if fieldKey, value, ok := entityDisplayNameCandidateValue(data, keysByLower, candidate); ok {
- return fieldKey, value, true
- }
- }
- return "", "", false
- }
- // DisplayNameFieldKey returns the data-file field used as the entity instance title, or "".
- func DisplayNameFieldKey(data any) string {
- mapData, ok := data.(map[string]any)
- if !ok {
- return ""
- }
- fieldKey, _, found := entityDisplayNameLookup(mapData)
- if !found {
- return ""
- }
- return fieldKey
- }
- func findEntityTitle(data any) string {
- mapData, ok := data.(map[string]any)
- if !ok {
- return "Untitled Entity"
- }
- _, value, found := entityDisplayNameLookup(mapData)
- if !found {
- return "Untitled Entity"
- }
- return value
- }
- func ClearEntitiesOfType(entityType string) {
- rwmutex.Lock()
- defer rwmutex.Unlock()
- delete(entities, entityType)
- }
|