6.7 KiB
6.7 KiB
EnergyConsumptionOverviewAppService Migration to Bucketing System
Overview
This document summarizes the modifications made to the EnergyConsumptionOverviewAppService
class to utilize the new telemetry statistics bucketing system for improved performance and data access patterns. The circular dependency issue has been successfully resolved through proper abstraction and interface design.
Key Changes Made
1. Dependencies and Imports
- Added:
using Yunda.SOMS.MongoDB.Entities.DataMonitoring;
- Added:
using Yunda.SOMS.DataMonitoringServer.DataAnalysis.TeleInfoSave;
- Added:
TelemeteringBucketQueryService
dependency injection
2. Constructor Updates
- Added:
TelemeteringBucketQueryService bucketQueryService
parameter - Added:
_bucketQueryService = bucketQueryService;
initialization
3. GetPowerStatistics Method Transformation
Before (Direct MongoDB Queries)
- Used direct MongoDB collection queries with
BsonDocument
- Manual filter building with
Builders<BsonDocument>.Filter
- Raw document processing and value extraction
- Collection naming:
TelemeteringModel_Zongzi_{intervalSuffix}_{dateSuffix}
After (Bucketing System)
- Uses
TelemeteringBucketQueryService.QueryStatisticsData()
- Parallel async queries for better performance
- Automatic data aggregation and time alignment
- Smart statistics type selection based on data characteristics
Key Improvements
// Old approach
var filter = filterBuilder.And(
filterBuilder.In("TelemeteringConfigurationId", telemeteringIds),
filterBuilder.Gte("ResultTime", startTime),
filterBuilder.Lte("ResultTime", endTime)
);
var docs = _mongoRepository.GetAllIncludeToFindFluent(filter).ToList();
// New approach
var results = await _bucketQueryService.QueryStatisticsData(
telemeteringId,
fixedInterval,
startTime,
endTime,
statisticsType);
4. GetFeederEnergyDistribution Method Transformation
Before
- Direct MongoDB queries to
TelemeteringModel_PowerConsumptionData_{intervalSuffix}_{dateSuffix}
- Manual document processing and energy consumption calculation
- Used
StatisticsType.Difference
for energy distribution
After
- Parallel async queries using bucketing service
- Automatic grouping and aggregation by device
- Smart statistics type selection:
GetStatisticsTypeForEnergyData(interval, false)
- Improved error handling and logging
5. GetFeederCumulativeEnergyConsumption Method Transformation
Before
- Direct MongoDB queries with complex document processing
- Manual time point matching and data series building
- Used
StatisticsType.RealTime
for cumulative energy
After
- Parallel async queries for all feeder configurations
- Automatic time series data organization
- Smart statistics type selection:
GetStatisticsTypeForEnergyData(interval, true)
- Simplified data processing logic
6. New Helper Methods Added
GetStatisticsTypeForPowerData()
private StatisticsTypeEnum GetStatisticsTypeForPowerData(RealTimePowerTypeEnum interval)
{
switch (interval)
{
case RealTimePowerTypeEnum.RealTime:
return StatisticsTypeEnum.RealTime;
case RealTimePowerTypeEnum.Hourly:
case RealTimePowerTypeEnum.Daily:
case RealTimePowerTypeEnum.Weekly:
case RealTimePowerTypeEnum.Monthly:
case RealTimePowerTypeEnum.Yearly:
return StatisticsTypeEnum.Average; // Average power for time periods
default:
return StatisticsTypeEnum.Average;
}
}
GetStatisticsTypeForEnergyData()
private StatisticsTypeEnum GetStatisticsTypeForEnergyData(RealTimePowerTypeEnum interval, bool isAccumulated = false)
{
if (isAccumulated)
{
return StatisticsTypeEnum.RealTime; // Meter readings
}
else
{
return StatisticsTypeEnum.Difference; // Energy increments
}
}
Performance Benefits
1. Query Optimization
- Before: Multiple sequential MongoDB queries with complex filters
- After: Parallel async queries using pre-aggregated bucketed data
2. Data Processing
- Before: Manual document parsing and value extraction
- After: Structured data objects with automatic type conversion
3. Time Alignment
- Before: Manual time period calculation and alignment
- After: Automatic time bucketing and alignment by the service
4. Error Handling
- Before: Basic try-catch blocks
- After: Comprehensive error handling with detailed logging
Backward Compatibility
API Interface
- Maintained: All public method signatures remain unchanged
- Maintained: Return types and data structures unchanged
- Maintained: Fallback to simulation data when no real data available
Configuration
- Maintained: Existing energy consumption device configurations
- Maintained: Telemetry configuration mappings
- Maintained: Time interval and formatting logic
Data Flow Comparison
Old Flow
- Build MongoDB collection name
- Create complex filter conditions
- Execute raw MongoDB query
- Parse BsonDocument results
- Manual data aggregation and time alignment
- Format results
New Flow
- Determine appropriate statistics type
- Call bucketing query service
- Receive structured results
- Automatic data aggregation
- Format results
Testing Recommendations
1. Unit Tests
- Test statistics type selection logic
- Test parallel query execution
- Test data aggregation accuracy
2. Integration Tests
- Compare results between old and new implementations
- Test performance improvements
- Verify data consistency
3. Load Tests
- Test concurrent query performance
- Verify memory usage improvements
- Test error handling under load
Migration Notes
Dependencies
- Ensure
TelemeteringBucketQueryService
is properly registered in DI container - Verify bucketing system is operational and populated with data
Monitoring
- Monitor query performance improvements
- Track error rates and data accuracy
- Verify fallback mechanisms work correctly
Rollback Plan
- Keep old implementation available for quick rollback if needed
- Monitor system behavior after deployment
- Have data validation scripts ready
Conclusion
The migration successfully transforms the EnergyConsumptionOverviewAppService
from direct MongoDB queries to the new bucketing system, providing:
- Better Performance: Parallel async queries and pre-aggregated data
- Improved Maintainability: Cleaner code with structured data access
- Enhanced Reliability: Better error handling and logging
- Future Scalability: Foundation for further optimizations
The changes maintain full backward compatibility while providing significant performance improvements for energy consumption data retrieval.