From 7f4faa2853dc7f42a043af537d477cb60a187782 Mon Sep 17 00:00:00 2001 From: Michele Fadda Date: Thu, 16 Jul 2015 12:52:14 +0200 Subject: [PATCH] converte string to int/float32 --- parser.go | 117 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 69 insertions(+), 48 deletions(-) diff --git a/parser.go b/parser.go index a04bd9d..7542599 100644 --- a/parser.go +++ b/parser.go @@ -3,56 +3,57 @@ package main import ( "log" + "strconv" "strings" "time" ) type Banner struct { - Legit string `json:"legit" bson:"legit"` - Spam string `json:"spam" bson:"spam"` - Virus string `json:"virus" bson:"virus"` + Legit int `json:"legit" bson:"legit"` + Spam int `json:"spam" bson:"spam"` + Virus int `json:"virus" bson:"virus"` } type Heloehlo struct { - Legit string `json:"legit" bson:"legit"` - Spam string `json:"spam" bson:"spam"` - Virus string `json:"virus" bson:"virus"` + Legit int `json:"legit" bson:"legit"` + Spam int `json:"spam" bson:"spam"` + Virus int `json:"virus" bson:"virus"` } type Auth struct { - Legit string `json:"legit" bson:"legit"` - Spam string `json:"spam" bson:"spam"` - Virus string `json:"virus" bson:"virus"` + Legit int `json:"legit" bson:"legit"` + Spam int `json:"spam" bson:"spam"` + Virus int `json:"virus" bson:"virus"` } type Mailfrom struct { - Legit string `json:"legit" bson:"legit"` - Spam string `json:"spam" bson:"spam"` - Virus string `json:"virus" bson:"virus"` + Legit int `json:"legit" bson:"legit"` + Spam int `json:"spam" bson:"spam"` + Virus int `json:"virus" bson:"virus"` } type Rcptto struct { - Legit string `json:"legit" bson:"legit"` - Spam string `json:"spam" bson:"spam"` - Virus string `json:"virus" bson:"virus"` + Legit int `json:"legit" bson:"legit"` + Spam int `json:"spam" bson:"spam"` + Virus int `json:"virus" bson:"virus"` } type Data struct { - Legit string `json:"legit" bson:"legit"` - Spam string `json:"spam" bson:"spam"` - Virus string `json:"virus" bson:"virus"` + Legit int `json:"legit" bson:"legit"` + Spam int `json:"spam" bson:"spam"` + Virus int `json:"virus" bson:"virus"` } type Content struct { - Legit string `json:"legit" bson:"legit"` - Spam string `json:"spam" bson:"spam"` - Virus string `json:"virus" bson:"virus"` + Legit int `json:"legit" bson:"legit"` + Spam int `json:"spam" bson:"spam"` + Virus int `json:"virus" bson:"virus"` } type Rate struct { - Smtpp string `json:"smtpp" bson:"smtpp"` - Contentp string `json:"contentp" bson:"contentp"` - Totalp string `json:"totalp" bson:"totalp"` + Smtpp float32 `json:"smtpp" bson:"smtpp"` + Contentp float32 `json:"contentp" bson:"contentp"` + Totalp float32 `json:"totalp" bson:"totalp"` } type MongoImp struct { @@ -68,6 +69,26 @@ type MongoImp struct { 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) { lines := strings.Split(body, "\n") @@ -91,74 +112,74 @@ func parse(body string, stime time.Time) { case "banner": switch key[2] { case "legit": - mimp.Banner.Legit = k[1] + mimp.Banner.Legit = stoi(k[1]) case "spam": - mimp.Banner.Spam = k[1] + mimp.Banner.Spam = stoi(k[1]) case "virus": - mimp.Banner.Virus = k[1] + mimp.Banner.Virus = stoi(k[1]) } case "heloehlo": switch key[2] { case "legit": - mimp.Heloehlo.Legit = k[1] + mimp.Heloehlo.Legit = stoi(k[1]) case "spam": - mimp.Heloehlo.Spam = k[1] + mimp.Heloehlo.Spam = stoi(k[1]) case "virus": - mimp.Heloehlo.Virus = k[1] + mimp.Heloehlo.Virus = stoi(k[1]) } case "auth": switch key[2] { case "legit": - mimp.Auth.Legit = k[1] + mimp.Auth.Legit = stoi(k[1]) case "spam": - mimp.Auth.Spam = k[1] + mimp.Auth.Spam = stoi(k[1]) case "virus": - mimp.Auth.Virus = k[1] + mimp.Auth.Virus = stoi(k[1]) } case "mailfrom": switch key[2] { case "legit": - mimp.Mailfrom.Legit = k[1] + mimp.Mailfrom.Legit = stoi(k[1]) case "spam": - mimp.Mailfrom.Spam = k[1] + mimp.Mailfrom.Spam = stoi(k[1]) case "virus": - mimp.Mailfrom.Virus = k[1] + mimp.Mailfrom.Virus = stoi(k[1]) } case "rcptto": switch key[2] { case "legit": - mimp.Rcptto.Legit = k[1] + mimp.Rcptto.Legit = stoi(k[1]) case "spam": - mimp.Rcptto.Spam = k[1] + mimp.Rcptto.Spam = stoi(k[1]) case "virus": - mimp.Rcptto.Virus = k[1] + mimp.Rcptto.Virus = stoi(k[1]) } case "data": switch key[2] { case "legit": - mimp.Data.Legit = k[1] + mimp.Data.Legit = stoi(k[1]) case "spam": - mimp.Data.Spam = k[1] + mimp.Data.Spam = stoi(k[1]) case "virus": - mimp.Data.Virus = k[1] + mimp.Data.Virus = stoi(k[1]) } case "content": switch key[2] { case "legit": - mimp.Content.Legit = k[1] + mimp.Content.Legit = stoi(k[1]) case "spam": - mimp.Content.Spam = k[1] + mimp.Content.Spam = stoi(k[1]) case "virus": - mimp.Content.Virus = k[1] + mimp.Content.Virus = stoi(k[1]) } case "rate": switch key[2] { case "smtpp": - mimp.Rate.Smtpp = k[1] + mimp.Rate.Smtpp = stof(k[1]) case "contentp": - mimp.Rate.Contentp = k[1] + mimp.Rate.Contentp = stof(k[1]) case "totalp": - mimp.Rate.Totalp = k[1] + mimp.Rate.Totalp = stof(k[1]) } } }