2025-09-19 16:17:46 +08:00
|
|
|
package mongo
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-10-11 14:56:11 +08:00
|
|
|
"encoding/json"
|
2025-12-05 17:54:25 +08:00
|
|
|
"time"
|
2025-09-19 16:17:46 +08:00
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2025-11-06 21:09:50 +08:00
|
|
|
dbevent string = "cl"
|
|
|
|
|
tbevent string = "events"
|
2025-09-19 16:17:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2025-10-23 18:02:29 +08:00
|
|
|
EventStatusHappen = iota
|
|
|
|
|
EventStatusDataAt
|
|
|
|
|
EventStatusReport
|
|
|
|
|
EventStatusConfirm
|
|
|
|
|
EventStatusPersist
|
|
|
|
|
EventStatusClose
|
2025-09-19 16:17:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2025-12-05 17:54:25 +08:00
|
|
|
EventActionHappen = "happen"
|
|
|
|
|
EventActionDataAt = "data_attach"
|
|
|
|
|
EventActionReport = "report"
|
|
|
|
|
EventActionConfirm = "confirm"
|
|
|
|
|
EventActionPersist = "persist"
|
|
|
|
|
EventActionClose = "close"
|
2025-09-19 16:17:46 +08:00
|
|
|
)
|
|
|
|
|
|
2025-12-05 17:54:25 +08:00
|
|
|
var EventStatusAction = []string{
|
|
|
|
|
EventStatusHappen: EventActionHappen,
|
|
|
|
|
EventStatusDataAt: EventActionDataAt,
|
|
|
|
|
EventStatusReport: EventActionReport,
|
|
|
|
|
EventStatusConfirm: EventActionConfirm,
|
|
|
|
|
EventStatusPersist: EventActionPersist,
|
|
|
|
|
EventStatusClose: EventActionClose,
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
type operation struct {
|
|
|
|
|
Action string `bson:"action" json:"action"`
|
|
|
|
|
OP string `bson:"op" json:"op"`
|
|
|
|
|
TS int64 `bson:"ts" json:"ts"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 17:54:25 +08:00
|
|
|
func GenOperation(action, op string) operation {
|
|
|
|
|
return operation{
|
|
|
|
|
Action: action,
|
|
|
|
|
OP: op,
|
|
|
|
|
TS: time.Now().UnixMilli(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
type Event struct {
|
|
|
|
|
Event string `bson:"event" json:"event"`
|
|
|
|
|
EventUUID string `bson:"event_uuid" json:"event_uuid"`
|
|
|
|
|
Type int `bson:"type" json:"type"`
|
|
|
|
|
Priority int `bson:"priority" json:"priority"` // 0~9
|
|
|
|
|
Status int `bson:"status" json:"status"`
|
2025-10-11 14:56:11 +08:00
|
|
|
Timestamp int64 `bson:"timestamp" json:"timestamp"`
|
2025-09-19 16:17:46 +08:00
|
|
|
From string `bson:"from" json:"from"`
|
|
|
|
|
Operations []*operation `bson:"operations" json:"operations"`
|
2025-11-20 20:58:51 +08:00
|
|
|
// others
|
2025-09-19 16:17:46 +08:00
|
|
|
Alarm *Alarm `bson:"alarm" json:"alarm"`
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 14:56:11 +08:00
|
|
|
func (e *Event) Marshall() ([]byte, error) {
|
|
|
|
|
return json.Marshal(e)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-20 20:58:51 +08:00
|
|
|
func InsertOneEvent(ctx context.Context, doc any) error {
|
2025-09-19 16:17:46 +08:00
|
|
|
_, err := getCollection(dbevent, tbevent).InsertOne(ctx, doc)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 17:54:25 +08:00
|
|
|
func InsertEvents(ctx context.Context, docs any) error {
|
2025-09-19 16:17:46 +08:00
|
|
|
_, err := getCollection(dbevent, tbevent).InsertMany(ctx, docs)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
func DeleteOneEvent[T bson.M | bson.D](ctx context.Context, filter T) error {
|
2025-09-19 16:17:46 +08:00
|
|
|
_, err := getCollection(dbevent, tbevent).DeleteOne(ctx, filter)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
func DeleteEvents[T bson.M | bson.D](ctx context.Context, filter T) error {
|
2025-09-19 16:17:46 +08:00
|
|
|
_, err := getCollection(dbevent, tbevent).DeleteMany(ctx, filter)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 17:54:25 +08:00
|
|
|
func UpdateOneEvent[T bson.M | bson.D](ctx context.Context, filter T, update T) error {
|
|
|
|
|
_, err := getCollection(dbevent, tbevent).UpdateOne(ctx, filter, update)
|
2025-09-19 16:17:46 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 17:54:25 +08:00
|
|
|
func UpdateEvents[T bson.M | bson.D](ctx context.Context, filter T, update T) error {
|
|
|
|
|
_, err := getCollection(dbevent, tbevent).UpdateMany(ctx, filter, update)
|
2025-09-19 16:17:46 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
func FindEventsWithPageLimit[T bson.M | bson.D](ctx context.Context, filter T,
|
2025-10-23 18:02:29 +08:00
|
|
|
sort int, page int64, limit int64) ([]*Event, error) {
|
2025-11-06 21:09:50 +08:00
|
|
|
|
|
|
|
|
opt := options.Find()
|
|
|
|
|
if sort == 1 || sort == -1 {
|
|
|
|
|
opt.SetSort(bson.D{{Key: "timestamp", Value: sort}})
|
2025-11-20 20:58:51 +08:00
|
|
|
} else {
|
|
|
|
|
opt.SetSort(bson.D{{Key: "_id", Value: 1}})
|
2025-11-06 21:09:50 +08:00
|
|
|
}
|
2025-12-05 17:54:25 +08:00
|
|
|
|
2025-11-06 21:09:50 +08:00
|
|
|
if page > 0 && limit > 0 {
|
|
|
|
|
opt.SetSkip(limit * (page - 1)).SetLimit(limit)
|
|
|
|
|
}
|
2025-10-23 18:02:29 +08:00
|
|
|
|
|
|
|
|
cursor, err := getCollection(dbevent, tbevent).Find(ctx, filter, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer cursor.Close(ctx)
|
|
|
|
|
|
|
|
|
|
var docs []*Event
|
|
|
|
|
for cursor.Next(ctx) {
|
|
|
|
|
doc := new(Event)
|
|
|
|
|
if err := cursor.Decode(doc); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
docs = append(docs, doc)
|
|
|
|
|
}
|
2025-11-06 21:09:50 +08:00
|
|
|
if err := cursor.Err(); err != nil {
|
|
|
|
|
return docs, err
|
|
|
|
|
}
|
2025-10-23 18:02:29 +08:00
|
|
|
|
|
|
|
|
return docs, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 16:17:46 +08:00
|
|
|
// sys: 0-hard/1-platform/2-application
|
|
|
|
|
//
|
2025-11-06 21:09:50 +08:00
|
|
|
// level:0-info/1-warn/2-error
|
2025-09-19 16:17:46 +08:00
|
|
|
func genEventType(sys int, level int) int {
|
|
|
|
|
return sys + level*3
|
|
|
|
|
}
|