Refactor: Extract shared domain aggregation logic in logs_report.go#3588
Refactor: Extract shared domain aggregation logic in logs_report.go#3588
Conversation
- Created aggregateDomainStats() helper to centralize domain aggregation - Created convertDomainsToSortedSlices() helper for domain map conversion - Refactored buildAccessLogSummary() to use shared helpers - Refactored buildFirewallLogSummary() to use shared helpers - Added comprehensive tests for all new helper functions - All existing tests pass, functionality unchanged Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR successfully refactors duplicate domain aggregation logic from buildAccessLogSummary and buildFirewallLogSummary by extracting shared helpers. The refactoring reduces code duplication (~45 lines) while preserving the firewall-specific RequestsByDomain tracking through a callback pattern.
Key Changes:
- Introduced
domainAggregationstruct andaggregateDomainStats()function to encapsulate shared aggregation logic - Added
convertDomainsToSortedSlices()helper for consistent domain map-to-slice conversion - Refactored both summary builders to use the new helpers (38% and 33% size reduction)
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pkg/cli/logs_report.go | Extracted shared domain aggregation logic into reusable helpers; refactored buildAccessLogSummary and buildFirewallLogSummary to use callback pattern |
| pkg/cli/logs_report_test.go | Added comprehensive tests for new helper functions and verified integration with refactored summary builders |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| // aggregateDomainStats aggregates domain statistics across runs | ||
| // This is a shared helper for both access log and firewall log summaries | ||
| func aggregateDomainStats(processedRuns []ProcessedRun, getAnalysis func(*ProcessedRun) (allowedDomains, deniedDomains []string, totalRequests, allowedCount, deniedCount int, exists bool)) *domainAggregation { |
There was a problem hiding this comment.
[nitpick] The callback function signature could benefit from named return values to improve readability, especially given the multiple int return values. Consider:
func aggregateDomainStats(processedRuns []ProcessedRun,
getAnalysis func(*ProcessedRun) (
allowedDomains, deniedDomains []string,
totalRequests, allowedCount, deniedCount int,
exists bool,
)) *domainAggregationThis makes it clearer what each return value represents when reading callback implementations.
| func aggregateDomainStats(processedRuns []ProcessedRun, getAnalysis func(*ProcessedRun) (allowedDomains, deniedDomains []string, totalRequests, allowedCount, deniedCount int, exists bool)) *domainAggregation { | |
| func aggregateDomainStats( | |
| processedRuns []ProcessedRun, | |
| getAnalysis func(*ProcessedRun) ( | |
| allowedDomains []string, | |
| deniedDomains []string, | |
| totalRequests int, | |
| allowedCount int, | |
| deniedCount int, | |
| exists bool, | |
| ), | |
| ) *domainAggregation { |
buildAccessLogSummaryandbuildFirewallLogSummarycontained ~45 lines of duplicate domain aggregation logic differing only in firewall-specific RequestsByDomain tracking.Changes
aggregateDomainStats()- Generic aggregation using callback pattern to extract analysis data from ProcessedRunconvertDomainsToSortedSlices()- Shared map-to-sorted-slice conversiondomainAggregationstruct - Encapsulates aggregation stateExample
Before (50 lines):
After (31 lines):
Firewall-specific logic (RequestsByDomain aggregation) remains explicit in its callback, preserving intent while eliminating structural duplication.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.