Files
oc-datacenter/controllers/datacenter.go

190 lines
6.0 KiB
Go
Raw Normal View History

package controllers
import (
2026-04-09 07:49:35 +02:00
"fmt"
"net/http"
"oc-datacenter/infrastructure/monitor"
2026-04-09 07:49:35 +02:00
"strconv"
"time"
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs"
2026-04-09 07:49:35 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
beego "github.com/beego/beego/v2/server/web"
"github.com/gorilla/websocket"
)
// Operations about workspace
type DatacenterController struct {
beego.Controller
}
2026-04-09 07:49:35 +02:00
func resourceTypeEnum(t string, special bool) []oclib.LibDataEnum {
e := []oclib.LibDataEnum{}
if special && t == "resource" {
return e
}
if t == "storage" || t == "live" {
e = append(e, oclib.LibDataEnum(oclib.LIVE_STORAGE))
}
if t == "datacenter" || t == "live" {
e = append(e, oclib.LibDataEnum(oclib.LIVE_DATACENTER))
}
return e
}
func (o *DatacenterController) collection(special bool) []oclib.LibDataEnum {
// Extrait le type depuis le segment d'URL après "resource"
// URL forme: /oc/resource/{type}/...
typ := o.Ctx.Input.Param(":type")
return resourceTypeEnum(typ, special)
}
// @Title Search
// @Description search datacenter
// @Param type path string true "the type you want to get"
// @Param search path string true "the word search you want to get"
// @Param is_draft query string false "draft wished"
// @Param offset query string false
// @Param limit query string false
// @Success 200 {workspace} models.workspace
// @router /:type/search/:search [get]
func (o *DatacenterController) Search() {
/*
* This is a sample of how to use the search function
* The search function is used to search for data in the database
* The search function takes in a filter and a data type
* The filter is a struct that contains the search parameters
* The data type is an enum that specifies the type of data to search for
* The search function returns a list of data that matches the filter
* The data is then returned as a json object
*/
// store and return Id or post with UUID
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
search := o.Ctx.Input.Param(":search")
if search == "*" {
search = ""
}
isDraft := o.Ctx.Input.Query("is_draft")
m := map[string][]utils.ShallowDBObject{}
for _, col := range o.collection(false) {
if m[col.String()] == nil {
m[col.String()] = []utils.ShallowDBObject{}
}
s := oclib.NewRequest(col, user, peerID, groups, nil).Search(&dbs.Filters{
Or: map[string][]dbs.Filter{
// "abstractlive.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
"abstractlive.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}, "", isDraft == "true", int64(offset), int64(limit))
fmt.Println(s)
m[col.String()] = append(m[col.String()], s.Data...)
}
o.Data["json"] = map[string]interface{}{
"data": m,
"code": 200,
"err": nil,
}
o.ServeJSON()
}
// @Title GetAll
// @Description find booking by id
2026-04-09 07:49:35 +02:00
// @Param type path string true "the word type you want to get"
// @Param is_draft query string false "draft wished"
2026-04-09 07:49:35 +02:00
// @Param offset query string false
// @Param limit query string false
// @Success 200 {booking} models.booking
// @router / [get]
func (o *DatacenterController) GetAll() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
2026-04-09 07:49:35 +02:00
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
m := map[string][]utils.ShallowDBObject{}
for _, col := range o.collection(false) {
if m[col.String()] == nil {
m[col.String()] = []utils.ShallowDBObject{}
}
s := oclib.NewRequest(oclib.LibDataEnum(col), user, peerID, groups, nil).LoadAll(isDraft == "true", int64(offset), int64(limit))
fmt.Println(s)
m[col.String()] = append(m[col.String()], s.Data...)
}
fmt.Println(m)
o.Data["json"] = map[string]interface{}{
"data": m,
"code": 200,
"err": nil,
}
o.ServeJSON()
}
// @Title Get
// @Description find booking by id
// @Param id path string true "the id you want to get"
2026-04-09 07:49:35 +02:00
// @Param type path string true "the word type you want to get"
// @Param is_draft query string false "draft wished"
// @Success 200 {booking} models.booking
2026-04-09 07:49:35 +02:00
// @router /:type/:id [get]
func (o *DatacenterController) Get() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
2026-04-09 07:49:35 +02:00
for _, col := range o.collection(false) {
data := oclib.NewRequest(col, user, peerID, groups, nil).LoadOne(id)
o.Data["json"] = data
if data.Data != nil {
break
}
2026-04-09 07:49:35 +02:00
}
o.ServeJSON()
}
2026-04-09 07:49:35 +02:00
// @Title Delete
// @Description find booking by id
// @Param id path string true "the id you want to get"
// @Param type path string true "the word type you want to get"
// @Param is_draft query string false "draft wished"
// @Success 200 {booking} models.booking
// @router /:type/:id [delete]
func (o *DatacenterController) Delete() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
for _, col := range o.collection(false) {
data := oclib.NewRequest(col, user, peerID, groups, nil).DeleteOne(id)
o.Data["json"] = data
if data.Data != nil {
break
}
}
o.ServeJSON()
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true }, // allow all origins
}
// @Title Log
// @Description find booking by id
// @Param id path string true "the id you want to get"
// @Success 200 {booking} models.booking
2026-04-09 07:49:35 +02:00
// @router /logs/:id [get]
func (o *DatacenterController) Log() {
// user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
conn, err := upgrader.Upgrade(o.Ctx.ResponseWriter, o.Ctx.Request, nil)
if err != nil {
o.Ctx.WriteString("WebSocket upgrade failed: " + err.Error())
return
}
defer conn.Close()
monitors, err := monitor.NewMonitorService()
if err != nil {
o.Ctx.WriteString("Monitor service unavailable: " + err.Error())
return
}
ctx := monitor.StreamRegistry.Register(id)
monitors.Stream(ctx, id, 1*time.Second, conn)
}