Observe + metrics

This commit is contained in:
mr
2026-04-29 11:43:52 +02:00
parent 4484c2d5d9
commit eaa983c92e
11 changed files with 703 additions and 198 deletions

View File

@@ -35,7 +35,8 @@ func (o *PeerController) Search() {
isDraft := o.Ctx.Input.Query("is_draft")
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).Search(nil, search, isDraft == "true", int64(offset), int64(limit))
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).Search(
nil, search, isDraft == "true", int64(offset), int64(limit))
o.ServeJSON()
}
@@ -58,7 +59,6 @@ func (o *PeerController) SearchExtended() {
fmt.Println(res, oclib.FiltersFromFlatMap(res, &peer.Peer{}))
data := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).Search(
oclib.FiltersFromFlatMap(res, &peer.Peer{}), "", isDraft == "true", int64(offset), int64(limit))
fmt.Println(data.Data[0].GetName())
o.Data["json"] = data
o.ServeJSON()
}
@@ -137,6 +137,24 @@ func (o *PeerController) Valid() {
o.Data["json"] = data
o.ServeJSON()
return
} else if l.Data != nil && l.ToPeer().Verify && (l.ToPeer().Relation == peer.MASTER || l.ToPeer().Relation == peer.PENDING_MASTER) {
data := req.UpdateOne(map[string]interface{}{
"verify": false,
"relation": peer.MASTER,
}, l.ToPeer().GetID())
fmt.Println(l.Data, data.Data)
data.Data.(*peer.Peer).Relation = peer.NANO // dest is a nano
if data.Data != nil {
b, _ := json.Marshal(data.Data)
go infrastructure.EmitNATS(user, groups, tools.PropalgationMessage{
DataType: tools.PEER.EnumIndex(),
Action: tools.PB_CREATE,
Payload: b,
})
}
o.Data["json"] = data
o.ServeJSON()
return
}
o.Data["json"] = map[string]interface{}{
"data": nil,
@@ -155,10 +173,13 @@ func (o *PeerController) Add() {
user, _, _ := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
infrastructure.SearchMu.Lock()
defer infrastructure.SearchMu.Unlock()
fmt.Println("qdqqds", infrastructure.SearchStreamAction)
if infrastructure.SearchStreamAction[user] != nil {
for _, p := range infrastructure.SearchStreamAction[user] {
if p.GetID() == id {
req := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PEER), nil)
p.NotInCatalog = false
o.Data["json"] = req.StoreOne(p.Serialize(p))
o.ServeJSON()
return
@@ -215,6 +236,10 @@ func (o *PeerController) Blacklist() {
// used from : peer ask, or response, only from peer origin is authorized to change...
func (o *PeerController) changeRelation(id string, dest *peer.Peer, user string, groups []string, relation peer.PeerRelation, request *oclib.Request) {
if dest.IsNano {
fmt.Println("can't change relation on nano")
return
}
infrastructure.SearchMu.Lock()
if dest == nil && infrastructure.SearchStreamAction[user] != nil { // add auto in base if not existing
req := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PEER), nil)
@@ -243,6 +268,8 @@ func (o *PeerController) changeRelation(id string, dest *peer.Peer, user string,
switch relation {
case peer.PARTNER:
relation = peer.PENDING_PARTNER
case peer.NANO:
relation = peer.PENDING_NANO
case peer.BLACKLIST:
relation = peer.NONE
}
@@ -250,7 +277,9 @@ func (o *PeerController) changeRelation(id string, dest *peer.Peer, user string,
if dest.Verify && relation == peer.PENDING_PARTNER {
relation = peer.PARTNER
}
fmt.Println("CHANGE REL", dest.GetID())
if dest.Verify && relation == peer.PENDING_NANO {
relation = peer.NANO
}
data := request.UpdateOne(map[string]interface{}{
"relation": relation,
}, dest.GetID())
@@ -304,7 +333,7 @@ func (o *PeerController) Delete() {
o.ServeJSON()
}
func Websocket(ctx ctx.Context, user string, conn *websocket.Conn) {
func Websocket(c ctx.Context, user string, conn *websocket.Conn) {
defer conn.Close()
done := make(chan struct{})
@@ -323,9 +352,9 @@ func Websocket(ctx ctx.Context, user string, conn *websocket.Conn) {
infrastructure.SearchMu.Lock()
delete(infrastructure.SearchStream, user)
delete(infrastructure.SearchStreamAction, user)
fmt.Println("DEFER")
infrastructure.SearchMu.Unlock()
}
fmt.Println("CLOSE !")
infrastructure.EmitNATS(user, nil, tools.PropalgationMessage{
Action: tools.PB_CLOSE_SEARCH,
DataType: tools.PEER.EnumIndex(),
@@ -337,16 +366,20 @@ func Websocket(ctx ctx.Context, user string, conn *websocket.Conn) {
if !ok {
continue
}
infrastructure.SearchMu.Lock()
infrastructure.SearchStreamAction[user] = append(infrastructure.SearchStreamAction[user], msg)
infrastructure.SearchMu.Unlock()
// Only accumulate peers in the action list (not online-change events).
if msg.Peer != nil {
fmt.Println("AAADDD")
infrastructure.SearchMu.Lock()
infrastructure.SearchStreamAction[user] = append(infrastructure.SearchStreamAction[user], msg.Peer)
infrastructure.SearchMu.Unlock()
fmt.Println("AAADDD", infrastructure.SearchStreamAction[user])
}
if conn.WriteJSON(msg) != nil {
continue
}
case <-done:
return
case <-ctx.Done():
case <-c.Done():
return
}
}