Skip to content

Commit 8f5bc53

Browse files
authored
feat(presto-client): include get query metadata method (#20)
1 parent 582c857 commit 8f5bc53

19 files changed

+509
-37
lines changed

apps/nest-server/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# NestJS Application
22

3-
This application was generated using [Nx](https://nx.dev).
3+
This is an example application to showcase how to use the Presto JS Client. Look at the app service file (src/app/app.service.ts) to see how to use the client for several different operations, and how to handle the results as well as the errors.
44

55
## Running the Application
66

apps/nest-server/src/app/app.controller.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, Param } from '@nestjs/common'
1+
import { Controller, Get, Query } from '@nestjs/common'
22

33
import { AppService } from './app.service'
44

@@ -7,7 +7,7 @@ export class AppController {
77
constructor(private readonly appService: AppService) {}
88

99
@Get('get-schemas')
10-
async getSchemas(@Param('catalog') catalog: string) {
10+
async getSchemas(@Query('catalog') catalog: string) {
1111
try {
1212
return await this.appService.getSchemas(catalog)
1313
} catch (err) {
@@ -33,6 +33,15 @@ export class AppController {
3333
}
3434
}
3535

36+
@Get('get-query-info')
37+
async getQueryInfo(@Query('queryId') queryId: string) {
38+
try {
39+
return await this.appService.getQueryInfo(queryId)
40+
} catch (err) {
41+
console.error(err)
42+
}
43+
}
44+
3645
@Get('presto-error')
3746
async getDataWithError() {
3847
try {

apps/nest-server/src/app/app.service.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@nestjs/common'
2-
import PrestoClient, { PrestoClientConfig, PrestoError } from '@prestodb/presto-js-client'
2+
import PrestoClient, { PrestoClientConfig, PrestoError, QueryInfo } from '@prestodb/presto-js-client'
33

44
@Injectable()
55
export class AppService {
@@ -54,6 +54,20 @@ export class AppService {
5454
}
5555
}
5656

57+
async getQueryInfo(queryId: string): Promise<QueryInfo | undefined> {
58+
const clientParams: PrestoClientConfig = {
59+
host: 'http://localhost',
60+
port: 8080,
61+
user: 'root',
62+
}
63+
const client = new PrestoClient(clientParams)
64+
try {
65+
return await client.getQueryInfo(queryId)
66+
} catch (error) {
67+
console.error(error.message)
68+
}
69+
}
70+
5771
async getDataWithError(): Promise<unknown> {
5872
const clientParams: PrestoClientConfig = {
5973
catalog: 'tpch',

presto-client/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ Additional notes
103103
- The `query` method will automatically retry the query if it fails due to a transient error.
104104
- The `query` method will cancel the query if the client is destroyed.
105105

106+
## Get Query metadata information
107+
108+
### Get Query Information
109+
110+
The `getQueryInfo` method retrieves comprehensive information about a specific query, based on its identifier. It returns metadata including status, execution details, statistics, and more, encapsulated within a `QueryInfo` object or undefined if the query does not exist.
111+
112+
#### Parameters
113+
114+
- `queryId`: The unique identifier string of the query for which information is being retrieved.
115+
116+
#### Example usage
117+
118+
```typescript
119+
const queryInfo = await prestoClient.getQueryInfo('your_query_id')
120+
console.log(queryInfo)
121+
```
122+
106123
## Query catalog, schema, table and column metadata
107124

108125
### Get Catalogs

presto-client/src/client.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { PrestoClientConfig, PrestoError, PrestoQuery, PrestoResponse } from './client.types'
2-
import { Column, Table } from './information-schema.types'
1+
import { QUERY_INFO_URL, STATEMENT_URL } from './constants'
2+
import {
3+
Column,
4+
PrestoClientConfig,
5+
PrestoError,
6+
PrestoQuery,
7+
PrestoResponse,
8+
QueryInfo,
9+
Table,
10+
} from './types'
311

412
export class PrestoClient {
513
private baseUrl: string
@@ -42,7 +50,7 @@ export class PrestoClient {
4250
timezone,
4351
user,
4452
}: PrestoClientConfig) {
45-
this.baseUrl = `${host || 'http://localhost'}:${port || 8080}/v1/statement`
53+
this.baseUrl = `${host || 'http://localhost'}:${port || 8080}`
4654
this.catalog = catalog
4755
this.interval = interval
4856
this.schema = schema
@@ -141,6 +149,25 @@ export class PrestoClient {
141149
) as Column[]
142150
}
143151

152+
/**
153+
* Retrieves all the information for a given query
154+
* @param {string} queryId The query identifier string
155+
* @returns {Promise<QueryInfo | undefined>} All the query information
156+
*/
157+
async getQueryInfo(queryId: string): Promise<QueryInfo | undefined> {
158+
const queryInfoResponse = await this.request({
159+
headers: this.headers,
160+
method: 'GET',
161+
url: `${this.baseUrl}${QUERY_INFO_URL}${queryId}`,
162+
})
163+
164+
if (queryInfoResponse.status !== 200) {
165+
throw new Error(`Query failed: ${JSON.stringify(await queryInfoResponse.text())}`)
166+
}
167+
168+
return (await queryInfoResponse.json()) as QueryInfo
169+
}
170+
144171
/**
145172
* Retrieves all schemas within a given catalog.
146173
* @param {string} catalog - The name of the catalog for which to retrieve schemas.
@@ -209,7 +236,12 @@ export class PrestoClient {
209236
headers['X-Presto-Schema'] = schema
210237
}
211238

212-
const firstResponse = await this.request({ body: query, headers, method: 'POST', url: this.baseUrl })
239+
const firstResponse = await this.request({
240+
body: query,
241+
headers,
242+
method: 'POST',
243+
url: `${this.baseUrl}${STATEMENT_URL}`,
244+
})
213245

214246
if (firstResponse.status !== 200) {
215247
throw new Error(`Query failed: ${JSON.stringify(await firstResponse.text())}`)

presto-client/src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const STATEMENT_URL = '/v1/statement'
2+
export const QUERY_INFO_URL = '/v1/query/'

presto-client/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export * from './client'
2-
export * from './client.types'
2+
export * from './types'
33

44
import PrestoClient from './client'
55
export default PrestoClient

presto-client/src/client.types.ts renamed to presto-client/src/types/client.types.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PrestoError } from './error.types'
2+
13
export interface PrestoClientConfig {
24
authorizationToken?: string
35
basicAuthentication?: {
@@ -39,19 +41,6 @@ export interface PrestoResponse {
3941
updateType: string
4042
}
4143

42-
export interface PrestoErrorObject extends Error {
43-
errorCode: number
44-
errorName: string
45-
errorType: string
46-
failureInfo: {
47-
message: string
48-
stack: string[]
49-
suppressed: string[]
50-
type: string
51-
}
52-
message: string
53-
}
54-
5544
export interface PrestoStats {
5645
state: string
5746
}
@@ -65,18 +54,3 @@ export interface PrestoQuery {
6554
export type GetPrestoDataParams = PrestoClientConfig & {
6655
query: string
6756
}
68-
69-
export class PrestoError extends Error implements PrestoErrorObject {
70-
errorCode: number
71-
errorName: string
72-
errorType: string
73-
failureInfo: PrestoErrorObject['failureInfo']
74-
75-
constructor({ errorCode, errorName, errorType, failureInfo, message }: PrestoErrorObject) {
76-
super(message)
77-
this.errorCode = errorCode
78-
this.errorName = errorName
79-
this.errorType = errorType
80-
this.failureInfo = failureInfo
81-
}
82-
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export interface PrestoErrorObject extends Error {
2+
errorCode: number
3+
errorName: string
4+
errorType: string
5+
failureInfo: {
6+
message: string
7+
stack: string[]
8+
suppressed: string[]
9+
type: string
10+
}
11+
message: string
12+
}
13+
14+
export class PrestoError extends Error implements PrestoErrorObject {
15+
errorCode: number
16+
errorName: string
17+
errorType: string
18+
failureInfo: PrestoErrorObject['failureInfo']
19+
20+
constructor({ errorCode, errorName, errorType, failureInfo, message }: PrestoErrorObject) {
21+
super(message)
22+
this.errorCode = errorCode
23+
this.errorName = errorName
24+
this.errorType = errorType
25+
this.failureInfo = failureInfo
26+
}
27+
}

presto-client/src/types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './client.types'
2+
export * from './error.types'
3+
export * from './information-schema.types'
4+
export * from './query-metadata'

0 commit comments

Comments
 (0)