-
Notifications
You must be signed in to change notification settings - Fork 1
/
asset_structure.go
231 lines (196 loc) · 6.67 KB
/
asset_structure.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// This file is part of the eliona project.
// Copyright © 2024 LEICOM iTEC AG. All Rights Reserved.
// ______ _ _
// | ____| (_)
// | |__ | |_ ___ _ __ __ _
// | __| | | |/ _ \| '_ \ / _` |
// | |____| | | (_) | | | | (_| |
// |______|_|_|\___/|_| |_|\__,_|
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package asset
import (
"fmt"
"time"
api "github.com/eliona-smart-building-assistant/go-eliona-api-client/v2"
)
type Root interface {
LocationalNode
FunctionalNode
}
type LocationalNode interface {
Asset
GetLocationalChildren() []LocationalNode
}
type FunctionalNode interface {
Asset
GetFunctionalChildren() []FunctionalNode
}
type Asset interface {
GetName() string
GetDescription() string
GetAssetType() string
GetGAI() string
GetAssetID(projectID string) (*int32, error)
SetAssetID(assetID int32, projectID string) error
}
// CreateAssets creates assets within the asset structure using bulk creation.
// It skips assets that have assetID != nil but keeps their children.
// NOTE: Prefer using CreateAssetsBulk method, avoiding complexity and too much
// juggling of asset structure.
func CreateAssets(root Root, projectId string) (createdCnt int, err error) {
var assetsToCreate []AssetWithParentReferences
err = collectAssetsToCreate(root, "", "", &assetsToCreate, projectId)
if err != nil {
return 0, fmt.Errorf("collecting assets to create: %v", err)
}
createdCnt, err = createAssets(assetsToCreate, projectId)
if err != nil {
return 0, fmt.Errorf("creating assets: %v", err)
}
return createdCnt, nil
}
// CreateAssetsAndUpsertData creates assets within the asset structure and upserts their data.
// It skips assets that have assetID != nil but keeps their children.
// It uses the createAssets() function to bulk create assets.
// NOTE: Prefer using CreateAssetsBulk method, avoiding complexity and too much
// juggling of asset structure.
func CreateAssetsAndUpsertData(root Root, projectId string, ts *time.Time, clientReference *string) (createdCnt int, err error) {
var assetsToCreate []AssetWithParentReferences
var dataToUpsert []Data
err = collectAssetsAndDataToCreate(root, "", "", &assetsToCreate, &dataToUpsert, projectId, ts, clientReference)
if err != nil {
return 0, fmt.Errorf("collecting assets and data to create: %v", err)
}
createdCnt, err = createAssets(assetsToCreate, projectId)
if err != nil {
return 0, fmt.Errorf("creating assets: %v", err)
}
// Upsert data for all assets (including those that already existed)
for _, data := range dataToUpsert {
err := UpsertAssetDataIfAssetExists(data)
if err != nil {
return createdCnt, fmt.Errorf("upserting data: %v", err)
}
}
return createdCnt, nil
}
// collectAssetsToCreate traverses the asset tree and collects assets that need to be created.
// Note: The parent asset is always added to the assets slice before its children.
func collectAssetsToCreate(node Asset, locationalParentGAI, functionalParentGAI string, assets *[]AssetWithParentReferences, projectId string) error {
assetID, err := node.GetAssetID(projectId)
if err != nil {
return fmt.Errorf("getting asset ID: %v", err)
}
if assetID == nil {
a := &AssetToCreate{
node: node,
locationalParentGAI: locationalParentGAI,
functionalParentGAI: functionalParentGAI,
}
*assets = append(*assets, a)
}
if ln, ok := node.(LocationalNode); ok {
for _, child := range ln.GetLocationalChildren() {
if child == nil {
continue
}
err := collectAssetsToCreate(child, node.GetGAI(), functionalParentGAI, assets, projectId)
if err != nil {
return err
}
}
}
if fn, ok := node.(FunctionalNode); ok {
for _, child := range fn.GetFunctionalChildren() {
if child == nil {
continue
}
err := collectAssetsToCreate(child, locationalParentGAI, node.GetGAI(), assets, projectId)
if err != nil {
return err
}
}
}
return nil
}
// collectAssetsAndDataToCreate traverses the asset tree and collects assets that need to be created,
// as well as data to be upserted.
// Note: The parent asset is always added to the assets slice before its children.
func collectAssetsAndDataToCreate(node Asset, locationalParentGAI, functionalParentGAI string, assets *[]AssetWithParentReferences, data *[]Data, projectId string, ts *time.Time, clientReference *string) error {
assetID, err := node.GetAssetID(projectId)
if err != nil {
return fmt.Errorf("getting asset ID: %v", err)
}
if assetID == nil {
a := &AssetToCreate{
node: node,
locationalParentGAI: locationalParentGAI,
functionalParentGAI: functionalParentGAI,
}
*assets = append(*assets, a)
} else {
// Prepare data to upsert for existing assets
dataToUpsert := Data{
AssetId: *assetID,
Timestamp: *api.NewNullableTime(ts),
ClientReference: *clientReference,
Data: node,
}
*data = append(*data, dataToUpsert)
}
if ln, ok := node.(LocationalNode); ok {
for _, child := range ln.GetLocationalChildren() {
if child == nil {
continue
}
err := collectAssetsAndDataToCreate(child, node.GetGAI(), functionalParentGAI, assets, data, projectId, ts, clientReference)
if err != nil {
return err
}
}
}
if fn, ok := node.(FunctionalNode); ok {
for _, child := range fn.GetFunctionalChildren() {
if child == nil {
continue
}
err := collectAssetsAndDataToCreate(child, locationalParentGAI, node.GetGAI(), assets, data, projectId, ts, clientReference)
if err != nil {
return err
}
}
}
return nil
}
// AssetToCreate implements AssetWithParentReferences and holds asset information for creation.
type AssetToCreate struct {
node Asset
locationalParentGAI string
functionalParentGAI string
}
func (a *AssetToCreate) GetName() string {
return a.node.GetName()
}
func (a *AssetToCreate) GetDescription() string {
return a.node.GetDescription()
}
func (a *AssetToCreate) GetAssetType() string {
return a.node.GetAssetType()
}
func (a *AssetToCreate) GetGAI() string {
return a.node.GetGAI()
}
func (a *AssetToCreate) GetLocationalParentGAI() string {
return a.locationalParentGAI
}
func (a *AssetToCreate) GetFunctionalParentGAI() string {
return a.functionalParentGAI
}
func (a *AssetToCreate) SetAssetID(assetID int32, projectID string) error {
return a.node.SetAssetID(assetID, projectID)
}