Skip to content

Service Documentation

Comprehensive Service Layer Documentation

This section provides detailed documentation for each service class in Altus 4, including architecture, implementation details, and code explanations.

Service Architecture

Altus 4 follows a layered service architecture where each service has specific responsibilities:

mermaid
graph LR
    A[Controllers<br/>• Route Handlers<br/>• Input Validation<br/>• Response Format] --> B[Services<br/>• Business Logic<br/>• Orchestration<br/>• Error Handling]
    B --> C[Data Layer<br/>• MySQL<br/>• Redis<br/>• OpenAI API]

Service Principles

  1. Single Responsibility - Each service has a focused purpose
  2. Dependency Injection - Services receive dependencies via constructor
  3. Interface-Based - Services implement clear interfaces
  4. Error Handling - Comprehensive error handling with custom exceptions
  5. Testability - Services are unit testable with mocked dependencies

Service Overview

ServicePurposeDependenciesKey Features
SearchServiceSearch orchestrationDatabaseService, AIService, CacheServiceMulti-database search, AI enhancement, caching
DatabaseServiceMySQL operationsmysql2/promiseConnection management, query execution, schema discovery
AIServiceAI/ML integrationOpenAI APISemantic search, query optimization, result categorization
CacheServiceCaching & analyticsRedis/ioredisSearch caching, analytics storage, performance optimization
UserServiceUser managementDatabaseServiceUser CRUD, password hashing, account management
ApiKeyServiceAPI key managementDatabaseServiceAPI key generation, validation, tiered permissions

Detailed Service Documentation

SearchService

The core search orchestration engine that coordinates multi-database searches with AI enhancements.

Key Responsibilities:

  • Orchestrate searches across multiple MySQL databases
  • Integrate AI-powered semantic search and query optimization
  • Manage search result caching and performance analytics
  • Generate search suggestions and trend insights
  • Transform raw database results into structured search responses

Code Highlights:

  • Complex async/await orchestration
  • Advanced caching strategies
  • AI service integration patterns
  • Error handling and fallback mechanisms

DatabaseService

MySQL database connection management and query execution service.

Key Responsibilities:

  • Manage MySQL connection pools for multiple databases
  • Execute full-text search queries with optimization
  • Discover database schemas and table structures
  • Handle connection testing and health monitoring
  • Encrypt and store database credentials securely

Code Highlights:

  • Connection pooling and lifecycle management
  • Dynamic query building and parameterization
  • Schema introspection and metadata extraction
  • Security patterns for credential handling

AIService

OpenAI integration service for AI-enhanced search capabilities.

Key Responsibilities:

  • Process queries with semantic understanding
  • Generate intelligent search suggestions
  • Categorize and enhance search results
  • Provide query optimization recommendations
  • Generate search trend insights and analytics

Code Highlights:

  • OpenAI API integration patterns
  • Prompt engineering and response processing
  • Error handling for external API failures
  • Rate limiting and quota management

CacheService

Redis-based caching and analytics service for performance optimization.

Key Responsibilities:

  • Cache search results for improved performance
  • Store and retrieve search analytics data
  • Manage popular queries and trending searches
  • Track user search patterns and behaviors
  • Provide real-time performance metrics

Code Highlights:

  • Redis data structures and operations
  • Cache invalidation strategies
  • Analytics data modeling
  • Performance monitoring patterns

UserService

User management service handling account lifecycle and basic authentication.

Key Responsibilities:

  • User registration and profile management
  • Password hashing and authentication
  • User permission and role management
  • Account lifecycle operations

Code Highlights:

  • bcrypt password hashing patterns
  • User data validation and sanitization
  • Security best practices

ApiKeyService

API key management service for B2B authentication and authorization.

Key Responsibilities:

  • Generate secure API keys with proper formatting
  • Validate API keys and extract user context
  • Manage API key lifecycle (create, update, revoke)
  • Implement tiered rate limiting and permissions
  • Track API key usage and analytics

Code Highlights:

  • Secure key generation and hashing
  • Prefix-based key lookup optimization
  • Tiered permission and rate limit enforcement
  • Usage tracking and analytics

Service Patterns

Dependency Injection Pattern

All services use constructor-based dependency injection:

typescript
export class SearchService {
  constructor(
    private databaseService: DatabaseService,
    private aiService: AIService,
    private cacheService: CacheService
  ) {}
}

Error Handling Pattern

Services use structured error handling with custom error types:

typescript
try {
  const results = await this.databaseService.executeSearch(query);
  return results;
} catch (error) {
  logger.error('Search failed:', error);
  throw new AppError('SEARCH_FAILED', 'Search operation failed', 500);
}

Async/Await Orchestration

Complex operations use Promise.allSettled for graceful failure handling:

typescript
const searchPromises = databases.map(async dbId => {
  return this.executeSearchOnDatabase(dbId, query, options);
});

const results = await Promise.allSettled(searchPromises);
const successful = results.filter(r => r.status === 'fulfilled');
const failed = results.filter(r => r.status === 'rejected');

Caching Strategy

Services implement intelligent caching with TTL and invalidation:

typescript
// Check cache first
const cacheKey = this.generateCacheKey(request);
const cached = await this.cacheService.get(cacheKey);
if (cached) return cached;

// Execute operation and cache result
const result = await this.performOperation(request);
await this.cacheService.set(cacheKey, result, 300); // 5 min TTL
return result;

Testing Services

Unit Testing Pattern

Each service has comprehensive unit tests with mocked dependencies:

typescript
describe('SearchService', () => {
  let searchService: SearchService;
  let mockDatabaseService: jest.Mocked<DatabaseService>;
  let mockAIService: jest.Mocked<AIService>;
  let mockCacheService: jest.Mocked<CacheService>;

  beforeEach(() => {
    mockDatabaseService = createMockDatabaseService();
    mockAIService = createMockAIService();
    mockCacheService = createMockCacheService();

    searchService = new SearchService(
      mockDatabaseService,
      mockAIService,
      mockCacheService
    );
  });

  it('should perform search successfully', async () => {
    // Test implementation
  });
});

Integration Testing

Services are tested with real dependencies in integration tests:

typescript
describe('SearchService Integration', () => {
  let searchService: SearchService;
  let databaseService: DatabaseService;

  beforeAll(async () => {
    // Setup real database connection for integration testing
    databaseService = new DatabaseService();
    // ... other real services
  });
});

Service Metrics

Performance Benchmarks

ServiceAvg Response TimeMemory UsageCPU Usage
SearchService250ms45MB15%
DatabaseService100ms25MB8%
AIService800ms30MB12%
CacheService5ms20MB3%
UserService50ms15MB5%

Error Rates

ServiceSuccess RateCommon ErrorsRetry Strategy
SearchService99.2%Database timeout, Cache missExponential backoff
DatabaseService99.8%Connection pool exhaustionConnection retry
AIService97.5%API rate limits, TimeoutRate limiting + retry
CacheService99.9%Redis connection issuesFailover to memory cache
UserService99.5%Database constraint violationsInput validation

Inter-Service Communication

Services communicate through well-defined interfaces:

typescript
// SearchService orchestrates other services
export class SearchService {
  async search(request: SearchRequest): Promise<SearchResponse> {
    // 1. Check cache
    const cached = await this.cacheService.get(cacheKey);
    if (cached) return cached;

    // 2. Process with AI (if enabled)
    let processedQuery = request.query;
    if (request.searchMode === 'semantic') {
      const aiResult = await this.aiService.processQuery(request.query);
      processedQuery = aiResult.optimizedQuery;
    }

    // 3. Execute database searches
    const results = await this.databaseService.executeSearch(
      processedQuery,
      request.databases
    );

    // 4. Enhance results with AI
    const categorized = await this.aiService.categorizeResults(results);

    // 5. Cache and return
    const response = { results: categorized /* ... */ };
    await this.cacheService.set(cacheKey, response, ttl);
    return response;
  }
}

Adding New Services

When adding a new service:

  1. Create interface defining the service contract
  2. Implement service class with proper dependency injection
  3. Add comprehensive tests (unit and integration)
  4. Document the service following this template
  5. Update service registration in the IoC container

Service Template

typescript
export interface INewService {
  methodName(params: ParamType): Promise<ReturnType>;
}

export class NewService implements INewService {
  constructor(
    private dependency1: IDependency1,
    private dependency2: IDependency2
  ) {}

  async methodName(params: ParamType): Promise<ReturnType> {
    try {
      // Implementation
      return result;
    } catch (error) {
      logger.error('Operation failed:', error);
      throw new AppError('OPERATION_FAILED', error.message);
    }
  }
}

Next Steps: