Skip to content

Commit

Permalink
Merge pull request #23 from DataReply/golint-improvements
Browse files Browse the repository at this point in the history
More changes to improve golint
  • Loading branch information
olgierdg authored Oct 13, 2019
2 parents 1c585c0 + 9c5b7fc commit 4834d5f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 65 deletions.
2 changes: 1 addition & 1 deletion arnutil/arnutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ func TestGetRegionFromARN(t *testing.T) {
if region != "" {
t.Fatal("Region parsed from wrong ARN was not empty")
}
}
}
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"html/template"

"github.com/DataReply/alertmanager-sns-forwarder/arnutil"
"github.com/DataReply/alertmanager-sns-forwarder/template_util"
"github.com/DataReply/alertmanager-sns-forwarder/templateutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -27,6 +27,7 @@ import (
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

// Alerts is a structure for grouping Prometheus Alerts
type Alerts struct {
Alerts []Alert `json:"alerts"`
CommonAnnotations map[string]interface{} `json:"commonAnnotations"`
Expand All @@ -39,6 +40,7 @@ type Alerts struct {
Version int `json:"version"`
}

// Alert is a structure for a single Prometheus Alert
type Alert struct {
Annotations map[string]interface{} `json:"annotations"`
EndsAt string `json:"endsAt"`
Expand All @@ -50,7 +52,7 @@ type Alert struct {
var (
log = logrus.New()

listenAddr = kingpin.Flag("addr", "Address on which to listen").Default(":9087").Envar("SNS_FORWARDER_ADDRESS").String()
listenAddr = kingpin.Flag("addr", "Address on which to listen").Default(":9087").Envar("SNS_FORWARDER_ADDRESS").String()
debug = kingpin.Flag("debug", "Debug mode").Default("false").Envar("SNS_FORWARDER_DEBUG").Bool()
arnPrefix = kingpin.Flag("arn-prefix", "Prefix to use for ARNs").Envar("SNS_FORWARDER_ARN_PREFIX").String()
snsSubject = kingpin.Flag("sns-subject", "SNS subject").Envar("SNS_SUBJECT").String()
Expand Down Expand Up @@ -87,14 +89,14 @@ var (

// Template addictional functions map
funcMap = template.FuncMap{
"str_FormatDate": template_util.Str_FormatDate,
"str_FormatDate": templateutil.StrFormatDate,
"str_UpperCase": strings.ToUpper,
"str_LowerCase": strings.ToLower,
"str_Title": strings.Title,
"str_FormatFloat": template_util.Str_FormatFloat,
"str_Format_Byte": template_util.Str_Format_Byte,
"str_Format_MeasureUnit": template_util.Str_Format_MeasureUnit,
"HasKey": template_util.HasKey,
"str_FormatFloat": templateutil.StrFormatFloat,
"str_Format_Byte": templateutil.StrFormatByte,
"str_Format_MeasureUnit": templateutil.StrFormatMeasureUnit,
"HasKey": templateutil.HasKey,
}
)

Expand Down Expand Up @@ -208,6 +210,7 @@ func loadTemplate(tmplPath *string) *template.Template {
return tmpH
}

// AlertFormatTemplate applies the template to the Alerts
func AlertFormatTemplate(alerts Alerts) string {
var bytesBuff bytes.Buffer
var err error
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func TestSNSAlertEndpoint(t *testing.T) {
req, _ = http.NewRequest("POST", "/alert/test-topic", strings.NewReader("test-payload"))
testHTTPResponse(t, r, req, http.StatusOK)

templatePath_ := "testdata/default.tmpl"
templatePath = &templatePath_
templatePathStr := "testdata/default.tmpl"
templatePath = &templatePathStr
tmpH = loadTemplate(templatePath)
svc = sns.New(mockJsonDataSession)
req, _ = http.NewRequest("POST", "/alert/test-topic", bytes.NewReader(data))
Expand Down
110 changes: 55 additions & 55 deletions template_util/template_util.go → templateutil/templateutil.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package template_util contains template functions from prometheus_bot
package template_util
// Package templateutil contains template functions from prometheus_bot
package templateutil

import (
"fmt"
Expand All @@ -22,7 +22,7 @@ const (
Eb
Zb
Yb
Information_Size_MAX
InformationSizeMAX
)

/**
Expand All @@ -37,10 +37,10 @@ const (
E
Z
Y
Scale_Size_MAX
ScaleSizeMAX
)

func RoundPrec(x float64, prec int) float64 {
func roundPrec(x float64, prec int) float64 {
if math.IsNaN(x) || math.IsInf(x, 0) {
return x
}
Expand All @@ -65,12 +65,8 @@ func RoundPrec(x float64, prec int) float64 {
return rounder / pow * sign
}

/******************************************************************************
*
* Function for formatting template
*
******************************************************************************/
func Str_Format_MeasureUnit(MeasureUnit string, value string, templateSplitToken string) string {
// StrFormatMeasureUnit formats the template
func StrFormatMeasureUnit(MeasureUnit string, value string, templateSplitToken string) string {
var RetStr string
MeasureUnit = strings.TrimSpace(MeasureUnit) // Remove space
SplittedMUnit := strings.SplitN(MeasureUnit, templateSplitToken, 3)
Expand All @@ -92,15 +88,15 @@ func Str_Format_MeasureUnit(MeasureUnit string, value string, templateSplitToken

switch SplittedMUnit[0] {
case "kb":
RetStr = Str_Format_Byte(value, Initial)
RetStr = StrFormatByte(value, Initial)
case "s":
RetStr = Str_Format_Scale(value, Initial)
RetStr = StrFormatScale(value, Initial)
case "f":
RetStr = Str_FormatFloat(value)
RetStr = StrFormatFloat(value)
case "i":
RetStr = Str_FormatInt(value)
RetStr = StrFormatInt(value)
default:
RetStr = Str_FormatInt(value)
RetStr = StrFormatInt(value)
}

if len(SplittedMUnit) > 1 {
Expand All @@ -110,110 +106,113 @@ func Str_Format_MeasureUnit(MeasureUnit string, value string, templateSplitToken
return RetStr
}

// Scale number for It measure unit
func Str_Format_Byte(in string, j1 int) string {
var str_Size string
// StrFormatByte scales number for It measure unit
func StrFormatByte(in string, j1 int) string {
var strSize string

f, err := strconv.ParseFloat(in, 64)

if err != nil {
panic(err)
}

for j1 = 0; j1 < (Information_Size_MAX + 1); j1++ {
for j1 = 0; j1 < (InformationSizeMAX + 1); j1++ {

if j1 >= Information_Size_MAX {
str_Size = "Yb"
if j1 >= InformationSizeMAX {
strSize = "Yb"
break
} else if f > 1024 {
f /= 1024.0
} else {

switch j1 {
case Kb:
str_Size = "Kb"
strSize = "Kb"
case Mb:
str_Size = "Mb"
strSize = "Mb"
case Gb:
str_Size = "Gb"
strSize = "Gb"
case Tb:
str_Size = "Tb"
strSize = "Tb"
case Pb:
str_Size = "Pb"
strSize = "Pb"
case Eb:
str_Size = "Eb"
strSize = "Eb"
case Zb:
str_Size = "Zb"
strSize = "Zb"
case Yb:
str_Size = "Yb"
strSize = "Yb"
}
break
}
}

str_fl := strconv.FormatFloat(f, 'f', 2, 64)
return fmt.Sprintf("%s %s", str_fl, str_Size)
strFl := strconv.FormatFloat(f, 'f', 2, 64)
return fmt.Sprintf("%s %s", strFl, strSize)
}

// Format number for fisics measure unit
func Str_Format_Scale(in string, j1 int) string {
var str_Size string
// StrFormatScale formats number for fisics measure unit
func StrFormatScale(in string, j1 int) string {
var strSize string

f, err := strconv.ParseFloat(in, 64)

if err != nil {
panic(err)
}

for j1 = 0; j1 < (Scale_Size_MAX + 1); j1++ {
for j1 = 0; j1 < (ScaleSizeMAX + 1); j1++ {

if j1 >= Scale_Size_MAX {
str_Size = "Y"
if j1 >= ScaleSizeMAX {
strSize = "Y"
break
} else if f > 1000 {
f /= 1000.0
} else {
switch j1 {
case K:
str_Size = "K"
strSize = "K"
case M:
str_Size = "M"
strSize = "M"
case G:
str_Size = "G"
strSize = "G"
case T:
str_Size = "T"
strSize = "T"
case P:
str_Size = "P"
strSize = "P"
case E:
str_Size = "E"
strSize = "E"
case Z:
str_Size = "Z"
strSize = "Z"
case Y:
str_Size = "Y"
strSize = "Y"
default:
str_Size = "Y"
strSize = "Y"
}
break
}
}

str_fl := strconv.FormatFloat(f, 'f', 2, 64)
return fmt.Sprintf("%s %s", str_fl, str_Size)
strFl := strconv.FormatFloat(f, 'f', 2, 64)
return fmt.Sprintf("%s %s", strFl, strSize)
}

func Str_FormatInt(i string) string {
// StrFormatInt formats as integer
func StrFormatInt(i string) string {
v, _ := strconv.ParseInt(i, 10, 64)
val := strconv.FormatInt(v, 10)
return val
}

func Str_FormatFloat(f string) string {
// StrFormatFloat formats as float
func StrFormatFloat(f string) string {
v, _ := strconv.ParseFloat(f, 64)
v = RoundPrec(v, 2)
v = roundPrec(v, 2)
return strconv.FormatFloat(v, 'f', -1, 64)
}

func Str_FormatDate(toformat string, templateTimeZone string, templateTimeOutFormat string) string {
// StrFormatDate formats as date
func StrFormatDate(toformat string, templateTimeZone string, templateTimeOutFormat string) string {

// Error handling
if templateTimeZone == "" {
Expand All @@ -237,8 +236,9 @@ func Str_FormatDate(toformat string, templateTimeZone string, templateTimeOutFor
return t.In(loc).Format(templateTimeOutFormat)
}

func HasKey(dict map[string]interface{}, key_search string) bool {
if _, ok := dict[key_search]; ok {
// HasKey checks if the map contains the key
func HasKey(dict map[string]interface{}, keySearch string) bool {
if _, ok := dict[keySearch]; ok {
return true
}
return false
Expand Down

0 comments on commit 4834d5f

Please sign in to comment.