@simonecoelhosfo/optimizely-mcp-server
Version:
Optimizely MCP Server for AI assistants with integrated CLI tools
177 lines • 6.13 kB
JavaScript
/**
* ViewAwareFieldResolver - Extends FieldLocalityResolver to support view-based queries
*
* This resolver adds view awareness to the existing field resolution system,
* allowing queries to be routed to views instead of complex JOINs when appropriate.
*/
import { FieldLocalityResolver } from './FieldLocalityResolver.js';
import { VIEW_FIELD_MAPPINGS } from '../generated/ViewFieldMappings.generated.js';
import { getLogger } from '../../logging/Logger.js';
const logger = getLogger();
export class ViewAwareFieldResolver extends FieldLocalityResolver {
viewMappings;
viewPreference = 'performance';
constructor() {
super();
this.viewMappings = new Map(Object.entries(VIEW_FIELD_MAPPINGS));
logger.info(`ViewAwareFieldResolver initialized with ${this.viewMappings.size} view mappings`);
}
/**
* Resolve field with view awareness
*/
resolveField(fieldName, context) {
// First check if this field has a view mapping
const viewResolution = this.resolveWithView(fieldName, context);
if (viewResolution.useView) {
logger.debug(`Field '${fieldName}' resolved to view: ${viewResolution.viewName}.${viewResolution.columnName}`);
return {
table: viewResolution.viewName,
column: viewResolution.columnName,
isView: true,
confidence: 1.0
};
}
// Fall back to legacy resolution
// Call the parent's method if it exists
if (typeof this.resolveFieldLocation === 'function') {
return this.resolveFieldLocation(fieldName, context);
}
// Otherwise return a default response
return {
table: 'unknown',
column: fieldName,
isView: false,
confidence: 0.5
};
}
/**
* Determine if a field should be resolved through a view
*/
resolveWithView(fieldName, context) {
const mapping = this.viewMappings.get(fieldName);
if (!mapping) {
return { useView: false };
}
// Check routing preferences
if (this.viewPreference === 'never') {
return { useView: false };
}
if (this.viewPreference === 'always' && mapping.routing.preferView) {
return {
useView: true,
viewName: mapping.view.viewName,
columnName: mapping.view.columnName
};
}
// Performance-based routing
if (this.shouldUseViewForPerformance(mapping, context)) {
return {
useView: true,
viewName: mapping.view.viewName,
columnName: mapping.view.columnName
};
}
return { useView: false };
}
/**
* Determine if view should be used based on performance characteristics
*/
shouldUseViewForPerformance(mapping, context) {
// Always use view for pre-computed fields
if (mapping.view.preComputed) {
return true;
}
// Check query context for aggregations or grouping
if (context?.hasAggregation || context?.hasGroupBy) {
return true;
}
// Check if this is a complex field that benefits from view
if (mapping.view.performance === 'fast' && mapping.routing.preferView) {
return true;
}
// Check if legacy path requires complex JOINs
if (mapping.legacy.tables.length > 1) {
return true;
}
return false;
}
/**
* Get all fields available in a specific view
*/
getViewFields(viewName) {
const fields = [];
for (const [fieldName, mapping] of this.viewMappings) {
if (mapping.view.viewName === viewName) {
fields.push(fieldName);
}
}
return fields;
}
/**
* Check if a field exists in any view
*/
hasViewMapping(fieldName) {
return this.viewMappings.has(fieldName);
}
/**
* Get the view name for a field
*/
getViewName(fieldName) {
return this.viewMappings.get(fieldName)?.view.viewName;
}
/**
* Set view preference for routing
*/
setViewPreference(preference) {
this.viewPreference = preference;
logger.info(`View preference set to: ${preference}`);
}
/**
* Get statistics about view coverage
*/
getViewCoverageStats() {
const totalFields = this.getAllFieldNames().length;
const viewMappedFields = this.viewMappings.size;
const preComputedFields = Array.from(this.viewMappings.values())
.filter(m => m.view.preComputed).length;
return {
totalFields,
viewMappedFields,
preComputedFields,
coveragePercent: (viewMappedFields / totalFields) * 100
};
}
/**
* Enhanced field information with view details
*/
getFieldInfo(fieldName) {
// Try to get base info from parent if method exists
let baseInfo = null;
if (typeof this.getFieldDetails === 'function') {
baseInfo = this.getFieldDetails(fieldName);
}
const viewMapping = this.viewMappings.get(fieldName);
if (viewMapping) {
return {
...baseInfo,
view: {
available: true,
viewName: viewMapping.view.viewName,
columnName: viewMapping.view.columnName,
preComputed: viewMapping.view.preComputed,
performance: viewMapping.view.performance
}
};
}
return baseInfo;
}
/**
* Get all fields from the parent FieldLocalityResolver
*/
getAllFieldNames() {
// This would need to be implemented based on the parent class structure
// For now, return a sample size
return Array.from({ length: 200 }, (_, i) => `field_${i}`);
}
}
//# sourceMappingURL=ViewAwareFieldResolver.js.map