IMPLEMENTATION_EXAMPLES.md•52.9 kB
# Implementation Examples - Optimization Architecture
**Companion Document to:** ARCHITECTURE_OPTIMIZATION.md
**Version:** 2.0.0
**Date:** 2025-01-13
This document provides concrete, production-ready code examples for the key components of the optimization architecture.
---
## Table of Contents
1. [Query Parser & Validator](#1-query-parser--validator)
2. [Field Selector Engine](#2-field-selector-engine)
3. [Filter Expression Evaluator](#3-filter-expression-evaluator)
4. [Smart Cache Manager](#4-smart-cache-manager)
5. [Data Transformer](#5-data-transformer)
6. [Compression Middleware](#6-compression-middleware)
7. [Metrics Collector](#7-metrics-collector)
8. [Enhanced Tool Example](#8-enhanced-tool-example)
---
## 1. Query Parser & Validator
```typescript
/**
* src/optimization/QueryParser.ts
*
* Parse enhanced query parameters and validate them
*/
import { z } from 'zod';
/**
* Filter Expression Schema
*/
const FilterExpressionSchema: z.ZodType<any> = z.lazy(() =>
z.object({
// Comparison operators
eq: z.record(z.any()).optional(),
ne: z.record(z.any()).optional(),
gt: z.record(z.number()).optional(),
gte: z.record(z.number()).optional(),
lt: z.record(z.number()).optional(),
lte: z.record(z.number()).optional(),
// String operators
contains: z.record(z.string()).optional(),
startsWith: z.record(z.string()).optional(),
endsWith: z.record(z.string()).optional(),
regex: z.record(z.string()).optional(),
// Array operators
in: z.record(z.array(z.any())).optional(),
notIn: z.record(z.array(z.any())).optional(),
// Date operators
between: z.record(z.tuple([z.date(), z.date()])).optional(),
// Logical operators
and: z.array(FilterExpressionSchema).optional(),
or: z.array(FilterExpressionSchema).optional(),
not: FilterExpressionSchema.optional(),
// Existence checks
exists: z.array(z.string()).optional(),
missing: z.array(z.string()).optional(),
})
);
/**
* Enhanced Query Parameters Schema
*/
const EnhancedQuerySchema = z.object({
// LEGACY (backward compatible)
from: z.number().int().min(0).optional(),
size: z.number().int().min(1).max(500).optional(),
verbosity: z.enum(['summary', 'compact', 'detailed', 'raw']).optional(),
// NEW - Field Selection
fields: z.string().optional(),
exclude: z.string().optional(),
// NEW - Advanced Filtering
filter: FilterExpressionSchema.optional(),
search: z.string().optional(),
// NEW - Sorting & Pagination
sort: z.string().optional(),
cursor: z.string().optional(),
pageSize: z.number().int().min(1).max(100).optional(),
// NEW - Aggregations
aggregate: z.object({
groupBy: z.array(z.string()),
metrics: z.record(
z.object({
type: z.enum(['sum', 'avg', 'min', 'max', 'count', 'distinct']),
field: z.string(),
})
),
having: FilterExpressionSchema.optional(),
}).optional(),
// NEW - Performance Hints
preferCache: z.boolean().optional(),
maxAge: z.number().int().min(0).optional(),
streaming: z.boolean().optional(),
// NEW - Response Format
format: z.enum(['json', 'jsonlines', 'csv', 'summary']).optional(),
compression: z.enum(['gzip', 'brotli', 'none']).optional(),
});
export type EnhancedQuery = z.infer<typeof EnhancedQuerySchema>;
export type FilterExpression = z.infer<typeof FilterExpressionSchema>;
/**
* Query Parser
*/
export class QueryParser {
/**
* Parse query parameters from request
*/
static parse(rawQuery: any): EnhancedQuery {
// Convert string booleans to actual booleans
const normalized = this.normalizeQuery(rawQuery);
// Validate and parse
try {
return EnhancedQuerySchema.parse(normalized);
} catch (error) {
if (error instanceof z.ZodError) {
throw new ValidationError(
'Invalid query parameters',
error.errors.map(e => ({
field: e.path.join('.'),
message: e.message,
}))
);
}
throw error;
}
}
/**
* Normalize query parameters (handle string conversions, etc.)
*/
private static normalizeQuery(raw: any): any {
const normalized: any = { ...raw };
// Convert string numbers to actual numbers
if (typeof normalized.from === 'string') {
normalized.from = parseInt(normalized.from, 10);
}
if (typeof normalized.size === 'string') {
normalized.size = parseInt(normalized.size, 10);
}
if (typeof normalized.pageSize === 'string') {
normalized.pageSize = parseInt(normalized.pageSize, 10);
}
if (typeof normalized.maxAge === 'string') {
normalized.maxAge = parseInt(normalized.maxAge, 10);
}
// Convert string booleans to actual booleans
if (typeof normalized.preferCache === 'string') {
normalized.preferCache = normalized.preferCache === 'true';
}
if (typeof normalized.streaming === 'string') {
normalized.streaming = normalized.streaming === 'true';
}
// Parse filter JSON if it's a string
if (typeof normalized.filter === 'string') {
try {
normalized.filter = JSON.parse(normalized.filter);
} catch {
throw new ValidationError('Invalid filter JSON');
}
}
// Parse aggregate JSON if it's a string
if (typeof normalized.aggregate === 'string') {
try {
normalized.aggregate = JSON.parse(normalized.aggregate);
} catch {
throw new ValidationError('Invalid aggregate JSON');
}
}
return normalized;
}
}
/**
* Query Validator
*/
export class QueryValidator {
/**
* Validate query semantics (beyond schema validation)
*/
static validate(query: EnhancedQuery): void {
// Validate sort format
if (query.sort) {
this.validateSort(query.sort);
}
// Validate field selection
if (query.fields) {
this.validateFields(query.fields);
}
// Validate filter expression logic
if (query.filter) {
this.validateFilter(query.filter);
}
// Validate aggregation semantics
if (query.aggregate) {
this.validateAggregation(query.aggregate);
}
// Validate pagination (can't use both old and new style)
if ((query.from !== undefined || query.size !== undefined) &&
(query.cursor !== undefined || query.pageSize !== undefined)) {
throw new ValidationError(
'Cannot mix legacy pagination (from/size) with new pagination (cursor/pageSize)'
);
}
}
/**
* Validate sort format: "field:asc" or "field1:desc,field2:asc"
*/
private static validateSort(sort: string): void {
const parts = sort.split(',');
for (const part of parts) {
const [field, direction] = part.split(':');
if (!field || !field.trim()) {
throw new ValidationError(`Invalid sort field: ${part}`);
}
if (direction && !['asc', 'desc'].includes(direction.toLowerCase())) {
throw new ValidationError(
`Invalid sort direction: ${direction}. Must be 'asc' or 'desc'`
);
}
}
}
/**
* Validate field selection format
*/
private static validateFields(fields: string): void {
if (fields === '*') {
return; // Wildcard is always valid
}
const fieldList = fields.split(',');
for (const field of fieldList) {
const trimmed = field.trim();
// Must be non-empty
if (!trimmed) {
throw new ValidationError('Empty field in field selection');
}
// Check for valid characters (alphanumeric, dots, underscores, wildcards)
if (!/^[a-zA-Z0-9_.*]+$/.test(trimmed)) {
throw new ValidationError(
`Invalid field name: ${trimmed}. Only alphanumeric, dots, underscores, and wildcards allowed`
);
}
}
}
/**
* Validate filter expression recursively
*/
private static validateFilter(filter: FilterExpression): void {
// Logical operators must have at least one sub-expression
if (filter.and && filter.and.length === 0) {
throw new ValidationError('AND expression must have at least one condition');
}
if (filter.or && filter.or.length === 0) {
throw new ValidationError('OR expression must have at least one condition');
}
// Recursively validate nested filters
if (filter.and) {
filter.and.forEach(f => this.validateFilter(f));
}
if (filter.or) {
filter.or.forEach(f => this.validateFilter(f));
}
if (filter.not) {
this.validateFilter(filter.not);
}
// Validate date ranges
if (filter.between) {
for (const [field, [start, end]] of Object.entries(filter.between)) {
if (start >= end) {
throw new ValidationError(
`Invalid date range for ${field}: start date must be before end date`
);
}
}
}
}
/**
* Validate aggregation query
*/
private static validateAggregation(aggregate: any): void {
if (aggregate.groupBy.length === 0) {
throw new ValidationError('Aggregation must have at least one groupBy field');
}
if (Object.keys(aggregate.metrics).length === 0) {
throw new ValidationError('Aggregation must have at least one metric');
}
// Validate metric definitions
for (const [name, metric] of Object.entries(aggregate.metrics)) {
if (!metric.field) {
throw new ValidationError(`Metric ${name} must specify a field`);
}
}
}
}
/**
* Validation Error
*/
export class ValidationError extends Error {
constructor(
message: string,
public details?: Array<{ field: string; message: string }>
) {
super(message);
this.name = 'ValidationError';
}
}
```
---
## 2. Field Selector Engine
```typescript
/**
* src/optimization/FieldSelector.ts
*
* Intelligent field selection engine
*/
export class FieldSelector {
/**
* Select fields from data based on field expression
*
* @param data - Source data (array or object)
* @param fieldExpr - Field expression ("*", "field1,field2", "obj.*")
* @param excludeExpr - Fields to exclude
* @returns Filtered data with selected fields only
*/
select<T>(
data: T | T[],
fieldExpr: string = '*',
excludeExpr?: string
): Partial<T> | Partial<T>[] {
const isArray = Array.isArray(data);
const items = isArray ? data : [data];
const fields = this.parseFieldExpression(fieldExpr);
const excludes = excludeExpr ? this.parseFieldExpression(excludeExpr) : [];
const filtered = items.map(item =>
this.selectFields(item, fields, excludes)
);
return isArray ? filtered : filtered[0];
}
/**
* Parse field expression into field paths
* Supports:
* - "*" - all fields
* - "field1,field2" - specific fields
* - "obj.*" - all fields in nested object
* - "obj.field" - specific nested field
*/
private parseFieldExpression(expr: string): string[] {
return expr.split(',').map(f => f.trim()).filter(f => f.length > 0);
}
/**
* Select specific fields from object
*/
private selectFields(
obj: any,
fields: string[],
excludes: string[]
): any {
// Handle wildcard
if (fields.includes('*')) {
return this.excludeFields(obj, excludes);
}
const result: any = {};
for (const field of fields) {
// Handle nested fields (e.g., "contact.name")
if (field.includes('.')) {
const value = this.getNestedField(obj, field);
if (value !== undefined) {
this.setNestedField(result, field, value);
}
} else if (field.endsWith('.*')) {
// Handle wildcard in nested object (e.g., "contact.*")
const prefix = field.slice(0, -2);
const nestedObj = this.getNestedField(obj, prefix);
if (nestedObj && typeof nestedObj === 'object') {
this.setNestedField(result, prefix, { ...nestedObj });
}
} else {
// Simple field
if (obj[field] !== undefined) {
result[field] = obj[field];
}
}
}
return this.excludeFields(result, excludes);
}
/**
* Get nested field value using dot notation
* Example: getNestedField({ contact: { name: 'John' } }, 'contact.name') => 'John'
*/
private getNestedField(obj: any, path: string): any {
return path.split('.').reduce((curr, key) => curr?.[key], obj);
}
/**
* Set nested field value using dot notation
* Example: setNestedField({}, 'contact.name', 'John') => { contact: { name: 'John' } }
*/
private setNestedField(obj: any, path: string, value: any): void {
const keys = path.split('.');
const lastKey = keys.pop()!;
const target = keys.reduce((curr, key) => {
if (!curr[key]) curr[key] = {};
return curr[key];
}, obj);
target[lastKey] = value;
}
/**
* Exclude fields from object
*/
private excludeFields(obj: any, excludes: string[]): any {
if (excludes.length === 0) return obj;
const result = { ...obj };
for (const exclude of excludes) {
if (exclude.includes('.')) {
// Handle nested exclusions
const keys = exclude.split('.');
const lastKey = keys.pop()!;
const target = this.getNestedField(result, keys.join('.'));
if (target && typeof target === 'object') {
delete target[lastKey];
}
} else {
delete result[exclude];
}
}
return result;
}
/**
* Calculate size reduction from field selection
*/
calculateReduction(original: any, selected: any): number {
const originalSize = Buffer.byteLength(JSON.stringify(original), 'utf8');
const selectedSize = Buffer.byteLength(JSON.stringify(selected), 'utf8');
return ((originalSize - selectedSize) / originalSize) * 100;
}
}
/**
* Usage Examples
*/
// Example 1: Select specific fields
const fieldSelector = new FieldSelector();
const job = {
jnid: 'job-123',
number: '1820',
status: 'Jobs In Progress',
total: 15000,
description: 'Long description...',
notes: 'Internal notes...',
contact: {
jnid: 'contact-456',
name: 'John Smith',
email: 'john@example.com',
phone: '555-1234',
},
};
// Select only essential fields (90% size reduction)
const essential = fieldSelector.select(job, 'jnid,number,status,total');
// Result: { jnid: 'job-123', number: '1820', status: 'Jobs In Progress', total: 15000 }
// Select with nested fields
const withContact = fieldSelector.select(job, 'jnid,number,contact.name,contact.email');
// Result: { jnid: 'job-123', number: '1820', contact: { name: 'John Smith', email: 'john@example.com' } }
// Select all except specific fields
const noNotes = fieldSelector.select(job, '*', 'description,notes');
// Result: all fields except description and notes
// Select all contact fields
const withFullContact = fieldSelector.select(job, 'jnid,number,contact.*');
// Result: { jnid: 'job-123', number: '1820', contact: { jnid: '...', name: '...', email: '...', phone: '...' } }
```
---
## 3. Filter Expression Evaluator
```typescript
/**
* src/optimization/FilterEvaluator.ts
*
* Evaluate filter expressions against data
*/
import { FilterExpression } from './QueryParser.js';
export class FilterEvaluator {
/**
* Evaluate filter expression against a single item
*/
evaluate(item: any, filter: FilterExpression): boolean {
// Logical operators
if (filter.and) {
return filter.and.every(f => this.evaluate(item, f));
}
if (filter.or) {
return filter.or.some(f => this.evaluate(item, f));
}
if (filter.not) {
return !this.evaluate(item, filter.not);
}
// Comparison operators
if (filter.eq) {
return this.evaluateComparison(item, filter.eq, (a, b) => a === b);
}
if (filter.ne) {
return this.evaluateComparison(item, filter.ne, (a, b) => a !== b);
}
if (filter.gt) {
return this.evaluateComparison(item, filter.gt, (a, b) => a > b);
}
if (filter.gte) {
return this.evaluateComparison(item, filter.gte, (a, b) => a >= b);
}
if (filter.lt) {
return this.evaluateComparison(item, filter.lt, (a, b) => a < b);
}
if (filter.lte) {
return this.evaluateComparison(item, filter.lte, (a, b) => a <= b);
}
// String operators
if (filter.contains) {
return this.evaluateStringOp(item, filter.contains, (a, b) =>
a.toLowerCase().includes(b.toLowerCase())
);
}
if (filter.startsWith) {
return this.evaluateStringOp(item, filter.startsWith, (a, b) =>
a.toLowerCase().startsWith(b.toLowerCase())
);
}
if (filter.endsWith) {
return this.evaluateStringOp(item, filter.endsWith, (a, b) =>
a.toLowerCase().endsWith(b.toLowerCase())
);
}
if (filter.regex) {
return this.evaluateRegex(item, filter.regex);
}
// Array operators
if (filter.in) {
return this.evaluateIn(item, filter.in, true);
}
if (filter.notIn) {
return this.evaluateIn(item, filter.notIn, false);
}
// Date operators
if (filter.between) {
return this.evaluateBetween(item, filter.between);
}
// Existence checks
if (filter.exists) {
return filter.exists.every(field => this.fieldExists(item, field));
}
if (filter.missing) {
return filter.missing.every(field => !this.fieldExists(item, field));
}
// Default: no filter means include all
return true;
}
/**
* Filter array of items
*/
filter<T>(items: T[], filter: FilterExpression): T[] {
return items.filter(item => this.evaluate(item, filter));
}
/**
* Evaluate comparison operator
*/
private evaluateComparison(
item: any,
conditions: Record<string, any>,
compareFn: (a: any, b: any) => boolean
): boolean {
for (const [field, value] of Object.entries(conditions)) {
const itemValue = this.getFieldValue(item, field);
if (itemValue === undefined) {
return false;
}
if (!compareFn(itemValue, value)) {
return false;
}
}
return true;
}
/**
* Evaluate string operator
*/
private evaluateStringOp(
item: any,
conditions: Record<string, string>,
compareFn: (a: string, b: string) => boolean
): boolean {
for (const [field, value] of Object.entries(conditions)) {
const itemValue = this.getFieldValue(item, field);
if (typeof itemValue !== 'string') {
return false;
}
if (!compareFn(itemValue, value)) {
return false;
}
}
return true;
}
/**
* Evaluate regex operator
*/
private evaluateRegex(item: any, conditions: Record<string, string>): boolean {
for (const [field, pattern] of Object.entries(conditions)) {
const itemValue = this.getFieldValue(item, field);
if (typeof itemValue !== 'string') {
return false;
}
const regex = new RegExp(pattern, 'i');
if (!regex.test(itemValue)) {
return false;
}
}
return true;
}
/**
* Evaluate in/notIn operator
*/
private evaluateIn(
item: any,
conditions: Record<string, any[]>,
shouldBeIn: boolean
): boolean {
for (const [field, values] of Object.entries(conditions)) {
const itemValue = this.getFieldValue(item, field);
const isIn = values.includes(itemValue);
if (isIn !== shouldBeIn) {
return false;
}
}
return true;
}
/**
* Evaluate between operator (for dates/numbers)
*/
private evaluateBetween(
item: any,
conditions: Record<string, [Date, Date]>
): boolean {
for (const [field, [start, end]] of Object.entries(conditions)) {
const itemValue = this.getFieldValue(item, field);
// Convert to Date if needed
const dateValue = itemValue instanceof Date
? itemValue
: new Date(itemValue);
if (dateValue < start || dateValue > end) {
return false;
}
}
return true;
}
/**
* Check if field exists (is not undefined/null)
*/
private fieldExists(item: any, field: string): boolean {
const value = this.getFieldValue(item, field);
return value !== undefined && value !== null;
}
/**
* Get field value using dot notation
*/
private getFieldValue(item: any, field: string): any {
return field.split('.').reduce((curr, key) => curr?.[key], item);
}
}
/**
* Usage Examples
*/
const evaluator = new FilterEvaluator();
const jobs = [
{ jnid: '1', status: 'Jobs In Progress', total: 5000, contact: { name: 'John Smith' } },
{ jnid: '2', status: 'Estimating', total: 12000, contact: { name: 'Jane Doe' } },
{ jnid: '3', status: 'Jobs In Progress', total: 25000, contact: { name: 'Bob Smith' } },
];
// Example 1: Simple equality
const inProgress = evaluator.filter(jobs, {
eq: { status: 'Jobs In Progress' },
});
// Result: jobs 1 and 3
// Example 2: Range filter
const highValue = evaluator.filter(jobs, {
and: [
{ gte: { total: 10000 } },
{ lte: { total: 30000 } },
],
});
// Result: jobs 2 and 3
// Example 3: String contains
const smiths = evaluator.filter(jobs, {
contains: { 'contact.name': 'Smith' },
});
// Result: jobs 1 and 3
// Example 4: Complex logic
const filtered = evaluator.filter(jobs, {
or: [
{ eq: { status: 'Estimating' } },
{
and: [
{ eq: { status: 'Jobs In Progress' } },
{ gte: { total: 20000 } },
],
},
],
});
// Result: jobs 2 and 3
```
---
## 4. Smart Cache Manager
```typescript
/**
* src/optimization/SmartCacheManager.ts
*
* Multi-tier intelligent cache manager
*/
import Redis from 'ioredis';
import { HandleStore } from './HandleStore.js';
interface CacheGetOptions {
maxAge?: number;
preferStale?: boolean;
}
interface CacheSetOptions {
ttl?: number;
tier?: 1 | 2 | 3;
}
interface CacheResult<T = any> {
data: T;
source: 'tier1' | 'tier2' | 'tier3';
tier: 1 | 2 | 3;
age?: number;
}
export class SmartCacheManager {
private tier1: Redis; // Hot cache (1-15 min TTL)
private tier2: Redis; // Warm cache (30-60 min TTL)
private tier3: HandleStore; // Large responses (handle-based)
private accessCounts: Map<string, number> = new Map();
private accessTimestamps: Map<string, number[]> = new Map();
constructor(
tier1Config: any,
tier2Config: any,
tier3Store: HandleStore
) {
this.tier1 = new Redis(tier1Config);
this.tier2 = new Redis(tier2Config);
this.tier3 = tier3Store;
}
/**
* Get from cache with intelligent tier fallback
*/
async get<T = any>(
key: string,
options: CacheGetOptions = {}
): Promise<CacheResult<T> | null> {
// Try Tier 1 (Hot cache)
const tier1Result = await this.tier1.get(key);
if (tier1Result) {
const data = this.deserialize<T>(tier1Result);
const age = await this.getAge(key, 1);
// Check if data is too stale
if (options.maxAge && age > options.maxAge && !options.preferStale) {
await this.tier1.del(key);
return null;
}
await this.trackAccess(key, 1);
return {
data,
source: 'tier1',
tier: 1,
age,
};
}
// Try Tier 2 (Warm cache)
const tier2Result = await this.tier2.get(key);
if (tier2Result) {
const data = this.deserialize<T>(tier2Result);
const age = await this.getAge(key, 2);
// Check if data is too stale
if (options.maxAge && age > options.maxAge && !options.preferStale) {
await this.tier2.del(key);
return null;
}
// Promote to Tier 1 if frequently accessed
if (await this.shouldPromote(key)) {
await this.tier1.setex(key, 300, tier2Result); // 5 min in Tier 1
}
await this.trackAccess(key, 2);
return {
data,
source: 'tier2',
tier: 2,
age,
};
}
// Try Tier 3 (Handle storage)
const handleKey = `${key}:handle`;
const handleRef = await this.tier1.get(handleKey);
if (handleRef) {
const { handle } = JSON.parse(handleRef);
const data = await this.tier3.get<T>(handle);
if (data) {
await this.trackAccess(key, 3);
return {
data,
source: 'tier3',
tier: 3,
};
}
}
return null;
}
/**
* Set in cache with intelligent tier selection
*/
async set<T = any>(
key: string,
value: T,
options: CacheSetOptions = {}
): Promise<void> {
const size = this.calculateSize(value);
const serialized = this.serialize(value);
// Route to appropriate tier based on size and access pattern
const tier = options.tier || this.selectTier(key, size);
switch (tier) {
case 1:
// Tier 1: Hot cache (5-15 min)
const ttl1 = options.ttl || 300; // 5 minutes default
await this.tier1.setex(key, ttl1, serialized);
await this.setAge(key, 1);
break;
case 2:
// Tier 2: Warm cache (30-60 min)
const ttl2 = options.ttl || 1800; // 30 minutes default
await this.tier2.setex(key, ttl2, serialized);
await this.setAge(key, 2);
break;
case 3:
// Tier 3: Large responses (handle-based)
const handle = await this.tier3.store(value, options.ttl || 900);
// Cache the handle reference in Tier 1
await this.tier1.setex(
`${key}:handle`,
options.ttl || 900,
JSON.stringify({ handle, size })
);
break;
}
await this.trackAccess(key, tier);
}
/**
* Invalidate cache key
*/
async invalidate(key: string): Promise<void> {
await Promise.all([
this.tier1.del(key),
this.tier2.del(key),
this.tier1.del(`${key}:handle`),
]);
}
/**
* Invalidate pattern (e.g., "jobs:*")
*/
async invalidatePattern(pattern: string): Promise<number> {
let count = 0;
// Invalidate in Tier 1
const tier1Keys = await this.scanKeys(this.tier1, pattern);
if (tier1Keys.length > 0) {
count += await this.tier1.del(...tier1Keys);
}
// Invalidate in Tier 2
const tier2Keys = await this.scanKeys(this.tier2, pattern);
if (tier2Keys.length > 0) {
count += await this.tier2.del(...tier2Keys);
}
return count;
}
/**
* Select cache tier based on size and access pattern
*/
private selectTier(key: string, size: number): 1 | 2 | 3 {
// Tier 3: Large responses (> 25 KB)
if (size > 25 * 1024) {
return 3;
}
// Tier 1: Small, frequently accessed (< 5 KB and accessed 3+ times recently)
if (size < 5 * 1024 && this.isFrequentlyAccessed(key)) {
return 1;
}
// Tier 2: Everything else
return 2;
}
/**
* Should promote item from Tier 2 to Tier 1
*/
private async shouldPromote(key: string): Promise<boolean> {
const count = this.getAccessCount(key);
// Promote if accessed 3+ times in last 5 minutes
return count >= 3;
}
/**
* Check if key is frequently accessed
*/
private isFrequentlyAccessed(key: string): boolean {
const timestamps = this.accessTimestamps.get(key) || [];
const now = Date.now();
const fiveMinutesAgo = now - 5 * 60 * 1000;
// Count accesses in last 5 minutes
const recentAccesses = timestamps.filter(t => t > fiveMinutesAgo).length;
return recentAccesses >= 3;
}
/**
* Track access event
*/
private async trackAccess(key: string, tier: number): Promise<void> {
// Increment access count
const current = this.accessCounts.get(key) || 0;
this.accessCounts.set(key, current + 1);
// Track timestamp
const timestamps = this.accessTimestamps.get(key) || [];
timestamps.push(Date.now());
// Keep only last 100 timestamps
if (timestamps.length > 100) {
timestamps.shift();
}
this.accessTimestamps.set(key, timestamps);
}
/**
* Get access count for key
*/
private getAccessCount(key: string): number {
return this.accessCounts.get(key) || 0;
}
/**
* Get age of cached data (in milliseconds)
*/
private async getAge(key: string, tier: number): Promise<number> {
const ageKey = `${key}:age`;
const redis = tier === 1 ? this.tier1 : this.tier2;
const timestamp = await redis.get(ageKey);
if (!timestamp) {
return 0;
}
return Date.now() - parseInt(timestamp, 10);
}
/**
* Set age timestamp
*/
private async setAge(key: string, tier: number): Promise<void> {
const ageKey = `${key}:age`;
const redis = tier === 1 ? this.tier1 : this.tier2;
await redis.set(ageKey, Date.now().toString());
}
/**
* Scan keys matching pattern
*/
private async scanKeys(redis: Redis, pattern: string): Promise<string[]> {
const keys: string[] = [];
let cursor = '0';
do {
const [nextCursor, foundKeys] = await redis.scan(
cursor,
'MATCH',
pattern,
'COUNT',
100
);
cursor = nextCursor;
keys.push(...foundKeys);
} while (cursor !== '0');
return keys;
}
/**
* Serialize data for storage
*/
private serialize(data: any): string {
return JSON.stringify(data);
}
/**
* Deserialize data from storage
*/
private deserialize<T>(data: string): T {
return JSON.parse(data);
}
/**
* Calculate data size
*/
private calculateSize(data: any): number {
return Buffer.byteLength(JSON.stringify(data), 'utf8');
}
}
```
---
## 5. Data Transformer
```typescript
/**
* src/optimization/DataTransformer.ts
*
* Transform raw data to optimized format
*/
import { FieldSelector } from './FieldSelector.js';
import { VerbosityLevel } from '../config/response.js';
interface TransformOptions {
verbosity: VerbosityLevel;
fields?: string;
exclude?: string;
includeSummary?: boolean;
flatten?: boolean;
maxTextLength?: number;
format: OutputFormat;
groupBy?: string;
}
type OutputFormat = 'json' | 'jsonlines' | 'csv' | 'summary';
interface TransformedResponse {
data: any;
metadata: {
transformTime: number;
originalSize: number;
optimizedSize: number;
reductionPercent: number;
fields: string[];
};
}
export class DataTransformer {
private fieldSelector: FieldSelector;
constructor() {
this.fieldSelector = new FieldSelector();
}
/**
* Transform raw data based on verbosity level and format
*/
async transform(
data: any,
options: TransformOptions
): Promise<TransformedResponse> {
const startTime = Date.now();
const originalSize = this.calculateSize(data);
// Step 1: Apply verbosity-based field selection
let transformed = this.applyVerbosity(data, options.verbosity);
// Step 2: Apply custom field selection if specified
if (options.fields) {
transformed = this.fieldSelector.select(
transformed,
options.fields,
options.exclude
);
}
// Step 3: Add summaries if requested
if (options.includeSummary) {
transformed = this.addSummaries(transformed, options);
}
// Step 4: Truncate long text fields
transformed = this.truncateLongFields(
transformed,
options.maxTextLength || 200
);
// Step 5: Format output
const formatted = this.formatOutput(transformed, options.format);
const optimizedSize = this.calculateSize(formatted);
const transformTime = Date.now() - startTime;
return {
data: formatted,
metadata: {
transformTime,
originalSize,
optimizedSize,
reductionPercent: ((originalSize - optimizedSize) / originalSize) * 100,
fields: this.getFieldNames(formatted),
},
};
}
/**
* Apply verbosity-based field selection
*/
private applyVerbosity(data: any, verbosity: VerbosityLevel): any {
const isArray = Array.isArray(data);
const items = isArray ? data : [data];
const fieldSets = this.getVerbosityFieldSet(verbosity);
const filtered = items.map(item => {
const result: any = {};
for (const field of fieldSets) {
if (item[field] !== undefined) {
result[field] = item[field];
}
}
return result;
});
return isArray ? filtered : filtered[0];
}
/**
* Get field set for verbosity level
*/
private getVerbosityFieldSet(verbosity: VerbosityLevel): string[] {
switch (verbosity) {
case 'summary':
// Ultra-minimal: only critical fields
return ['jnid', 'number', 'status', 'total', 'date_created'];
case 'compact':
// Default: essential fields for most use cases
return [
'jnid',
'number',
'status',
'total',
'date_created',
'date_updated',
'status_name',
'sales_rep_name',
'contact',
'address_line1',
'city',
'state',
'record_type_name',
'owners',
];
case 'detailed':
// Comprehensive: most fields without heavy data
return [
'jnid',
'number',
'status',
'status_name',
'total',
'date_created',
'date_updated',
'date_start',
'date_end',
'sales_rep',
'sales_rep_name',
'contact',
'related',
'tags',
'address_line1',
'address_line2',
'city',
'state',
'zip',
'record_type',
'record_type_name',
'owners',
'location_id',
'custom',
];
case 'raw':
// Everything (no filtering)
return ['*'];
}
}
/**
* Add statistical summaries for arrays
*/
private addSummaries(data: any, options: TransformOptions): any {
if (!Array.isArray(data)) {
return data;
}
const summary = this.generateArraySummary(data, options);
return {
summary,
items: data,
};
}
/**
* Generate statistical summary for array
*/
private generateArraySummary(items: any[], options: TransformOptions): any {
if (items.length === 0) {
return { count: 0 };
}
const summary: any = {
count: items.length,
};
// Detect numeric fields and calculate stats
const firstItem = items[0];
const numericFields = Object.keys(firstItem).filter(
key => typeof firstItem[key] === 'number'
);
for (const field of numericFields) {
const values = items
.map(item => item[field])
.filter(v => v != null && !isNaN(v));
if (values.length > 0) {
summary[field] = {
min: Math.min(...values),
max: Math.max(...values),
avg: values.reduce((a, b) => a + b, 0) / values.length,
sum: values.reduce((a, b) => a + b, 0),
};
}
}
// Group by categorical fields if requested
if (options.groupBy) {
summary.groupedBy = this.groupByField(items, options.groupBy);
}
return summary;
}
/**
* Group items by field
*/
private groupByField(items: any[], field: string): any {
const grouped: Record<string, any> = {};
for (const item of items) {
const value = item[field];
const key = value?.toString() || 'null';
if (!grouped[key]) {
grouped[key] = {
count: 0,
items: [],
};
}
grouped[key].count++;
grouped[key].items.push(item.jnid || item.id);
}
return grouped;
}
/**
* Truncate long text fields
*/
private truncateLongFields(data: any, maxLength: number): any {
const truncate = (obj: any): any => {
if (typeof obj === 'string' && obj.length > maxLength) {
return obj.substring(0, maxLength) + '...[truncated]';
}
if (Array.isArray(obj)) {
return obj.map(truncate);
}
if (obj && typeof obj === 'object') {
const result: any = {};
for (const [key, value] of Object.entries(obj)) {
result[key] = truncate(value);
}
return result;
}
return obj;
};
return truncate(data);
}
/**
* Format output based on requested format
*/
private formatOutput(data: any, format: OutputFormat): any {
switch (format) {
case 'summary':
return this.generateSummary(data);
case 'jsonlines':
return this.toJSONLines(data);
case 'csv':
return this.toCSV(data);
default:
return data;
}
}
/**
* Generate summary representation
*/
private generateSummary(data: any): any {
if (!Array.isArray(data)) {
return {
type: typeof data,
preview: JSON.stringify(data).substring(0, 100),
};
}
return {
count: data.length,
sample: data.slice(0, 3),
};
}
/**
* Convert to JSON Lines format (one JSON object per line)
*/
private toJSONLines(data: any): string {
if (!Array.isArray(data)) {
return JSON.stringify(data);
}
return data.map(item => JSON.stringify(item)).join('\n');
}
/**
* Convert to CSV format
*/
private toCSV(data: any): string {
if (!Array.isArray(data) || data.length === 0) {
return '';
}
// Get headers from first item
const headers = Object.keys(data[0]);
// Create CSV header row
const headerRow = headers.join(',');
// Create data rows
const dataRows = data.map(item => {
return headers.map(header => {
const value = item[header];
// Handle special cases
if (value === null || value === undefined) {
return '';
}
if (typeof value === 'object') {
return JSON.stringify(value).replace(/"/g, '""');
}
const str = value.toString();
// Quote if contains comma, newline, or quote
if (str.includes(',') || str.includes('\n') || str.includes('"')) {
return `"${str.replace(/"/g, '""')}"`;
}
return str;
}).join(',');
});
return [headerRow, ...dataRows].join('\n');
}
/**
* Get field names from data
*/
private getFieldNames(data: any): string[] {
if (Array.isArray(data)) {
if (data.length === 0) return [];
return Object.keys(data[0]);
}
if (typeof data === 'object' && data !== null) {
return Object.keys(data);
}
return [];
}
/**
* Calculate data size
*/
private calculateSize(data: any): number {
return Buffer.byteLength(JSON.stringify(data), 'utf8');
}
}
```
---
## 6. Compression Middleware
```typescript
/**
* src/optimization/CompressionMiddleware.ts
*
* Intelligent response compression
*/
import { promisify } from 'util';
import { gzip, brotliCompress } from 'zlib';
import { Request, Response, NextFunction } from 'express';
const gzipAsync = promisify(gzip);
const brotliAsync = promisify(brotliCompress);
type CompressionAlgorithm = 'gzip' | 'brotli' | 'none';
export class CompressionMiddleware {
private readonly COMPRESSION_THRESHOLD = 1024; // 1 KB
/**
* Express middleware for intelligent compression
*/
middleware() {
return async (req: Request, res: Response, next: NextFunction) => {
// Capture original res.json method
const originalJson = res.json.bind(res);
// Override res.json
res.json = async (data: any) => {
try {
const json = JSON.stringify(data);
const size = Buffer.byteLength(json, 'utf8');
// Skip compression if below threshold
if (size < this.COMPRESSION_THRESHOLD) {
return originalJson(data);
}
// Choose compression algorithm
const algorithm = this.selectAlgorithm(req, size);
if (algorithm === 'none') {
return originalJson(data);
}
// Compress
const compressed = await this.compress(json, algorithm);
// Set headers
res.setHeader('Content-Encoding', algorithm);
res.setHeader('Content-Type', 'application/json');
res.setHeader('X-Uncompressed-Size', size.toString());
res.setHeader('X-Compression-Ratio',
(compressed.length / size).toFixed(2)
);
// Send compressed response
return res.send(compressed);
} catch (error) {
// Fallback to uncompressed on error
console.error('Compression error:', error);
return originalJson(data);
}
};
next();
};
}
/**
* Select compression algorithm based on client support and size
*/
private selectAlgorithm(req: Request, size: number): CompressionAlgorithm {
const acceptEncoding = req.headers['accept-encoding'] || '';
// Prefer Brotli for larger payloads (better compression)
if (size > 50000 && acceptEncoding.includes('br')) {
return 'brotli';
}
// Use GZIP for medium payloads (faster)
if (acceptEncoding.includes('gzip')) {
return 'gzip';
}
return 'none';
}
/**
* Compress data using selected algorithm
*/
private async compress(
data: string,
algorithm: CompressionAlgorithm
): Promise<Buffer> {
const buffer = Buffer.from(data, 'utf8');
switch (algorithm) {
case 'gzip':
return await gzipAsync(buffer);
case 'brotli':
return await brotliAsync(buffer);
default:
return buffer;
}
}
}
/**
* Usage in Express app
*/
// import { CompressionMiddleware } from './optimization/CompressionMiddleware';
//
// const compressionMiddleware = new CompressionMiddleware();
// app.use(compressionMiddleware.middleware());
```
---
## 7. Metrics Collector
```typescript
/**
* src/optimization/MetricsCollector.ts
*
* Comprehensive metrics collection for optimization
*/
interface ResponseMetrics {
endpoint: string;
verbosity: string;
sizeBytes: number;
originalSize?: number;
reductionPercent?: number;
latencyMs: number;
cached?: boolean;
tier?: number;
compressionRatio?: number;
}
interface OptimizationMetrics {
// Response size metrics
avgResponseSize: number;
avgResponseSizeBefore: number;
reductionPercent: number;
// Token usage metrics
avgTokenUsage: number;
avgTokenUsageBefore: number;
tokenSavings: number;
// Cache performance
cacheHitRate: number;
avgCacheLatency: number;
tier1HitRate: number;
tier2HitRate: number;
tier3HitRate: number;
// Optimization usage
fieldSelectionUsage: number;
compressionUsage: number;
avgCompressionRatio: number;
streamingUsage: number;
}
export class MetricsCollector {
private responses: ResponseMetrics[] = [];
private readonly MAX_SAMPLES = 10000;
/**
* Track response metrics
*/
trackResponse(metrics: ResponseMetrics): void {
this.responses.push({
...metrics,
timestamp: Date.now(),
} as any);
// Keep only recent samples
if (this.responses.length > this.MAX_SAMPLES) {
this.responses.shift();
}
}
/**
* Calculate optimization metrics
*/
calculateMetrics(periodMs: number = 3600000): OptimizationMetrics {
const cutoff = Date.now() - periodMs;
const recent = this.responses.filter(r => (r as any).timestamp > cutoff);
if (recent.length === 0) {
return this.getEmptyMetrics();
}
// Response size metrics
const avgResponseSize = this.average(recent.map(r => r.sizeBytes));
const withOriginal = recent.filter(r => r.originalSize);
const avgResponseSizeBefore = withOriginal.length > 0
? this.average(withOriginal.map(r => r.originalSize!))
: avgResponseSize;
const reductionPercent = withOriginal.length > 0
? this.average(withOriginal.map(r => r.reductionPercent!))
: 0;
// Token usage (estimate: 1 token ≈ 4 bytes)
const avgTokenUsage = avgResponseSize / 4;
const avgTokenUsageBefore = avgResponseSizeBefore / 4;
const tokenSavings = avgTokenUsageBefore - avgTokenUsage;
// Cache performance
const cached = recent.filter(r => r.cached);
const cacheHitRate = cached.length / recent.length;
const avgCacheLatency = cached.length > 0
? this.average(cached.map(r => r.latencyMs))
: 0;
const tier1Hits = cached.filter(r => r.tier === 1).length;
const tier2Hits = cached.filter(r => r.tier === 2).length;
const tier3Hits = cached.filter(r => r.tier === 3).length;
const totalCached = tier1Hits + tier2Hits + tier3Hits;
const tier1HitRate = totalCached > 0 ? tier1Hits / totalCached : 0;
const tier2HitRate = totalCached > 0 ? tier2Hits / totalCached : 0;
const tier3HitRate = totalCached > 0 ? tier3Hits / totalCached : 0;
// Optimization usage
const withCompression = recent.filter(r => r.compressionRatio);
const compressionUsage = withCompression.length / recent.length;
const avgCompressionRatio = withCompression.length > 0
? this.average(withCompression.map(r => r.compressionRatio!))
: 1;
return {
avgResponseSize,
avgResponseSizeBefore,
reductionPercent,
avgTokenUsage,
avgTokenUsageBefore,
tokenSavings,
cacheHitRate,
avgCacheLatency,
tier1HitRate,
tier2HitRate,
tier3HitRate,
fieldSelectionUsage: 0, // TODO: track
compressionUsage,
avgCompressionRatio,
streamingUsage: 0, // TODO: track
};
}
/**
* Get metrics by endpoint
*/
getEndpointMetrics(endpoint: string, periodMs: number = 3600000): any {
const cutoff = Date.now() - periodMs;
const filtered = this.responses.filter(
r => (r as any).timestamp > cutoff && r.endpoint === endpoint
);
if (filtered.length === 0) {
return null;
}
return {
endpoint,
requestCount: filtered.length,
avgResponseSize: this.average(filtered.map(r => r.sizeBytes)),
avgLatency: this.average(filtered.map(r => r.latencyMs)),
cacheHitRate: filtered.filter(r => r.cached).length / filtered.length,
};
}
/**
* Get top endpoints by response size
*/
getTopEndpointsBySize(limit: number = 10): any[] {
const byEndpoint = new Map<string, ResponseMetrics[]>();
for (const response of this.responses) {
const endpoint = response.endpoint;
if (!byEndpoint.has(endpoint)) {
byEndpoint.set(endpoint, []);
}
byEndpoint.get(endpoint)!.push(response);
}
const aggregated = Array.from(byEndpoint.entries()).map(([endpoint, metrics]) => ({
endpoint,
avgSize: this.average(metrics.map(m => m.sizeBytes)),
requestCount: metrics.length,
}));
return aggregated
.sort((a, b) => b.avgSize - a.avgSize)
.slice(0, limit);
}
/**
* Calculate average
*/
private average(values: number[]): number {
if (values.length === 0) return 0;
return values.reduce((a, b) => a + b, 0) / values.length;
}
/**
* Get empty metrics (for when no data available)
*/
private getEmptyMetrics(): OptimizationMetrics {
return {
avgResponseSize: 0,
avgResponseSizeBefore: 0,
reductionPercent: 0,
avgTokenUsage: 0,
avgTokenUsageBefore: 0,
tokenSavings: 0,
cacheHitRate: 0,
avgCacheLatency: 0,
tier1HitRate: 0,
tier2HitRate: 0,
tier3HitRate: 0,
fieldSelectionUsage: 0,
compressionUsage: 0,
avgCompressionRatio: 1,
streamingUsage: 0,
};
}
}
```
---
## 8. Enhanced Tool Example
```typescript
/**
* Example: Enhanced get_jobs tool with full optimization
*/
import { QueryParser, QueryValidator } from '../optimization/QueryParser.js';
import { FilterEvaluator } from '../optimization/FilterEvaluator.js';
import { DataTransformer } from '../optimization/DataTransformer.js';
import { SmartCacheManager } from '../optimization/SmartCacheManager.js';
import { MetricsCollector } from '../optimization/MetricsCollector.js';
import { jobNimbusClient } from '../services/jobNimbusClient.js';
interface GetJobsParams {
// Field selection
fields?: string;
exclude?: string;
// Filtering
filter?: any;
search?: string;
// Sorting & Pagination
sort?: string;
pageSize?: number;
cursor?: string;
// Verbosity
verbosity?: 'summary' | 'compact' | 'detailed' | 'raw';
// Performance hints
preferCache?: boolean;
maxAge?: number;
// Response format
format?: 'json' | 'jsonlines' | 'csv' | 'summary';
}
export async function getJobsEnhanced(params: GetJobsParams) {
const startTime = Date.now();
try {
// Step 1: Parse and validate query
const query = QueryParser.parse(params);
QueryValidator.validate(query);
// Step 2: Build cache key
const cacheKey = buildCacheKey('jobs', 'list', query);
// Step 3: Check cache
const cached = await smartCache.get(cacheKey, {
maxAge: params.maxAge,
preferStale: params.preferCache,
});
if (cached) {
// Track metrics
metricsCollector.trackResponse({
endpoint: 'get_jobs',
verbosity: params.verbosity || 'compact',
sizeBytes: calculateSize(cached.data),
latencyMs: Date.now() - startTime,
cached: true,
tier: cached.tier,
});
return {
success: true,
data: cached.data,
metadata: {
source: 'cache',
tier: cached.tier,
latencyMs: Date.now() - startTime,
cached: true,
},
};
}
// Step 4: Fetch from API
const rawData = await jobNimbusClient.getJobs({
// Convert to JobNimbus API format
from: 0,
size: params.pageSize || 20,
});
// Step 5: Apply filter if specified
let filtered = rawData;
if (query.filter) {
const evaluator = new FilterEvaluator();
filtered = evaluator.filter(rawData, query.filter);
}
// Step 6: Transform and optimize
const transformer = new DataTransformer();
const transformed = await transformer.transform(filtered, {
verbosity: params.verbosity || 'compact',
fields: params.fields,
exclude: params.exclude,
format: params.format || 'json',
includeSummary: params.format === 'summary',
});
// Step 7: Cache for future requests
await smartCache.set(cacheKey, transformed.data, {
ttl: 300, // 5 minutes
});
// Step 8: Track metrics
metricsCollector.trackResponse({
endpoint: 'get_jobs',
verbosity: params.verbosity || 'compact',
sizeBytes: transformed.metadata.optimizedSize,
originalSize: transformed.metadata.originalSize,
reductionPercent: transformed.metadata.reductionPercent,
latencyMs: Date.now() - startTime,
cached: false,
});
return {
success: true,
data: transformed.data,
metadata: {
source: 'api',
latencyMs: Date.now() - startTime,
cached: false,
optimization: transformed.metadata,
},
};
} catch (error) {
console.error('Error in get_jobs:', error);
return {
success: false,
error: error.message,
metadata: {
latencyMs: Date.now() - startTime,
},
};
}
}
/**
* Build cache key from query
*/
function buildCacheKey(resource: string, operation: string, query: any): string {
const hash = hashQuery(query);
return `${resource}:${operation}:${hash}`;
}
/**
* Hash query for cache key
*/
function hashQuery(query: any): string {
const crypto = require('crypto');
const str = JSON.stringify(query);
return crypto.createHash('md5').update(str).digest('hex').substring(0, 8);
}
/**
* Calculate data size
*/
function calculateSize(data: any): number {
return Buffer.byteLength(JSON.stringify(data), 'utf8');
}
```
---
## Conclusion
These implementation examples provide production-ready code for the core optimization components:
1. **Query Parser & Validator** - Robust query parsing with Zod validation
2. **Field Selector Engine** - Powerful field selection with dot notation
3. **Filter Expression Evaluator** - Complex filtering with logical operators
4. **Smart Cache Manager** - Multi-tier intelligent caching
5. **Data Transformer** - Verbosity-based data optimization
6. **Compression Middleware** - Intelligent response compression
7. **Metrics Collector** - Comprehensive performance tracking
8. **Enhanced Tool Example** - Full integration of all components
Each component is:
- **Production-ready** with error handling
- **Well-documented** with usage examples
- **Type-safe** with TypeScript
- **Testable** with clear interfaces
- **Performant** with optimization in mind
Next steps:
1. Review code examples
2. Adapt to your specific needs
3. Implement unit tests
4. Deploy incrementally (Phase 1 first)
5. Monitor and iterate