Files
oc-datacenter/controllers/datacenter.go
2026-04-10 15:20:41 +02:00

259 lines
8.3 KiB
Go

package controllers
import (
"encoding/json"
"fmt"
"net/http"
"oc-datacenter/infrastructure/monitor"
"strconv"
"time"
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/live"
"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
}
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
// @Param type path string true "the word type you want to get"
// @Param is_draft query string false "draft wished"
// @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")
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"
// @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 [get]
func (o *DatacenterController) Get() {
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).LoadOne(id)
o.Data["json"] = data
if data.Data != nil {
break
}
}
o.ServeJSON()
}
// @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()
}
func GetResource(typ oclib.LibDataEnum) interface{} {
if typ == oclib.LibDataEnum(oclib.LIVE_DATACENTER) {
return &live.LiveDatacenter{}
}
if typ == oclib.LibDataEnum(oclib.LIVE_STORAGE) {
return &live.LiveStorage{}
}
return &live.AbstractLive{}
}
// @Title Search
// @Description search workspace
// @Param is_draft query string false
// @Param offset query string false
// @Param limit query string false
// @Param data body json true "body for data content (Json format)"
// @Success 200 {workspace} models.workspace
// @router /:type/extended/search [post]
func (o *DatacenterController) SearchExtended() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
// store and return Id or post with UUIDLibDataEnum
isDraft := o.Ctx.Input.Query("is_draft")
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(100000), &res)
m := map[string][]utils.ShallowDBObject{}
for _, col := range o.collection(false) {
if m[col.String()] == nil {
m[col.String()] = []utils.ShallowDBObject{}
}
fmt.Println(res, oclib.FiltersFromFlatMap(res, GetResource(col)))
s := oclib.NewRequest(col, user, peerID, groups, nil).Search(
oclib.FiltersFromFlatMap(res, GetResource(col)), "", isDraft == "true", int64(offset), int64(limit))
m[col.String()] = append(m[col.String()], s.Data...)
}
o.Data["json"] = map[string]interface{}{
"data": m,
"code": 200,
"err": nil,
}
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
// @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)
}
// @Title Post
// @Description search resources across all types
// @Param type path string true "the type you want to get"
// @Param data body json true "body for data content (Json format)"
// @Success 200 {resource} models.resource
// @router /:type [post]
func (o *DatacenterController) Post() {
libs := o.collection(true)
if len(libs) == 0 {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 500,
"err": "not a proper type",
}
}
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(100000), &res)
o.Data["json"] = oclib.NewRequest(libs[0], user, peerID, groups, nil).StoreOne(res)
o.ServeJSON()
}