This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggregator.go
84 lines (72 loc) · 2.08 KB
/
aggregator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package uyuniapi
import (
"log"
"reflect"
)
type DataAggregator struct {
vidman *VIDManager
shards map[string]interface{}
}
// Constructor
func NewDataAggregator(vidmanager *VIDManager) *DataAggregator {
agr := new(DataAggregator)
agr.vidman = vidmanager
agr.shards = make(map[string]interface{})
return agr
}
// Aggregate
func (agr *DataAggregator) aggregate(fqdn string, data interface{}) {
agr.shards[fqdn] = data
}
// Multiplex data from different sources into one
func (agr *DataAggregator) Multiplex() interface{} {
data, muxed := agr.getMuxResult()
if !muxed {
data = agr.getSingleResult()
}
return data
}
func (agr *DataAggregator) getSingleResult() interface{} {
for _, ctx := range agr.vidman.GetContextFQDNs() {
res := agr.shards[ctx]
if res != nil {
return agr.remapIds(ctx, res)
}
}
return nil
}
// Merge multiple structure results from different hosts, if any.
func (agr *DataAggregator) getMuxResult() (interface{}, bool) {
var data interface{} = nil
if len(agr.shards) > 0 {
for _, rpcRef := range agr.vidman.GetContextFQDNs() {
res := agr.shards[rpcRef]
if res != nil && reflect.TypeOf(res).Kind() == reflect.Slice {
if data == nil {
data = make([]interface{}, 0)
}
for _, dataRef := range res.([]interface{}) {
dataType := reflect.TypeOf(dataRef).Kind()
if dataType == reflect.Map {
data = append(data.([]interface{}), agr.remapIds(rpcRef, dataRef))
} else if dataType == reflect.String {
data = append(data.([]interface{}), dataRef)
} else {
log.Println("Data type", reflect.TypeOf(dataRef).Kind(), "is not yet supported")
}
}
}
}
}
return data, data != nil
}
// Remap system IDs
func (agr *DataAggregator) remapIds(context string, data interface{}) interface{} {
var rData interface{} = data
if reflect.TypeOf(data).Kind() == reflect.Map {
if systemId, ok := data.(map[string]interface{})["id"]; ok { // TODO: check if it is really string/interface map
data.(map[string]interface{})["id"] = agr.vidman.GetContext(context).ToVirtualId(int(systemId.(int64)))
}
}
return rData
}