-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (98 loc) · 2.56 KB
/
index.js
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
const dotenv = require('dotenv')
dotenv.config()
// const xmla = require('xmla4js')
const xmla = require('./xmla4js')
const SSAS_URL = process.env.ISAPI_ENDPOINT
const username = process.env.DB_USER
const password = process.env.DB_PWD
const authHeader = `Basic ${Buffer.from(
username + ':' + (password ?? ''),
).toString('base64')}`
console.log(SSAS_URL)
const xmlaClient = new xmla.Xmla()
function executeTabularQuery(xmla, cubeName, query) {
return new Promise((resolve, reject) => {
const options = {
url: SSAS_URL,
async: true,
statement: query,
properties: {
Catalog: cubeName,
},
headers: {
Authorization: authHeader,
},
success: function (_xmla, _xmlaRequest, xmlaResponse) {
resolve(xmlaResponse.fetchAllAsObject())
},
error: function (_xmla, _xmlaRequest, exception) {
reject(exception)
},
}
xmla.executeTabular(options)
})
}
function executeMDXQuery(xmla, cubeName, query) {
return new Promise((resolve, reject) => {
const options = {
url: SSAS_URL,
async: true,
statement: query,
properties: {
Catalog: cubeName,
},
headers: {
Authorization: authHeader,
},
success: function (_xmla, _xmlaRequest, xmlaResponse) {
try {
const data = fetchAsObject.call(xmlaResponse)
resolve(data)
} catch (error) {
reject(error)
}
},
error: function (_xmla, _xmlaRequest, exception) {
reject(exception)
},
}
xmla.executeMultiDimensional(options)
})
}
const start = async () => {
console.time('query')
const CUBE = 'Cubo Nissan'
const mdxQuery = `
DRILLTHROUGH
SELECT {[Measures].[Matriculaciones]} ON COLUMNS
FROM [Cubo Nissan]
WHERE (
{[Fecha].[Año - Mes - Día].[Año].&[2023].&[Junio]},
{[Color].[Color].&[3695]}
)
`
const daxQuery = `
EVALUATE
CALCULATETABLE (
DETAILROWS ([Matriculaciones]),
'Fecha'[Fecha] >= DATE (2023, 6, 1),
'Fecha'[Fecha] <= DATE (2023, 6, 30),
'Color'[Color] = "3695"
)
`
// const query = `
// SELECT {[Measures].[Matriculaciones]} ON COLUMNS,
// NON EMPTY [Fecha].[Año - Mes - Día].[Año] ON ROWS
// FROM [Cubo Nissan]
// `
try {
// const res = await executeMDXQuery(xmlaClient, CUBE, query)
const res = await executeTabularQuery(xmlaClient, CUBE, daxQuery)
// console.log(res)
console.log(res.length)
} catch (err) {
console.log(err)
}
console.timeEnd('query')
}
start()