|
@@ -6,10 +6,20 @@ import (
|
|
|
"database/sql"
|
|
|
"devel.mephi.ru/dyokunev/cardlogger-parser/models"
|
|
|
"fmt"
|
|
|
+ _ "github.com/go-sql-driver/mysql"
|
|
|
+ "github.com/xaionaro/reform"
|
|
|
+ "github.com/xaionaro/reform/dialects/mysql"
|
|
|
"net"
|
|
|
+ "os"
|
|
|
+ "runtime"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ SQL_LOGGER_TRACEBACK_DEPTH int = 10
|
|
|
+)
|
|
|
+
|
|
|
func checkError(err error) {
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
@@ -17,6 +27,7 @@ func checkError(err error) {
|
|
|
}
|
|
|
|
|
|
type stage int
|
|
|
+
|
|
|
const (
|
|
|
STAGE_IDLE stage = 0
|
|
|
STAGE_KEEPALIVE stage = 1
|
|
@@ -39,8 +50,15 @@ func gotKeepAlive(port int) {
|
|
|
fmt.Println("SQL error: ", err)
|
|
|
}
|
|
|
|
|
|
+ keepaliveRecord.Counter++
|
|
|
keepaliveRecord.UpdatedAt = time.Now()
|
|
|
- err = keepaliveRecord.Save()
|
|
|
+
|
|
|
+ if err == sql.ErrNoRows {
|
|
|
+ keepaliveRecord.Port = port
|
|
|
+ err = keepaliveRecord.Create()
|
|
|
+ } else {
|
|
|
+ err = keepaliveRecord.Update()
|
|
|
+ }
|
|
|
if err != nil {
|
|
|
fmt.Println("SQL error: ", err)
|
|
|
}
|
|
@@ -50,11 +68,18 @@ func gotValue(port int, valueA [3]int) {
|
|
|
//fmt.Println("gotValue(): port == ", port, "; valueA == ", valueA)
|
|
|
if valueA[0] != valueA[1] || valueA[1] != valueA[2] {
|
|
|
fmt.Println("values doesn't match: ", valueA, "; port == ", port)
|
|
|
+ if valueA[0] != valueA[1] && valueA[1] == valueA[2] {
|
|
|
+ valueA[0] = valueA[1]
|
|
|
+ fmt.Println("corrected value: ", valueA[0])
|
|
|
+ }
|
|
|
+ if valueA[0] != valueA[1] && valueA[1] != valueA[2] && valueA[0] != valueA[2] {
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
err := models.LogRecord{
|
|
|
- Port: port,
|
|
|
- Value: valueA[0],
|
|
|
+ Port: port,
|
|
|
+ Value: int64(valueA[0]),
|
|
|
CreatedAt: time.Now(),
|
|
|
}.Create()
|
|
|
|
|
@@ -63,7 +88,98 @@ func gotValue(port int, valueA [3]int) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+type smartLogger struct {
|
|
|
+ dbName string
|
|
|
+ traceEnable bool
|
|
|
+ errorEnable bool
|
|
|
+}
|
|
|
+
|
|
|
+func (logger smartLogger) queryWrapper(query string) string {
|
|
|
+ var where string
|
|
|
+ for i := 2; i < 32; i++ {
|
|
|
+ _, filePath, _, ok := runtime.Caller(i)
|
|
|
+ if !ok {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ if strings.HasSuffix(filePath, "_reform.go") {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if filePath == `<autogenerated>` {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ whereArray := make([]string, SQL_LOGGER_TRACEBACK_DEPTH, SQL_LOGGER_TRACEBACK_DEPTH)
|
|
|
+ for j := 0; j < SQL_LOGGER_TRACEBACK_DEPTH; j++ {
|
|
|
+ _, filePath, line, ok := runtime.Caller(i + j)
|
|
|
+ pathParts := strings.Split(filePath, "/")
|
|
|
+ fileName := pathParts[len(pathParts)-1]
|
|
|
+ if !ok || strings.HasSuffix(fileName, ".s") {
|
|
|
+ whereArray = whereArray[SQL_LOGGER_TRACEBACK_DEPTH-j:]
|
|
|
+ break
|
|
|
+ }
|
|
|
+ whereArray[SQL_LOGGER_TRACEBACK_DEPTH-1-j] = fmt.Sprintf("%v:%v", fileName, line)
|
|
|
+ }
|
|
|
+ where = "[" + strings.Join(whereArray, " -> ") + "] "
|
|
|
+ break
|
|
|
+ }
|
|
|
+ return fmt.Sprintf("%v[db:%s] %s", where, logger.dbName, query)
|
|
|
+}
|
|
|
+func (logger *smartLogger) SetTraceEnable(enable bool) {
|
|
|
+ logger.traceEnable = enable
|
|
|
+}
|
|
|
+func (logger *smartLogger) SetErrorEnable(enable bool) {
|
|
|
+ logger.errorEnable = enable
|
|
|
+}
|
|
|
+func (logger smartLogger) Before(query string, args []interface{}) {
|
|
|
+ if logger.traceEnable {
|
|
|
+ fmt.Println(logger.queryWrapper(query), args)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+func (logger smartLogger) After(query string, args []interface{}, d time.Duration, err error) {
|
|
|
+ if err != nil {
|
|
|
+ if logger.errorEnable {
|
|
|
+ fmt.Println(logger.queryWrapper(query), args, d, err)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if logger.traceEnable {
|
|
|
+ fmt.Println(logger.queryWrapper(query), args, d, err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func initDB() {
|
|
|
+ simpleDB, err := sql.Open("mysql", "parser:"+os.Getenv("MYSQL_PASSWORD")+"@unix(/var/run/mysqld/mysqld.sock)/db?charset=utf8&parseTime=true")
|
|
|
+ if err != nil {
|
|
|
+ panic(fmt.Errorf("Cannot connect to DB: %s", err.Error()))
|
|
|
+ }
|
|
|
+
|
|
|
+ logger := smartLogger{}
|
|
|
+ logger.SetTraceEnable(true)
|
|
|
+ logger.SetErrorEnable(true)
|
|
|
+
|
|
|
+ DB := reform.NewDB(simpleDB, mysql.Dialect, logger)
|
|
|
+
|
|
|
+ // Setting up table "keepalive"
|
|
|
+ {
|
|
|
+ _, err := models.KeepaliveRecordTable.CreateTableIfNotExists(DB)
|
|
|
+ checkError(err)
|
|
|
+ models.KeepaliveRecordSQL.SetDefaultDB(DB)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Setting up table "log"
|
|
|
+ {
|
|
|
+ _, err := models.LogRecordTable.CreateTableIfNotExists(DB)
|
|
|
+ checkError(err)
|
|
|
+ models.LogRecordSQL.SetDefaultDB(DB)
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
+ initDB()
|
|
|
+
|
|
|
addr, err := net.ResolveUDPAddr("udp", ":36400")
|
|
|
checkError(err)
|
|
|
|
|
@@ -131,9 +247,9 @@ func main() {
|
|
|
|
|
|
return
|
|
|
errorReset:
|
|
|
- fmt.Println("got invalid char (", c, ") from port ", port ,", reset (curStatus == ", curStatus, ").")
|
|
|
+ fmt.Println("got invalid char (", c, ") from port ", port, ", reset (curStatus == ", curStatus, ").")
|
|
|
reset:
|
|
|
-// fmt.Println("reset")
|
|
|
+ // fmt.Println("reset")
|
|
|
curStatus = status{}
|
|
|
}
|
|
|
|