diff --git a/backend/goserver/goserver b/backend/goserver/goserver index 55164a0..5b8b0e7 100755 Binary files a/backend/goserver/goserver and b/backend/goserver/goserver differ diff --git a/backend/goserver/main.go b/backend/goserver/main.go index d649129..7103942 100644 --- a/backend/goserver/main.go +++ b/backend/goserver/main.go @@ -1,57 +1,52 @@ package main import ( + "encoding/json" "fmt" "io/ioutil" sw "kritaServers/backend/goserver/server" "net/http" - "time" - - _ "github.com/lib/pq" ) type ColorGroup struct { ID int Name string Colors []string } func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) fmt.Printf("REQUEST") bodyBuffer, _ := ioutil.ReadAll(r.Body) fmt.Printf("after parse") fmt.Println(string(bodyBuffer)) sw.InsertGeneralInfo(bodyBuffer) } -func viewHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "

Last requests

%s
", "in console") +type Person struct { + Name string + Phone string } func main() { fmt.Printf("hello") - // connectionString := - // fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=disable", user, password, dbname, host, port) + var m map[string]interface{} sw.InitDB() - defer sw.Db.Close() + defer sw.Session.Close() + + jsonString := `{"Name":"Alice","Body":"Hello","Time":1294706395881547000}` + err := json.Unmarshal([]byte(jsonString), &m) + if err != nil { + return + } + + c := sw.Session.DB("d").C("collectio2n") + c.Insert(m) http.HandleFunc("/receiver/submit/org.krita.krita", handler) http.HandleFunc("/GoogleLogin", sw.HandleGoogleLogin) http.HandleFunc("/GoogleCallback", sw.HandleGoogleCallback) - http.HandleFunc("/agregatedData", sw.AgregatedDataHandler) - - http.HandleFunc("/", viewHandler) - - //ticker := time.NewTicker(time.Minute * 2) - ticker := time.NewTicker(time.Minute * 10) - go func() { - for t := range ticker.C { - sw.CollectData() - fmt.Println("Tick at", t) - } - }() http.ListenAndServe(":8080", nil) } diff --git a/backend/goserver/server/agregate.go b/backend/goserver/server/agregate.go deleted file mode 100644 index 38f7782..0000000 --- a/backend/goserver/server/agregate.go +++ /dev/null @@ -1,25 +0,0 @@ -package server - -import ( - "fmt" - "net/http" -) - -func AgregatedDataHandler(w http.ResponseWriter, r *http.Request) { - const selectQuery = "SELECT data from agregatedinfo order by generatedTime desc limit 1" - rows, err := Db.Query(selectQuery) - checkErr(err) - var data []byte - - for rows.Next() { - err := rows.Scan(&data) - checkErr(err) - fmt.Printf("frontend request from db") - fmt.Printf(string(data)) - // fmt.Printf(string(cameToServer)) - - } - w.Header().Set("Content-Type", "application/json") - w.Write(data) - -} diff --git a/backend/goserver/server/app.go b/backend/goserver/server/app.go index b105165..6a3e2d7 100644 --- a/backend/goserver/server/app.go +++ b/backend/goserver/server/app.go @@ -1,20 +1,21 @@ package server -import "database/sql" +import ( + "gopkg.in/mgo.v2" +) func checkErr(err error) { if err != nil { panic(err) } } -var Db *sql.DB +var Session *mgo.Session func InitDB() { var err error - Db, err = sql.Open("postgres", "user=root password=1111 dbname=root") //небезопасно, но пока сойдет - - err = Db.Ping() + Session, err = mgo.Dial("mongodb://localhost") checkErr(err) + } diff --git a/backend/goserver/server/collectData.go b/backend/goserver/server/collectData.go deleted file mode 100644 index ad5624a..0000000 --- a/backend/goserver/server/collectData.go +++ /dev/null @@ -1,167 +0,0 @@ -package server - -import ( - "encoding/json" - "fmt" - md "kritaServers/backend/goserver/server/models" - "strconv" -) - -func count(query string, whatCount string) float64 { - rows, err := Db.Query(query, whatCount) - checkErr(err) - var count int - - for rows.Next() { - err := rows.Scan(&count) - checkErr(err) - fmt.Println("scanned!") - - } - return float64(count) //golang хранит числа в json только в флоат64 -} - -// //ПЕРЕМЕННОЕ КОЛИЧЕСТВО АРГУМЕНТОВ СДЕЛАТЬ -// func countOther(query string, whatCount1 string, whatCount2 string) float64 { -// rows, err := Db.Query(query, whatCount1, whatCount2) -// checkErr(err) -// var count int - -// for rows.Next() { -// err := rows.Scan(&count) -// checkErr(err) -// fmt.Println("scanned other platforms!") - -// } -// return float64(count) //golang хранит числа в json только в флоат64 -// } -func countOther(query string, queryNotEql string, args ...string) float64 { - execQuery := query - for i, _ := range args { //Зачем я использую препаред стейтмент, когда можно без него? - if i != 0 { - execQuery += " and " - } - //execQuery += queryNotEql + "'" + v + "'" - execQuery += queryNotEql + "$" + strconv.Itoa(i+1) - - } - //execQuery += ";" - fmt.Println(execQuery) - temp, _ := Db.Prepare(execQuery) - fmt.Println(len(args)) - - ///rows, err := temp.Query(args) - old := args - new := make([]interface{}, len(old)) - for i, v := range old { - new[i] = v - } - fmt.Println(new...) - rows, err := temp.Query(new...) - - checkErr(err) - - var count int - - for rows.Next() { - err := rows.Scan(&count) - checkErr(err) - fmt.Println("scanned other abstruct platforms!") - - } - return float64(count) //golang хранит числа в json только в флоат64 -} - -func CollectData() { - var result md.CollectedData - - const osQuery = "select count( data -> 'platform'->>'os') as os from generalInfo where data -> 'platform'->>'os' = $1" - result.Platform.Os.Linux = count(osQuery, "linux") - fmt.Println(result.Platform.Os.Linux) - result.Platform.Os.Windows = count(osQuery, "windows") - result.Platform.Os.Mac = count(osQuery, "mac") - - const osOtherQuery = "select count( data -> 'platform'->>'os') as os from generalInfo WHERE " - const osOtherNotEql = "data -> 'platform'->>'os' !=" - result.Platform.Os.Other = countOther(osOtherQuery, osOtherNotEql, "linux", "windows", "mac") - - const osWindowsVersionQuery = "select count( data -> 'platform'->>'version') from generalInfo WHERE data -> 'platform'->>'os' ='windows' and data -> 'platform'->>'version'=$1" - result.Platform.Version.Windows.V7 = count(osWindowsVersionQuery, "7") - result.Platform.Version.Windows.V8 = count(osWindowsVersionQuery, "8") - result.Platform.Version.Windows.V81 = count(osWindowsVersionQuery, "8.1") - result.Platform.Version.Windows.V10 = count(osWindowsVersionQuery, "10") - - const osWindowsVersionOtherQuery = "select count( data -> 'platform'->>'version') from generalInfo WHERE data -> 'platform'->>'os' ='windows' " - const osWindowsVersionOtherNotEql = " data -> 'platform'->>'version' != " - result.Platform.Version.Windows.Other = countOther(osWindowsVersionOtherQuery, osWindowsVersionOtherNotEql, "7", "8", "8.1", "10") - - //не уверен, что именно так пишется нужные версии linux - const osLinuxVersionQuery = "select count( data -> 'platform'->>'version') from generalInfo WHERE data -> 'platform'->>'os' ='linux' and data -> 'platform'->>'version'=$1" - result.Platform.Version.Linux.Ubuntu1404 = count(osLinuxVersionQuery, "ubuntu-14.04") - result.Platform.Version.Linux.Ubuntu1410 = count(osLinuxVersionQuery, "ubuntu-14.10") - result.Platform.Version.Linux.Ubuntu1504 = count(osLinuxVersionQuery, "ubuntu-15.04") - result.Platform.Version.Linux.Ubuntu1510 = count(osLinuxVersionQuery, "ubuntu-15.10") - result.Platform.Version.Linux.Ubuntu1604 = count(osLinuxVersionQuery, "ubuntu-16.04") - result.Platform.Version.Linux.Ubuntu1610 = count(osLinuxVersionQuery, "ubuntu-16.10") - result.Platform.Version.Linux.Ubuntu1704 = count(osLinuxVersionQuery, "ubuntu-17.04") - - const osLinuxVersionOtherQuery = "select count( data -> 'platform'->>'version') from generalInfo WHERE data -> 'platform'->>'os' ='linux' " - const osLinuxVersionOtherNotEql = " data -> 'platform'->>'version' != " - result.Platform.Version.Linux.Other = countOther(osLinuxVersionOtherQuery, osLinuxVersionOtherNotEql, "ubuntu-14.04", "ubuntu-14.10", "ubuntu-15.04", "ubuntu-15.10", "ubuntu-16.04", "ubuntu-16.10", "ubuntu-17.04") - - const osMacVersionQuery = "select count( data -> 'platform'->>'version') from generalInfo WHERE data -> 'platform'->>'os' ='mac' and data -> 'platform'->>'version'=$1" - result.Platform.Version.Mac.V1012 = count(osMacVersionQuery, "10.12") - - const osMacVersionOtherQuery = "select count( data -> 'platform'->>'version') from generalInfo WHERE data -> 'platform'->>'os' ='mac' and data -> 'platform'->>'version'=$1" - const osMacVersionOtherNotEql = " data -> 'platform'->>'version' != " - result.Platform.Version.Mac.Other = countOther(osMacVersionOtherQuery, osMacVersionOtherNotEql, "10.12") - - //const osOtherQuery = "select count( data -> 'platform'->>'os') as os from generalInfo where data -> 'platform'->>'os' != $1 and data -> 'platform'->>'os'!=$2" - //result.Platform.Os.Other = countOther(osOtherQuery, "windows", "linux") - - const archCPUQuery = "select count( data -> 'cpu'->>'architecture') from generalInfo where data -> 'cpu'->>'architecture' = $1" - result.CPU.Architecture.X86_64 = count(archCPUQuery, "x86_64") - result.CPU.Architecture.X86 = count(archCPUQuery, "i386") // может быть что-то другое нужно - const archCPUOtherQuery = "select count( data -> 'cpu'->>'architecture') from generalInfo where " - const archCPUOtherNotEql = "data -> 'cpu'->>'architecture' !=" - result.CPU.Architecture.Other = countOther(archCPUOtherQuery, archCPUOtherNotEql, "x86_64", "i386") - - const coreCountQuery = "select data -> 'cpu'->>'count' from generalInfo where data -> 'cpu'->>'count' = $1" - result.CPU.Cores.C1 = count(coreCountQuery, "1") - result.CPU.Cores.C2 = count(coreCountQuery, "2") - result.CPU.Cores.C3 = count(coreCountQuery, "3") - result.CPU.Cores.C4 = count(coreCountQuery, "4") - result.CPU.Cores.C6 = count(coreCountQuery, "6") - result.CPU.Cores.C8 = count(coreCountQuery, "8") - - const coreCountOtherQuery = "select count( data -> 'cpu'->>'count') from generalInfo where " - const coreCountOtherNotEql = "data -> 'cpu'->>'count' !=" - result.CPU.Cores.Other = countOther(coreCountOtherQuery, coreCountOtherNotEql, "1", "2", "3", "4", "6", "8") - - const compilerTypeQuery = "select count(data->'compiler'->>'type') from generalInfo where data->'compiler'->>'type' = $1" - result.Compiler.Type.GCC = count(compilerTypeQuery, "GCC") - result.Compiler.Type.Clang = count(compilerTypeQuery, "Clang") - result.Compiler.Type.MSVC = count(compilerTypeQuery, "MSVC") - - const compilerTypeOtherQuery = "select count(data->'compiler'->>'type') from generalInfo where " - const compilerTypeOtherNotEql = "data->'compiler'->>'type' !=" - result.Compiler.Type.Other = countOther(compilerTypeOtherQuery, compilerTypeOtherNotEql, "GCC", "Clang", "MSVC") - - const localeLanguageQuery = "select count(data->'locale'->>'language') from generalInfo where data->'locale'->>'language' = $1" - result.Locale.Language.English = count(localeLanguageQuery, "English") - result.Locale.Language.Russian = count(localeLanguageQuery, "Russian") - - const localeLanguageOtherQuery = "select count(data->'locale'->>'language') from generalInfo where " - const localeLanguageOtherNotEql = "data->'locale'->>'language' !=" - result.Locale.Language.Other = countOther(localeLanguageOtherQuery, localeLanguageOtherNotEql, "English", "Russian") - - fmt.Println("BEFORE JSON") - - const insertQuery = "INSERT into agregatedInfo(data) VALUES($1)" - b, err := json.Marshal(result) - fmt.Println("SUCCESS JSON") - fmt.Println(b) - checkErr(err) - _, err = Db.Exec(insertQuery, b) - checkErr(err) -} diff --git a/backend/goserver/server/infsertInfo.go b/backend/goserver/server/infsertInfo.go new file mode 100644 index 0000000..9f2ee03 --- /dev/null +++ b/backend/goserver/server/infsertInfo.go @@ -0,0 +1,18 @@ +package server + +import ( + "encoding/json" + "fmt" + md "kritaServers/backend/goserver/server/models" +) + +func InsertGeneralInfo(request []byte) { + var model md.Request + + err := json.Unmarshal(request, &model) + checkErr(err) + c := Session.DB("telemetry").C("installInfo") + c.Insert(model) + fmt.Println("inserted info!") + +} diff --git a/backend/goserver/server/insertIntallInfo.go b/backend/goserver/server/insertIntallInfo.go deleted file mode 100644 index ab266a2..0000000 --- a/backend/goserver/server/insertIntallInfo.go +++ /dev/null @@ -1,14 +0,0 @@ -package server - -import ( - "fmt" -) - -func InsertGeneralInfo(request []byte) { - const insertQuery = "INSERT into generalinfo(data) VALUES($1)" - fmt.Println(string(request)) - s := string(request[:]) - _, err := Db.Exec(insertQuery, s) - checkErr(err) - -} diff --git a/backend/goserver/server/models/collectModel.go b/backend/goserver/server/models/collectModel.go deleted file mode 100644 index 9b4e909..0000000 --- a/backend/goserver/server/models/collectModel.go +++ /dev/null @@ -1,70 +0,0 @@ -package models - -type CollectedData struct { - Platform struct { - Os struct { - Windows float64 - Linux float64 - Mac float64 - Other float64 - } - Version struct { - Windows struct { - V7 float64 - V8 float64 - V81 float64 - V10 float64 - Other float64 - } - Linux struct { - Ubuntu1404 float64 - Ubuntu1410 float64 - Ubuntu1504 float64 - Ubuntu1510 float64 - Ubuntu1604 float64 - Ubuntu1610 float64 - Ubuntu1704 float64 - Other float64 - } - Mac struct { - V1012 float64 - Other float64 - } - } - } - CPU struct { - Architecture struct { - X86_64 float64 - X86 float64 //на самом деле x86_64 c маленькой буквы - Other float64 - } - Cores struct { - C1 float64 - C2 float64 - C3 float64 - C4 float64 - C6 float64 - C8 float64 - Other float64 - } - } - Compiler struct { - Type struct { - GCC float64 - Clang float64 - MSVC float64 - Other float64 - } - // Version struct { //подумать что можно сделать - // V5dot4 float64 - // Other float64 - // } - } - Locale struct { - Language struct { - English float64 - Russian float64 - Other float64 - } - } -}