Skip to content

Commit 3930481

Browse files
Merge pull request #1 from FastNetMon/copilot/update-compression-logging
Add compression ratio logging for rotated pcap uploads
2 parents 86d5249 + 618d27a commit 3930481

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
33
#
44
# Binaries for programs and plugins
5+
/cloud_pcap
56
*.exe
67
*.exe~
78
*.dll

callback.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func compressAndUpload(cfg *Config, pcapPath string) error {
2121

2222
log.Printf("Processing %s", pcapPath)
2323

24-
if _, err := os.Stat(pcapPath); err != nil {
24+
originalInfo, err := os.Stat(pcapPath)
25+
if err != nil {
2526
return fmt.Errorf("pcap file not found: %w", err)
2627
}
2728

@@ -32,6 +33,17 @@ func compressAndUpload(cfg *Config, pcapPath string) error {
3233
return fmt.Errorf("bzip2 failed: %s: %w", string(out), err)
3334
}
3435
compressedPath := pcapPath + ".bz2"
36+
compressedInfo, err := os.Stat(compressedPath)
37+
if err != nil {
38+
return fmt.Errorf("stat compressed file: %w", err)
39+
}
40+
compressionStats := formatCompressionStats(
41+
filepath.Base(pcapPath),
42+
filepath.Base(compressedPath),
43+
originalInfo.Size(),
44+
compressedInfo.Size(),
45+
)
46+
log.Printf("Compression complete: %s", compressionStats)
3547

3648
// Upload to S3 with year/month/day folder structure
3749
now := time.Now().UTC()
@@ -54,6 +66,20 @@ func compressAndUpload(cfg *Config, pcapPath string) error {
5466
return nil
5567
}
5668

69+
func formatCompressionStats(originalName, compressedName string, originalSize, compressedSize int64) string {
70+
ratioText := "ratio unavailable"
71+
72+
if originalSize == 0 {
73+
ratioText = "ratio unavailable: empty input file"
74+
} else if compressedSize == 0 {
75+
ratioText = "ratio unavailable: empty compressed file"
76+
} else {
77+
ratioText = fmt.Sprintf("ratio %.2f:1", float64(originalSize)/float64(compressedSize))
78+
}
79+
80+
return fmt.Sprintf("%s -> %s (%d bytes -> %d bytes, %s)", originalName, compressedName, originalSize, compressedSize, ratioText)
81+
}
82+
5783
func uploadToS3(cfg *Config, filePath, key string) error {
5884
ctx := context.Background()
5985

callback_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestFormatCompressionStats(t *testing.T) {
6+
t.Run("includes compression ratio", func(t *testing.T) {
7+
got := formatCompressionStats("capture.pcap", "capture.pcap.bz2", 1024, 256)
8+
want := "capture.pcap -> capture.pcap.bz2 (1024 bytes -> 256 bytes, ratio 4.00:1)"
9+
if got != want {
10+
t.Fatalf("formatCompressionStats() = %q, want %q", got, want)
11+
}
12+
})
13+
14+
t.Run("handles zero compressed size", func(t *testing.T) {
15+
got := formatCompressionStats("capture.pcap", "capture.pcap.bz2", 1024, 0)
16+
want := "capture.pcap -> capture.pcap.bz2 (1024 bytes -> 0 bytes, ratio unavailable: empty compressed file)"
17+
if got != want {
18+
t.Fatalf("formatCompressionStats() = %q, want %q", got, want)
19+
}
20+
})
21+
22+
t.Run("handles zero original size", func(t *testing.T) {
23+
got := formatCompressionStats("capture.pcap", "capture.pcap.bz2", 0, 128)
24+
want := "capture.pcap -> capture.pcap.bz2 (0 bytes -> 128 bytes, ratio unavailable: empty input file)"
25+
if got != want {
26+
t.Fatalf("formatCompressionStats() = %q, want %q", got, want)
27+
}
28+
})
29+
}

0 commit comments

Comments
 (0)