|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/webishdev/node_dashboard/internal/metrics" |
| 8 | +) |
| 9 | + |
| 10 | +// ChartConfig holds configuration for SVG chart generation |
| 11 | +type ChartConfig struct { |
| 12 | + Width int |
| 13 | + Height int |
| 14 | + Color string |
| 15 | +} |
| 16 | + |
| 17 | +// GenerateSVGLineChart creates an inline SVG line chart from data points |
| 18 | +func GenerateSVGLineChart(dataPoints []metrics.DataPoint, config ChartConfig) string { |
| 19 | + if len(dataPoints) == 0 { |
| 20 | + return generateEmptyChart(config) |
| 21 | + } |
| 22 | + |
| 23 | + // Find min and max values for scaling |
| 24 | + minVal, maxVal := findMinMax(dataPoints) |
| 25 | + |
| 26 | + // Add some padding to the range |
| 27 | + valueRange := maxVal - minVal |
| 28 | + if valueRange == 0 { |
| 29 | + valueRange = 1 // Avoid division by zero |
| 30 | + } |
| 31 | + |
| 32 | + // Generate SVG path |
| 33 | + pathData := generatePathData(dataPoints, config, minVal, maxVal) |
| 34 | + |
| 35 | + // Create the complete SVG |
| 36 | + svg := fmt.Sprintf(`<svg width="%d" height="%d" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg"> |
| 37 | + <defs> |
| 38 | + <linearGradient id="gradient-%s" x1="0%%" y1="0%%" x2="0%%" y2="100%%"> |
| 39 | + <stop offset="0%%" style="stop-color:%s;stop-opacity:0.3" /> |
| 40 | + <stop offset="100%%" style="stop-color:%s;stop-opacity:0.1" /> |
| 41 | + </linearGradient> |
| 42 | + </defs> |
| 43 | + <rect width="100%%" height="100%%" fill="none"/> |
| 44 | + <path d="%s" stroke="%s" stroke-width="2" fill="none"/> |
| 45 | + <path d="%s" fill="url(#gradient-%s)" opacity="0.3"/> |
| 46 | + </svg>`, |
| 47 | + config.Width, config.Height, config.Width, config.Height, |
| 48 | + strings.ReplaceAll(config.Color, "#", ""), config.Color, config.Color, |
| 49 | + pathData, config.Color, |
| 50 | + generateAreaPath(dataPoints, config, minVal, maxVal), strings.ReplaceAll(config.Color, "#", "")) |
| 51 | + |
| 52 | + return svg |
| 53 | +} |
| 54 | + |
| 55 | +// generateEmptyChart creates an empty chart when no data is available |
| 56 | +func generateEmptyChart(config ChartConfig) string { |
| 57 | + return fmt.Sprintf(`<svg width="%d" height="%d" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg"> |
| 58 | + <rect width="100%%" height="100%%" fill="none"/> |
| 59 | + <text x="50%%" y="50%%" text-anchor="middle" fill="#9CA3AF" font-size="12">No data</text> |
| 60 | + </svg>`, config.Width, config.Height, config.Width, config.Height) |
| 61 | +} |
| 62 | + |
| 63 | +// findMinMax finds the minimum and maximum values in the data points |
| 64 | +func findMinMax(dataPoints []metrics.DataPoint) (float64, float64) { |
| 65 | + if len(dataPoints) == 0 { |
| 66 | + return 0, 100 |
| 67 | + } |
| 68 | + |
| 69 | + min := dataPoints[0].Value |
| 70 | + max := dataPoints[0].Value |
| 71 | + |
| 72 | + for _, point := range dataPoints { |
| 73 | + if point.Value < min { |
| 74 | + min = point.Value |
| 75 | + } |
| 76 | + if point.Value > max { |
| 77 | + max = point.Value |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Ensure we have a reasonable range for percentage values |
| 82 | + if min > 0 { |
| 83 | + min = 0 |
| 84 | + } |
| 85 | + if max < 100 { |
| 86 | + max = 100 |
| 87 | + } |
| 88 | + |
| 89 | + return min, max |
| 90 | +} |
| 91 | + |
| 92 | +// generatePathData creates the SVG path data for the line chart |
| 93 | +func generatePathData(dataPoints []metrics.DataPoint, config ChartConfig, minVal, maxVal float64) string { |
| 94 | + if len(dataPoints) == 0 { |
| 95 | + return "" |
| 96 | + } |
| 97 | + |
| 98 | + var pathParts []string |
| 99 | + valueRange := maxVal - minVal |
| 100 | + if valueRange == 0 { |
| 101 | + valueRange = 1 |
| 102 | + } |
| 103 | + |
| 104 | + for i, point := range dataPoints { |
| 105 | + x := float64(i) * float64(config.Width-1) / float64(len(dataPoints)-1) |
| 106 | + y := float64(config.Height-1) - ((point.Value-minVal)/valueRange)*float64(config.Height-1) |
| 107 | + |
| 108 | + if i == 0 { |
| 109 | + pathParts = append(pathParts, fmt.Sprintf("M %.2f %.2f", x, y)) |
| 110 | + } else { |
| 111 | + pathParts = append(pathParts, fmt.Sprintf("L %.2f %.2f", x, y)) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return strings.Join(pathParts, " ") |
| 116 | +} |
| 117 | + |
| 118 | +// generateAreaPath creates the SVG path data for the filled area under the line |
| 119 | +func generateAreaPath(dataPoints []metrics.DataPoint, config ChartConfig, minVal, maxVal float64) string { |
| 120 | + if len(dataPoints) == 0 { |
| 121 | + return "" |
| 122 | + } |
| 123 | + |
| 124 | + var pathParts []string |
| 125 | + valueRange := maxVal - minVal |
| 126 | + if valueRange == 0 { |
| 127 | + valueRange = 1 |
| 128 | + } |
| 129 | + |
| 130 | + // Start from bottom left |
| 131 | + pathParts = append(pathParts, fmt.Sprintf("M 0 %d", config.Height)) |
| 132 | + |
| 133 | + // Draw line to first point |
| 134 | + firstY := float64(config.Height-1) - ((dataPoints[0].Value-minVal)/valueRange)*float64(config.Height-1) |
| 135 | + pathParts = append(pathParts, fmt.Sprintf("L 0 %.2f", firstY)) |
| 136 | + |
| 137 | + // Draw the line |
| 138 | + for i, point := range dataPoints { |
| 139 | + x := float64(i) * float64(config.Width-1) / float64(len(dataPoints)-1) |
| 140 | + y := float64(config.Height-1) - ((point.Value-minVal)/valueRange)*float64(config.Height-1) |
| 141 | + pathParts = append(pathParts, fmt.Sprintf("L %.2f %.2f", x, y)) |
| 142 | + } |
| 143 | + |
| 144 | + // Close the path at bottom right |
| 145 | + lastX := float64(len(dataPoints)-1) * float64(config.Width-1) / float64(len(dataPoints)-1) |
| 146 | + pathParts = append(pathParts, fmt.Sprintf("L %.2f %d", lastX, config.Height)) |
| 147 | + pathParts = append(pathParts, "Z") |
| 148 | + |
| 149 | + return strings.Join(pathParts, " ") |
| 150 | +} |
| 151 | + |
| 152 | +// GetChartConfigs returns predefined chart configurations for different metrics |
| 153 | +func GetChartConfigs() map[string]ChartConfig { |
| 154 | + return map[string]ChartConfig{ |
| 155 | + "cpu": { |
| 156 | + Width: 200, |
| 157 | + Height: 60, |
| 158 | + Color: "#3B82F6", // blue-500 |
| 159 | + }, |
| 160 | + "memory": { |
| 161 | + Width: 200, |
| 162 | + Height: 60, |
| 163 | + Color: "#10B981", // green-500 |
| 164 | + }, |
| 165 | + "disk": { |
| 166 | + Width: 200, |
| 167 | + Height: 60, |
| 168 | + Color: "#8B5CF6", // purple-500 |
| 169 | + }, |
| 170 | + } |
| 171 | +} |
0 commit comments