group tasks

main
alex 2023-04-23 00:08:13 +02:00
parent b9b4081514
commit 1e184cc5ce
7 changed files with 150 additions and 23 deletions

View File

@ -1,4 +1,46 @@
{
"category": "Test",
"name": "hello world"
}
"category": "Test",
"name": "hello world",
"tasks": [
{
"name": "Test1",
"onFinish": "pause",
"undoPossible": false,
"scriptPath": "",
"parameter": [],
"feedback": ""
},
{
"name": "Test2",
"onFinish": "nextStep",
"undoPossible": false,
"scriptPath": "",
"parameter": [],
"feedback": ""
},
{
"name": "Test3",
"onFinish": "nextStep",
"undoPossible": false,
"scriptPath": "",
"parameter": [],
"feedback": ""
},
{
"name": "Test3",
"onFinish": "nextStep",
"undoPossible": false,
"scriptPath": "",
"parameter": [],
"feedback": ""
},
{
"name": "Test3",
"onFinish": "nextStep",
"undoPossible": false,
"scriptPath": "",
"parameter": [],
"feedback": ""
}
]
}

View File

@ -7,6 +7,8 @@ import (
"github.com/rs/zerolog/log"
)
// read from file structure
var CategoryGroups []CategoryGroup
type CategoryGroup struct {
@ -14,23 +16,6 @@ type CategoryGroup struct {
Groups []Group `json:"groups"`
}
func addCategoryGroup(group Group) {
for index, categoryGroup := range CategoryGroups {
if categoryGroup.Category == group.Category {
CategoryGroups[index].Groups = append(CategoryGroups[index].Groups, group)
return
}
}
categoryGroup := CategoryGroup{
Category: group.Category,
}
categoryGroup.Groups = append(categoryGroup.Groups, group)
CategoryGroups = append(CategoryGroups, categoryGroup)
}
type Group struct {
Category string `json:"category"`
Id string `json:"id"`
@ -61,6 +46,23 @@ type TaskParameter struct {
Global bool `json:"global"`
}
func addCategoryGroup(group Group) {
for index, categoryGroup := range CategoryGroups {
if categoryGroup.Category == group.Category {
CategoryGroups[index].Groups = append(CategoryGroups[index].Groups, group)
return
}
}
categoryGroup := CategoryGroup{
Category: group.Category,
}
categoryGroup.Groups = append(categoryGroup.Groups, group)
CategoryGroups = append(CategoryGroups, categoryGroup)
}
func ReadGroups() {
root := "./groupTasks/groups/"

View File

@ -30,6 +30,8 @@ func InitDatabase() {
db.AutoMigrate(&structs.User{})
db.AutoMigrate(&structs.UserSession{})
db.AutoMigrate(&structs.GroupTasks{})
db.AutoMigrate(&structs.GroupTaskStepsLogs{})
//createUser()
}

View File

@ -0,0 +1,35 @@
package structs
import (
"time"
)
// structure for database
const (
GroupTasksStatusFinished uint8 = 0
GroupTasksStatusRunning uint8 = 1
GroupTasksStatusCanceled uint8 = 3
GroupTasksStatusFailed uint8 = 4
)
type GroupTasks struct {
Id string
Category string
GroupId string
GroupName string
CurrentTasksStep uint8
NumberOfSteps uint8
Status uint8
StartedAt time.Time
RememberId string `gorm:"-"` // used by the web client who requested this to open the modal after the backend sent the NewGroupTaskStarted message
}
type GroupTaskStepsLogs struct {
GroupTasksId string
Step uint8
Status uint8
Log string `gorm:"type:text"`
StartedAt time.Time
EndedAt time.Time
}

View File

@ -29,7 +29,7 @@ type SendSocketMessage struct {
type ReceivedMessage struct {
Cmd int
Body any
Body map[string]interface{}
}
func (socketClient *SocketClient) SendCloseMessage() error {
@ -80,7 +80,8 @@ func (socketClient *SocketClient) writeMessage(messageType int, message SendSock
type InitUserSocketConnection struct {
User UserData
CategoryGroups []grouptasks.CategoryGroup
CategoryGroups []grouptasks.CategoryGroup // config specific group tasks
GroupTasks []GroupTasks // in database saved group tasks
}
type UserData struct {

View File

@ -18,6 +18,12 @@ const (
const (
SentInitUserSocketConnection = 1
SentCmdUpdateConnectedUsers = 2
SentCmdNewGroupTaskStarted = 3
)
// commands received from web clients
const (
ReceivedStartGroupTasks = 1
)
var (

View File

@ -9,8 +9,10 @@ import (
"janex/admin-dashboard-backend/modules/structs"
"janex/admin-dashboard-backend/modules/utils"
"janex/admin-dashboard-backend/socketclients"
"time"
"github.com/gofiber/websocket/v2"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
)
@ -51,6 +53,7 @@ func RunHub() {
Email: user.Email,
},
CategoryGroups: grouptasks.CategoryGroups,
GroupTasks: GetAllGroupTasks(),
},
})
@ -64,7 +67,35 @@ func RunHub() {
continue
}
log.Info().Msgf("Received message: %s", receivedMessage)
log.Info().Msgf("Received message: %s", receivedMessage, receivedMessage.Cmd)
switch receivedMessage.Cmd {
case utils.ReceivedStartGroupTasks:
log.Debug().Msgf("received start group tasks %s", receivedMessage.Body["id"], receivedMessage.Body["category"])
groupTasks := &structs.GroupTasks{
Id: uuid.New().String(),
Category: receivedMessage.Body["category"].(string),
GroupId: receivedMessage.Body["id"].(string),
GroupName: receivedMessage.Body["groupName"].(string),
CurrentTasksStep: 1,
NumberOfSteps: uint8(receivedMessage.Body["numberOfSteps"].(float64)),
Status: structs.GroupTasksStatusRunning,
StartedAt: time.Now(),
RememberId: receivedMessage.Body["rememberId"].(string),
}
database.DB.Create(groupTasks)
socketclients.BroadcastMessage(structs.SendSocketMessage{
Cmd: utils.SentCmdNewGroupTaskStarted,
Body: groupTasks,
})
break
default:
log.Error().Msgf("Received unknown message: %s", receivedMessage)
break
}
case connection := <-unregister:
cache.DeleteClientByConn(connection)
@ -73,3 +104,11 @@ func RunHub() {
}
}
}
func GetAllGroupTasks() []structs.GroupTasks {
var groupTasks []structs.GroupTasks
database.DB.Find(&groupTasks)
return groupTasks
}