Skip to content

Commit

Permalink
add more basic server routes
Browse files Browse the repository at this point in the history
  • Loading branch information
mSulimenko committed Dec 5, 2024
1 parent 176aa97 commit 458c65b
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 22 deletions.
16 changes: 8 additions & 8 deletions server/db/seed_data/servers.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"_id": { "$oid": "650e822f5f1e4e0001a0be01" },
"hostname": "server_1",
"address": "192.168.1.1",
"description": "Main processing server for job tasks.",
"description": "Main processing server for jobs.",
"status": "active",
"created_at": "2023-10-10T08:00:00Z",
"updated_at": "2023-10-10T08:00:00Z",
"current_tasks": [
"current_jobs": [
{ "$oid": "650e7c3f5f1e4e0001a0bdf3" }
],
"completed_tasks": [
"completed_jobs": [
{ "$oid": "650e7c3f5f1e4e0001a0bdf7" }
],
"cpu_info": "Intel Xeon E5-2680 v4",
Expand All @@ -21,12 +21,12 @@
"_id": { "$oid": "650e832f5f1e4e0001a0be02" },
"hostname": "server_2",
"address": "192.168.1.2",
"description": "Backup server for task processing.",
"description": "Backup server for job processing.",
"status": "inactive",
"created_at": "2023-11-05T14:30:00Z",
"updated_at": "2023-11-05T14:30:00Z",
"current_tasks": [],
"completed_tasks": [],
"current_jobs": [],
"completed_jobs": [],
"cpu_info": "AMD EPYC 7742",
"gpu_info": "NVIDIA A100",
"ram_size_gb": 512
Expand All @@ -39,10 +39,10 @@
"status": "active",
"created_at": "2023-11-20T09:00:00Z",
"updated_at": "2023-11-20T09:00:00Z",
"current_tasks": [
"current_jobs": [
{ "$oid": "650e7c3f5f1e4e0001a0bdf4" }
],
"completed_tasks": [],
"completed_jobs": [],
"cpu_info": "Intel Xeon Platinum 8280",
"gpu_info": "NVIDIA Quadro RTX 8000",
"ram_size_gb": 128
Expand Down
129 changes: 129 additions & 0 deletions server/handlers/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,132 @@ func DeleteServer(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"message": "Server deleted successfully"})
}

func GetServerCurrentJobs(w http.ResponseWriter, r *http.Request) {
serverID := chi.URLParam(r, "id")
serversCollection := db.GetCollection("servers")

serverIDObj, err := primitive.ObjectIDFromHex(serverID)
if err != nil {
http.Error(w, "Invalid server ID", http.StatusBadRequest)
return
}

var server models.Server
err = serversCollection.FindOne(context.Background(), bson.M{"_id": serverIDObj}).Decode(&server)
if err != nil {
http.Error(w, "Server not found", http.StatusNotFound)
return
}

jobsCollection := db.GetCollection("jobs")
cursor, err := jobsCollection.Find(context.Background(), bson.M{"_id": bson.M{"$in": server.CurrentJobs}})
if err != nil {
http.Error(w, "Error fetching current jobs", http.StatusInternalServerError)
return
}
defer cursor.Close(context.Background())

var jobs []models.Job
err = cursor.All(context.Background(), &jobs)
if err != nil {
http.Error(w, "Error processing current jobs", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(jobs)
}

func GetServerCompletedJobs(w http.ResponseWriter, r *http.Request) {
serverID := chi.URLParam(r, "id")
serversCollection := db.GetCollection("servers")

serverIDObj, err := primitive.ObjectIDFromHex(serverID)
if err != nil {
http.Error(w, "Invalid server ID", http.StatusBadRequest)
return
}

var server models.Server
err = serversCollection.FindOne(context.Background(), bson.M{"_id": serverIDObj}).Decode(&server)
if err != nil {
http.Error(w, "Server not found", http.StatusNotFound)
return
}

jobsCollection := db.GetCollection("jobs")
cursor, err := jobsCollection.Find(context.Background(), bson.M{"_id": bson.M{"$in": server.CompletedJobs}})
if err != nil {
http.Error(w, "Error fetching completed jobs", http.StatusInternalServerError)
return
}
defer cursor.Close(context.Background())

var jobs []models.Job
err = cursor.All(context.Background(), &jobs)
if err != nil {
http.Error(w, "Error processing completed jobs", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(jobs)
}

func AddJobToServer(w http.ResponseWriter, r *http.Request) {
serverID := chi.URLParam(r, "id")
jobID := chi.URLParam(r, "job_id")
serversCollection := db.GetCollection("servers")
jobsCollection := db.GetCollection("jobs")

serverIDObj, err := primitive.ObjectIDFromHex(serverID)
if err != nil {
http.Error(w, "Invalid server ID", http.StatusBadRequest)
return
}

jobIDObj, err := primitive.ObjectIDFromHex(jobID)
if err != nil {
http.Error(w, "Invalid job ID", http.StatusBadRequest)
return
}

var server models.Server
err = serversCollection.FindOne(context.Background(), bson.M{"_id": serverIDObj}).Decode(&server)
if err != nil {
http.Error(w, "Server not found", http.StatusNotFound)
return
}

var job models.Job
err = jobsCollection.FindOne(context.Background(), bson.M{"_id": jobIDObj}).Decode(&job)
if err != nil {
http.Error(w, "Job not found", http.StatusNotFound)
return
}

_, err = serversCollection.UpdateOne(
context.Background(),
bson.M{"_id": serverIDObj},
bson.M{"$push": bson.M{"current_jobs": jobIDObj}},
)
if err != nil {
http.Error(w, "Error adding job to server", http.StatusInternalServerError)
return
}

_, err = jobsCollection.UpdateOne(
context.Background(),
bson.M{"_id": jobIDObj},
bson.M{"$set": bson.M{"host_id": serverIDObj}},
)
if err != nil {
http.Error(w, "Error updating job with server info", http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"message": "Job successfully added to server"})
}
24 changes: 12 additions & 12 deletions server/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ type Job struct {
}

type Server struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Hostname string `bson:"hostname" json:"hostname"`
Address string `bson:"address" json:"address"`
Description string `bson:"description" json:"description"`
Status string `bson:"status" json:"status"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
UpdatedAt time.Time `bson:"updated_at" json:"updated_at"`
CurrentTasks []primitive.ObjectID `bson:"current_tasks" json:"current_tasks"`
CompletedTasks []primitive.ObjectID `bson:"completed_tasks" json:"completed_tasks"`
CPUInfo string `bson:"cpu_info" json:"cpu_info"`
GPUInfo string `bson:"gpu_info" json:"gpu_info"`
RAMSizeGB int32 `bson:"ram_size_gb" json:"ram_size_gb"`
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Hostname string `bson:"hostname" json:"hostname"`
Address string `bson:"address" json:"address"`
Description string `bson:"description" json:"description"`
Status string `bson:"status" json:"status"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
UpdatedAt time.Time `bson:"updated_at" json:"updated_at"`
CurrentJobs []primitive.ObjectID `bson:"current_jobs" json:"current_jobs"`
CompletedJobs []primitive.ObjectID `bson:"completed_jobs" json:"completed_jobs"`
CPUInfo string `bson:"cpu_info" json:"cpu_info"`
GPUInfo string `bson:"gpu_info" json:"gpu_info"`
RAMSizeGB int32 `bson:"ram_size_gb" json:"ram_size_gb"`
}
5 changes: 3 additions & 2 deletions server/routes/server_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ func ServerRoutes(r chi.Router) {
r.Patch("/servers/{id}", handlers.PatchServer)
r.Delete("/servers/{id}", handlers.DeleteServer)

//r.Get("/servers/{id}/tasks", handlers.GetServerTasks) // Список текущих заданий сервера
//r.Post("/servers/{id}/tasks", handlers.AddServerTask) // Добавление задания серверу
r.Get("/servers/{id}/currentJobs", handlers.GetServerCurrentJobs)
r.Get("/servers/{id}/completedJobs", handlers.GetServerCompletedJobs)
r.Post("/servers/{id}/jobs/{job_id}", handlers.AddJobToServer)
//r.Delete("/servers/{id}/tasks/{task_id}", handlers.RemoveServerTask) // Удаление задания с сервера
//r.Post("/servers/{id}/tasks/{task_id}/complete", handlers.CompleteTask) // Завершение задания
//
Expand Down

0 comments on commit 458c65b

Please sign in to comment.