- **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.
2.9 KiB
⚡ Query Optimization
This document describes how the Zabbix GraphQL API optimizes queries to reduce data fetching from Zabbix, improving performance and reducing network load.
🚀 Overview
The optimization works by analyzing the requested GraphQL fields and only fetching the necessary data from the Zabbix API. This is achieved through:
- Output Reduction: Dynamically setting the
outputparameter in Zabbix requests. - Parameter Skipping: Automatically removing optional Zabbix parameters (like
selectTagsorselectItems) if the corresponding GraphQL fields are not requested.
🏗️ Implementation Details
1. ZabbixRequest Enhancement
The base ZabbixRequest class handles the core optimization logic:
optimizeZabbixParams(params, output): This method modifies the Zabbix parameters. It filters theoutputarray to match the requested fields and removes "skippable" parameters based on rules.skippableZabbixParams: A map that defines dependencies between Zabbix parameters and GraphQL fields.- Example:
this.skippableZabbixParams.set("selectTags", "tags")meansselectTagswill be removed iftagsis not in the requested output.
- Example:
2. Parameter Mapping
The GraphqlParamsToNeededZabbixOutput class provides static methods to map GraphQL query arguments and the selection set (GraphQLResolveInfo) to a list of needed Zabbix fields.
3. Resolver Integration
Resolvers use the mapper to determine the required output and pass it to the datasource:
const output = GraphqlParamsToNeededZabbixOutput.mapAllHosts(args, info);
return await new ZabbixQueryHostsRequestWithItemsAndInventory(...)
.executeRequestThrowError(dataSources.zabbixAPI, new ParsedArgs(args), output);
4. Indirect Dependencies
Some GraphQL fields are not directly returned by Zabbix but are computed from other data. The optimization logic ensures these dependencies are handled:
state: Requesting thestatefield on aDevicerequires Zabbixitems. The mapper automatically addsitemsto the requested output ifstateis present.
🛠️ Configuration
Optimization rules are defined in the constructor of specialized ZabbixRequest classes.
📋 Supported Optimizations
- Hosts & Devices:
selectParentTemplatesskipped ifparentTemplatesnot requested.selectTagsandselectInheritedTagsskipped iftagsnot requested.selectHostGroupsskipped ifhostgroupsnot requested.selectItemsskipped ifitems(orstate) not requested.selectInventoryskipped ifinventorynot requested.
- Templates:
selectItemsskipped ifitemsnot requested.
✅ Verification
You can verify that optimization is working by:
- Enabling
debuglog level (LOG_LEVEL=debug). - Observing the Zabbix request bodies in the logs.
- Checking that the
outputarray is minimized andselect*parameters are omitted when not needed.