Skip to content

Commit

Permalink
feat(slow-query): support slow query search by resource group (#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch authored Feb 6, 2024
1 parent 2171c6e commit e6ac0d1
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 19 deletions.
27 changes: 27 additions & 0 deletions pkg/apiserver/resource_manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"net/http"
"regexp"
"sort"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -46,6 +48,7 @@ func registerRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
{
endpoint.GET("/config", s.GetConfig)
endpoint.GET("/information", s.GetInformation)
endpoint.GET("/information/group_names", s.resourceGroupNamesHandler)
endpoint.GET("/calibrate/hardware", s.GetCalibrateByHardware)
endpoint.GET("/calibrate/actual", s.GetCalibrateByActual)
}
Expand Down Expand Up @@ -96,6 +99,30 @@ func (s *Service) GetInformation(c *gin.Context) {
c.JSON(http.StatusOK, cfg)
}

// @Summary List all resource groups
// @Router /resource_manager/information/group_names [get]
// @Security JwtAuth
// @Success 200 {object} []string
// @Failure 401 {object} rest.ErrorResponse
func (s *Service) resourceGroupNamesHandler(c *gin.Context) {
type groupSchemas struct {
Groups string `gorm:"column:NAME"`
}
var result []groupSchemas
db := utils.GetTiDBConnection(c)
err := db.Raw("SELECT NAME FROM INFORMATION_SCHEMA.RESOURCE_GROUPS").Scan(&result).Error
if err != nil {
rest.Error(c, err)
return
}
strs := []string{}
for _, v := range result {
strs = append(strs, strings.ToLower(v.Groups))
}
sort.Strings(strs)
c.JSON(http.StatusOK, strs)
}

type CalibrateResponse struct {
EstimatedCapacity int `json:"estimated_capacity" gorm:"column:QUOTA"`
}
Expand Down
19 changes: 12 additions & 7 deletions pkg/apiserver/slowquery/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ const (
)

type GetListRequest struct {
BeginTime int `json:"begin_time" form:"begin_time"`
EndTime int `json:"end_time" form:"end_time"`
DB []string `json:"db" form:"db"`
Limit int `json:"limit" form:"limit"`
Text string `json:"text" form:"text"`
OrderBy string `json:"orderBy" form:"orderBy"`
IsDesc bool `json:"desc" form:"desc"`
BeginTime int `json:"begin_time" form:"begin_time"`
EndTime int `json:"end_time" form:"end_time"`
DB []string `json:"db" form:"db"`
ResourceGroup []string `json:"resource_group" form:"resource_group"`
Limit int `json:"limit" form:"limit"`
Text string `json:"text" form:"text"`
OrderBy string `json:"orderBy" form:"orderBy"`
IsDesc bool `json:"desc" form:"desc"`

// for showing slow queries in the statement detail page
Plans []string `json:"plans" form:"plans"`
Expand Down Expand Up @@ -79,6 +80,10 @@ func QuerySlowLogList(req *GetListRequest, sysSchema *utils.SysSchema, db *gorm.
tx = tx.Where("DB IN (?)", req.DB)
}

if len(req.ResourceGroup) > 0 {
tx = tx.Where("RESOURCE_GROUP IN (?)", req.ResourceGroup)
}

// more robust
if req.OrderBy == "" {
req.OrderBy = "timestamp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,39 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati



setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
*
* @summary List all resource groups
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
resourceManagerInformationGroupNamesGet: async (options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
const localVarPath = `/resource_manager/information/group_names`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;

// authentication JwtAuth required
await setApiKeyToObject(localVarHeaderParameter, "Authorization", configuration)



setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
Expand Down Expand Up @@ -2809,11 +2842,12 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
* @param {number} [limit]
* @param {string} [orderBy]
* @param {Array<string>} [plans] for showing slow queries in the statement detail page
* @param {Array<string>} [resourceGroup]
* @param {string} [text]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
slowQueryListGet: async (beginTime?: number, db?: Array<string>, desc?: boolean, digest?: string, endTime?: number, fields?: string, limit?: number, orderBy?: string, plans?: Array<string>, text?: string, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
slowQueryListGet: async (beginTime?: number, db?: Array<string>, desc?: boolean, digest?: string, endTime?: number, fields?: string, limit?: number, orderBy?: string, plans?: Array<string>, resourceGroup?: Array<string>, text?: string, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
const localVarPath = `/slow_query/list`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
Expand Down Expand Up @@ -2865,6 +2899,10 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
localVarQueryParameter['plans'] = plans;
}

if (resourceGroup) {
localVarQueryParameter['resource_group'] = resourceGroup;
}

if (text !== undefined) {
localVarQueryParameter['text'] = text;
}
Expand Down Expand Up @@ -4761,6 +4799,16 @@ export const DefaultApiFp = function(configuration?: Configuration) {
const localVarAxiosArgs = await localVarAxiosParamCreator.resourceManagerInformationGet(options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @summary List all resource groups
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async resourceManagerInformationGroupNamesGet(options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<Array<string>>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.resourceManagerInformationGroupNamesGet(options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
* Get available field names by slowquery table columns
* @summary Get available field names
Expand Down Expand Up @@ -4818,12 +4866,13 @@ export const DefaultApiFp = function(configuration?: Configuration) {
* @param {number} [limit]
* @param {string} [orderBy]
* @param {Array<string>} [plans] for showing slow queries in the statement detail page
* @param {Array<string>} [resourceGroup]
* @param {string} [text]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async slowQueryListGet(beginTime?: number, db?: Array<string>, desc?: boolean, digest?: string, endTime?: number, fields?: string, limit?: number, orderBy?: string, plans?: Array<string>, text?: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<Array<SlowqueryModel>>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.slowQueryListGet(beginTime, db, desc, digest, endTime, fields, limit, orderBy, plans, text, options);
async slowQueryListGet(beginTime?: number, db?: Array<string>, desc?: boolean, digest?: string, endTime?: number, fields?: string, limit?: number, orderBy?: string, plans?: Array<string>, resourceGroup?: Array<string>, text?: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<Array<SlowqueryModel>>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.slowQueryListGet(beginTime, db, desc, digest, endTime, fields, limit, orderBy, plans, resourceGroup, text, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
Expand Down Expand Up @@ -5815,6 +5864,15 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa
resourceManagerInformationGet(options?: any): AxiosPromise<Array<ResourcemanagerResourceInfoRowDef>> {
return localVarFp.resourceManagerInformationGet(options).then((request) => request(axios, basePath));
},
/**
*
* @summary List all resource groups
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
resourceManagerInformationGroupNamesGet(options?: any): AxiosPromise<Array<string>> {
return localVarFp.resourceManagerInformationGroupNamesGet(options).then((request) => request(axios, basePath));
},
/**
* Get available field names by slowquery table columns
* @summary Get available field names
Expand Down Expand Up @@ -5868,12 +5926,13 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa
* @param {number} [limit]
* @param {string} [orderBy]
* @param {Array<string>} [plans] for showing slow queries in the statement detail page
* @param {Array<string>} [resourceGroup]
* @param {string} [text]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
slowQueryListGet(beginTime?: number, db?: Array<string>, desc?: boolean, digest?: string, endTime?: number, fields?: string, limit?: number, orderBy?: string, plans?: Array<string>, text?: string, options?: any): AxiosPromise<Array<SlowqueryModel>> {
return localVarFp.slowQueryListGet(beginTime, db, desc, digest, endTime, fields, limit, orderBy, plans, text, options).then((request) => request(axios, basePath));
slowQueryListGet(beginTime?: number, db?: Array<string>, desc?: boolean, digest?: string, endTime?: number, fields?: string, limit?: number, orderBy?: string, plans?: Array<string>, resourceGroup?: Array<string>, text?: string, options?: any): AxiosPromise<Array<SlowqueryModel>> {
return localVarFp.slowQueryListGet(beginTime, db, desc, digest, endTime, fields, limit, orderBy, plans, resourceGroup, text, options).then((request) => request(axios, basePath));
},
/**
* Start a profiling task group
Expand Down Expand Up @@ -6949,6 +7008,13 @@ export interface DefaultApiSlowQueryListGetRequest {
*/
readonly plans?: Array<string>

/**
*
* @type {Array<string>}
* @memberof DefaultApiSlowQueryListGet
*/
readonly resourceGroup?: Array<string>

/**
*
* @type {string}
Expand Down Expand Up @@ -8189,6 +8255,17 @@ export class DefaultApi extends BaseAPI {
return DefaultApiFp(this.configuration).resourceManagerInformationGet(options).then((request) => request(this.axios, this.basePath));
}

/**
*
* @summary List all resource groups
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof DefaultApi
*/
public resourceManagerInformationGroupNamesGet(options?: AxiosRequestConfig) {
return DefaultApiFp(this.configuration).resourceManagerInformationGroupNamesGet(options).then((request) => request(this.axios, this.basePath));
}

/**
* Get available field names by slowquery table columns
* @summary Get available field names
Expand Down Expand Up @@ -8245,7 +8322,7 @@ export class DefaultApi extends BaseAPI {
* @memberof DefaultApi
*/
public slowQueryListGet(requestParameters: DefaultApiSlowQueryListGetRequest = {}, options?: AxiosRequestConfig) {
return DefaultApiFp(this.configuration).slowQueryListGet(requestParameters.beginTime, requestParameters.db, requestParameters.desc, requestParameters.digest, requestParameters.endTime, requestParameters.fields, requestParameters.limit, requestParameters.orderBy, requestParameters.plans, requestParameters.text, options).then((request) => request(this.axios, this.basePath));
return DefaultApiFp(this.configuration).slowQueryListGet(requestParameters.beginTime, requestParameters.db, requestParameters.desc, requestParameters.digest, requestParameters.endTime, requestParameters.fields, requestParameters.limit, requestParameters.orderBy, requestParameters.plans, requestParameters.resourceGroup, requestParameters.text, options).then((request) => request(this.axios, this.basePath));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export interface SlowqueryGetListRequest {
* @memberof SlowqueryGetListRequest
*/
'plans'?: Array<string>;
/**
*
* @type {Array<string>}
* @memberof SlowqueryGetListRequest
*/
'resource_group'?: Array<string>;
/**
*
* @type {string}
Expand Down
42 changes: 42 additions & 0 deletions ui/packages/tidb-dashboard-client/swagger/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,33 @@
}
}
},
"/resource_manager/information/group_names": {
"get": {
"security": [
{
"JwtAuth": []
}
],
"summary": "List all resource groups",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/rest.ErrorResponse"
}
}
}
}
},
"/slow_query/available_fields": {
"get": {
"security": [
Expand Down Expand Up @@ -2502,6 +2529,15 @@
"name": "plans",
"in": "query"
},
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi",
"name": "resource_group",
"in": "query"
},
{
"type": "string",
"name": "text",
Expand Down Expand Up @@ -4960,6 +4996,12 @@
"type": "string"
}
},
"resource_group": {
"type": "array",
"items": {
"type": "string"
}
},
"text": {
"type": "string"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class DataSource implements ISlowQueryDataSource {
return client.getInstance().infoListDatabases(options)
}

infoListResourceGroupNames(options?: ReqConfig) {
return client.getInstance().resourceManagerInformationGroupNamesGet(options)
}

slowQueryAvailableFieldsGet(options?: ReqConfig) {
return client.getInstance().slowQueryAvailableFieldsGet(options)
}
Expand All @@ -29,6 +33,7 @@ class DataSource implements ISlowQueryDataSource {
limit?: number,
orderBy?: string,
plans?: Array<string>,
resourceGroup?: Array<string>,
text?: string,
options?: ReqConfig
) {
Expand All @@ -43,6 +48,7 @@ class DataSource implements ISlowQueryDataSource {
limit,
orderBy,
plans,
resourceGroup,
text
},
options
Expand Down Expand Up @@ -132,6 +138,7 @@ export const ctx: (cfg: Partial<ISlowQueryConfig>) => ISlowQueryContext = (
enableExport: true,
showDBFilter: true,
showDigestFilter: false,
showResourceGroupFilter: true,
...cfg
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class DataSource implements IStatementDataSource {
return client.getInstance().infoListDatabases(options)
}

infoListResourceGroupNames(options?: ReqConfig) {
return client.getInstance().resourceManagerInformationGroupNamesGet(options)
}

statementsAvailableFieldsGet(options?: ReqConfig) {
return client.getInstance().statementsAvailableFieldsGet(options)
}
Expand Down Expand Up @@ -123,6 +127,7 @@ class DataSource implements IStatementDataSource {
limit?: number,
orderBy?: string,
plans?: Array<string>,
resourceGroup?: Array<string>,
text?: string,
options?: ReqConfig
) {
Expand All @@ -137,6 +142,7 @@ class DataSource implements IStatementDataSource {
limit,
orderBy,
plans,
resourceGroup,
text
},
options
Expand Down
Loading

0 comments on commit e6ac0d1

Please sign in to comment.