| name | maintainx-performance-tuning |
| description | Optimize MaintainX API integration performance.
Use when experiencing slow API responses, optimizing data fetching,
or improving integration throughput with MaintainX.
Trigger with phrases like "maintainx performance", "maintainx slow",
"optimize maintainx", "maintainx caching", "maintainx faster".
|
| allowed-tools | Read, Write, Edit, Bash(npm:*) |
| version | 1.0.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
MaintainX Performance Tuning
Overview
Optimize your MaintainX integration for maximum performance with caching, efficient queries, and connection pooling.
Prerequisites
- MaintainX integration working
- Redis or in-memory cache available
- Performance metrics baseline
Performance Metrics Baseline
| Operation | Target | Acceptable |
|---|
| List work orders | <500ms | <1000ms |
| Get single work order | <200ms | <500ms |
| Create work order | <500ms | <1000ms |
| Paginated fetch (100 items) | <1000ms | <2000ms |
Instructions
Step 1: Response Caching
import { Redis } from 'ioredis';
interface CacheConfig {
ttlSeconds: number;
prefix: string;
}
class CacheManager {
private redis: Redis;
private prefix: string;
constructor(redisUrl: string, prefix = 'maintainx') {
this.redis = new Redis(redisUrl);
this.prefix = prefix;
}
private key(key: string): string {
return `${this.prefix}:${key}`;
}
async get<T>(key: string): Promise<T | null> {
const data = await this.redis.get(this.key(key));
if (!data) ;
.(data) T;
}
set<T>(: , : T, ttlSeconds = ): <> {
..(
.(key),
.(value),
,
ttlSeconds
);
}
(: ): <> {
..(.(key));
}
(: ): <> {
keys = ..(.(pattern));
(keys. > ) {
..(...keys);
}
}
}
{
: ;
: ;
() {
. = client;
. = cache;
}
(: ): <> {
cacheKey = ;
cached = ..<>(cacheKey);
(cached) {
.();
cached;
}
workOrder = ..(id);
..(cacheKey, workOrder, );
workOrder;
}
(?: ): <> {
cacheKey = ;
cached = ..<>(cacheKey);
(cached) {
cached;
}
response = ..(params);
..(cacheKey, response, );
response;
}
(: ): <> {
workOrder = ..(data);
..();
workOrder;
}
(: ): <> {
..();
..();
}
}
Step 2: Connection Pooling
import axios, { AxiosInstance } from 'axios';
import https from 'https';
import http from 'http';
function createOptimizedClient(): AxiosInstance {
const httpsAgent = new https.Agent({
keepAlive: true,
keepAliveMsecs: 30000,
maxSockets: 50,
maxFreeSockets: 10,
timeout: 30000,
});
const httpAgent = new http.Agent({
keepAlive: true,
keepAliveMsecs: 30000,
maxSockets: 50,
maxFreeSockets: 10,
timeout: 30000,
});
return axios.create({
baseURL: 'https://api.getmaintainx.com/v1',
timeout: 30000,
httpsAgent,
httpAgent,
headers: {
'Authorization': `Bearer ${process.env.MAINTAINX_API_KEY}`,
: ,
: ,
},
: ,
});
}
{ createOptimizedClient };
Step 3: Efficient Pagination
interface EfficientPaginationOptions {
batchSize: number;
maxConcurrent: number;
delayBetweenBatches: number;
}
async function* streamWorkOrders(
client: MaintainXClient,
params: WorkOrderQueryParams = {},
options: Partial<EfficientPaginationOptions> = {}
): AsyncGenerator<WorkOrder, void, unknown> {
const { batchSize = 100 } = options;
let cursor: string | undefined;
do {
const response = await client.getWorkOrders({
...params,
cursor,
limit: batchSize,
});
for (const workOrder of response.workOrders) {
yield workOrder;
}
cursor = response.nextCursor || undefined;
} while (cursor);
}
async function fetchAllWorkOrdersOptimized(
: ,
: = {}
): <[]> {
: [] = [];
: | ;
: <> | = ;
{
: ;
(prefetchPromise) {
response = prefetchPromise;
} {
response = client.({ ...params, cursor, : });
}
allWorkOrders.(...response.);
cursor = response. || ;
(cursor) {
prefetchPromise = client.({ ...params, cursor, : });
} {
prefetchPromise = ;
}
} (cursor);
allWorkOrders;
}
() {
processed = ;
( workOrder (client, { : })) {
(workOrder);
processed++;
(processed % === ) {
.();
}
}
}
Step 4: Request Deduplication
class RequestDeduplicator {
private pending: Map<string, Promise<any>> = new Map();
async dedupe<T>(key: string, operation: () => Promise<T>): Promise<T> {
if (this.pending.has(key)) {
console.log(`Deduplicating request: ${key}`);
return this.pending.get(key) as Promise<T>;
}
const promise = operation().finally(() => {
this.pending.delete(key);
});
this.pending.set(key, promise);
return promise;
}
}
class DeduplicatedMaintainXClient {
private client: ;
: ;
() {
. = client;
. = ();
}
(: ): <> {
..(
,
..(id)
);
}
(: ): <> {
..(
,
..(id)
);
}
}
Step 5: Batch Data Loading
import DataLoader from 'dataloader';
const workOrderLoader = new DataLoader<string, WorkOrder>(
async (ids: readonly string[]) => {
console.log(`Batch loading ${ids.length} work orders`);
const workOrders = await Promise.all(
ids.map(id => client.getWorkOrder(id))
);
return workOrders;
},
{
maxBatchSize: 20,
batchScheduleFn: (callback) => setTimeout(callback, 10),
cache: true,
}
);
const assetLoader = new DataLoader<string, Asset>(
async (ids: readonly string[]) => {
assets = .(
ids.( client.(id))
);
assets;
}
);
() {
workOrders = .(
workOrderIds.( workOrderLoader.(id))
);
assetIds = workOrders
.( wo.)
.() [];
assets = .(
assetIds.( assetLoader.(id))
);
{ workOrders, assets };
}
Step 6: Performance Monitoring
import { performance } from 'perf_hooks';
interface PerformanceMetric {
operation: string;
duration: number;
timestamp: Date;
success: boolean;
}
class PerformanceMonitor {
private metrics: PerformanceMetric[] = [];
async measure<T>(
operation: string,
fn: () => Promise<T>
): Promise<T> {
const start = performance.now();
let success = true;
try {
return await fn();
} catch (error) {
success = false;
throw error;
} finally {
const duration = performance.now() - start;
this.metrics.push({
operation,
duration,
timestamp: new Date(),
success,
});
if (duration > 1000) {
.();
}
}
}
(?: ): {
filtered = operation
? ..( m. === operation)
: .;
(filtered. === ) ;
durations = filtered.( m.);
{
: filtered.,
: durations.( a + b, ) / durations.,
: .(...durations),
: .(...durations),
: (durations, ),
: (durations, ),
: filtered.( m.). / filtered.,
};
}
}
(): {
sorted = [...arr].( a - b);
index = .((p / ) * sorted.) - ;
sorted[index];
}
monitor = ();
workOrders = monitor.(
,
client.({ : })
);
.(, monitor.());
Output
- Caching layer implemented
- Connection pooling configured
- Efficient pagination patterns
- Request deduplication
- DataLoader for batch loading
- Performance monitoring
Performance Checklist
Resources
Next Steps
For cost optimization, see maintainx-cost-tuning.