forked from hagen1778/fasthttploader
/
utils.go
91 lines (78 loc) · 1.88 KB
/
utils.go
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
package report
import (
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
)
func uint64SliceToString(sl []uint64) string {
str := []string{}
for _, v := range sl {
str = append(str, strconv.FormatInt(int64(v), 10))
}
return strings.Join(str[:], ",")
}
func float64SliceToString(sl []float64) string {
str := []string{}
for _, v := range sl {
str = append(str, strconv.FormatFloat(v, 'f', 8, 64))
}
return strings.Join(str[:], ",")
}
// rate calculate difference between current and previous value
func rate(sl []uint64, step float64) []float64 {
result := make([]float64, len(sl))
if len(sl) < 2 {
return result
}
for i := 1; i < len(sl); i++ {
curValue := float64(sl[i-1])
lastValue := float64(sl[i])
// avoid of unnatural gaps when counter metrics flushed
if lastValue < curValue {
lastValue = curValue
}
result[i] = (lastValue - curValue) / step
}
return result
}
func mustPwd() string {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
return pwd
}
// OpenBrowser opens given url by default browser
func OpenBrowser(fileName string) error {
var err error
url := fmt.Sprintf("file:///%s/%s", mustPwd(), fileName)
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("start", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return err
}
// PrintOpenBrowser prints command to open given url by default browser from console
func PrintOpenBrowser(fileName string) (command string, err error) {
url := fmt.Sprintf("file:///%s/%s", mustPwd(), fileName)
switch runtime.GOOS {
case "linux":
command = fmt.Sprintf("xdg-open %s", url)
case "windows":
command = fmt.Sprintf("start %s", url)
case "darwin":
command = fmt.Sprintf("open %s", url)
default:
err = fmt.Errorf("unsupported platform")
}
return
}