|
| 1 | +package function |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "staticbackend/internal" |
| 6 | + "time" |
| 7 | + |
| 8 | + "go.mongodb.org/mongo-driver/bson" |
| 9 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 10 | + "go.mongodb.org/mongo-driver/mongo" |
| 11 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 12 | +) |
| 13 | + |
| 14 | +// ExecData represents a server-side function with its name, code and execution |
| 15 | +// history |
| 16 | +type ExecData struct { |
| 17 | + ID primitive.ObjectID `bson:"_id" json:"id"` |
| 18 | + FunctionName string `bson:"name" json:"name"` |
| 19 | + TriggerTopic string `bson:"tr" json:"trigger"` |
| 20 | + Code string `bson:"code" json:"code"` |
| 21 | + Version int `bson:"v" json:"version"` |
| 22 | + LastUpdated time.Time `bson:"lu" json:"lastUpdated"` |
| 23 | + History []ExecHistory `bson:"h" json:"history"` |
| 24 | +} |
| 25 | + |
| 26 | +// ExecHistory represents a function run ending result |
| 27 | +type ExecHistory struct { |
| 28 | + Version int `bson:"v" json:"version"` |
| 29 | + Started time.Time `bson:"s" json:"started"` |
| 30 | + Completed time.Time `bson:"c" json:"completed"` |
| 31 | + Success bool `bson:"ok" json:"success"` |
| 32 | + Output []string `bson:"out" json:"output"` |
| 33 | +} |
| 34 | + |
| 35 | +func Add(db *mongo.Database, data ExecData) (string, error) { |
| 36 | + data.ID = primitive.NewObjectID() |
| 37 | + data.Version = 1 |
| 38 | + data.LastUpdated = time.Now() |
| 39 | + |
| 40 | + ctx := context.Background() |
| 41 | + if _, err := db.Collection("sb_functions").InsertOne(ctx, data); err != nil { |
| 42 | + return "", err |
| 43 | + } |
| 44 | + |
| 45 | + return data.ID.Hex(), nil |
| 46 | +} |
| 47 | + |
| 48 | +func Update(db *mongo.Database, id, code, trigger string) error { |
| 49 | + oid, err := primitive.ObjectIDFromHex(id) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + update := bson.M{ |
| 55 | + "$set": bson.M{"code": code, "lu": time.Now(), "tr": trigger}, |
| 56 | + "$inc": bson.M{"v": 1}, |
| 57 | + } |
| 58 | + filter := bson.M{internal.FieldID: oid} |
| 59 | + |
| 60 | + ctx := context.Background() |
| 61 | + res := db.Collection("sb_functions").FindOneAndUpdate(ctx, filter, update) |
| 62 | + if err := res.Err(); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func GetForExecution(db *mongo.Database, name string) (ExecData, error) { |
| 70 | + var result ExecData |
| 71 | + |
| 72 | + filter := bson.M{"name": name} |
| 73 | + |
| 74 | + ctx := context.Background() |
| 75 | + opt := &options.FindOneOptions{} |
| 76 | + opt.SetProjection(bson.M{"h": -1}) |
| 77 | + |
| 78 | + sr := db.Collection("sb_functions").FindOne(ctx, filter, opt) |
| 79 | + if err := sr.Decode(&result); err != nil { |
| 80 | + return result, err |
| 81 | + } else if err := sr.Err(); err != nil { |
| 82 | + return result, err |
| 83 | + } |
| 84 | + |
| 85 | + return result, nil |
| 86 | +} |
| 87 | + |
| 88 | +func GetByID(db *mongo.Database, id string) (ExecData, error) { |
| 89 | + var result ExecData |
| 90 | + |
| 91 | + oid, err := primitive.ObjectIDFromHex(id) |
| 92 | + if err != nil { |
| 93 | + return result, err |
| 94 | + } |
| 95 | + |
| 96 | + filter := bson.M{internal.FieldID: oid} |
| 97 | + |
| 98 | + ctx := context.Background() |
| 99 | + sr := db.Collection("sb_functions").FindOne(ctx, filter) |
| 100 | + if err := sr.Decode(&result); err != nil { |
| 101 | + return result, err |
| 102 | + } else if err := sr.Err(); err != nil { |
| 103 | + return result, err |
| 104 | + } |
| 105 | + |
| 106 | + return result, nil |
| 107 | +} |
| 108 | + |
| 109 | +func List(db *mongo.Database) ([]ExecData, error) { |
| 110 | + opt := &options.FindOptions{} |
| 111 | + opt.SetProjection(bson.M{"h": -1}) |
| 112 | + |
| 113 | + ctx := context.Background() |
| 114 | + cur, err := db.Collection("sb_functions").Find(ctx, bson.M{}, opt) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + defer cur.Close(ctx) |
| 119 | + |
| 120 | + var results []ExecData |
| 121 | + for cur.Next(ctx) { |
| 122 | + var ed ExecData |
| 123 | + err := cur.Decode(&ed) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + results = append(results, ed) |
| 129 | + } |
| 130 | + |
| 131 | + return results, nil |
| 132 | +} |
| 133 | + |
| 134 | +func Delete(db *mongo.Database, name string) error { |
| 135 | + filter := bson.M{"name": name} |
| 136 | + |
| 137 | + ctx := context.Background() |
| 138 | + if _, err := db.Collection("sb_functions").DeleteOne(ctx, filter); err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
0 commit comments