feat(query-optimization): implement GraphQL query optimization and enhance regression suite
- **Optimization**: Implemented automatic Zabbix parameter optimization by analyzing GraphQL selection sets. - **ZabbixRequest**: Added optimizeZabbixParams with support for skippable parameters and implied field dependencies (e.g., state -> items). - **Resolvers**: Updated allHosts, allDevices, allHostGroups, and templates to pass requested fields to data sources. - **Data Sources**: Optimized ZabbixQueryHostsGenericRequest and ZabbixQueryTemplatesRequest to skip unnecessary Zabbix API calls. - **Regression Tests**: Enhanced RegressionTestExecutor with new tests for optimization (REG-OPT, REG-OPT-NEG), state retrieval (REG-STATE), dependent items (REG-DEP), and empty results (REG-EMPTY). - **Documentation**: Created query_optimization.md How-To guide and updated roadmap.md, README.md, and tests.md. - **Bug Fixes**: Fixed deviceType tag assignment during host import and corrected ZabbixCreateHostRequest to support tags.
This commit is contained in:
parent
ad104acde2
commit
97a0f70fd6
16 changed files with 835 additions and 69 deletions
26
src/datasources/graphql-params-to-zabbix-output.ts
Normal file
26
src/datasources/graphql-params-to-zabbix-output.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import {GraphQLResolveInfo} from "graphql";
|
||||
import {getRequestedFields} from "../api/graphql_utils.js";
|
||||
import {
|
||||
QueryAllDevicesArgs,
|
||||
QueryAllHostGroupsArgs,
|
||||
QueryAllHostsArgs,
|
||||
QueryTemplatesArgs
|
||||
} from "../schema/generated/graphql.js";
|
||||
|
||||
export class GraphqlParamsToNeededZabbixOutput {
|
||||
static mapAllHosts(args: QueryAllHostsArgs, info: GraphQLResolveInfo): string[] {
|
||||
return getRequestedFields(info);
|
||||
}
|
||||
|
||||
static mapAllDevices(args: QueryAllDevicesArgs, info: GraphQLResolveInfo): string[] {
|
||||
return getRequestedFields(info);
|
||||
}
|
||||
|
||||
static mapAllHostGroups(args: QueryAllHostGroupsArgs, info: GraphQLResolveInfo): string[] {
|
||||
return getRequestedFields(info);
|
||||
}
|
||||
|
||||
static mapTemplates(args: QueryTemplatesArgs, info: GraphQLResolveInfo): string[] {
|
||||
return getRequestedFields(info);
|
||||
}
|
||||
}
|
||||
|
|
@ -80,12 +80,12 @@ export class ZabbixAPI
|
|||
return super.post(path, request);
|
||||
}
|
||||
|
||||
async executeRequest<T extends ZabbixResult, A extends ParsedArgs>(zabbixRequest: ZabbixRequest<T, A>, args?: A, throwApiError: boolean = true): Promise<T | ZabbixErrorResult> {
|
||||
return throwApiError ? zabbixRequest.executeRequestThrowError(this, args) : zabbixRequest.executeRequestReturnError(this, args);
|
||||
async executeRequest<T extends ZabbixResult, A extends ParsedArgs>(zabbixRequest: ZabbixRequest<T, A>, args?: A, throwApiError: boolean = true, output?: string[]): Promise<T | ZabbixErrorResult> {
|
||||
return throwApiError ? zabbixRequest.executeRequestThrowError(this, args, output) : zabbixRequest.executeRequestReturnError(this, args, output);
|
||||
}
|
||||
|
||||
async requestByPath<T extends ZabbixResult, A extends ParsedArgs = ParsedArgs>(path: string, args?: A, authToken?: string | null, cookies?: string, throwApiError: boolean = true) {
|
||||
return this.executeRequest<T, A>(new ZabbixRequest<T>(path, authToken, cookies), args, throwApiError);
|
||||
async requestByPath<T extends ZabbixResult, A extends ParsedArgs = ParsedArgs>(path: string, args?: A, authToken?: string | null, cookies?: string, throwApiError: boolean = true, output?: string[]) {
|
||||
return this.executeRequest<T, A>(new ZabbixRequest<T>(path, authToken, cookies), args, throwApiError, output);
|
||||
}
|
||||
|
||||
async getLocations(args?: ParsedArgs, authToken?: string, cookies?: string) {
|
||||
|
|
|
|||
|
|
@ -17,10 +17,14 @@ export class ZabbixQueryHostsGenericRequest<T extends ZabbixResult, A extends Pa
|
|||
|
||||
constructor(path: string, authToken?: string | null, cookie?: string | null) {
|
||||
super(path, authToken, cookie);
|
||||
this.skippableZabbixParams.set("selectParentTemplates", "parentTemplates");
|
||||
this.skippableZabbixParams.set("selectTags", "tags");
|
||||
this.skippableZabbixParams.set("selectInheritedTags", "tags");
|
||||
this.skippableZabbixParams.set("selectHostGroups", "hostgroups");
|
||||
}
|
||||
|
||||
createZabbixParams(args?: A): ZabbixParams {
|
||||
return {
|
||||
createZabbixParams(args?: A, output?: string[]): ZabbixParams {
|
||||
return this.optimizeZabbixParams({
|
||||
...super.createZabbixParams(args),
|
||||
selectParentTemplates: [
|
||||
"templateid",
|
||||
|
|
@ -43,7 +47,7 @@ export class ZabbixQueryHostsGenericRequest<T extends ZabbixResult, A extends Pa
|
|||
"description",
|
||||
"parentTemplates"
|
||||
]
|
||||
};
|
||||
}, output);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -67,10 +71,12 @@ export class ZabbixQueryHostsMetaRequest extends ZabbixQueryHostsGenericRequest<
|
|||
export class ZabbixQueryHostsGenericRequestWithItems<T extends ZabbixResult, A extends ParsedArgs = ParsedArgs> extends ZabbixQueryHostsGenericRequest<T, A> {
|
||||
constructor(path: string, authToken?: string | null, cookie?: string) {
|
||||
super(path, authToken, cookie);
|
||||
this.skippableZabbixParams.set("selectItems", "items");
|
||||
this.impliedFields.set("state", ["items"]);
|
||||
}
|
||||
|
||||
createZabbixParams(args?: A): ZabbixParams {
|
||||
return {
|
||||
createZabbixParams(args?: A, output?: string[]): ZabbixParams {
|
||||
return this.optimizeZabbixParams({
|
||||
...super.createZabbixParams(args),
|
||||
selectItems: [
|
||||
"itemid",
|
||||
|
|
@ -99,13 +105,13 @@ export class ZabbixQueryHostsGenericRequestWithItems<T extends ZabbixResult, A e
|
|||
"description",
|
||||
"parentTemplates"
|
||||
],
|
||||
};
|
||||
}, output);
|
||||
}
|
||||
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: A): Promise<ZabbixErrorResult | T> {
|
||||
let result = await super.executeRequestReturnError(zabbixAPI, args);
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: A, output?: string[]): Promise<ZabbixErrorResult | T> {
|
||||
let result = await super.executeRequestReturnError(zabbixAPI, args, output);
|
||||
|
||||
if (result && !isZabbixErrorResult(result)) {
|
||||
if (result && !isZabbixErrorResult(result) && (!output || output.includes("items.preprocessing"))) {
|
||||
const hosts = <ZabbixHost[]>result;
|
||||
const hostids = hosts.map(h => h.hostid);
|
||||
|
||||
|
|
@ -125,21 +131,29 @@ export class ZabbixQueryHostsGenericRequestWithItems<T extends ZabbixResult, A e
|
|||
for (let device of hosts) {
|
||||
for (let item of device.items || []) {
|
||||
item.preprocessing = itemidToPreprocessing.get(item.itemid.toString());
|
||||
if (!item.lastclock ) {
|
||||
let values = await new ZabbixQueryHistoryRequest(this.authToken, this.cookie).executeRequestReturnError(
|
||||
zabbixAPI, new ZabbixHistoryGetParams(item.itemid, ["clock", "value", "itemid"], 1, item.value_type))
|
||||
if (isZabbixErrorResult(values)) {
|
||||
return values;
|
||||
}
|
||||
if (values.length) {
|
||||
let latestValue = values[0];
|
||||
item.lastvalue = latestValue.value;
|
||||
item.lastclock = latestValue.clock;
|
||||
} else {
|
||||
item.lastvalue = null;
|
||||
item.lastclock = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result && !isZabbixErrorResult(result) && (!output || output.includes("items.lastclock") || output.includes("items.lastvalue"))) {
|
||||
const hosts = <ZabbixHost[]>result;
|
||||
for (let device of hosts) {
|
||||
for (let item of device.items || []) {
|
||||
if (!item.lastclock) {
|
||||
let values = await new ZabbixQueryHistoryRequest(this.authToken, this.cookie).executeRequestReturnError(
|
||||
zabbixAPI, new ZabbixHistoryGetParams(item.itemid, ["clock", "value", "itemid"], 1, item.value_type))
|
||||
if (isZabbixErrorResult(values)) {
|
||||
return values;
|
||||
}
|
||||
if (values.length) {
|
||||
let latestValue = values[0];
|
||||
item.lastvalue = latestValue.value;
|
||||
item.lastclock = latestValue.clock;
|
||||
} else {
|
||||
item.lastvalue = null;
|
||||
item.lastclock = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -153,15 +167,16 @@ export class ZabbixQueryHostsGenericRequestWithItems<T extends ZabbixResult, A e
|
|||
export class ZabbixQueryHostsGenericRequestWithItemsAndInventory<T extends ZabbixResult, A extends ParsedArgs = ParsedArgs> extends ZabbixQueryHostsGenericRequestWithItems<T, A> {
|
||||
constructor(path: string, authToken?: string | null, cookie?: string) {
|
||||
super(path, authToken, cookie);
|
||||
this.skippableZabbixParams.set("selectInventory", "inventory");
|
||||
}
|
||||
|
||||
createZabbixParams(args?: A): ZabbixParams {
|
||||
return {
|
||||
createZabbixParams(args?: A, output?: string[]): ZabbixParams {
|
||||
return this.optimizeZabbixParams({
|
||||
...super.createZabbixParams(args),
|
||||
selectInventory: [
|
||||
"location", "location_lat", "location_lon"
|
||||
]
|
||||
};
|
||||
}, output);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -201,6 +216,7 @@ export interface ZabbixCreateHostInputParams extends ZabbixParams {
|
|||
templateids?: [number];
|
||||
hostgroupids?: [number];
|
||||
macros?: { macro: string, value: string }[];
|
||||
tags?: { tag: string, value: string }[];
|
||||
additionalParams?: any;
|
||||
}
|
||||
|
||||
|
|
@ -231,6 +247,9 @@ class ZabbixCreateHostParams implements ZabbixParams {
|
|||
if (inputParams.macros) {
|
||||
this.macros = inputParams.macros;
|
||||
}
|
||||
if (inputParams.tags) {
|
||||
this.tags = inputParams.tags;
|
||||
}
|
||||
}
|
||||
|
||||
host: string
|
||||
|
|
@ -246,6 +265,7 @@ class ZabbixCreateHostParams implements ZabbixParams {
|
|||
templates?: any
|
||||
groups?: any
|
||||
macros?: { macro: string, value: string }[]
|
||||
tags?: { tag: string, value: string }[]
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -148,17 +148,57 @@ export class ZabbixRequest<T extends ZabbixResult, A extends ParsedArgs = Parsed
|
|||
protected requestBodyTemplate: ZabbixRequestBody;
|
||||
protected method: string
|
||||
protected prepResult: T | ZabbixErrorResult | undefined = undefined
|
||||
protected skippableZabbixParams: Map<string, string> = new Map();
|
||||
protected impliedFields: Map<string, string[]> = new Map();
|
||||
|
||||
constructor(public path: string, public authToken?: string | null, public cookie?: string | null) {
|
||||
this.method = path.split(".", 2).join(".");
|
||||
this.requestBodyTemplate = new ZabbixRequestBody(this.method);
|
||||
}
|
||||
|
||||
createZabbixParams(args?: A): ZabbixParams {
|
||||
return args?.zabbix_params || {}
|
||||
optimizeZabbixParams(params: ZabbixParams, output?: string[]): ZabbixParams {
|
||||
if (!output || output.length === 0) {
|
||||
return params;
|
||||
}
|
||||
|
||||
const requestedTopLevelFields = Array.from(new Set(output.map(field => field.split('.')[0])));
|
||||
|
||||
// Apply implied fields (e.g. "state" implies "items")
|
||||
let neededTopLevelFields = [...requestedTopLevelFields];
|
||||
this.impliedFields.forEach((implied, field) => {
|
||||
if (requestedTopLevelFields.includes(field)) {
|
||||
neededTopLevelFields.push(...implied.map(f => f.split('.')[0]));
|
||||
}
|
||||
});
|
||||
|
||||
const topLevelOutput = Array.from(new Set(neededTopLevelFields));
|
||||
|
||||
// Reduce output subfields
|
||||
if (params.output) {
|
||||
if (Array.isArray(params.output)) {
|
||||
params.output = params.output.filter(field => topLevelOutput.includes(field));
|
||||
} else if (params.output === "extend") {
|
||||
params.output = topLevelOutput;
|
||||
}
|
||||
} else {
|
||||
params.output = topLevelOutput;
|
||||
}
|
||||
|
||||
// Remove skippable parameters
|
||||
this.skippableZabbixParams.forEach((neededField, skippableParam) => {
|
||||
if (!topLevelOutput.includes(neededField) && params.hasOwnProperty(skippableParam)) {
|
||||
delete params[skippableParam];
|
||||
}
|
||||
});
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
getRequestBody(args?: A, zabbixParams?: ZabbixParams): ZabbixRequestBody {
|
||||
createZabbixParams(args?: A, output?: string[]): ZabbixParams {
|
||||
return this.optimizeZabbixParams(args?.zabbix_params || {}, output)
|
||||
}
|
||||
|
||||
getRequestBody(args?: A, zabbixParams?: ZabbixParams, output?: string[]): ZabbixRequestBody {
|
||||
let params: ZabbixParams
|
||||
if (Array.isArray(args?.zabbix_params)) {
|
||||
params = args?.zabbix_params.map(paramsObj => {
|
||||
|
|
@ -168,7 +208,7 @@ export class ZabbixRequest<T extends ZabbixResult, A extends ParsedArgs = Parsed
|
|||
return paramsObj;
|
||||
})
|
||||
} else {
|
||||
params = {...this.requestBodyTemplate.params, ...zabbixParams ?? this.createZabbixParams(args)}
|
||||
params = {...this.requestBodyTemplate.params, ...zabbixParams ?? this.createZabbixParams(args, output)}
|
||||
}
|
||||
return params ? {
|
||||
...this.requestBodyTemplate,
|
||||
|
|
@ -204,12 +244,12 @@ export class ZabbixRequest<T extends ZabbixResult, A extends ParsedArgs = Parsed
|
|||
return this.prepResult;
|
||||
}
|
||||
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: A): Promise<T | ZabbixErrorResult> {
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: A, output?: string[]): Promise<T | ZabbixErrorResult> {
|
||||
let prepareResult = await this.prepare(zabbixAPI, args);
|
||||
if (prepareResult) {
|
||||
return prepareResult;
|
||||
}
|
||||
let requestBody = this.getRequestBody(args);
|
||||
let requestBody = this.getRequestBody(args, undefined, output);
|
||||
|
||||
try {
|
||||
|
||||
|
|
@ -236,8 +276,8 @@ export class ZabbixRequest<T extends ZabbixResult, A extends ParsedArgs = Parsed
|
|||
}
|
||||
}
|
||||
|
||||
async executeRequestThrowError(zabbixApi: ZabbixAPI, args?: A): Promise<T> {
|
||||
let response = await this.executeRequestReturnError(zabbixApi, args);
|
||||
async executeRequestThrowError(zabbixApi: ZabbixAPI, args?: A, output?: string[]): Promise<T> {
|
||||
let response = await this.executeRequestReturnError(zabbixApi, args, output);
|
||||
if (isZabbixErrorResult(response)) {
|
||||
throw new GraphQLError(`Called Zabbix path ${this.path} with error: ${response.error.message || "Zabbix error."} ${response.error.data}`, {
|
||||
extensions: {
|
||||
|
|
|
|||
|
|
@ -15,20 +15,21 @@ export interface ZabbixQueryTemplateResponse {
|
|||
export class ZabbixQueryTemplatesRequest extends ZabbixRequest<ZabbixQueryTemplateResponse[]> {
|
||||
constructor(authToken?: string | null, cookie?: string | null,) {
|
||||
super("template.get", authToken, cookie);
|
||||
this.skippableZabbixParams.set("selectItems", "items");
|
||||
}
|
||||
|
||||
createZabbixParams(args?: ParsedArgs): ZabbixParams {
|
||||
return {
|
||||
createZabbixParams(args?: ParsedArgs, output?: string[]): ZabbixParams {
|
||||
return this.optimizeZabbixParams({
|
||||
"selectItems": "extend",
|
||||
"output": "extend",
|
||||
...args?.zabbix_params
|
||||
};
|
||||
}, output);
|
||||
}
|
||||
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: ParsedArgs): Promise<ZabbixErrorResult | ZabbixQueryTemplateResponse[]> {
|
||||
let result = await super.executeRequestReturnError(zabbixAPI, args);
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: ParsedArgs, output?: string[]): Promise<ZabbixErrorResult | ZabbixQueryTemplateResponse[]> {
|
||||
let result = await super.executeRequestReturnError(zabbixAPI, args, output);
|
||||
|
||||
if (result && !isZabbixErrorResult(result) && Array.isArray(result)) {
|
||||
if (result && !isZabbixErrorResult(result) && Array.isArray(result) && (!output || output.includes("items.preprocessing"))) {
|
||||
const templateids = result.map(t => t.templateid);
|
||||
if (templateids.length > 0) {
|
||||
// Batch fetch preprocessing for all items of these templates
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue