@@ -15,37 +15,45 @@ import {
1515 API_MIGRATE_ILM_POLICY_URL ,
1616 REPORTING_MANAGEMENT_HOME ,
1717} from '../../../common/constants' ;
18- import {
19- DownloadReportFn ,
20- JobId ,
21- ManagementLinkFn ,
22- ReportApiJSON ,
23- ReportDocument ,
24- ReportSource ,
25- } from '../../../common/types' ;
18+ import { DownloadReportFn , JobId , ManagementLinkFn , ReportApiJSON } from '../../../common/types' ;
2619import { add } from '../../notifier/job_completion_notifications' ;
27-
28- export interface JobQueueEntry {
29- _id : string ;
30- _source : ReportSource ;
31- }
20+ import { Job } from '../job' ;
3221
3322export interface JobContent {
3423 content : string ;
3524 content_type : boolean ;
3625}
3726
38- interface JobParams {
39- [ paramName : string ] : any ;
40- }
41-
4227export interface DiagnoseResponse {
4328 help : string [ ] ;
4429 success : boolean ;
4530 logs : string ;
4631}
4732
48- export class ReportingAPIClient {
33+ interface JobParams {
34+ [ paramName : string ] : any ;
35+ }
36+
37+ interface IReportingAPI {
38+ getReportURL ( jobId : string ) : string ;
39+ downloadReport ( jobId : string ) : void ;
40+ deleteReport ( jobId : string ) : Promise < void > ;
41+ list ( page : number , jobIds : string [ ] ) : Promise < Job [ ] > ;
42+ total ( ) : Promise < number > ;
43+ getError ( jobId : string ) : Promise < JobContent > ;
44+ getInfo ( jobId : string ) : Promise < Job > ;
45+ findForJobIds ( jobIds : string [ ] ) : Promise < Job [ ] > ;
46+ getReportingJobPath ( exportType : string , jobParams : JobParams ) : string ;
47+ createReportingJob ( exportType : string , jobParams : any ) : Promise < Job > ;
48+ getServerBasePath ( ) : string ;
49+ verifyConfig ( ) : Promise < DiagnoseResponse > ;
50+ verifyBrowser ( ) : Promise < DiagnoseResponse > ;
51+ verifyScreenCapture ( ) : Promise < DiagnoseResponse > ;
52+ getManagementLink : ManagementLinkFn ;
53+ getDownloadLink : DownloadReportFn ;
54+ }
55+
56+ export class ReportingAPIClient implements IReportingAPI {
4957 private http : HttpSetup ;
5058
5159 constructor ( http : HttpSetup ) {
@@ -71,68 +79,75 @@ export class ReportingAPIClient {
7179 } ) ;
7280 }
7381
74- public list = ( page = 0 , jobIds : string [ ] = [ ] ) : Promise < JobQueueEntry [ ] > => {
82+ public async list ( page = 0 , jobIds : string [ ] = [ ] ) {
7583 const query = { page } as any ;
7684 if ( jobIds . length > 0 ) {
7785 // Only getting the first 10, to prevent URL overflows
7886 query . ids = jobIds . slice ( 0 , 10 ) . join ( ',' ) ;
7987 }
8088
81- return this . http . get ( `${ API_LIST_URL } /list` , {
89+ const jobQueueEntries : ReportApiJSON [ ] = await this . http . get ( `${ API_LIST_URL } /list` , {
8290 query,
8391 asSystemRequest : true ,
8492 } ) ;
85- } ;
8693
87- public total ( ) : Promise < number > {
88- return this . http . get ( `${ API_LIST_URL } /count` , {
94+ return jobQueueEntries . map ( ( report ) => new Job ( report ) ) ;
95+ }
96+
97+ public async total ( ) {
98+ return await this . http . get ( `${ API_LIST_URL } /count` , {
8999 asSystemRequest : true ,
90100 } ) ;
91101 }
92102
93- public getContent ( jobId : string ) : Promise < JobContent > {
94- return this . http . get ( `${ API_LIST_URL } /output/${ jobId } ` , {
103+ public async getError ( jobId : string ) {
104+ return await this . http . get ( `${ API_LIST_URL } /output/${ jobId } ` , {
95105 asSystemRequest : true ,
96106 } ) ;
97107 }
98108
99- public getInfo ( jobId : string ) : Promise < ReportApiJSON > {
100- return this . http . get ( `${ API_LIST_URL } /info/${ jobId } ` , {
109+ public async getInfo ( jobId : string ) {
110+ const report : ReportApiJSON = await this . http . get ( `${ API_LIST_URL } /info/${ jobId } ` , {
101111 asSystemRequest : true ,
102112 } ) ;
113+ return new Job ( report ) ;
103114 }
104115
105- public findForJobIds = ( jobIds : JobId [ ] ) : Promise < ReportDocument [ ] > => {
106- return this . http . fetch ( `${ API_LIST_URL } /list` , {
116+ public async findForJobIds ( jobIds : JobId [ ] ) {
117+ const reports : ReportApiJSON [ ] = await this . http . fetch ( `${ API_LIST_URL } /list` , {
107118 query : { page : 0 , ids : jobIds . join ( ',' ) } ,
108119 method : 'GET' ,
109120 } ) ;
110- } ;
121+ return reports . map ( ( report ) => new Job ( report ) ) ;
122+ }
111123
112124 /*
113125 * Return a URL to queue a job, with the job params encoded in the query string of the URL. Used for copying POST URL
114126 */
115- public getReportingJobPath = ( exportType : string , jobParams : JobParams ) => {
127+ public getReportingJobPath ( exportType : string , jobParams : JobParams ) {
116128 const params = stringify ( { jobParams : rison . encode ( jobParams ) } ) ;
117129 return `${ this . http . basePath . prepend ( API_BASE_GENERATE ) } /${ exportType } ?${ params } ` ;
118- } ;
130+ }
119131
120132 /*
121133 * Sends a request to queue a job, with the job params in the POST body
122134 */
123- public createReportingJob = async ( exportType : string , jobParams : any ) => {
135+ public async createReportingJob ( exportType : string , jobParams : any ) {
124136 const jobParamsRison = rison . encode ( jobParams ) ;
125- const resp = await this . http . post ( `${ API_BASE_GENERATE } /${ exportType } ` , {
126- method : 'POST' ,
127- body : JSON . stringify ( {
128- jobParams : jobParamsRison ,
129- } ) ,
130- } ) ;
137+ const resp : { job : ReportApiJSON } = await this . http . post (
138+ `${ API_BASE_GENERATE } /${ exportType } ` ,
139+ {
140+ method : 'POST' ,
141+ body : JSON . stringify ( {
142+ jobParams : jobParamsRison ,
143+ } ) ,
144+ }
145+ ) ;
131146
132147 add ( resp . job . id ) ;
133148
134- return resp ;
135- } ;
149+ return new Job ( resp . job ) ;
150+ }
136151
137152 public getManagementLink : ManagementLinkFn = ( ) =>
138153 this . http . basePath . prepend ( REPORTING_MANAGEMENT_HOME ) ;
@@ -148,28 +163,31 @@ export class ReportingAPIClient {
148163 /*
149164 * Diagnostic-related API calls
150165 */
151- public verifyConfig = ( ) : Promise < DiagnoseResponse > =>
152- this . http . post ( `${ API_BASE_URL } /diagnose/config` , {
166+ public async verifyConfig ( ) {
167+ return await this . http . post ( `${ API_BASE_URL } /diagnose/config` , {
153168 asSystemRequest : true ,
154169 } ) ;
170+ }
155171
156172 /*
157173 * Diagnostic-related API calls
158174 */
159- public verifyBrowser = ( ) : Promise < DiagnoseResponse > =>
160- this . http . post ( `${ API_BASE_URL } /diagnose/browser` , {
175+ public async verifyBrowser ( ) {
176+ return await this . http . post ( `${ API_BASE_URL } /diagnose/browser` , {
161177 asSystemRequest : true ,
162178 } ) ;
179+ }
163180
164181 /*
165182 * Diagnostic-related API calls
166183 */
167- public verifyScreenCapture = ( ) : Promise < DiagnoseResponse > =>
168- this . http . post ( `${ API_BASE_URL } /diagnose/screenshot` , {
184+ public async verifyScreenCapture ( ) {
185+ return await this . http . post ( `${ API_BASE_URL } /diagnose/screenshot` , {
169186 asSystemRequest : true ,
170187 } ) ;
188+ }
171189
172- public migrateReportingIndicesIlmPolicy = ( ) : Promise < void > => {
173- return this . http . put ( `${ API_MIGRATE_ILM_POLICY_URL } ` ) ;
174- } ;
190+ public async migrateReportingIndicesIlmPolicy ( ) {
191+ return await this . http . put ( `${ API_MIGRATE_ILM_POLICY_URL } ` ) ;
192+ }
175193}
0 commit comments