converte string to int/float32
This commit is contained in:
parent
d50dca76dd
commit
7f4faa2853
1 changed files with 69 additions and 48 deletions
117
parser.go
117
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue