forked from gepser/markdown-progress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.go
More file actions
267 lines (225 loc) · 6.23 KB
/
progress.go
File metadata and controls
267 lines (225 loc) · 6.23 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package progress
import (
"bytes"
"log"
"math"
"net/http"
"os"
"path"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"unicode/utf8"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
// Data ... is the collection of inputs we need to fill our template
type Data struct {
BackgroundColor string
Label string
Progress int
PickedColor string
}
const (
gcloudFuncSourceDir = "serverless_function_source_code"
minPercentage = 0.0
maxPercentage = 100.0
totalBarWidth = 90.0
cacheControlValue = "public, max-age=300"
maxLabelRunes = 64
)
var (
grey = "#555"
red = "#d9534f"
yellow = "#f0ad4e"
green = "#5cb85c"
hexColorPattern = regexp.MustCompile(`^[0-9a-fA-F]{6}$`)
progressTemplate *template.Template
progressTemplateErr error
)
func fixDir() {
fileInfo, err := os.Stat(gcloudFuncSourceDir)
if err == nil && fileInfo.IsDir() {
_ = os.Chdir(gcloudFuncSourceDir)
}
}
func clampPercentage(percentage float64) float64 {
if percentage < minPercentage {
return minPercentage
}
if percentage > maxPercentage {
return maxPercentage
}
return percentage
}
func percentageToWidth(percentage float64) int {
return int((totalBarWidth * percentage) / maxPercentage)
}
func parseOptionalColor(raw string) (string, bool) {
if raw == "" {
return "", true
}
if !hexColorPattern.MatchString(raw) {
return "", false
}
return "#" + strings.ToLower(raw), true
}
func pickColor(percentage float64, successColor string, warningColor string, dangerColor string) string {
pickedColor := green
if successColor != "" {
pickedColor = successColor
}
if percentage >= 0 && percentage < 33 {
if dangerColor != "" {
pickedColor = dangerColor
} else {
pickedColor = red
}
} else if percentage >= 33 && percentage < 70 {
if warningColor != "" {
pickedColor = warningColor
} else {
pickedColor = yellow
}
}
return pickedColor
}
func formatNumber(value float64) string {
formatted := strconv.FormatFloat(value, 'f', 2, 64)
formatted = strings.TrimRight(formatted, "0")
formatted = strings.TrimRight(formatted, ".")
if formatted == "-0" || formatted == "" {
return "0"
}
return formatted
}
func formatPercentLabel(value float64) string {
return formatNumber(value) + "%"
}
func init() {
fixDir()
progressTemplate, progressTemplateErr = template.ParseFiles("progress.html")
functions.HTTP("Progress", Progress)
}
// Progress ... Entrypoint of our Cloud Function
func Progress(w http.ResponseWriter, r *http.Request) {
start := time.Now()
statusCode := http.StatusOK
defer func() {
log.Printf(
"method=%s path=%s status=%d duration_ms=%d",
r.Method,
r.URL.Path,
statusCode,
time.Since(start).Milliseconds(),
)
}()
if r.Method != http.MethodGet && r.Method != http.MethodHead {
statusCode = http.StatusMethodNotAllowed
w.Header().Set("Allow", "GET, HEAD")
http.Error(w, "method not allowed", statusCode)
return
}
if progressTemplateErr != nil {
statusCode = http.StatusInternalServerError
log.Printf("unable to parse progress template: %v", progressTemplateErr)
http.Error(w, "template unavailable", statusCode)
return
}
id := path.Base(strings.TrimSuffix(r.URL.Path, "/"))
value, err := strconv.ParseFloat(id, 64)
if err != nil || math.IsNaN(value) || math.IsInf(value, 0) {
statusCode = http.StatusBadRequest
http.Error(w, "percentage must be a number", statusCode)
return
}
// Read and validate colors if provided.
successColor, ok := parseOptionalColor(r.URL.Query().Get("successColor"))
if !ok {
statusCode = http.StatusBadRequest
http.Error(w, "successColor must be a 6-character hex value", statusCode)
return
}
warningColor, ok := parseOptionalColor(r.URL.Query().Get("warningColor"))
if !ok {
statusCode = http.StatusBadRequest
http.Error(w, "warningColor must be a 6-character hex value", statusCode)
return
}
dangerColor, ok := parseOptionalColor(r.URL.Query().Get("dangerColor"))
if !ok {
statusCode = http.StatusBadRequest
http.Error(w, "dangerColor must be a 6-character hex value", statusCode)
return
}
barColor, ok := parseOptionalColor(r.URL.Query().Get("barColor"))
if !ok {
statusCode = http.StatusBadRequest
http.Error(w, "barColor must be a 6-character hex value", statusCode)
return
}
customLabel := r.URL.Query().Get("label")
if utf8.RuneCountInString(customLabel) > maxLabelRunes {
statusCode = http.StatusBadRequest
http.Error(w, "label is too long (max 64 characters)", statusCode)
return
}
minRaw := r.URL.Query().Get("min")
maxRaw := r.URL.Query().Get("max")
hasMin := minRaw != ""
hasMax := maxRaw != ""
if hasMin != hasMax {
statusCode = http.StatusBadRequest
http.Error(w, "min and max must be provided together", statusCode)
return
}
percentage := clampPercentage(value)
if hasMin {
minValue, minErr := strconv.ParseFloat(minRaw, 64)
maxValue, maxErr := strconv.ParseFloat(maxRaw, 64)
if minErr != nil || maxErr != nil || math.IsNaN(minValue) || math.IsNaN(maxValue) || math.IsInf(minValue, 0) || math.IsInf(maxValue, 0) {
statusCode = http.StatusBadRequest
http.Error(w, "min and max must be numeric values", statusCode)
return
}
if maxValue <= minValue {
statusCode = http.StatusBadRequest
http.Error(w, "max must be greater than min", statusCode)
return
}
normalized := ((value - minValue) / (maxValue - minValue)) * maxPercentage
percentage = clampPercentage(normalized)
}
pickedColor := pickColor(percentage, successColor, warningColor, dangerColor)
if barColor != "" {
pickedColor = barColor
}
label := formatPercentLabel(percentage)
if hasMin {
label = formatNumber(value)
}
if customLabel != "" {
label = customLabel
}
data := Data{
BackgroundColor: grey,
Label: label,
Progress: percentageToWidth(percentage),
PickedColor: pickedColor,
}
buf := new(bytes.Buffer)
err = progressTemplate.Execute(buf, data)
if err != nil {
statusCode = http.StatusInternalServerError
log.Printf("unable to render progress template: %v", err)
http.Error(w, "failed to render SVG", statusCode)
return
}
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", cacheControlValue)
if r.Method == http.MethodHead {
return
}
_, _ = w.Write(buf.Bytes())
}