From 9a8530dbfd051512071a71219aa1579d49c080de Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 13 Oct 2023 09:00:11 +0200 Subject: [PATCH] impemented pagination from roese utils --- go.mod | 4 +-- go.sum | 16 +++------- groupTasks/groups/rex-robots/test.py | 6 ++-- .../robots/__pycache__/rex.cpython-39.pyc | Bin 2554 -> 2750 bytes groupTasks/libs/robots/rex.py | 27 ++++++++++------- main.go | 2 +- modules/equipment/equipment.go | 7 +++-- modules/grouptasks/grouptasks.go | 7 +++-- modules/notification/notification.go | 7 +++-- modules/structs/query.go | 5 ---- modules/utils/globals.go | 3 ++ modules/utils/utils.go | 28 ------------------ routers/router/api/v1/equipment/equipment.go | 3 +- .../router/api/v1/grouptasks/grouptasks.go | 6 ++-- .../api/v1/notification/notification.go | 6 ++-- 15 files changed, 52 insertions(+), 75 deletions(-) delete mode 100644 modules/structs/query.go diff --git a/go.mod b/go.mod index d07c823..13be141 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module jannex/admin-dashboard-backend go 1.21.0 require ( - git.ex.umbach.dev/Alex/roese-utils v1.0.10 + git.ex.umbach.dev/Alex/roese-utils v1.0.11 github.com/gofiber/fiber/v2 v2.49.2 github.com/gofiber/websocket/v2 v2.1.6 github.com/google/uuid v1.3.1 @@ -12,7 +12,7 @@ require ( github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee golang.org/x/crypto v0.7.0 gorm.io/driver/mysql v1.5.0 - gorm.io/gorm v1.25.0 + gorm.io/gorm v1.25.5 ) require ( diff --git a/go.sum b/go.sum index 7b7a94f..51d2ed5 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,5 @@ -git.ex.umbach.dev/Alex/roese-utils v1.0.6 h1:dqel8hRA+7IEwxPN1ld88UQu+ZnccECwWuwRbXDdh3U= -git.ex.umbach.dev/Alex/roese-utils v1.0.6/go.mod h1:tjq6m6lNFo0TzAvq8jHhlK48IGxi+SrlrQwf9WYg408= -git.ex.umbach.dev/Alex/roese-utils v1.0.7 h1:eLw6u1ibPeG6zd702Q3Ge8VebABiz0KQJkzr3Rp++HM= -git.ex.umbach.dev/Alex/roese-utils v1.0.7/go.mod h1:tjq6m6lNFo0TzAvq8jHhlK48IGxi+SrlrQwf9WYg408= -git.ex.umbach.dev/Alex/roese-utils v1.0.8 h1:ph18N52R9UsJ0AsJW95W6afa9qKlMDMh60xE4wlfFi8= -git.ex.umbach.dev/Alex/roese-utils v1.0.8/go.mod h1:tjq6m6lNFo0TzAvq8jHhlK48IGxi+SrlrQwf9WYg408= -git.ex.umbach.dev/Alex/roese-utils v1.0.9 h1:MshCYgFc22t4h9KjfE4hvuHbUcrqkbzxC3J4nqGoyuk= -git.ex.umbach.dev/Alex/roese-utils v1.0.9/go.mod h1:tjq6m6lNFo0TzAvq8jHhlK48IGxi+SrlrQwf9WYg408= -git.ex.umbach.dev/Alex/roese-utils v1.0.10 h1:pAtvtWrDSuVKGyusKPG093+DsnNc2ek/6k/9Qgz7acE= -git.ex.umbach.dev/Alex/roese-utils v1.0.10/go.mod h1:tjq6m6lNFo0TzAvq8jHhlK48IGxi+SrlrQwf9WYg408= +git.ex.umbach.dev/Alex/roese-utils v1.0.11 h1:iPrVktpUG0WYLTexoI3iJqi/KojaB5Cs0hCF/GFs92c= +git.ex.umbach.dev/Alex/roese-utils v1.0.11/go.mod h1:mov3ZaoSu+GBQU1uXN/AfSK6MF3/3WxUk38Tb6IKxbI= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= @@ -92,5 +84,5 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/mysql v1.5.0 h1:6hSAT5QcyIaty0jfnff0z0CLDjyRgZ8mlMHLqSt7uXM= gorm.io/driver/mysql v1.5.0/go.mod h1:FFla/fJuCvyTi7rJQd27qlNX2v3L6deTR1GgTjSOLPo= gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= -gorm.io/gorm v1.25.0 h1:+KtYtb2roDz14EQe4bla8CbQlmb9dN3VejSai3lprfU= -gorm.io/gorm v1.25.0/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= +gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/groupTasks/groups/rex-robots/test.py b/groupTasks/groups/rex-robots/test.py index 22f7052..fd4c988 100644 --- a/groupTasks/groups/rex-robots/test.py +++ b/groupTasks/groups/rex-robots/test.py @@ -6,7 +6,9 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..') from libs.robots import rex -rexRobot = rex.Rex("B24", "Drucker leeren") +rexRobot = rex.Rex("test", "Drucker leeren") # rexRobot.moveToXYZ(1, 2, 3) -rexRobot.move_to_x(5) \ No newline at end of file +rexRobot.move_to_x(5) + +rexRobot.finish() \ No newline at end of file diff --git a/groupTasks/libs/robots/__pycache__/rex.cpython-39.pyc b/groupTasks/libs/robots/__pycache__/rex.cpython-39.pyc index 1d7427dc164dd9db62cfc70d766f42322d819391..ef5044b03aff806e5a6c4bc53d1a086bf7282ce2 100644 GIT binary patch literal 2750 zcmb_e&2HO95Z>jI6fIeHoH%ydG%1myg$2Y`0>o`$7)8(kDSC(wZh#g-0fN@jkrY`} zb}7dql#_k!8~9)!{Yt#{)K~DS-z+IvwqqcNQrOv_8LsB%n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b1XKM-`J8EPTBw;h)hS{ zX~W6Ff>4WQqi4qb4VQV23^s$XSZ?}Wq8@D4gyy@fch))!;8K}FKi!86cyXpA{TDmF z3QM9KmPey+#;xu=e5jEFqeI@2_h@McTDnF;S~r9NK6_&vFkQY`^vtlVp10?!Vc1$N z6dFOL+^7aBTwPvVTwX5hE)^=lb|`~J0q*D0*sQBy8>$<^?EEyj^2`>TaZRjooycMQ z$b!v!%%rdWt{KT^h39~)H~^uh!6}cJd9;G-qH9RBavT7BeSN(#&;zF#?E0lJDDBNe zpUqc!=NupX!Tf#x{#?{byv(B{&*G#1Uw%6e{E6s>^`)!p(82LU(Y1S}_IKh$OV*%o zD1ex$Q1))28$<@UgZWbZ`|P)u;Jngr#Fd=NqB7wz3pQ3)miMK>a}Ea8zlbc@&dv|glU9J2$vE5kwjwP1Wbiu s;M8Q?;f&+3;Fsl=W`Hh7FoKDIM^0m1?Yp{SzSir`IzC;6WY8V^1yxt$b^rhX diff --git a/groupTasks/libs/robots/rex.py b/groupTasks/libs/robots/rex.py index 5f43bbd..653d1a1 100644 --- a/groupTasks/libs/robots/rex.py +++ b/groupTasks/libs/robots/rex.py @@ -6,17 +6,18 @@ class Rex: """ This class represents a rex robot. """ - def __init__(self, rexName, jobName): - self.rexName = rexName + def __init__(self, robotName, jobName, address='http://localhost:50055'): + self.robotName = robotName self.jobId = uuid.uuid4().__str__() self.jobName = jobName + self.address = address self.x = 0 self.y = 0 self.z = 0 self.connectedModule = 0 - + def _request_data(self): - return {'robotName': self.rexName, 'jobid': self.jobId, 'jobName': self.jobName} + return {'robotName': self.robotName, 'jobId': self.jobId, 'jobName': self.jobName} def _post_request(self, json): obj = {} @@ -24,7 +25,9 @@ class Rex: obj = self._request_data() obj['task'] = json - res = requests.post('http://localhost:50055/v1/control/0', json=obj) + res = requests.post(self.address + '/v1/control/1', json=obj) + + print("status code", res.status_code) def move_to_x(self, x): """ @@ -54,23 +57,25 @@ class Rex: self.y = y self.z = z - print('Robot: ' + self.rexName + ' moved to (' + str(self.x) + ', ' + str(self.y) + ')') + print('Robot: ' + self.robotName + ' moved to (' + str(self.x) + ', ' + str(self.y) + ')') - # requests.post('http://localhost:50055/v1/control/move', json={'x': self.x, 'y': self.y, 'z': self.z}) - self._post_request({'x': self.x, 'y': self.y, 'z': self.z}) def change_connected_module(self, module): """ Change the connected module. """ - print('Robot: ' + self.rexName + ' changed connected module to ' + str(module)) + print('Robot: ' + self.robotName + ' changed connected module to ' + str(module)) - def finish(): + def finish(self): """ Finish the robot. This will update the robot status to free. """ print('Robot finished') - # TODO: request to the server to finish the robot and update the robot status to free \ No newline at end of file + # TODO: request to the server to finish the robot and update the robot status to free + + res = requests.post(self.address + '/v1/control/1/finish', json={'robotName': self.robotName, 'jobId': self.jobId}) + + print("status code", res.status_code) \ No newline at end of file diff --git a/main.go b/main.go index 9fab868..2e69699 100644 --- a/main.go +++ b/main.go @@ -72,7 +72,7 @@ INVEX_API_TOKEN=api_token`) config.LoadConfig() - rslogger.InitLogger(config.Cfg.Debug, config.Cfg.ColorizedOutput, config.Cfg.LogManagerServerUrl) + rslogger.InitLogger(false, config.Cfg.ColorizedOutput, config.Cfg.LogManagerServerUrl) if os.Getenv("DOCKER") != "" { fmt.Println("Waiting for mariadb docker") diff --git a/modules/equipment/equipment.go b/modules/equipment/equipment.go index 82033b8..55fcf6e 100644 --- a/modules/equipment/equipment.go +++ b/modules/equipment/equipment.go @@ -13,6 +13,7 @@ import ( "strconv" "strings" + "git.ex.umbach.dev/Alex/roese-utils/rspagination" "github.com/gofiber/fiber/v2" "github.com/google/uuid" "github.com/rs/zerolog/log" @@ -111,10 +112,10 @@ func CreateEquipmentDocumentation(c *fiber.Ctx, body structs.CreateEquipmentDocu return c.JSON(fiber.Map{"message": "ok"}) } -func GetEquipmentDocumentations(stockItemId string, query structs.PageQuery, c *fiber.Ctx) error { +func GetEquipmentDocumentations(stockItemId string, query rspagination.PageQuery, c *fiber.Ctx) error { var documentations []structs.EquipmentDocumentation - utils.DbPageQuery(query, + rspagination.DbPageQuery(database.DB, query, utils.EquipmentDocumentationsPaginationLimit, &documentations, "created_at DESC", @@ -135,7 +136,7 @@ func GetEquipmentDocumentations(stockItemId string, query structs.PageQuery, c * return c.SendStatus(fiber.StatusInternalServerError) } } else { - totalPages = utils.GetTotalPages(utils.EquipmentDocumentationsPaginationLimit, + totalPages = rspagination.GetTotalPages(database.DB, utils.EquipmentDocumentationsPaginationLimit, &documentations, "stock_item_id = ?", stockItemId) diff --git a/modules/grouptasks/grouptasks.go b/modules/grouptasks/grouptasks.go index 2eb4ea1..c301256 100644 --- a/modules/grouptasks/grouptasks.go +++ b/modules/grouptasks/grouptasks.go @@ -18,6 +18,7 @@ import ( "strings" "time" + "git.ex.umbach.dev/Alex/roese-utils/rspagination" "git.ex.umbach.dev/Alex/roese-utils/rsutils" "github.com/google/uuid" "github.com/rs/zerolog/log" @@ -736,10 +737,10 @@ func updateGroupTaskSteps(groupTaskStep structs.GroupTaskSteps, category string) }) } -func GetAllGroupTasks(category string, query structs.PageQuery) []structs.GroupTasks { +func GetAllGroupTasks(category string, query rspagination.PageQuery) []structs.GroupTasks { var groupTasks []structs.GroupTasks - utils.DbPageQuery(query, + rspagination.DbPageQuery(database.DB, query, utils.GroupTasksPaginationLimit, &groupTasks, "started_at DESC", @@ -861,7 +862,7 @@ func StartGroupTask(userId string, groupTask structs.GroupTasks) { TotalPages int }{ GroupTask: groupTask, - TotalPages: utils.GetTotalPages(utils.GroupTasksPaginationLimit, + TotalPages: rspagination.GetTotalPages(database.DB, utils.GroupTasksPaginationLimit, []structs.GroupTasks{}, "category = ?", groupTask.Category), diff --git a/modules/notification/notification.go b/modules/notification/notification.go index e5e7dac..94cc20f 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -7,6 +7,7 @@ import ( "jannex/admin-dashboard-backend/socketclients" "time" + "git.ex.umbach.dev/Alex/roese-utils/rspagination" "github.com/gofiber/fiber/v2" "github.com/google/uuid" ) @@ -19,10 +20,10 @@ func GetTotalNotifications(userId string) int { return int(totalNotifications) } -func GetNotifications(query structs.PageQuery, userId string) []structs.Notification { +func GetNotifications(query rspagination.PageQuery, userId string) []structs.Notification { var notifications []structs.Notification - utils.DbPageQuery(query, + rspagination.DbPageQuery(database.DB, query, utils.NotificationsPaginationLimit, ¬ifications, "created_at DESC", @@ -93,7 +94,7 @@ func AddNotification(c *fiber.Ctx, body structs.AddNotificationRequest) error { TotalPages int }{ Notification: notify, - TotalPages: utils.GetTotalPages(utils.NotificationsPaginationLimit, + TotalPages: rspagination.GetTotalPages(database.DB, utils.NotificationsPaginationLimit, []structs.Notification{}, "user_id = ?", userId), diff --git a/modules/structs/query.go b/modules/structs/query.go deleted file mode 100644 index b8a764f..0000000 --- a/modules/structs/query.go +++ /dev/null @@ -1,5 +0,0 @@ -package structs - -type PageQuery struct { - Page int -} diff --git a/modules/utils/globals.go b/modules/utils/globals.go index b8c03be..04aedbe 100644 --- a/modules/utils/globals.go +++ b/modules/utils/globals.go @@ -202,6 +202,8 @@ const ( PermissionUserProfileApiKeys = "user_profile.api_keys" _consoles = "consoles." PermissionConsolesView = _consoles + "view" + + PermissionRoboticsView = "robotics.view" ) var SystemPermissions = []string{ @@ -226,6 +228,7 @@ var SystemPermissions = []string{ PermissionAdminAreaManageRemoveLogManagerServerConnection, PermissionUserProfileApiKeys, PermissionConsolesView, + PermissionRoboticsView, } var DynamicGroupTasksPermissions = []string{ diff --git a/modules/utils/utils.go b/modules/utils/utils.go index cd2af17..d782e73 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -1,14 +1,10 @@ package utils import ( - "jannex/admin-dashboard-backend/modules/database" - "jannex/admin-dashboard-backend/modules/structs" - "math" "time" "github.com/gofiber/fiber/v2" "github.com/rs/zerolog/log" - "gorm.io/gorm" ) func GetXAuhorizationHeader(c *fiber.Ctx) string { @@ -33,27 +29,3 @@ func IsPasswordLengthValid(password string) bool { return true } - -// GetTotalPages returns total pages for pagination -// Example whereQuery = "stock_item_id = ?" and args = stockItemId is Where("stock_item_id = ?", stockItemId) -func GetTotalPages(paginationLimit int, any interface{}, whereQuery interface{}, args ...interface{}) int { - var totalPages int64 - - database.DB.Model(any). - Where(whereQuery, args). - Count(&totalPages) - - return int(math.Ceil(float64(totalPages) / float64(paginationLimit))) -} - -func GetPageOffset(page int, paginationLimit int) int { - return (page - 1) * paginationLimit -} - -func DbPageQuery(query structs.PageQuery, paginationLimit int, result any, orderBy, whereQuery interface{}, args ...interface{}) *gorm.DB { - return database.DB.Limit(paginationLimit). - Offset(GetPageOffset(query.Page, paginationLimit)). - Where(whereQuery, args). - Order(orderBy). - Find(result) -} diff --git a/routers/router/api/v1/equipment/equipment.go b/routers/router/api/v1/equipment/equipment.go index 8280726..ab26bc8 100644 --- a/routers/router/api/v1/equipment/equipment.go +++ b/routers/router/api/v1/equipment/equipment.go @@ -6,6 +6,7 @@ import ( "jannex/admin-dashboard-backend/modules/utils" "jannex/admin-dashboard-backend/socketclients" + "git.ex.umbach.dev/Alex/roese-utils/rspagination" "git.ex.umbach.dev/Alex/roese-utils/rsutils" "github.com/gofiber/fiber/v2" ) @@ -91,7 +92,7 @@ func GetEquipmentDocumentations(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusBadRequest) } - var query structs.PageQuery + var query rspagination.PageQuery if err := rsutils.QueryParserHelper(c, &query); err != nil { return c.SendStatus(fiber.StatusBadRequest) diff --git a/routers/router/api/v1/grouptasks/grouptasks.go b/routers/router/api/v1/grouptasks/grouptasks.go index 389027c..3e99edc 100644 --- a/routers/router/api/v1/grouptasks/grouptasks.go +++ b/routers/router/api/v1/grouptasks/grouptasks.go @@ -3,11 +3,13 @@ package grouptasks import ( "encoding/json" "jannex/admin-dashboard-backend/modules/cache" + "jannex/admin-dashboard-backend/modules/database" "jannex/admin-dashboard-backend/modules/grouptasks" "jannex/admin-dashboard-backend/modules/structs" "jannex/admin-dashboard-backend/modules/utils" "jannex/admin-dashboard-backend/socketclients" + "git.ex.umbach.dev/Alex/roese-utils/rspagination" "git.ex.umbach.dev/Alex/roese-utils/rsutils" "git.ex.umbach.dev/Alex/roese-utils/rsvalidator" "github.com/gofiber/fiber/v2" @@ -55,7 +57,7 @@ func GetGroupTasks(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusUnauthorized) } - var query structs.PageQuery + var query rspagination.PageQuery if err := c.QueryParser(&query); err != nil { return c.SendStatus(fiber.StatusBadRequest) @@ -72,7 +74,7 @@ func GetGroupTasks(c *fiber.Ctx) error { return c.JSON(structs.GroupTasksResponse{ CategoryGroup: categoryGroup, GroupTasks: grouptasks.GetAllGroupTasks(params.Category, query), - TotalPages: utils.GetTotalPages(utils.GroupTasksPaginationLimit, + TotalPages: rspagination.GetTotalPages(database.DB, utils.GroupTasksPaginationLimit, []structs.GroupTasks{}, "category = ?", params.Category), diff --git a/routers/router/api/v1/notification/notification.go b/routers/router/api/v1/notification/notification.go index 0e4fee1..2c15712 100644 --- a/routers/router/api/v1/notification/notification.go +++ b/routers/router/api/v1/notification/notification.go @@ -1,10 +1,12 @@ package notification import ( + "jannex/admin-dashboard-backend/modules/database" "jannex/admin-dashboard-backend/modules/notification" "jannex/admin-dashboard-backend/modules/structs" "jannex/admin-dashboard-backend/modules/utils" + "git.ex.umbach.dev/Alex/roese-utils/rspagination" "git.ex.umbach.dev/Alex/roese-utils/rsutils" "github.com/gofiber/fiber/v2" ) @@ -71,7 +73,7 @@ func GetNotifications(c *fiber.Ctx) error { // '500': // description: Failed to get notifications - var query structs.PageQuery + var query rspagination.PageQuery if err := rsutils.QueryParserHelper(c, &query); err != nil { return c.SendStatus(fiber.StatusBadRequest) @@ -81,7 +83,7 @@ func GetNotifications(c *fiber.Ctx) error { return c.JSON(structs.NotificationsResponse{ Notifications: notification.GetNotifications(query, userId), - TotalPages: utils.GetTotalPages(utils.NotificationsPaginationLimit, + TotalPages: rspagination.GetTotalPages(database.DB, utils.NotificationsPaginationLimit, []structs.Notification{}, "user_id = ?", userId),