storage.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package entities
  2. /**
  3. * The ephemeralvariablemap is used "only" for variable substitution in config
  4. * titles, shell arguments, etc, in the foorm of {{ key }}, like Jinja2.
  5. *
  6. * OliveTin itself really only ever "writes" to this map, mostly by loading
  7. * EntityFiles, and the only form of "reading" is for the variable substitution
  8. * in configs.
  9. */
  10. import (
  11. "maps"
  12. "sort"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. )
  17. type entityInstancesByKey map[string]*Entity
  18. type EntitiesByClass map[string]entityInstancesByKey
  19. var (
  20. rwmutex = sync.RWMutex{}
  21. entities EntitiesByClass
  22. )
  23. func init() {
  24. rwmutex.Lock()
  25. entities = make(EntitiesByClass, 0)
  26. rwmutex.Unlock()
  27. }
  28. func GetEntities() EntitiesByClass {
  29. rwmutex.RLock()
  30. copiedEntities := make(EntitiesByClass, len(entities))
  31. for entityName, entityInstances := range entities {
  32. copiedInstances := make(entityInstancesByKey, len(entityInstances))
  33. maps.Copy(copiedInstances, entityInstances)
  34. copiedEntities[entityName] = copiedInstances
  35. }
  36. rwmutex.RUnlock()
  37. return copiedEntities
  38. }
  39. func GetEntityInstances(entityName string) entityInstancesByKey {
  40. rwmutex.RLock()
  41. defer rwmutex.RUnlock()
  42. if entities, ok := entities[entityName]; ok {
  43. copiedInstances := make(entityInstancesByKey, len(entities))
  44. maps.Copy(copiedInstances, entities)
  45. return copiedInstances
  46. }
  47. return make(entityInstancesByKey, 0)
  48. }
  49. func GetEntityInstancesOrdered(entityName string) []*Entity {
  50. instances := GetEntityInstances(entityName)
  51. if len(instances) == 0 {
  52. return nil
  53. }
  54. keys := make([]string, 0, len(instances))
  55. for key := range instances {
  56. keys = append(keys, key)
  57. }
  58. sort.Slice(keys, func(i, j int) bool {
  59. return compareEntityKeys(keys[i], keys[j]) < 0
  60. })
  61. result := make([]*Entity, 0, len(keys))
  62. for _, key := range keys {
  63. result = append(result, instances[key])
  64. }
  65. return result
  66. }
  67. //gocyclo:ignore
  68. func compareEntityKeys(a, b string) int {
  69. ai, errA := strconv.ParseInt(a, 10, 64)
  70. bi, errB := strconv.ParseInt(b, 10, 64)
  71. if errA == nil && errB == nil {
  72. if ai < bi {
  73. return -1
  74. }
  75. if ai > bi {
  76. return 1
  77. }
  78. return 0
  79. }
  80. if a < b {
  81. return -1
  82. }
  83. if a > b {
  84. return 1
  85. }
  86. return 0
  87. }
  88. func AddEntity(entityName string, entityKey string, data any) {
  89. rwmutex.Lock()
  90. if _, ok := entities[entityName]; !ok {
  91. entities[entityName] = make(entityInstancesByKey, 0)
  92. }
  93. entities[entityName][entityKey] = &Entity{
  94. Data: data,
  95. UniqueKey: entityKey,
  96. Title: findEntityTitle(data),
  97. }
  98. rwmutex.Unlock()
  99. }
  100. var entityDisplayNameCandidates = []string{"title", "name", "id", "hostname", "host", "label"}
  101. func entityDisplayNameKeysByLower(data map[string]any) map[string]string {
  102. keys := make(map[string]string, len(data))
  103. for k := range data {
  104. lower := strings.ToLower(k)
  105. existing, exists := keys[lower]
  106. if exists && k >= existing {
  107. continue
  108. }
  109. keys[lower] = k
  110. }
  111. return keys
  112. }
  113. func entityDisplayNameCandidateValue(data map[string]any, keysByLower map[string]string, candidate string) (fieldKey string, value string, ok bool) {
  114. lookupKey, exists := keysByLower[strings.ToLower(candidate)]
  115. if !exists {
  116. return "", "", false
  117. }
  118. rawValue, exists := data[lookupKey]
  119. if !exists {
  120. return "", "", false
  121. }
  122. valueStr, isString := rawValue.(string)
  123. if !isString {
  124. return "", "", false
  125. }
  126. return lookupKey, valueStr, true
  127. }
  128. func entityDisplayNameLookup(data map[string]any) (fieldKey string, value string, found bool) {
  129. keysByLower := entityDisplayNameKeysByLower(data)
  130. for _, candidate := range entityDisplayNameCandidates {
  131. if fieldKey, value, ok := entityDisplayNameCandidateValue(data, keysByLower, candidate); ok {
  132. return fieldKey, value, true
  133. }
  134. }
  135. return "", "", false
  136. }
  137. // DisplayNameFieldKey returns the data-file field used as the entity instance title, or "".
  138. func DisplayNameFieldKey(data any) string {
  139. mapData, ok := data.(map[string]any)
  140. if !ok {
  141. return ""
  142. }
  143. fieldKey, _, found := entityDisplayNameLookup(mapData)
  144. if !found {
  145. return ""
  146. }
  147. return fieldKey
  148. }
  149. func findEntityTitle(data any) string {
  150. mapData, ok := data.(map[string]any)
  151. if !ok {
  152. return "Untitled Entity"
  153. }
  154. _, value, found := entityDisplayNameLookup(mapData)
  155. if !found {
  156. return "Untitled Entity"
  157. }
  158. return value
  159. }
  160. func ClearEntitiesOfType(entityType string) {
  161. rwmutex.Lock()
  162. defer rwmutex.Unlock()
  163. delete(entities, entityType)
  164. }