1+ import { stringify , NIL , parse } from 'uuid' ;
12import IOperation , { FetchOptions , GetSchemaOptions , FinishedOptions , defaultMaxRows } from '../contracts/IOperation' ;
23import HiveDriver from '../hive/HiveDriver' ;
34import {
@@ -13,12 +14,15 @@ import OperationStatusHelper from './OperationStatusHelper';
1314import SchemaHelper from './SchemaHelper' ;
1415import FetchResultsHelper from './FetchResultsHelper' ;
1516import CompleteOperationHelper from './CompleteOperationHelper' ;
17+ import IDBSQLLogger , { LogLevel } from '../contracts/IDBSQLLogger' ;
1618
1719export default class DBSQLOperation implements IOperation {
1820 private driver : HiveDriver ;
1921
2022 private operationHandle : TOperationHandle ;
2123
24+ private logger : IDBSQLLogger ;
25+
2226 private _status : OperationStatusHelper ;
2327
2428 private _schema : SchemaHelper ;
@@ -27,9 +31,15 @@ export default class DBSQLOperation implements IOperation {
2731
2832 private _completeOperation : CompleteOperationHelper ;
2933
30- constructor ( driver : HiveDriver , operationHandle : TOperationHandle , directResults ?: TSparkDirectResults ) {
34+ constructor (
35+ driver : HiveDriver ,
36+ operationHandle : TOperationHandle ,
37+ logger : IDBSQLLogger ,
38+ directResults ?: TSparkDirectResults ,
39+ ) {
3140 this . driver = driver ;
3241 this . operationHandle = operationHandle ;
42+ this . logger = logger ;
3343 this . _status = new OperationStatusHelper ( this . driver , this . operationHandle , directResults ?. operationStatus ) ;
3444 this . _schema = new SchemaHelper ( this . driver , this . operationHandle , directResults ?. resultSetMetadata ) ;
3545 this . _data = new FetchResultsHelper ( this . driver , this . operationHandle , [ directResults ?. resultSet ] ) ;
@@ -38,6 +48,11 @@ export default class DBSQLOperation implements IOperation {
3848 this . operationHandle ,
3949 directResults ?. closeOperation ,
4050 ) ;
51+ this . logger . log ( LogLevel . debug , `Operation created with id: ${ this . getId ( ) } ` ) ;
52+ }
53+
54+ getId ( ) {
55+ return stringify ( this . operationHandle ?. operationId ?. guid || parse ( NIL ) ) ;
4156 }
4257
4358 /**
@@ -56,6 +71,7 @@ export default class DBSQLOperation implements IOperation {
5671 const chunk = await this . fetchChunk ( options ) ;
5772 data . push ( chunk ) ;
5873 } while ( await this . hasMoreRows ( ) ) ; // eslint-disable-line no-await-in-loop
74+ this . logger ?. log ( LogLevel . debug , `Fetched all data from operation with id: ${ this . getId ( ) } ` ) ;
5975
6076 return data . flat ( ) ;
6177 }
@@ -79,6 +95,10 @@ export default class DBSQLOperation implements IOperation {
7995 return Promise . all ( [ this . _schema . fetch ( ) , this . _data . fetch ( options ?. maxRows || defaultMaxRows ) ] ) . then (
8096 ( [ schema , data ] ) => {
8197 const result = getResult ( schema , data ? [ data ] : [ ] ) ;
98+ this . logger ?. log (
99+ LogLevel . debug ,
100+ `Fetched chunk of size: ${ options ?. maxRows || defaultMaxRows } from operation with id: ${ this . getId ( ) } ` ,
101+ ) ;
82102 return Promise . resolve ( result ) ;
83103 } ,
84104 ) ;
@@ -90,6 +110,7 @@ export default class DBSQLOperation implements IOperation {
90110 * @throws {StatusError }
91111 */
92112 async status ( progress : boolean = false ) : Promise < TGetOperationStatusResp > {
113+ this . logger ?. log ( LogLevel . debug , `Fetching status for operation with id: ${ this . getId ( ) } ` ) ;
93114 return this . _status . status ( progress ) ;
94115 }
95116
@@ -98,6 +119,7 @@ export default class DBSQLOperation implements IOperation {
98119 * @throws {StatusError }
99120 */
100121 cancel ( ) : Promise < Status > {
122+ this . logger ?. log ( LogLevel . debug , `Operation with id: ${ this . getId ( ) } canceled.` ) ;
101123 return this . _completeOperation . cancel ( ) ;
102124 }
103125
@@ -106,6 +128,7 @@ export default class DBSQLOperation implements IOperation {
106128 * @throws {StatusError }
107129 */
108130 close ( ) : Promise < Status > {
131+ this . logger ?. log ( LogLevel . debug , `Closing operation with id: ${ this . getId ( ) } ` ) ;
109132 return this . _completeOperation . close ( ) ;
110133 }
111134
@@ -126,6 +149,7 @@ export default class DBSQLOperation implements IOperation {
126149 }
127150
128151 await this . _status . waitUntilReady ( options ) ;
152+ this . logger ?. log ( LogLevel . debug , `Fetching schema for operation with id: ${ this . getId ( ) } ` ) ;
129153
130154 return this . _schema . fetch ( ) ;
131155 }
0 commit comments