Skip to content

Commit

Permalink
Merge pull request #38 from joshtyf/feature/implement-api-step-executor
Browse files Browse the repository at this point in the history
Implement API step executor
  • Loading branch information
joshtyf authored Jan 25, 2024
2 parents 35f1ab3 + 07a2c9a commit 88cf3ab
Show file tree
Hide file tree
Showing 16 changed files with 429 additions and 18 deletions.
44 changes: 41 additions & 3 deletions backend/database/mongo_seed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ func main() {
panic(err)
}

serviceReqId, err := primitive.ObjectIDFromHex("F2D8E1A73B964C5E7A0F81D9")
if err != nil {
panic(err)
}
serviceRequest := models.ServiceRequestModel{
Id: serviceReqId,
PipelineId: "1",
PipelineVersion: 1,
Status: "Pending",
Status: models.Success,
Remarks: "This is a test service request.",
CreatedOn: time.Now(),
LastUpdated: time.Now(),
CreatedOn: time.Date(2024, time.January, 1, 1, 0, 0, 0, time.UTC),
LastUpdated: time.Date(2024, time.January, 1, 1, 0, 0, 0, time.UTC),
}

res, err := database.NewServiceRequest(c).Create(&serviceRequest)
Expand All @@ -34,4 +39,37 @@ func main() {
} else {
panic("Inserted ID is not an ObjectID")
}
pipelineUuid, err := primitive.ObjectIDFromHex("8A7F3EBCD951246A5F0E9B87")
if err != nil {
panic(err)
}
pipeline := models.PipelineModel{
PipelineName: "Test Pipeline",
Id: pipelineUuid,
Version: 1,
FirstStepName: "step1",
Steps: []models.PipelineStepModel{
{
StepName: "step1",
StepType: models.APIStep,
NextStepName: "",
PrevStepName: "",
Parameters: map[string]string{
"method": "GET",
"url": "https://example.com",
},
IsTerminalStep: true,
},
},
}

res, err = database.NewPipeline(c).Create(&pipeline)
if err != nil {
panic(err)
}
if oid, ok := res.InsertedID.(primitive.ObjectID); ok {
logger.Info("Inserted pipeline", map[string]interface{}{"id": oid.String()})
} else {
panic("Inserted ID is not an ObjectID")
}
}
1 change: 1 addition & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (

require (
github.com/golang/snappy v0.0.1 // indirect
github.com/gookit/event v1.1.1 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gookit/event v1.1.1 h1:heYaXz4N2/0bgvGIwpEEfdW3PAaSWesGHy9/C+ZnZRI=
github.com/gookit/event v1.1.1/go.mod h1:n0XbUJ4kqoCwhLOMeyyitM1AKa+dPgUPuy1vSybHXjg=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
Expand Down
18 changes: 14 additions & 4 deletions backend/src/api/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package handlers
import (
"encoding/json"
"net/http"
"time"

"github.com/gookit/event"
"github.com/gorilla/mux"
handlermodels "github.com/joshtyf/flowforge/src/api/handlers/models"
"github.com/joshtyf/flowforge/src/database"
"github.com/joshtyf/flowforge/src/database/client"
dbmodels "github.com/joshtyf/flowforge/src/database/models"
"github.com/joshtyf/flowforge/src/events"
_ "github.com/lib/pq"
"go.mongodb.org/mongo-driver/bson/primitive"
)

/////////////////// Handlers ///////////////////
Expand Down Expand Up @@ -65,20 +69,26 @@ func CreateServiceRequest(w http.ResponseWriter, r *http.Request) {
JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError)
return
}
var srm *dbmodels.ServiceRequestModel
err = json.NewDecoder(r.Body).Decode(&srm)
srm := &dbmodels.ServiceRequestModel{
CreatedOn: time.Now(),
LastUpdated: time.Now(),
Status: dbmodels.Pending,
}
err = json.NewDecoder(r.Body).Decode(srm)
w.Header().Set("Content-Type", "application/json")
if err != nil {
JSONError(w, handlermodels.NewHttpError(err), http.StatusBadRequest)
return
}
_, err = database.NewServiceRequest(client).Create(srm)
res, err := database.NewServiceRequest(client).Create(srm)
if err != nil {
JSONError(w, handlermodels.NewHttpError(err), http.StatusInternalServerError)
return
}
insertedId, _ := res.InsertedID.(primitive.ObjectID)
srm.Id = insertedId
event.FireAsync(events.NewNewServiceRequestEvent(srm))
w.WriteHeader(http.StatusCreated)
return
}

/////////////////// Helper Functions ///////////////////
Expand Down
41 changes: 41 additions & 0 deletions backend/src/database/models/pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package models

import (
"time"

"go.mongodb.org/mongo-driver/bson/primitive"
)

type PipelineStepType string

const (
APIStep PipelineStepType = "API"
)

type PipelineStepModel struct {
StepName string `bson:"step_name" json:"step_name"`
StepType PipelineStepType `bson:"step_type" json:"step_type"`
NextStepName string `bson:"next_step_name" json:"next_step_name"`
PrevStepName string `bson:"prev_step_name" json:"prev_step_name"`
Parameters map[string]string `bson:"parameters" json:"parameters"`
IsTerminalStep bool `bson:"is_terminal_step" json:"is_terminal_step"`
}

type PipelineModel struct {
Id primitive.ObjectID `bson:"_id,omitempty" json:"uuid,omitempty"` // unique id for the pipeline
PipelineName string `bson:"pipeline_name" json:"pipeline_name"`
Version int `bson:"version" json:"version"`
PrevVersionId primitive.ObjectID `bson:"prev_version_id" json:"prev_version_id"`
FirstStepName string `bson:"first_step_name" json:"first_step_name"`
Steps []PipelineStepModel `bson:"steps" json:"steps"`
CreatedOn time.Time `bson:"created_on" json:"created_on"`
}

func (p *PipelineModel) GetPipelineStep(name string) *PipelineStepModel {
for _, step := range p.Steps {
if step.StepName == name {
return &step
}
}
return nil
}
25 changes: 25 additions & 0 deletions backend/src/database/models/pipeline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package models

import "testing"

func TestGetPipelineStep(t *testing.T) {
pipeline := PipelineModel{
Steps: []PipelineStepModel{{StepName: "step1"}, {StepName: "step2"}},
}
t.Run("Get step 1", func(t *testing.T) {
step := pipeline.GetPipelineStep("step1")
if step == nil {
t.Errorf("Expected step1, got nil")
return
}
if step.StepName != "step1" {
t.Errorf("Expected step1, got %s", step.StepName)
}
})
t.Run("Expect nil result", func(t *testing.T) {
step := pipeline.GetPipelineStep("step3")
if step != nil {
t.Errorf("Expected nil result, got %v", step)
}
})
}
23 changes: 16 additions & 7 deletions backend/src/database/models/service_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

type ServiceRequestStatus string

const (
Pending ServiceRequestStatus = "Pending"
Running ServiceRequestStatus = "Running"
Success ServiceRequestStatus = "Success"
Failure ServiceRequestStatus = "Failure"
)

type ServiceRequestModel struct {
Id primitive.ObjectID `bson:"_id,omitempty"`
PipelineId string `bson:"pipeline_id"`
PipelineVersion int `bson:"pipeline_version"`
Status string `bson:"status"`
CreatedOn time.Time `bson:"created_on"`
LastUpdated time.Time `bson:"last_updated"`
Remarks string `bson:"remarks"`
Id primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
PipelineId string `bson:"pipeline_id" json:"pipeline_id"` // should we use primitive.ObjectID here?
PipelineVersion int `bson:"pipeline_version" json:"pipeline_version"`
Status ServiceRequestStatus `bson:"status" json:"status"`
CreatedOn time.Time `bson:"created_on" json:"created_on"`
LastUpdated time.Time `bson:"last_updated" json:"last_updated"`
Remarks string `bson:"remarks" json:"remarks"`
// FormData FormData `bson:"form_data"`
}
39 changes: 39 additions & 0 deletions backend/src/database/pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package database

import (
"context"
"time"

"github.com/joshtyf/flowforge/src/database/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)

type Pipeline struct {
c *mongo.Client
}

func NewPipeline(c *mongo.Client) *Pipeline {
return &Pipeline{c: c}
}

func (p *Pipeline) Create(pm *models.PipelineModel) (*mongo.InsertOneResult, error) {
pm.CreatedOn = time.Now()
res, err := p.c.Database(DatabaseName).Collection("pipelines").InsertOne(context.Background(), pm)
return res, err
}

func (p *Pipeline) GetById(id string) (*models.PipelineModel, error) {
objId, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
res := p.c.Database(DatabaseName).Collection("pipelines").FindOne(context.Background(), bson.M{"_id": objId})
if res.Err() != nil {
return nil, res.Err()
}
pipeline := &models.PipelineModel{}
res.Decode(pipeline)
return pipeline, nil
}
11 changes: 10 additions & 1 deletion backend/src/database/service_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func (sr *ServiceRequest) Create(srm *models.ServiceRequestModel) (*mongo.Insert
}

func (sr *ServiceRequest) GetById(id string) (*models.ServiceRequestModel, error) {

objectId, _ := primitive.ObjectIDFromHex(id)
result := sr.c.Database(DatabaseName).Collection("service_requests").FindOne(context.Background(), bson.M{"_id": objectId})
if result.Err() != nil {
Expand All @@ -47,3 +46,13 @@ func (sr *ServiceRequest) GetAll() ([]*models.ServiceRequestModel, error) {
}
return srms, nil
}

func (sr *ServiceRequest) UpdateStatus(id string, status models.ServiceRequestStatus) error {
objectId, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
_, err = sr.c.Database(DatabaseName).Collection("service_requests").UpdateOne(
context.Background(), bson.M{"_id": objectId}, bson.M{"$set": bson.M{"status": status}})
return err
}
27 changes: 27 additions & 0 deletions backend/src/events/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package events

import (
"github.com/gookit/event"
"github.com/joshtyf/flowforge/src/database/models"
)

const (
NewServiceRequestEventName = "NewServiceRequestEvent"
)

type NewServiceRequestEvent struct {
event.BasicEvent
serviceRequest *models.ServiceRequestModel
}

func NewNewServiceRequestEvent(serviceRequest *models.ServiceRequestModel) *NewServiceRequestEvent {
e := &NewServiceRequestEvent{
serviceRequest: serviceRequest,
}
e.SetName(NewServiceRequestEventName)
return e
}

func (e *NewServiceRequestEvent) ServiceRequest() *models.ServiceRequestModel {
return e.serviceRequest
}
Loading

0 comments on commit 88cf3ab

Please sign in to comment.