From b02dddb47ef8dfcceced8be691b62f8c42273875 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Tue, 1 Aug 2017 18:31:18 +0200 Subject: [PATCH] added log system, and added mongodb geolocation --- .gitignore | 1 + errors.go | 4 ++-- log.go | 18 ++++++++++++++++++ main.go | 15 ++++++++------- mongoModels.go | 33 +++++++++++++++++++-------------- readDataset.go | 16 +++++++++++----- 6 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 log.go diff --git a/.gitignore b/.gitignore index 44fae40..11864f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.csv *.csv.gz +*.log diff --git a/errors.go b/errors.go index 9e83a92..e997ec1 100644 --- a/errors.go +++ b/errors.go @@ -1,9 +1,9 @@ package main -import "fmt" +import "log" func check(err error) { if err != nil { - fmt.Println(err) + log.Println(err) } } diff --git a/log.go b/log.go new file mode 100644 index 0000000..f81dbbc --- /dev/null +++ b/log.go @@ -0,0 +1,18 @@ +package main + +import ( + "io" + "log" + "os" + "time" +) + +func savelog() { + timeS := time.Now().String() + logFile, err := os.OpenFile("log-"+timeS+".log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) + if err != nil { + panic(err) + } + mw := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(mw) +} diff --git a/main.go b/main.go index 4e9affe..cf6d09d 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,10 @@ package main import ( - "fmt" "log" "net/http" "os" - "github.com/fatih/color" "github.com/gorilla/handlers" mgo "gopkg.in/mgo.v2" ) @@ -14,6 +12,8 @@ import ( var cellCollection *mgo.Collection func main() { + savelog() + //connect with mongodb readMongodbConfig("./mongodbConfig.json") session, err := getSession() @@ -22,17 +22,18 @@ func main() { if len(os.Args) > 1 { if os.Args[1] == "-dataset" { - color.Blue("starting to read dataset") + log.Println("starting to read dataset") readDataset("cell_towers.csv") - color.Blue("finished reading dataset") + //readDataset("dataModel_head.csv") + log.Println("finished reading dataset") } } //http server start readServerConfig("./serverConfig.json") - color.Green("server running") - fmt.Print("port: ") - color.Green(serverConfig.ServerPort) + log.Println("server running") + log.Print("port: ") + log.Println(serverConfig.ServerPort) router := NewRouter() headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"}) diff --git a/mongoModels.go b/mongoModels.go index e40dd6c..2103eb8 100644 --- a/mongoModels.go +++ b/mongoModels.go @@ -1,18 +1,23 @@ package main +type LocationModel struct { + Type string `json:"type"` + Coordinates []float64 `json:"coordinates"` +} type CellModel struct { - Radio string `json:"radio"` - MCC string `json:"mcc"` - Net int `json:"net"` - Area int `json:"area"` - Cell int `json:"cell"` - Unit int `json:"unit"` - Lat float64 `json:"lat"` - Lon float64 `json:"lon"` - Range float64 `json:"range"` - Samples int `json:"samples"` - Changeable string `json:"changeable"` - Created int64 `json:"created"` - Updated int64 `json:"updated"` - AverageSignal float64 `json:"averagesignal"` + Radio string `json:"radio"` + MCC string `json:"mcc"` + Net int `json:"net"` + Area int `json:"area"` + Cell int `json:"cell"` + Unit int `json:"unit"` + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` + Location LocationModel `json:"location"` + Range float64 `json:"range"` + Samples int `json:"samples"` + Changeable string `json:"changeable"` + Created int64 `json:"created"` + Updated int64 `json:"updated"` + AverageSignal float64 `json:"averagesignal"` } diff --git a/readDataset.go b/readDataset.go index 8289651..f88c0c6 100644 --- a/readDataset.go +++ b/readDataset.go @@ -2,7 +2,7 @@ package main import ( "bufio" - "fmt" + "log" "os" "strconv" "strings" @@ -29,6 +29,13 @@ func readDataset(path string) { cell.Unit, _ = strconv.Atoi(line[5]) cell.Lon, _ = strconv.ParseFloat(line[6], 64) cell.Lat, _ = strconv.ParseFloat(line[7], 64) + var location LocationModel + location.Type = "Point" + lon, _ := strconv.ParseFloat(line[6], 64) + lat, _ := strconv.ParseFloat(line[7], 64) + location.Coordinates = append(location.Coordinates, lat) + location.Coordinates = append(location.Coordinates, lon) + cell.Location = location cell.Range, _ = strconv.ParseFloat(line[8], 64) cell.Samples, _ = strconv.Atoi(line[9]) cell.Changeable = line[10] @@ -42,8 +49,7 @@ func readDataset(path string) { } lineNum++ } - fmt.Print("line num: ") - fmt.Println(lineNum) - fmt.Print("time elapsed from start: ") - fmt.Println(time.Since(tStart)) + log.Println("line num: " + strconv.Itoa(lineNum)) + log.Print("time elapsed from start: ") + log.Println(time.Since(tStart)) }