-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.go
More file actions
136 lines (116 loc) · 3.91 KB
/
Copy pathoutput.go
File metadata and controls
136 lines (116 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package layers
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"time"
"ghostshell/app/layers/common"
"github.com/jung-kurt/gofpdf"
"go.uber.org/zap" // Importing the Zap logger
)
// WriteOutput writes the test results to the specified format and file.
func WriteOutput(results []common.TestResult, format string, outputPath string) error {
logger, _ := zap.NewProduction() // Create a new logger instance
defer logger.Sync() // Flushes buffer, if any
switch format {
case "csv":
if err := writeCSV(results, outputPath, logger); err != nil {
logger.Error("Failed to write CSV", zap.String("outputPath", outputPath), zap.Error(err))
return err
}
case "pdf":
if err := writePDF(results, outputPath, logger); err != nil {
logger.Error("Failed to write PDF", zap.String("outputPath", outputPath), zap.Error(err))
return err
}
case "json":
if err := writeJSON(results, outputPath, logger); err != nil {
logger.Error("Failed to write JSON", zap.String("outputPath", outputPath), zap.Error(err))
return err
}
default:
return fmt.Errorf("unsupported output format: %s", format)
}
logger.Info("Report written successfully", zap.String("format", format), zap.String("outputPath", outputPath))
return nil
}
// writeCSV generates a CSV report of the test results.
func writeCSV(results []common.TestResult, outputPath string, logger *zap.Logger) error {
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("error creating CSV file: %w", err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
// Write CSV header
if err := writer.Write([]string{"Layer", "Status", "Message"}); err != nil {
return fmt.Errorf("error writing CSV header: %w", err)
}
// Write test results
for _, result := range results {
// Add test result to the appropriate row
row := []string{
fmt.Sprintf("%d", result.Layer),
result.Name,
string(result.Status),
result.Message,
result.StartTime.Format(time.RFC3339),
result.EndTime.Format(time.RFC3339),
fmt.Sprintf("%.2f", result.Metrics.Duration.Milliseconds()),
fmt.Sprintf("%.2f", result.Metrics.TransferRate),
fmt.Sprintf("%.2f", result.Metrics.Latency.Milliseconds()),
fmt.Sprintf("%.2f", result.Metrics.PacketLoss),
fmt.Sprintf("%.2f", result.Metrics.ResponseTime.Milliseconds()),
}
if err := writer.Write(row); err != nil {
return fmt.Errorf("error writing CSV row: %w", err)
}
}
logger.Info("CSV report written", zap.String("outputPath", outputPath))
return nil
}
// writePDF generates a PDF report of the test results.
func writePDF(results []common.TestResult, outputPath string, logger *zap.Logger) error {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "B", 16)
// Add title
pdf.Cell(40, 10, "OSI Layer Test Report")
pdf.Ln(12)
// Table header
pdf.SetFont("Arial", "B", 12)
pdf.Cell(30, 10, "Layer")
pdf.Cell(40, 10, "Status")
pdf.Cell(120, 10, "Message")
pdf.Ln(10)
// Table rows
pdf.SetFont("Arial", "", 12)
for _, result := range results {
pdf.Cell(30, 10, fmt.Sprintf("%d", result.Layer))
pdf.Cell(40, 10, string(result.Status))
pdf.MultiCell(120, 10, result.Message, "", "", false)
}
// Save PDF file
if err := pdf.OutputFileAndClose(outputPath); err != nil {
return fmt.Errorf("error writing PDF file: %w", err)
}
logger.Info("PDF report written", zap.String("outputPath", outputPath))
return nil
}
// writeJSON generates a JSON report of the test results.
func writeJSON(results []common.TestResult, outputPath string, logger *zap.Logger) error {
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("error creating JSON file: %w", err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(results); err != nil {
return fmt.Errorf("error writing JSON file: %w", err)
}
logger.Info("JSON report written", zap.String("outputPath", outputPath))
return nil
}