-
Notifications
You must be signed in to change notification settings - Fork 0
/
CellService.go
550 lines (474 loc) · 14.5 KB
/
CellService.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package tm1go
import (
"encoding/json"
"fmt"
"math"
"math/rand"
"strings"
"sync"
)
// Service for reading and writing TM1 cube cells
type CellService struct {
rest *RestService
cube *CubeService
file *FileService
process *ProcessService
}
// NewCellService creates a new cell service
func NewCellService(rest *RestService, cube *CubeService, file *FileService, process *ProcessService) *CellService {
return &CellService{rest: rest, cube: cube, file: file, process: process}
}
// CreateCellSet creates a cellset
func (cs *CellService) CreateCellSet(mdx string, sandboxName string) (string, error) {
url := "/ExecuteMDX"
url, err := AddURLParameters(url, map[string]string{"!sandbox": sandboxName})
if err != nil {
return "", err
}
data := map[string]string{"MDX": mdx}
jsonData, err := json.Marshal(data)
if err != nil {
return "", err
}
response, err := cs.rest.POST(url, string(jsonData), nil, 0, nil)
if err != nil {
return "", err
}
defer response.Body.Close()
result := Cellset{}
err = json.NewDecoder(response.Body).Decode(&result)
if err != nil {
return "", err
}
return result.ID, nil
}
// DeleteCellSet deletes a cellset
func (cs *CellService) DeleteCellSet(cellsetID string) error {
url := "/Cellsets('" + cellsetID + "')"
_, err := cs.rest.DELETE(url, nil, 0, nil)
return err
}
// ExtractCellSetCount extracts the number of cells in a cellset
func (cs *CellService) ExtractCellSetCount(cellsetID string, sandboxName string) (int, error) {
url := "/Cellsets('" + cellsetID + "')/Cells/$count"
url, err := AddURLParameters(url, map[string]string{"!sandbox": sandboxName})
if err != nil {
return 0, err
}
response, err := cs.rest.GET(url, nil, 0, nil)
if err != nil {
return 0, err
}
defer response.Body.Close()
count := 0
err = json.NewDecoder(response.Body).Decode(&count)
if err != nil {
return 0, err
}
return count, nil
}
// ExtractCellsetCells extracts cells from a cellset
func (cs *CellService) ExtractCellsetCellsRaw(cellsetID string, cellProperties []string, top, skip int, skipZeros, skipConsolidatedCells, skipRuleDerivedCells bool, sandboxName string) ([]Cell, error) {
if cellProperties == nil {
cellProperties = []string{"Value"}
}
if skipRuleDerivedCells {
cellProperties = append(cellProperties, "RuleDerived", "Updateable")
}
if skipConsolidatedCells {
cellProperties = append(cellProperties, "Consolidated")
}
if skip > 0 || skipZeros || skipRuleDerivedCells || skipConsolidatedCells {
if !SliceContains(cellProperties, "Ordinal") {
cellProperties = append(cellProperties, "Ordinal")
}
}
var filters []string
if skipZeros || skipConsolidatedCells || skipRuleDerivedCells {
if skipZeros {
filters = append(filters, "Value ne 0 and Value ne null and Value ne ''")
}
if skipConsolidatedCells {
filters = append(filters, "Consolidated eq false")
}
if skipRuleDerivedCells {
filters = append(filters, "RuleDerived eq false")
}
}
filterCells := strings.Join(filters, " and ")
cellPropertiesStr := strings.Join(cellProperties, ",")
topCells := ""
if top > 0 {
topCells = fmt.Sprintf(";$top=%d", top)
}
skipCells := ""
if skip > 0 {
skipCells = fmt.Sprintf(";$skip=%d", skip)
}
filterCellsParam := ""
if filterCells != "" {
filterCellsParam = fmt.Sprintf(";$filter=%s", filterCells)
}
url := fmt.Sprintf("/Cellsets('%s')?$expand=Cells($select=%s%s%s%s)", cellsetID, cellPropertiesStr, topCells, skipCells, filterCellsParam)
if sandboxName != "" {
err := error(nil)
url, err = AddURLParameters(url, map[string]string{"!sandbox": sandboxName})
if err != nil {
return nil, err
}
}
response, err := cs.rest.GET(url, nil, 0, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
result := Cellset{}
err = json.NewDecoder(response.Body).Decode(&result)
if err != nil {
return nil, err
}
return result.Cells, nil
}
// ExtractCellsetCellsAsync extracts cells from a cellset asynchronously
func (cs *CellService) ExtractCellsetCellsAsync(cellsetID string, cellProperties []string, sandboxName string, maxWorkers int) ([]Cell, error) {
cellCount, err := cs.ExtractCellSetCount(cellsetID, sandboxName)
if err != nil {
return nil, err
}
if cellCount == 0 {
return make([]Cell, 0), nil
}
if maxWorkers > cellCount {
maxWorkers = 1
}
partionSize := int(math.Ceil(float64(cellCount) / float64(maxWorkers)))
top := partionSize
result := make([]Cell, cellCount)
wg := sync.WaitGroup{}
errChan := make(chan error, maxWorkers)
for i := 0; i < maxWorkers; i++ {
skip := i * partionSize
wg.Add(1)
go func(skip int) {
defer wg.Done()
cells, err := cs.ExtractCellsetCellsRaw(cellsetID, cellProperties, top, skip, false, false, false, sandboxName)
if err != nil {
errChan <- err
return
}
copy(result[skip:skip+len(cells)], cells)
}(skip)
}
wg.Wait()
close(errChan)
if len(errChan) > 0 {
return nil, <-errChan
}
return result, nil
}
// UpdateCellset updates values inside a cellset
func (cs *CellService) UpdateCellset(cellsetID string, values []interface{}, valuesOrdinalOffset int, sandboxName string) error {
url := fmt.Sprintf("/Cellsets('%s')/Cells", cellsetID)
url, err := AddURLParameters(url, map[string]string{"!sandbox": sandboxName})
if err != nil {
return err
}
data := []map[string]interface{}{}
for o, value := range values {
data = append(data, map[string]interface{}{
"Ordinal": o + valuesOrdinalOffset,
"Value": value,
})
}
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
_, err = cs.rest.PATCH(url, string(jsonData), nil, 0, nil)
return err
}
// UpdateCellsetMDX updates values inside a cellset using MDX
func (cs *CellService) UpdateCellsetMDX(mdx string, values []interface{}, sandboxName string) error {
cellsetID, err := cs.CreateCellSet(mdx, sandboxName)
if err != nil {
return err
}
defer cs.DeleteCellSet(cellsetID)
return cs.UpdateCellset(cellsetID, values, 0, sandboxName)
}
// UpdateCellsetAsync updates values inside a cellset asynchronously
/* func (cs *CellService) UpdateCellsetAsync(cellsetID string, values []interface{}, sandboxName string, maxWorkers int) error {
cellCount, err := cs.ExtractCellSetCount(cellsetID, sandboxName)
if err != nil {
return err
}
if cellCount == 0 {
return nil
}
if maxWorkers > cellCount {
maxWorkers = 1
}
partionSize := int(math.Ceil(float64(cellCount) / float64(maxWorkers)))
wg := sync.WaitGroup{}
errChan := make(chan error, maxWorkers)
for i := 0; i < maxWorkers; i++ {
skip := i * partionSize
wg.Add(1)
go func(skip int) {
defer wg.Done()
end := skip + partionSize
if end > len(values) {
end = len(values)
}
err := cs.UpdateCellset(cellsetID, values[skip:end], skip, sandboxName)
if err != nil {
errChan <- err
return
}
}(skip)
}
wg.Wait()
close(errChan)
if len(errChan) > 0 {
return <-errChan
}
return nil
} */
// CellGet retrieves a cell
// First parameter is the cube name followed by the element names
func (cs *CellService) CellGet(params ...string) (interface{}, error) {
if len(params) < 3 {
return nil, fmt.Errorf("at least 3 parameters are required")
}
cubeName := params[0]
// Get cube dimensions
dimensions, err := cs.cube.GetDimensionNames(cubeName)
if err != nil {
return nil, err
}
if len(dimensions) != len(params)-1 {
return nil, fmt.Errorf("number of dimensions does not match number of elements")
}
// Build MDX
mdxBuilder := NewMDXBuilder(cubeName)
for i, dimension := range dimensions {
if i == 0 {
mdxBuilder.AddMemberOnColumns(dimension, dimension, params[i+1])
} else {
mdxBuilder.AddMemberOnWhere(dimension, dimension, params[i+1])
}
}
mdx, err := mdxBuilder.ToString()
if err != nil {
return nil, err
}
// Execute MDX
cellsetID, err := cs.CreateCellSet(mdx, "")
if err != nil {
return nil, err
}
defer cs.DeleteCellSet(cellsetID)
cell, err := cs.ExtractCellsetCellsRaw(cellsetID, []string{"Value"}, 1, 0, false, false, false, "")
if err != nil {
return nil, err
}
return cell[0].Value, nil
}
// CellPut updates a cell
// First parameter is the value, second parameter is the cube name followed by the element names
func (cs *CellService) CellPut(params ...interface{}) error {
if len(params) < 4 {
return fmt.Errorf("at least 4 parameters are required")
}
cubeName, ok := params[1].(string)
if !ok {
return fmt.Errorf("cubeName must be of type string")
}
var elements = make([]string, 0, len(params)-2)
for _, element := range params[2:] {
elementStr, ok := element.(string)
if !ok {
return fmt.Errorf("element must be of type string")
}
elements = append(elements, elementStr)
}
// Get cube dimensions
dimensions, err := cs.cube.GetDimensionNames(cubeName)
if err != nil {
return err
}
if len(dimensions) != len(elements) {
return fmt.Errorf("number of dimensions does not match number of elements")
}
url := fmt.Sprintf("/Cubes('%s')/tm1.Update", cubeName)
type CellUpdateStruct struct {
Value interface{} `json:"Value"`
Cells []struct {
Tuple []string `json:"Tuple@odata.bind"`
} `json:"Cells"`
}
data := CellUpdateStruct{
Value: params[0],
Cells: []struct {
Tuple []string `json:"Tuple@odata.bind"`
}{{
Tuple: make([]string, len(elements)),
}},
}
for i, element := range elements {
data.Cells[0].Tuple[i] = fmt.Sprintf("Dimensions('%s')/Hierarchies('%s')/Elements('%s')", dimensions[i], dimensions[i], element)
}
// Convert data to JSON
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
// Execute request
_, err = cs.rest.POST(url, string(jsonData), nil, 0, nil)
return err
}
// ExtractCellsetCells extracts cells from a cellset
func (cs *CellService) ExtractCellsetAxesAndCube(cellsetID string, sandboxName string) (*Cellset, error) {
url := fmt.Sprintf("/Cellsets('%s')?$expand=Cube($select=Name),Axes($select=Ordinal,Cardinality;$expand=Hierarchies($select=Name,UniqueName;$expand=Dimension($select=Name)),Tuples($expand=Members($select=Name,UniqueName,Attributes,DisplayInfoAbove,DisplayInfo,Type)))", cellsetID)
if sandboxName != "" {
err := error(nil)
url, err = AddURLParameters(url, map[string]string{"!sandbox": sandboxName})
if err != nil {
return nil, err
}
}
response, err := cs.rest.GET(url, nil, 0, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
result := &Cellset{}
err = json.NewDecoder(response.Body).Decode(&result)
if err != nil {
return nil, err
}
return result, nil
}
// UpdateCellsetFromDataframe updates values inside a cellset using dataframe
func (cs *CellService) UpdateCellsetFromDataframe(cubeName string, df *DataFrame, sandboxName string) error {
// Build mdx
mdx, err := df.ToMDX(cubeName)
if err != nil {
return err
}
cellsetID, err := cs.CreateCellSet(mdx, sandboxName)
if err != nil {
return err
}
defer cs.DeleteCellSet(cellsetID)
return cs.UpdateCellset(cellsetID, df.Columns[df.Headers[len(df.Headers)-1]], 0, sandboxName)
}
// ExecuteMdxToDataframe executes an MDX query and returns the result as a dataframe
func (cs *CellService) ExecuteMdxToDataframe(mdx string, sandboxName string) (*DataFrame, error) {
cellsetID, err := cs.CreateCellSet(mdx, sandboxName)
if err != nil {
return nil, err
}
defer cs.DeleteCellSet(cellsetID)
cellsetMetadata, err := cs.ExtractCellsetAxesAndCube(cellsetID, sandboxName)
if err != nil {
return nil, err
}
cellsetCells, err := cs.ExtractCellsetCellsRaw(cellsetID, []string{"Value"}, 0, 0, false, false, false, sandboxName)
if err != nil {
return nil, err
}
cellset := &Cellset{
ID: cellsetID,
Cube: cellsetMetadata.Cube,
Cells: cellsetCells,
Axes: cellsetMetadata.Axes,
}
df, err := CellSetToDataFrame(cellset)
if err != nil {
return nil, err
}
return df, nil
}
// Read via blob
func (cs *CellService) ExecuteMDXViaBlob(mdx string, sandboxName string) (*DataFrame, error) {
// create temp mdx view
// create unbound process and asciioutput data from the view into a file (csv)
// get the file using file service
// process csv file into dataframe
// cleanup temp view and file
// return dataframe
return &DataFrame{}, nil
}
// UpdateCellsetFromDataframeViaBlob Writes data to a cube via blob. Numeric valus only.
func (cs *CellService) UpdateCellsetFromDataframeViaBlob(cubeName string, df *DataFrame, sandboxName string) error {
// Convert dataframe into csv file
dataBytes, err := df.ConvertToCSVBytes()
if err != nil {
return err
}
// Upload file to tm1 using file service
randomNumber := 10000 + rand.Intn(90000)
randomName := fmt.Sprintf("tm1go_dataload_temp_%d.csv", randomNumber)
err = cs.file.CreateCompressed(randomName, nil, dataBytes)
if err != nil {
return err
}
// Check TM1 version
// v11 requires .blb extension for source files
if !IsV1GreaterOrEqualToV2(cs.rest.version, "12.0.0") {
randomName = randomName + ".blb"
}
// Create unboud process and use the file as a source to load data into cube
process := NewProcess(randomName)
process.DataSource.Type = "ASCII"
process.DataSource.AsciiDecimalSeparator = "."
process.DataSource.AsciiDelimiterChar = ","
process.DataSource.AsciiDelimiterType = "Character"
process.DataSource.AsciiHeaderRecords = 1
process.DataSource.AsciiQuoteCharacter = "\""
process.DataSource.AsciiThousandSeparator = ","
process.DataSource.DataSourceNameForClient = randomName
process.DataSource.DataSourceNameForServer = randomName
process.Variables = make([]ProcessVariable, len(df.Headers))
// Add dimension variables
for i := 0; i < len(df.Headers)-1; i++ {
process.Variables[i] = ProcessVariable{
Name: fmt.Sprintf("v%d", i+1),
Type: "String",
StartByte: 0,
EndByte: 0,
Position: i + 1,
}
}
// Add last value variable
valueVariable := fmt.Sprintf("v%d", len(df.Headers))
process.Variables[len(df.Headers)-1] = ProcessVariable{
Name: valueVariable,
Type: "Numeric",
StartByte: 0,
EndByte: 0,
Position: len(df.Headers),
}
// Add process script
scriptString := "CellPutN(" + valueVariable + ",'" + cubeName + "',"
for i := 0; i < len(df.Headers)-1; i++ {
scriptString += "v" + fmt.Sprintf("%d", i+1) + ","
}
scriptString = scriptString[:len(scriptString)-1]
scriptString += ");"
process.DataProcedure = scriptString
executeProcessResult, err := cs.process.ExecuteProcessWithReturn(process, nil, 0)
if err != nil {
return err
}
if executeProcessResult.ProcessExecuteStatusCode != 0 {
return fmt.Errorf("error executing process: %s", executeProcessResult.ProcessExecuteStatusCode.ToString())
}
// Cleanup: delete file
err = cs.file.Delete(strings.TrimSuffix(randomName, ".blb"), nil)
if err != nil {
return err
}
return nil
}