- Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathutils.ts
162 lines (147 loc) · 5.23 KB
/
utils.ts
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
/*
MIT License
Copyright (c) 2021 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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 NONINFRINGEMENT. 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.
*/
importpathfrom'path';
import{Looker40SDKasLookerSDK,IDashboard,IRenderTask}from'@looker/sdk';
/**
* Resolve a file name relative to the root of the repository
* @param fileName to path to the root relative to the examples folder
*/
exportconstrootFile=(fileName: string)=>{
constresult=path.join(__dirname,'/../..',fileName);
returnresult;
};
/**
* Path the ini file name to the root folder of the repository
* @param fileName name of ini file. defaults to `looker.ini`
*/
exportconstrootIni=(fileName='looker.ini')=>{
returnrootFile(fileName);
};
/**
* Find a dashboard by title
* @param sdk Looker SDK object
* @param {string} title of dashboard
* @returns {Promise<IDashboard>} the matched dashboard
*/
exportconstgetDashboard=async(sdk: LookerSDK,title: string)=>{
const[dash]=awaitsdk.ok(sdk.search_dashboards({ title }));
if(!dash){
console.warn(`No dashboard titled "${title}" was found`);
constall=awaitsdk.ok(sdk.all_dashboards('id,title'));
consttitles=all.map((t)=>`${t.id}:${t.title}`);
console.log(`Available dashboards are:\n${titles.join('\n')}\n`);
}
returndash;
};
/**
* Get a tile by title from a dashboard
* @param {IDashboard} dash Dashboard to search
* @param {string} title Title title to find
* @returns {IDashboardElement | undefined} Returns the found tile or undefined
*/
exportconstgetDashboardTile=(dash: IDashboard,title: string)=>{
title=title.toLowerCase();
if(!dash.dashboard_elements)returnundefined;
const[tile]=dash.dashboard_elements.filter(
(t)=>String(t.title).toLowerCase()===title
);
if(!tile){
console.warn(
`No tile titled "${title}" found on Dashboard "${dash.title}"`
);
consttiles=dash.dashboard_elements
.filter((t)=>typeoft.query_id==='number')
.map((t)=>t.title);
console.log(`Available tiles with queries are:\n${tiles.join('\n')}\n`);
}
returntile;
};
/**
* Wait specified milliseconds
* @param {number} ms time in milliseconds
* @returns {Promise<unknown>} promise timeout
*/
exportconstsleep=async(ms: number)=>{
returnnewPromise((resolve)=>{
setTimeout(resolve,ms);
});
};
/**
* Progress ticker callback type
*/
exporttypeProgressTicker=(
elapsed: number,
message: string|IRenderTask
)=>void;
/**
* Default render progress tick reporter
* @param {number} elapsed Number of seconds elapsed for render task
* @param {string | IRenderTask} message Message to output or render task poll (if failed)
*/
constdefaultProgress=(elapsed: number,message: string|IRenderTask)=>{
if(typeofmessage==='string'){
if(elapsed>=0){
console.log(`${elapsed} seconds elapsed ${message}`);
}else{
console.log(message);
}
}else{
// expected to be a poll if we got here
constpoll=messageasIRenderTask;
if(poll.status==='failure'){
console.error('Render failed. Details:');
console.error({ poll });
consterr=newError();
err.message=`${poll.status}: ${poll.status_detail} for ${poll.result_format} render by User ID ${poll.user_id}`;
throwerr;
}
}
};
/**
* General-purpose "wait for render task to complete" function
* @param {LookerSDK} sdk LookerSDK object
* @param {string} taskId render task id
* @param {ProgressTicker} progressTick callback for progress ticks
* @param {number} pause number of seconds to wait before checking progress. Defaults to 0.5
* @returns {Promise<string>} Results of render task. Can be a binary string
*/
exportconstwaitForRender=async(
sdk: LookerSDK,
taskId: string,
progressTick: ProgressTicker=defaultProgress,
pause=0.5
)=>{
// poll the render task until it completes
letelapsed=0.0;
constdelay=pause*1000;// convert seconds to milliseconds
while(true){
constpoll=awaitsdk.ok(sdk.render_task(taskId));
if(poll.status==='failure'){
progressTick(-1,poll);
return;
}
if(poll.status==='success'){
break;
}
awaitsleep(delay);
progressTick((elapsed+=pause),'');
}
returnawaitsdk.ok(sdk.render_task_results(taskId));
};