converte string to int/float32

This commit is contained in:
Michele Fadda 2015-07-16 12:52:14 +02:00
parent d50dca76dd
commit 7f4faa2853

117
parser.go
View file

@ -3,56 +3,57 @@ package main
import ( import (
"log" "log"
"strconv"
"strings" "strings"
"time" "time"
) )
type Banner struct { type Banner struct {
Legit string `json:"legit" bson:"legit"` Legit int `json:"legit" bson:"legit"`
Spam string `json:"spam" bson:"spam"` Spam int `json:"spam" bson:"spam"`
Virus string `json:"virus" bson:"virus"` Virus int `json:"virus" bson:"virus"`
} }
type Heloehlo struct { type Heloehlo struct {
Legit string `json:"legit" bson:"legit"` Legit int `json:"legit" bson:"legit"`
Spam string `json:"spam" bson:"spam"` Spam int `json:"spam" bson:"spam"`
Virus string `json:"virus" bson:"virus"` Virus int `json:"virus" bson:"virus"`
} }
type Auth struct { type Auth struct {
Legit string `json:"legit" bson:"legit"` Legit int `json:"legit" bson:"legit"`
Spam string `json:"spam" bson:"spam"` Spam int `json:"spam" bson:"spam"`
Virus string `json:"virus" bson:"virus"` Virus int `json:"virus" bson:"virus"`
} }
type Mailfrom struct { type Mailfrom struct {
Legit string `json:"legit" bson:"legit"` Legit int `json:"legit" bson:"legit"`
Spam string `json:"spam" bson:"spam"` Spam int `json:"spam" bson:"spam"`
Virus string `json:"virus" bson:"virus"` Virus int `json:"virus" bson:"virus"`
} }
type Rcptto struct { type Rcptto struct {
Legit string `json:"legit" bson:"legit"` Legit int `json:"legit" bson:"legit"`
Spam string `json:"spam" bson:"spam"` Spam int `json:"spam" bson:"spam"`
Virus string `json:"virus" bson:"virus"` Virus int `json:"virus" bson:"virus"`
} }
type Data struct { type Data struct {
Legit string `json:"legit" bson:"legit"` Legit int `json:"legit" bson:"legit"`
Spam string `json:"spam" bson:"spam"` Spam int `json:"spam" bson:"spam"`
Virus string `json:"virus" bson:"virus"` Virus int `json:"virus" bson:"virus"`
} }
type Content struct { type Content struct {
Legit string `json:"legit" bson:"legit"` Legit int `json:"legit" bson:"legit"`
Spam string `json:"spam" bson:"spam"` Spam int `json:"spam" bson:"spam"`
Virus string `json:"virus" bson:"virus"` Virus int `json:"virus" bson:"virus"`
} }
type Rate struct { type Rate struct {
Smtpp string `json:"smtpp" bson:"smtpp"` Smtpp float32 `json:"smtpp" bson:"smtpp"`
Contentp string `json:"contentp" bson:"contentp"` Contentp float32 `json:"contentp" bson:"contentp"`
Totalp string `json:"totalp" bson:"totalp"` Totalp float32 `json:"totalp" bson:"totalp"`
} }
type MongoImp struct { type MongoImp struct {
@ -68,6 +69,26 @@ type MongoImp struct {
Rate Rate `json:rate` Rate Rate `json:rate`
} }
func stoi(num string) int {
n, err := strconv.Atoi(num)
if err != nil {
log.Printf("Error converting to int: ", err)
return 0
}
return n
}
func stof(num string) float32 {
n, err := strconv.ParseFloat(num, 32)
if err != nil {
log.Printf("Error converting to int: ", err)
return 0.0
}
return float32(n)
}
func parse(body string, stime time.Time) { func parse(body string, stime time.Time) {
lines := strings.Split(body, "\n") lines := strings.Split(body, "\n")
@ -91,74 +112,74 @@ func parse(body string, stime time.Time) {
case "banner": case "banner":
switch key[2] { switch key[2] {
case "legit": case "legit":
mimp.Banner.Legit = k[1] mimp.Banner.Legit = stoi(k[1])
case "spam": case "spam":
mimp.Banner.Spam = k[1] mimp.Banner.Spam = stoi(k[1])
case "virus": case "virus":
mimp.Banner.Virus = k[1] mimp.Banner.Virus = stoi(k[1])
} }
case "heloehlo": case "heloehlo":
switch key[2] { switch key[2] {
case "legit": case "legit":
mimp.Heloehlo.Legit = k[1] mimp.Heloehlo.Legit = stoi(k[1])
case "spam": case "spam":
mimp.Heloehlo.Spam = k[1] mimp.Heloehlo.Spam = stoi(k[1])
case "virus": case "virus":
mimp.Heloehlo.Virus = k[1] mimp.Heloehlo.Virus = stoi(k[1])
} }
case "auth": case "auth":
switch key[2] { switch key[2] {
case "legit": case "legit":
mimp.Auth.Legit = k[1] mimp.Auth.Legit = stoi(k[1])
case "spam": case "spam":
mimp.Auth.Spam = k[1] mimp.Auth.Spam = stoi(k[1])
case "virus": case "virus":
mimp.Auth.Virus = k[1] mimp.Auth.Virus = stoi(k[1])
} }
case "mailfrom": case "mailfrom":
switch key[2] { switch key[2] {
case "legit": case "legit":
mimp.Mailfrom.Legit = k[1] mimp.Mailfrom.Legit = stoi(k[1])
case "spam": case "spam":
mimp.Mailfrom.Spam = k[1] mimp.Mailfrom.Spam = stoi(k[1])
case "virus": case "virus":
mimp.Mailfrom.Virus = k[1] mimp.Mailfrom.Virus = stoi(k[1])
} }
case "rcptto": case "rcptto":
switch key[2] { switch key[2] {
case "legit": case "legit":
mimp.Rcptto.Legit = k[1] mimp.Rcptto.Legit = stoi(k[1])
case "spam": case "spam":
mimp.Rcptto.Spam = k[1] mimp.Rcptto.Spam = stoi(k[1])
case "virus": case "virus":
mimp.Rcptto.Virus = k[1] mimp.Rcptto.Virus = stoi(k[1])
} }
case "data": case "data":
switch key[2] { switch key[2] {
case "legit": case "legit":
mimp.Data.Legit = k[1] mimp.Data.Legit = stoi(k[1])
case "spam": case "spam":
mimp.Data.Spam = k[1] mimp.Data.Spam = stoi(k[1])
case "virus": case "virus":
mimp.Data.Virus = k[1] mimp.Data.Virus = stoi(k[1])
} }
case "content": case "content":
switch key[2] { switch key[2] {
case "legit": case "legit":
mimp.Content.Legit = k[1] mimp.Content.Legit = stoi(k[1])
case "spam": case "spam":
mimp.Content.Spam = k[1] mimp.Content.Spam = stoi(k[1])
case "virus": case "virus":
mimp.Content.Virus = k[1] mimp.Content.Virus = stoi(k[1])
} }
case "rate": case "rate":
switch key[2] { switch key[2] {
case "smtpp": case "smtpp":
mimp.Rate.Smtpp = k[1] mimp.Rate.Smtpp = stof(k[1])
case "contentp": case "contentp":
mimp.Rate.Contentp = k[1] mimp.Rate.Contentp = stof(k[1])
case "totalp": case "totalp":
mimp.Rate.Totalp = k[1] mimp.Rate.Totalp = stof(k[1])
} }
} }
} }