@@ -19,6 +19,7 @@ import { MetaInfoType, MetaObject, MetaObjectPayload } from "../models/meta";
1919import {
2020 getCurrentToken ,
2121 getHttpsAgent ,
22+ IToken ,
2223} from "../services/kdbInsights/codeFlowLogin" ;
2324import { InsightsNode } from "../services/kdbTreeProvider" ;
2425import { GetDataObjectPayload } from "../models/data" ;
@@ -38,6 +39,7 @@ import { InsightsConfig, InsightsEndpoints } from "../models/config";
3839import { convertTimeToTimestamp } from "../utils/dataSource" ;
3940import { ScratchpadRequestBody } from "../models/scratchpad" ;
4041import { StructuredTextResults } from "../models/queryResult" ;
42+ import { UDARequestBody } from "../models/uda" ;
4143
4244export class InsightsConnection {
4345 public connected : boolean ;
@@ -160,13 +162,15 @@ export class InsightsConnection {
160162 import : "servicebroker/scratchpad/import/data" ,
161163 importSql : "servicebroker/scratchpad/import/sql" ,
162164 importQsql : "servicebroker/scratchpad/import/qsql" ,
165+ importUDA : "servicebroker/scratchpad/import/uda" ,
163166 reset : "servicebroker/scratchpad/reset" ,
164167 } ,
165168 serviceGateway : {
166169 meta : "servicegateway/meta" ,
167170 data : "servicegateway/data" ,
168171 sql : "servicegateway/qe/sql" ,
169172 qsql : "servicegateway/qe/qsql" ,
173+ udaBase : "servicegateway/" ,
170174 } ,
171175 } ;
172176 // uncomment this WHEN the insights version is available
@@ -395,6 +399,157 @@ export class InsightsConnection {
395399 }
396400 }
397401
402+ public async generateToken ( ) : Promise < IToken | undefined > {
403+ const token = await getCurrentToken (
404+ this . node . details . server ,
405+ this . node . details . alias ,
406+ this . node . details . realm || "insights" ,
407+ ! ! this . node . details . insecure ,
408+ ) ;
409+ if ( token === undefined ) {
410+ tokenUndefinedError ( this . connLabel ) ;
411+ }
412+ return token ;
413+ }
414+
415+ public async getUDAQuery (
416+ udaReqBody : UDARequestBody ,
417+ ) : Promise < any | undefined > {
418+ if ( this . connected && this . connEndpoints ) {
419+ const udaEndpoint =
420+ this . connEndpoints . serviceGateway . udaBase +
421+ udaReqBody . name . split ( "." ) . slice ( 1 ) . join ( "/" ) ;
422+ const udaURL = new url . URL ( udaEndpoint , this . node . details . server ) ;
423+ const token = await this . generateToken ( ) ;
424+ if ( token === undefined ) {
425+ return undefined ;
426+ }
427+
428+ const headers = {
429+ Authorization : `Bearer ${ token . accessToken } ` ,
430+ Accept : "application/octet-stream" ,
431+ "Content-Type" : "application/json" ,
432+ } ;
433+
434+ const body = udaReqBody . params ;
435+
436+ const options : AxiosRequestConfig = {
437+ method : "post" ,
438+ url : udaURL . toString ( ) ,
439+ data : body ,
440+ headers : headers ,
441+ responseType : "arraybuffer" ,
442+ httpsAgent : getHttpsAgent ( this . node . details . insecure ) ,
443+ } ;
444+ const results = await window . withProgress (
445+ {
446+ location : ProgressLocation . Notification ,
447+ cancellable : false ,
448+ } ,
449+ async ( progress , token ) => {
450+ token . onCancellationRequested ( ( ) => {
451+ kdbOutputLog ( `User cancelled the Datasource Run.` , "WARNING" ) ;
452+ } ) ;
453+
454+ progress . report ( { message : "Query executing..." } ) ;
455+
456+ return await axios ( options )
457+ . then ( ( response : any ) => {
458+ kdbOutputLog (
459+ `[Datasource RUN] Status: ${ response . status } .` ,
460+ "INFO" ,
461+ ) ;
462+ if ( isCompressed ( response . data ) ) {
463+ response . data = uncompress ( response . data ) ;
464+ }
465+ return {
466+ error : "" ,
467+ arrayBuffer : response . data . buffer
468+ ? response . data . buffer
469+ : response . data ,
470+ } ;
471+ } )
472+ . catch ( ( error : any ) => {
473+ kdbOutputLog (
474+ `[Datasource RUN] Status: ${ error . response . status } .` ,
475+ "INFO" ,
476+ ) ;
477+ return {
478+ error : { buffer : error . response . data } ,
479+ arrayBuffer : undefined ,
480+ } ;
481+ } ) ;
482+ } ,
483+ ) ;
484+ return results ;
485+ }
486+ }
487+
488+ public async getUDAScratchpadQuery (
489+ udaReqBody : UDARequestBody ,
490+ ) : Promise < any | undefined > {
491+ if ( this . connected && this . connEndpoints ) {
492+ const isTableView = udaReqBody . returnFormat === "structuredText" ;
493+ const udaURL = new url . URL (
494+ this . connEndpoints . scratchpad . importUDA ,
495+ this . node . details . server ,
496+ ) ;
497+ const token = await this . generateToken ( ) ;
498+ if ( token === undefined ) {
499+ return undefined ;
500+ }
501+ const username = jwtDecode < JwtUser > ( token . accessToken ) ;
502+ if ( username === undefined || username . preferred_username === "" ) {
503+ invalidUsernameJWT ( this . connLabel ) ;
504+ }
505+
506+ const headers = {
507+ Authorization : `Bearer ${ token . accessToken } ` ,
508+ Username : username . preferred_username ,
509+ "Content-Type" : "application/json" ,
510+ } ;
511+
512+ const udaResponse = await window . withProgress (
513+ {
514+ location : ProgressLocation . Notification ,
515+ cancellable : false ,
516+ } ,
517+ async ( _progress , token ) => {
518+ token . onCancellationRequested ( ( ) => {
519+ kdbOutputLog ( `User cancelled the UDA execution.` , "WARNING" ) ;
520+ } ) ;
521+
522+ const udaRes = await axios
523+ . post ( udaURL . toString ( ) , udaReqBody , {
524+ headers,
525+ httpsAgent : getHttpsAgent ( this . node . details . insecure ) ,
526+ } )
527+ . then ( ( response : any ) => {
528+ if ( response . data . error ) {
529+ return response . data ;
530+ } else {
531+ kdbOutputLog ( `[UDA] Status: ${ response . status } ` , "INFO" ) ;
532+ if ( ! response . data . error ) {
533+ if ( isTableView ) {
534+ response . data = JSON . parse (
535+ response . data . data ,
536+ ) as StructuredTextResults ;
537+ }
538+ return response . data ;
539+ }
540+ return response . data ;
541+ }
542+ } ) ;
543+ return udaRes ;
544+ } ,
545+ ) ;
546+ return udaResponse ;
547+ } else {
548+ this . noConnectionOrEndpoints ( ) ;
549+ }
550+ return undefined ;
551+ }
552+
398553 public async getScratchpadQuery (
399554 query : string ,
400555 context ?: string ,
@@ -444,7 +599,7 @@ export class InsightsConnection {
444599 "Content-Type" : "application/json" ,
445600 } ;
446601
447- const spReponse = await window . withProgress (
602+ const spResponse = await window . withProgress (
448603 {
449604 location : ProgressLocation . Notification ,
450605 cancellable : false ,
@@ -503,7 +658,7 @@ export class InsightsConnection {
503658 return spRes ;
504659 } ,
505660 ) ;
506- return spReponse ;
661+ return spResponse ;
507662 } else {
508663 this . noConnectionOrEndpoints ( ) ;
509664 }
0 commit comments