minor refactoring

This commit is contained in:
sqshq 2019-07-28 13:15:34 -04:00
parent 5d730b5fec
commit 24eb7bf571
4 changed files with 22 additions and 15 deletions

View File

@ -391,9 +391,9 @@ textboxes:
<details><summary>Java application uptime example</summary> <details><summary>Java application uptime example</summary>
- Prerequisite: download [jmxterm jar file](https://docs.cyclopsgroup.org/jmxterm)
```yml ```yml
# prerequisite: download [jmxterm jar file](https://docs.cyclopsgroup.org/jmxterm)
textboxes: textboxes:
- title: Java application uptime - title: Java application uptime
multistep-init: multistep-init:

View File

@ -16,10 +16,9 @@ const (
crashPath = "/telemetry/crash" crashPath = "/telemetry/crash"
registrationPath = "/license/registration" registrationPath = "/license/registration"
verificationPath = "/license/verification" verificationPath = "/license/verification"
jsonContentType = "application/json"
) )
// BackendClient is used to verify license and to send telemetry reports // BackendClient is used to verify license and to send telemetry
// for analyses (anonymous usage data statistics and crash reports) // for analyses (anonymous usage data statistics and crash reports)
type BackendClient struct { type BackendClient struct {
client http.Client client http.Client
@ -39,7 +38,8 @@ func (c *BackendClient) ReportInstallation(statistics *metadata.Statistics) {
c.ReportCrash(err.Error(), statistics) c.ReportCrash(err.Error(), statistics)
} }
_, err = http.Post(backendUrl+installationPath, jsonContentType, buf) _, err = sendRequest(backendUrl+installationPath, buf)
if err != nil { if err != nil {
c.ReportCrash(err.Error(), statistics) c.ReportCrash(err.Error(), statistics)
} }
@ -53,7 +53,7 @@ func (c *BackendClient) ReportStatistics(statistics *metadata.Statistics) {
c.ReportCrash(err.Error(), statistics) c.ReportCrash(err.Error(), statistics)
} }
_, err = http.Post(backendUrl+statisticsPath, jsonContentType, buf) _, err = sendRequest(backendUrl+statisticsPath, buf)
if err != nil { if err != nil {
c.ReportCrash(err.Error(), statistics) c.ReportCrash(err.Error(), statistics)
} }
@ -75,7 +75,7 @@ func (c *BackendClient) ReportCrash(error string, statistics *metadata.Statistic
return return
} }
_, _ = http.Post(backendUrl+crashPath, jsonContentType, buf) _, _ = sendRequest(backendUrl+crashPath, buf)
} }
func (c *BackendClient) RegisterLicenseKey(licenseKey string, statistics *metadata.Statistics) (*metadata.License, error) { func (c *BackendClient) RegisterLicenseKey(licenseKey string, statistics *metadata.Statistics) (*metadata.License, error) {
@ -94,8 +94,7 @@ func (c *BackendClient) RegisterLicenseKey(licenseKey string, statistics *metada
c.ReportCrash(err.Error(), statistics) c.ReportCrash(err.Error(), statistics)
} }
response, err := http.Post( response, err := sendRequest(backendUrl+registrationPath, buf)
backendUrl+registrationPath, jsonContentType, buf)
if err != nil { if err != nil {
return nil, err return nil, err
@ -126,8 +125,7 @@ func (c *BackendClient) VerifyLicenseKey(licenseKey string) (*metadata.License,
c.ReportCrash(err.Error(), nil) c.ReportCrash(err.Error(), nil)
} }
response, err := http.Post( response, err := sendRequest(backendUrl+verificationPath, buf)
backendUrl+verificationPath, jsonContentType, buf)
if err != nil { if err != nil {
return nil, err return nil, err
@ -143,3 +141,13 @@ func (c *BackendClient) VerifyLicenseKey(licenseKey string) (*metadata.License,
return &license, nil return &license, nil
} }
func sendRequest(url string, body *bytes.Buffer) (resp *http.Response, err error) {
c := http.DefaultClient
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return c.Do(req)
}

View File

@ -61,10 +61,10 @@ func (g *Gauge) ConsumeSample(sample *data.Sample) {
if err != nil { if err != nil {
g.HandleConsumeFailure("Failed to parse a number", err, sample) g.HandleConsumeFailure("Failed to parse a number", err, sample)
return return
} else {
g.HandleConsumeSuccess()
} }
g.HandleConsumeSuccess()
switch sample.Label { switch sample.Label {
case MinValueLabel: case MinValueLabel:
g.minValue = float g.minValue = float

View File

@ -22,9 +22,8 @@ var AsciiLogo = []string{
func FormatValue(value float64, scale int) string { func FormatValue(value float64, scale int) string {
if math.Abs(value) == math.MaxFloat64 { if math.Abs(value) == math.MaxFloat64 {
return "Inf" return "Inf"
} else {
return formatTrailingDigits(addRadixChars(value), scale)
} }
return formatTrailingDigits(addRadixChars(value), scale)
} }
func FormatDelta(value float64, scale int) string { func FormatDelta(value float64, scale int) string {