From 368d507b23a5e09727d93ec0f1f8fac12105fa09 Mon Sep 17 00:00:00 2001 From: "H.R. Shadhin" Date: Fri, 16 Dec 2022 01:43:55 +0600 Subject: [PATCH] fix: column types --- app/location/delivery/http/request.go | 21 ++---- .../repository/mysql/location_test.go | 5 +- .../repository/pgsql/location_test.go | 5 +- .../repository/sqlite/location_test.go | 5 +- app/location/usecase/location.go | 9 ++- app/location/usecase/location_test.go | 7 +- app/model/location.go | 68 ++++++------------- .../mysql/000001_init_schema.up.sql | 4 +- .../pgsql/000001_init_schema.up.sql | 6 +- .../sqlite/000001_init_schema.up.sql | 2 +- 10 files changed, 49 insertions(+), 83 deletions(-) diff --git a/app/location/delivery/http/request.go b/app/location/delivery/http/request.go index a6c41a5..12bb72d 100644 --- a/app/location/delivery/http/request.go +++ b/app/location/delivery/http/request.go @@ -3,15 +3,13 @@ package http import ( "net/http" "ot-recorder/app/model" - "ot-recorder/infrastructure/config" - "time" ) type PingRequest struct { Type string `json:"_type" validate:"required"` Tst int64 `json:"tst" validate:"required"` - Acc int8 `json:"acc"` - Alt int8 `json:"alt"` + Acc int16 `json:"acc"` + Alt int16 `json:"alt"` Batt int8 `json:"batt"` Bs int8 `json:"bs"` Lat float64 `json:"lat" validate:"required"` @@ -19,20 +17,17 @@ type PingRequest struct { M int8 `json:"m"` T string `json:"t"` Tid string `json:"tid"` - Vac int8 `json:"vac"` - Vel int8 `json:"vel"` + Vac int16 `json:"vac"` + Vel int16 `json:"vel"` Bssid string `json:"BSSID"` Ssid string `json:"SSID"` } func mapLocationRequestToModel(req *PingRequest, headers *http.Header) *model.Location { - createdAt := parseEpochTimeToLocal(req.Tst, config.Get().App.TimeZone) - createdAtC := model.CustomDateTime(createdAt.Format("2006-01-02 15:04:05")) - return &model.Location{ Username: headers.Get("x-limit-u"), Device: headers.Get("x-limit-d"), - CreatedAt: createdAtC, + CreatedAt: req.Tst, Acc: req.Acc, Alt: req.Alt, Batt: req.Batt, @@ -49,9 +44,3 @@ func mapLocationRequestToModel(req *PingRequest, headers *http.Header) *model.Lo IP: headers.Get("X-Real-IP"), } } - -func parseEpochTimeToLocal(epoch int64, tz string) time.Time { - et := time.Unix(epoch, 0) - loc, _ := time.LoadLocation(tz) - return et.In(loc) -} diff --git a/app/location/repository/mysql/location_test.go b/app/location/repository/mysql/location_test.go index 05b14ab..5dfe474 100644 --- a/app/location/repository/mysql/location_test.go +++ b/app/location/repository/mysql/location_test.go @@ -12,11 +12,10 @@ import ( ) func TestCreateLocation(t *testing.T) { - nowTime := time.Now().UTC().Format("2006-01-02 15:04:05") l := &model.Location{ Username: "dev", Device: "phoneAndroid", - CreatedAt: model.CustomDateTime(nowTime), + CreatedAt: time.Now().Unix(), Acc: 13, Alt: -42, Batt: 40, @@ -75,7 +74,7 @@ func TestGetUserLastLocation(t *testing.T) { rows := sqlmock.NewRows([]string{ "id", "username", "device", "created_at", "acc", "alt", "batt", "bs", "lat", "lon", "m", "t", "tid", "vac", "vel", "bssid", "ssid", "ip"}). - AddRow(1, "dev", "phoneAndroid", time.Now().UTC(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p", + AddRow(1, "dev", "phoneAndroid", time.Now().Unix(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p", "p1", 1, 0, "", "", "") query := "SELECT \\* FROM locations WHERE username = \\? ORDER BY created_at DESC LIMIT 1" diff --git a/app/location/repository/pgsql/location_test.go b/app/location/repository/pgsql/location_test.go index cb6bfbd..8fb932c 100644 --- a/app/location/repository/pgsql/location_test.go +++ b/app/location/repository/pgsql/location_test.go @@ -12,11 +12,10 @@ import ( ) func TestCreateLocation(t *testing.T) { - nowTime := time.Now().UTC().Format("2006-01-02 15:04:05") l := &model.Location{ Username: "dev", Device: "phoneAndroid", - CreatedAt: model.CustomDateTime(nowTime), + CreatedAt: time.Now().Unix(), Acc: 13, Alt: -42, Batt: 40, @@ -75,7 +74,7 @@ func TestGetUserLastLocation(t *testing.T) { rows := sqlmock.NewRows([]string{ "id", "username", "device", "created_at", "acc", "alt", "batt", "bs", "lat", "lon", "m", "t", "tid", "vac", "vel", "bssid", "ssid", "ip"}). - AddRow(1, "dev", "phoneAndroid", time.Now().UTC(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p", + AddRow(1, "dev", "phoneAndroid", time.Now().Unix(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p", "p1", 1, 0, "", "", "") query := "SELECT \\* FROM locations WHERE username = \\$1 ORDER BY created_at DESC LIMIT 1" diff --git a/app/location/repository/sqlite/location_test.go b/app/location/repository/sqlite/location_test.go index e561e60..a1fae59 100644 --- a/app/location/repository/sqlite/location_test.go +++ b/app/location/repository/sqlite/location_test.go @@ -12,11 +12,10 @@ import ( ) func TestCreateLocation(t *testing.T) { - nowTime := time.Now().UTC().Format("2006-01-02 15:04:05") l := &model.Location{ Username: "dev", Device: "phoneAndroid", - CreatedAt: model.CustomDateTime(nowTime), + CreatedAt: time.Now().Unix(), Acc: 13, Alt: -42, Batt: 40, @@ -75,7 +74,7 @@ func TestGetUserLastLocation(t *testing.T) { rows := sqlmock.NewRows([]string{ "id", "username", "device", "created_at", "acc", "alt", "batt", "bs", "lat", "lon", "m", "t", "tid", "vac", "vel", "bssid", "ssid", "ip"}). - AddRow(1, "dev", "phoneAndroid", time.Now().UTC(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p", + AddRow(1, "dev", "phoneAndroid", time.Now().Unix(), 13, -42, 40, 1, 23.0000000, 90.0000000, 1, "p", "p1", 1, 0, "", "", "") query := "SELECT \\* FROM locations WHERE username = \\? ORDER BY created_at DESC LIMIT 1" diff --git a/app/location/usecase/location.go b/app/location/usecase/location.go index 1eed9b0..24535cd 100644 --- a/app/location/usecase/location.go +++ b/app/location/usecase/location.go @@ -8,6 +8,7 @@ import ( "net/http" "ot-recorder/app/model" "ot-recorder/app/response" + "ot-recorder/infrastructure/config" "time" "github.com/sirupsen/logrus" @@ -64,7 +65,7 @@ func (u *locationUsecase) LastLocation( location = &model.LocationDetails{ Username: l.Username, Device: l.Device, - DateTime: l.CreatedAt.String(), + DateTime: parseEpochTimeToLocal(l.CreatedAt, config.Get().App.TimeZone).Format("2006-01-02 15:04:05"), Accuracy: l.Acc, Altitude: l.Alt, BatteryLevel: fmt.Sprintf("%d%s", l.Batt, "%"), @@ -81,3 +82,9 @@ func (u *locationUsecase) LastLocation( return location, nil } + +func parseEpochTimeToLocal(epoch int64, tz string) time.Time { + et := time.Unix(epoch, 0) + loc, _ := time.LoadLocation(tz) + return et.In(loc) +} diff --git a/app/location/usecase/location_test.go b/app/location/usecase/location_test.go index 1a0b130..929ce69 100644 --- a/app/location/usecase/location_test.go +++ b/app/location/usecase/location_test.go @@ -15,12 +15,11 @@ import ( func TestCreateLocation(t *testing.T) { mockLocationRepo := new(mocks.LocationRepository) - nowTime := time.Now().UTC().Format("2006-01-02 15:04:05") mockLocation := model.Location{ Username: "dev", Device: "phoneAndroid", - CreatedAt: model.CustomDateTime(nowTime), + CreatedAt: time.Now().Unix(), Acc: 13, Alt: -42, Batt: 40, @@ -53,12 +52,10 @@ func TestCreateLocation(t *testing.T) { func TestGetUserLastLocation(t *testing.T) { mockLocationRepo := new(mocks.LocationRepository) - nowTime := time.Now().UTC().Format("2006-01-02 15:04:05") - mockLocation := model.Location{ Username: "dev", Device: "phoneAndroid", - CreatedAt: model.CustomDateTime(nowTime), + CreatedAt: time.Now().Unix(), Acc: 13, Alt: -42, Batt: 40, diff --git a/app/model/location.go b/app/model/location.go index 5513a92..cebd1e5 100644 --- a/app/model/location.go +++ b/app/model/location.go @@ -2,66 +2,42 @@ package model import ( "context" - "database/sql/driver" - "time" ) -type CustomDateTime string - -func (cdt CustomDateTime) Value() (driver.Value, error) { - return string(cdt), nil -} - -func (cdt *CustomDateTime) Scan(value interface{}) error { - if t, ok := value.(time.Time); ok { - *cdt = CustomDateTime(t.Format("2006-01-02 15:04:05")) - } else if t, ok := value.(string); ok { - *cdt = CustomDateTime(t) - } else if t, ok := value.([]uint8); ok { - *cdt = CustomDateTime(t) - } - - return nil -} - -func (cdt CustomDateTime) String() string { - return string(cdt) -} - type Location struct { - ID int64 `json:"id"` - Username string `json:"username"` - Device string `json:"device"` - CreatedAt CustomDateTime `json:"created_at"` - Acc int8 `json:"acc"` - Alt int8 `json:"alt"` - Batt int8 `json:"batt"` - Bs int8 `json:"bs"` - Lat float64 `json:"lat"` - Lon float64 `json:"lon"` - M int8 `json:"m"` - T string `json:"t"` - Tid string `json:"tid"` - Vac int8 `json:"vac"` - Vel int8 `json:"vel"` - Bssid string `json:"bssid"` - Ssid string `json:"ssid"` - IP string `json:"ip"` + ID int64 `json:"id"` + Username string `json:"username"` + Device string `json:"device"` + CreatedAt int64 `json:"created_at"` + Acc int16 `json:"acc"` + Alt int16 `json:"alt"` + Batt int8 `json:"batt"` + Bs int8 `json:"bs"` + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` + M int8 `json:"m"` + T string `json:"t"` + Tid string `json:"tid"` + Vac int16 `json:"vac"` + Vel int16 `json:"vel"` + Bssid string `json:"bssid"` + Ssid string `json:"ssid"` + IP string `json:"ip"` } type LocationDetails struct { Username string `json:"username"` Device string `json:"device"` DateTime string `json:"date_time"` - Accuracy int8 `json:"accuracy,omitempty"` - Altitude int8 `json:"altitude,omitempty"` + Accuracy int16 `json:"accuracy,omitempty"` + Altitude int16 `json:"altitude,omitempty"` BatteryLevel string `json:"battery_level,omitempty"` BatteryStatus string `json:"battery_status,omitempty"` Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` Mode string `json:"mode,omitempty"` - VerticalAccuracy int8 `json:"vertical_accuracy,omitempty"` - Velocity int8 `json:"velocity,omitempty"` + VerticalAccuracy int16 `json:"vertical_accuracy,omitempty"` + Velocity int16 `json:"velocity,omitempty"` WifiName string `json:"wifi_name,omitempty"` WifiMAC string `json:"wifi_mac,omitempty"` IPAddress string `json:"ip_address"` diff --git a/infrastructure/db/migrations/mysql/000001_init_schema.up.sql b/infrastructure/db/migrations/mysql/000001_init_schema.up.sql index 39369d5..64ea142 100644 --- a/infrastructure/db/migrations/mysql/000001_init_schema.up.sql +++ b/infrastructure/db/migrations/mysql/000001_init_schema.up.sql @@ -2,12 +2,12 @@ CREATE TABLE `locations` ( `id` bigint PRIMARY KEY AUTO_INCREMENT, `username` varchar(20) NOT NULL, `device` varchar(20) NOT NULL, - `created_at` timestamp NOT NULL, + `created_at` bigint NOT NULL, `acc` smallint, `alt` smallint, `batt` smallint, `bs` smallint, - `lat` decimal(9,6) NOT NULL, + `lat` decimal(8,6) NOT NULL, `lon` decimal(9,6) NOT NULL, `m` smallint, `t` varchar(1), diff --git a/infrastructure/db/migrations/pgsql/000001_init_schema.up.sql b/infrastructure/db/migrations/pgsql/000001_init_schema.up.sql index c1a4723..d428f30 100644 --- a/infrastructure/db/migrations/pgsql/000001_init_schema.up.sql +++ b/infrastructure/db/migrations/pgsql/000001_init_schema.up.sql @@ -2,13 +2,13 @@ CREATE TABLE "locations" ( "id" bigserial PRIMARY KEY, "username" varchar(20) NOT NULL, "device" varchar(20) NOT NULL, - "created_at" TIMESTAMP(0) NOT NULL, + "created_at" bigint NOT NULL, "acc" smallint, "alt" smallint, "batt" smallint, "bs" smallint, - "lat" decimal(9,6) NOT NULL, - "lon" decimal(9,6) NOT NULL, + "lat" REAL NOT NULL, + "lon" REAL NOT NULL, "m" smallint, "t" varchar(1), "tid" varchar(2), diff --git a/infrastructure/db/migrations/sqlite/000001_init_schema.up.sql b/infrastructure/db/migrations/sqlite/000001_init_schema.up.sql index 71e1bf6..49b2aec 100644 --- a/infrastructure/db/migrations/sqlite/000001_init_schema.up.sql +++ b/infrastructure/db/migrations/sqlite/000001_init_schema.up.sql @@ -2,7 +2,7 @@ CREATE TABLE `locations` ( `id` INTEGER NOT NULL, `username` TEXT NOT NULL, `device` TEXT NOT NULL, - `created_at` TEXT NOT NULL, + `created_at` INTEGER NOT NULL, `acc` INTEGER, `alt` INTEGER, `batt` INTEGER,