Skip to content

Commit

Permalink
add bottom up relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel-Haeberli committed Jun 16, 2023
1 parent c21c78d commit 7369e75
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const TABLE_REPOSITORIES_NAME = "repositories"
const TABLE_REPOSITORIES_FIELD_ID = "id"
const TABLE_REPOSITORIES_FIELD_NAME = "name"
const TABLE_REPOSITORIES_FIELD_URL = "url"
const TABLE_REPOSITORIES_FIELD_APPLICATIONS = "applications"

const TABLE_APPLICATIONS_NAME = "applications"
const TABLE_APPLICATIONS_FIELD_ID = "id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@ import (
)

type ApplicationController struct {
appService *services.ApplicationService
appService *services.ApplicationService
repositoryService *services.RepositoryService
}

func NewApplicationController(appService *services.ApplicationService) *ApplicationController {
func NewApplicationController(appService *services.ApplicationService, repoService *services.RepositoryService) *ApplicationController {

appController := new(ApplicationController)
appController.appService = appService
appController.repositoryService = repoService

return appController
}

func (ac *ApplicationController) AddRepositoryToApplications(repoAddedEvent *RepositoryAddedEvent) error {

repositoryRecord, err := ac.repositoryService.FindForName(repoAddedEvent.RepositoryName)
if err != nil {
return err
}

return ac.appService.AddRepository(repositoryRecord, repoAddedEvent.Applications)
}

func (ac *ApplicationController) AddApplication(record *models.Record, conf *config.MomentumConfig) error {

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type RepositoryAddedEvent struct {
RepositoryName string
Applications []*models.Record
Deployments []*models.Record
}

Expand Down Expand Up @@ -60,7 +61,7 @@ func (rc *RepositoryController) AddRepository(record *models.Record, conf *confi
return err
}

_, deployments, err := rc.repositoryService.SyncRepositoryFromDisk(repo, record)
_, apps, deployments, err := rc.repositoryService.SyncRepositoryFromDisk(repo, record)
if err != nil {
fmt.Println("ERROR:", err.Error())
utils.DirDelete(path)
Expand All @@ -69,6 +70,7 @@ func (rc *RepositoryController) AddRepository(record *models.Record, conf *confi

repoAddedEvent := new(RepositoryAddedEvent)
repoAddedEvent.RepositoryName = repoName
repoAddedEvent.Applications = apps
repoAddedEvent.Deployments = deployments

rc.repositoryAddedEventChannel <- repoAddedEvent
Expand Down
10 changes: 8 additions & 2 deletions momentum-backend/momentum-core/momentum-dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewDispatcher(config *conf.MomentumConfig, pb *pocketbase.PocketBase) *Mome
repoService := services.NewRepositoryService(pb.Dao(), appService)

dispatcher.RepositoryController = controllers.NewRepositoryController(repoService, deploymentService, REPOSITORY_ADDED_EVENT_CHANNEL)
dispatcher.ApplicationsController = controllers.NewApplicationController(appService)
dispatcher.ApplicationsController = controllers.NewApplicationController(appService, repoService)
dispatcher.StagesController = controllers.NewStageController(stageService)
dispatcher.DeploymentController = controllers.NewDeploymentController(deploymentService, repoService)

Expand Down Expand Up @@ -136,7 +136,13 @@ func (d *MomentumDispatcher) setupRepositoryAddedEventChannelObserver() {

err := d.DeploymentController.AddRepositoryToDeployments(event)
if err != nil {
fmt.Println("failed adding relationship to deployments for repository after reciving RepositoryAddedEvent:", event, err, err.Error())
fmt.Println("failed adding relationship to deployments for repository after receiving RepositoryAddedEvent:", event, err, err.Error())
return err
}

err = d.ApplicationsController.AddRepositoryToApplications(event)
if err != nil {
fmt.Println("failed adding relationship to applications for repository after receiving RepositoryAddedEvent:", event, err, err.Error())
return err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package momentumservices

import (
"errors"
"fmt"
consts "momentum/momentum-core/momentum-config"
tree "momentum/momentum-core/momentum-tree"

Expand All @@ -26,29 +28,55 @@ func NewApplicationService(dao *daos.Dao, stageService *StageService) *Applicati
return appService
}

func (as *ApplicationService) SyncApplicationsFromDisk(n *tree.Node, record *models.Record) ([]string, error) {
func (as *ApplicationService) SyncApplicationsFromDisk(n *tree.Node, record *models.Record) ([]*models.Record, error) {

recs := make([]string, 0)
recs := make([]*models.Record, 0)
apps := n.Apps()
for _, a := range apps {
fmt.Println(a)
}
for _, app := range apps {

stages, err := as.stageService.SyncStagesFromDisk(app)
if err != nil {
return nil, err
}

rec, err := as.createWithoutEvent(app.NormalizedPath(), stages)
stageIds := make([]string, 0)
for _, stage := range stages {
stageIds = append(stageIds, stage.Id)
}

rec, err := as.createWithoutEvent(app.NormalizedPath(), stageIds)
if err != nil {
return nil, err
}

err = as.stageService.AddParentApplication(stages, rec)
err = as.stageService.AddParentApplication(stageIds, rec)

recs = append(recs, rec.Id)
recs = append(recs, rec)
}
return recs, nil
}

func (as *ApplicationService) AddRepository(repositoryRecord *models.Record, applications []*models.Record) error {

if repositoryRecord.Collection().Name != consts.TABLE_REPOSITORIES_NAME {
return errors.New("repositoryRecord is not record of repositories collection")
}

for _, app := range applications {

app.Set(consts.TABLE_APPLICATIONS_FIELD_PARENTREPOSITORY, repositoryRecord.Id)
err := as.saveWithoutEvent(app)
if err != nil {
return err
}
}

return nil
}

func (as *ApplicationService) GetApplicationCollection() (*models.Collection, error) {

return as.dao.FindCollectionByNameOrId(consts.TABLE_APPLICATIONS_NAME)
Expand All @@ -65,7 +93,9 @@ func (as *ApplicationService) createWithoutEvent(name string, stageIds []string)
appRecord.Set(consts.TABLE_APPLICATIONS_FIELD_NAME, name)
appRecord.Set(consts.TABLE_APPLICATIONS_FIELD_STAGES, stageIds)

err = as.dao.Clone().SaveRecord(appRecord)
return appRecord, as.saveWithoutEvent(appRecord)
}

return appRecord, err
func (as *ApplicationService) saveWithoutEvent(record *models.Record) error {
return as.dao.Clone().SaveRecord(record)
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (ds *DeploymentService) AddRepository(repositoryRecord *models.Record, depl
for _, depl := range deployments {

depl.Set(consts.TABLE_DEPLOYMENTS_FIELD_REPOSITORIES, append(depl.Get(consts.TABLE_DEPLOYMENTS_FIELD_REPOSITORIES).([]string), repositoryRecord.Id))
err := ds.dao.SaveRecord(depl)
err := ds.saveWithoutEvent(depl)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,52 @@ func NewRepositoryService(dao *daos.Dao, appService *ApplicationService) *Reposi
return repositoryService
}

func (rs *RepositoryService) SyncRepositoryFromDisk(n *tree.Node, record *models.Record) (*models.Record, []*models.Record, error) {
func (rs *RepositoryService) SyncRepositoryFromDisk(n *tree.Node, record *models.Record) (*models.Record, []*models.Record, []*models.Record, error) {

appRecordIds, err := rs.applicationService.SyncApplicationsFromDisk(n, record)
appRecords, err := rs.applicationService.SyncApplicationsFromDisk(n, record)
if err != nil {
return nil, nil, apis.NewApiError(500, err.Error(), nil)
return nil, nil, nil, apis.NewApiError(500, err.Error(), nil)
}

appRecIds := make([]string, 0)
for _, appRec := range appRecords {
appRecIds = append(appRecIds, appRec.Id)
}
record.Set(consts.TABLE_REPOSITORIES_FIELD_APPLICATIONS, appRecIds)

// this complex loop is necessary because we need to know which deployments must add the repository
// which is currently created, when the creation of the repository is finished.
// TODO for a future refactoring: extract logic to specific services.
deployments := make([]*models.Record, 0)
for _, applicationRecordId := range appRecordIds {
for _, applicationRecord := range appRecords {

appRecord, err := rs.dao.FindRecordById(consts.TABLE_APPLICATIONS_NAME, applicationRecordId)
appRecord, err := rs.dao.FindRecordById(consts.TABLE_APPLICATIONS_NAME, applicationRecord.Id)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

stagesIds := appRecord.Get(consts.TABLE_APPLICATIONS_FIELD_STAGES).([]string)
for _, stageId := range stagesIds {

stageRec, err := rs.dao.FindRecordById(consts.TABLE_STAGES_NAME, stageId)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

deploymentIds := stageRec.Get(consts.TABLE_STAGES_FIELD_DEPLOYMENTS).([]string)
for _, deploymentId := range deploymentIds {

deploymentRec, err := rs.dao.FindRecordById(consts.TABLE_DEPLOYMENTS_NAME, deploymentId)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

deployments = append(deployments, deploymentRec)
}
}
}

return record, deployments, nil
return record, appRecords, deployments, nil
}

func (rs *RepositoryService) FindForName(name string) (*models.Record, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func NewStageService(dao *daos.Dao, deploymentService *DeploymentService, keyVal
return stageService
}

func (ss *StageService) SyncStagesFromDisk(n *tree.Node) ([]string, error) {
func (ss *StageService) SyncStagesFromDisk(n *tree.Node) ([]*models.Record, error) {

stages := n.AllStages()
stageIds := make([]string, 0)
stageRecords := make([]*models.Record, 0)
var lastStageNode *tree.Node = nil
var lastStage *models.Record = nil
for _, stage := range stages {
Expand Down Expand Up @@ -70,12 +70,12 @@ func (ss *StageService) SyncStagesFromDisk(n *tree.Node) ([]string, error) {
}
}

stageIds = append(stageIds, stageRecord.Id)
stageRecords = append(stageRecords, stageRecord)
lastStage = stageRecord
lastStageNode = stage
}

return stageIds, nil
return stageRecords, nil
}

func (ss *StageService) AddParentApplication(stageIds []string, app *models.Record) error {
Expand All @@ -86,7 +86,7 @@ func (ss *StageService) AddParentApplication(stageIds []string, app *models.Reco

for _, stageId := range stageIds {

stage, err := ss.dao.FindRecordById(consts.TABLE_APPLICATIONS_NAME, stageId)
stage, err := ss.dao.FindRecordById(consts.TABLE_STAGES_NAME, stageId)
if err != nil {
return err
}
Expand Down
31 changes: 31 additions & 0 deletions momentum-backend/pb_migrations/1686915608_updated_repositories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
migrate((db) => {
const dao = new Dao(db)
const collection = dao.findCollectionByNameOrId("os5ld33mgj3dj7b")

// add
collection.schema.addField(new SchemaField({
"system": false,
"id": "nmmegt3m",
"name": "applications",
"type": "relation",
"required": false,
"unique": false,
"options": {
"collectionId": "wf40hpyi2wvpb7y",
"cascadeDelete": false,
"minSelect": null,
"maxSelect": null,
"displayFields": []
}
}))

return dao.saveCollection(collection)
}, (db) => {
const dao = new Dao(db)
const collection = dao.findCollectionByNameOrId("os5ld33mgj3dj7b")

// remove
collection.schema.removeField("nmmegt3m")

return dao.saveCollection(collection)
})
33 changes: 33 additions & 0 deletions momentum-backend/pb_migrations/1686919800_updated_repositories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
migrate((db) => {
const dao = new Dao(db)
const collection = dao.findCollectionByNameOrId("os5ld33mgj3dj7b")

// remove
collection.schema.removeField("r3jb5rhz")

return dao.saveCollection(collection)
}, (db) => {
const dao = new Dao(db)
const collection = dao.findCollectionByNameOrId("os5ld33mgj3dj7b")

// add
collection.schema.addField(new SchemaField({
"system": false,
"id": "r3jb5rhz",
"name": "status",
"type": "select",
"required": true,
"unique": false,
"options": {
"maxSelect": 1,
"values": [
"PENDING",
"SYNCING",
"UP-TO-DATE",
"ERROR"
]
}
}))

return dao.saveCollection(collection)
})

0 comments on commit 7369e75

Please sign in to comment.