'use strict' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import * as azdata from 'azdata' ; import { TextDecoder } from 'util' ; import { newPanel , WebViewWithUri } from 'common-backend' ; import { MssqlExtensionApi , IFileNode } from './mssqlapis' ; let current = undefined ; export function activate ( context ) { context . subscriptions . push ( vscode . commands . registerCommand ( 'sanddance.view' , ( commandContext ) => { if ( ! commandContext ) { vscode . window . showErrorMessage ( 'No file was specified for the View in SandDance command' ) ; return ; } if ( commandContext instanceof vscode . Uri ) { viewFileUriInSandDance ( < vscode . Uri > commandContext , context ) ; } else if ( commandContext . nodeInfo ) { downloadAndViewInSandDance ( commandContext , context ) ; } } ) ) ; vscode . commands . executeCommand ( 'setContext' , 'showVisualizer' , true ) ; azdata . queryeditor . registerQueryEventListener ( { async onQueryEvent ( type , document , args ) { if ( type === 'visualize' ) { const providerid = document . providerId ; const provider = azdata . dataprotocol . getProvider ( providerid , azdata . DataProviderType . QueryProvider ) ; let data = await provider . getQueryRows ( { ownerUri : document . uri , batchIndex : args . batchId , resultSetIndex : args . id , rowsStartIndex : 0 , rowsCount : args . rowCount } ) ; let rows = data . resultSubset . rows ; let columns = args . columnInfo ; let rowsCount = args . rowCount ; let jsonArray = [ ] ; interface jsonType { [ key ] : any ; } for ( let row = 0 ; row < rowsCount ; row ++ ) { let jsonObject = { } ; for ( let col = 0 ; col < columns . length ; col ++ ) { if ( ! rows [ row ] [ col ] . isNull ) { jsonObject [ columns [ col ] . columnName ] = rows [ row ] [ col ] . displayValue ; } } jsonArray . push ( jsonObject ) ; } viewInSandDance ( ( ) => { return new Promise < string > ( resolve => resolve ( JSON . stringify ( jsonArray ) ) ) ; } , document . uri , 'json' , context ) ; } } } ) ; } async function downloadAndViewInSandDance ( commandContext , context ) { try { let file = await getHdfsFileAsString ( commandContext ) ; if ( file ) { const { contents } = file ; viewInSandDance ( ( ) => new Promise < string > ( resolve => resolve ( contents ) ) , file . fsUriPath , path . extname ( file . fsUriPath ) . substring ( 1 ) , context ) ; } } catch ( e ) { const error = e as Error ; vscode . window . showErrorMessage ( `Error viewing in sanddance: ${ error . message ? error . message : error }` ) ; } } function viewInSandDance ( rawTextPromise , uriFsPath , type , context , uriTabName ? ) { const columnToShowIn = vscode . window . activeTextEditor ? vscode . window . activeTextEditor . viewColumn : undefined ; if ( current && current . uriFsPath !== uriFsPath ) { current . panel . dispose ( ) ; current = undefined ; } if ( current ) { current . panel . reveal ( columnToShowIn ) ; } else { current = newPanel ( context , uriFsPath , uriTabName ) ; current . panel . onDidDispose ( ( ) => { current = undefined ; } , null , context . subscriptions ) ; current . panel . webview . onDidReceiveMessage ( message => { switch ( message . command ) { case 'getFileContent' : { rawTextPromise ( ) . then ( rawText => { if ( current && current . panel . visible ) { const dataFile = { type , rawText } ; const compactUI = context . globalState . get ( 'compactUI' ) ; current . panel . webview . postMessage ( { command : 'gotFileContent' , dataFile , compactUI } ) ; } } ) ; break ; } case 'setCompactUI' : { context . globalState . update ( 'compactUI' , message . compactUI ) ; break ; } } } , undefined , context . subscriptions ) ; } } function viewFileUriInSandDance ( fileUri , context , uriTabName ? ) { const p = ( ) => new Promise < string > ( resolve => { vscode . workspace . fs . readFile ( fileUri ) . then ( uint8array => { resolve ( new TextDecoder ( ) . decode ( uint8array ) ) ; } ) ; } ) ; viewInSandDance ( p , fileUri . fsPath , path . extname ( fileUri . fsPath ) . substring ( 1 ) , context , uriTabName ) ; } export async function getHdfsFileAsString ( commandContext ) { let extension = vscode . extensions . getExtension ( 'Microsoft.mssql' ) ; if ( extension ) { let extensionApi = extension . exports ; let browser = extensionApi . getMssqlObjectExplorerBrowser ( ) ; let node = await browser . getNode < IFileNode > ( commandContext ) ; let contents = await node . getFileContentsAsString ( ) ; if ( contents !== undefined ) { return { contents , fsUriPath : node . getNodeInfo ( ) . label } ; } } } export function deactivate ( ) { vscode . commands . executeCommand ( 'setContext' , 'showVisualizer' , false ) ; }