Skip to content

Commit

Permalink
extract sync process in separate service
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel-Haeberli committed Jun 20, 2023
1 parent 10d3502 commit b211dc0
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ type RepositoryAddedEvent struct {
}

type RepositoryController struct {
repositorySyncService *services.RepositorySyncService
repositoryService *services.RepositoryService
deploymentService *services.DeploymentService
repositoryAddedEventChannel chan *RepositoryAddedEvent
kustomizeValidation *kustomizeclient.KustomizationValidationService
}

func NewRepositoryController(
repoSyncService *services.RepositorySyncService,
repoService *services.RepositoryService,
deploymentService *services.DeploymentService,
repositoryAddedEventChannel chan *RepositoryAddedEvent,
kustomizeValidator *kustomizeclient.KustomizationValidationService) *RepositoryController {

repoController := new(RepositoryController)

repoController.repositorySyncService = repoSyncService
repoController.repositoryService = repoService
repoController.deploymentService = deploymentService
repoController.repositoryAddedEventChannel = repositoryAddedEventChannel
repoController.kustomizeValidation = kustomizeValidator

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

_, apps, deployments, err := rc.repositoryService.SyncRepositoryFromDisk(repo, record)
_, apps, deployments, err := rc.repositorySyncService.SyncRepositoryFromDisk(repo, record)
if err != nil {
fmt.Println("ERROR:", err.Error())
utils.DirDelete(path)
Expand Down
3 changes: 2 additions & 1 deletion momentum-backend/momentum-core/momentum-dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func NewDispatcher(config *conf.MomentumConfig, pb *pocketbase.PocketBase) *Mome
stageService := services.NewStageService(pb.Dao(), deploymentService, keyValueService)
appService := services.NewApplicationService(pb.Dao(), stageService)
repoService := services.NewRepositoryService(pb.Dao(), appService)
repoSyncService := services.NewRepositorySyncService(pb.Dao(), appService, stageService, deploymentService, keyValueService)

dispatcher.kustomizeValidator = kustomizeclient.NewKustomizationValidationService(dispatcher.Config, repoService)

dispatcher.RepositoryController = controllers.NewRepositoryController(repoService, deploymentService, REPOSITORY_ADDED_EVENT_CHANNEL, dispatcher.kustomizeValidator)
dispatcher.RepositoryController = controllers.NewRepositoryController(repoSyncService, repoService, REPOSITORY_ADDED_EVENT_CHANNEL, dispatcher.kustomizeValidator)
dispatcher.ApplicationsController = controllers.NewApplicationController(appService, repoService)
dispatcher.StagesController = controllers.NewStageController(stageService)
dispatcher.DeploymentController = controllers.NewDeploymentController(deploymentService, repoService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package momentumservices

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

"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
Expand All @@ -28,37 +26,6 @@ func NewApplicationService(dao *daos.Dao, stageService *StageService) *Applicati
return appService
}

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

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
}

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(stageIds, rec)

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package momentumservices
import (
"errors"
consts "momentum/momentum-core/momentum-config"
tree "momentum/momentum-core/momentum-tree"

"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
Expand All @@ -28,32 +27,6 @@ func NewDeploymentService(dao *daos.Dao, keyValueService *KeyValueService) *Depl
return deplyomentService
}

func (ds *DeploymentService) SyncDeploymentsFromDisk(n *tree.Node) ([]*models.Record, error) {

deployments := n.AllDeployments()

deploymentIds := make([]*models.Record, 0)
for _, deployment := range deployments {

deploymentRecord, err := ds.createWithoutEvent(deployment.NormalizedPath())
if err != nil {
return nil, err
}

if deployment.Kind == tree.File {

err := ds.keyValueService.SyncFile(deployment, deploymentRecord)
if err != nil {
return nil, err
}
}

deploymentIds = append(deploymentIds, deploymentRecord)
}

return deploymentIds, nil
}

func (ds *DeploymentService) AddParentStage(stage *models.Record, deployments []*models.Record) error {

if stage.Collection().Name != consts.TABLE_STAGES_NAME {
Expand Down
122 changes: 0 additions & 122 deletions momentum-backend/momentum-core/momentum-services/keyvalue-service.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package momentumservices

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

"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
Expand All @@ -28,15 +24,6 @@ func NewKeyValueService(dao *daos.Dao) *KeyValueService {
return keyValueService
}

func (kvs *KeyValueService) SyncFile(n *tree.Node, parentArtifact *models.Record) error {

if n.Kind != tree.File {
return errors.New("can only sync nodes of type file")
}

return kvs.syncChildren(n.Children, parentArtifact, n.NormalizedPath())
}

func (kvs *KeyValueService) GetKeyValueCollection() (*models.Collection, error) {

coll, err := kvs.dao.FindCollectionByNameOrId(consts.TABLE_KEYVALUE_NAME)
Expand All @@ -47,115 +34,6 @@ func (kvs *KeyValueService) GetKeyValueCollection() (*models.Collection, error)
return coll, nil
}

func (kvs *KeyValueService) syncChildren(children []*tree.Node, parentArtifact *models.Record, filename string) error {

var err error = nil

for _, child := range children {

if len(child.Children) > 0 {

kvs.syncChildren(child.Children, parentArtifact, filename)
} else {

if child.Value == "" {
fmt.Println("empty leaf at:", child.FullPath())
break
}

kvColl, err := kvs.GetKeyValueCollection()
if err != nil {
break
}

propertyPath := strings.Split(child.FullPath(), filename)[1]

childRecord := models.NewRecord(kvColl)
childRecord.Set(consts.TABLE_KEYVALUE_FIELD_KEY, propertyPath)
childRecord.Set(consts.TABLE_KEYVALUE_FIELD_VALUE, child.Value)

err = kvs.saveWithoutEvent(childRecord)
if err != nil {
break
}

err = kvs.addParentArtifact(parentArtifact, childRecord)
if err != nil {
break
}

currentKeyValues, ok := parentArtifact.Get(consts.GENERIC_FIELD_KEYVALUES).([]string)
if ok {
parentArtifact.Set(consts.GENERIC_FIELD_KEYVALUES, append(currentKeyValues, childRecord.Id))
} else {
parentArtifact.Set(consts.GENERIC_FIELD_KEYVALUES, childRecord.Id)
}
err = kvs.saveWithoutEvent(parentArtifact)
if err != nil {
break
}
}
}

return err
}

func (kvs *KeyValueService) addParentArtifact(parentArtifact *models.Record, keyValues *models.Record) error {

switch parentArtifact.Collection().Name {
case consts.TABLE_STAGES_NAME:
return kvs.addParentStage(parentArtifact, []*models.Record{keyValues})
case consts.TABLE_DEPLOYMENTS_NAME:
return kvs.addParentDeployment(parentArtifact, []*models.Record{keyValues})
default:
return errors.New("invalid parent record type")
}
}

func (kvs *KeyValueService) addParentStage(stage *models.Record, keyValues []*models.Record) error {

if stage.Collection().Name != consts.TABLE_STAGES_NAME {
return errors.New("parent stage must be record of collection stages")
}

for _, kv := range keyValues {

if kv.Collection().Name != consts.TABLE_KEYVALUE_NAME {
return errors.New("expected keyvalues record type to add parent stage")
}

kv.Set(consts.TABLE_KEYVALUE_FIELD_PARENTSTAGE, stage.Id)
err := kvs.saveWithoutEvent(kv)
if err != nil {
return err
}
}

return nil
}

func (kvs *KeyValueService) addParentDeployment(deployment *models.Record, keyValues []*models.Record) error {

if deployment.Collection().Name != consts.TABLE_DEPLOYMENTS_NAME {
return errors.New("parent deployment must be record of collection deploments")
}

for _, kv := range keyValues {

if kv.Collection().Name != consts.TABLE_KEYVALUE_NAME {
return errors.New("expected keyvalues record type to add parent deployment")
}

kv.Set(consts.TABLE_KEYVALUE_FIELD_PARENTDEPLOYMENT, deployment.Id)
err := kvs.saveWithoutEvent(kv)
if err != nil {
return err
}
}

return nil
}

func (kvs *KeyValueService) saveWithoutEvent(record *models.Record) error {
return kvs.dao.Clone().SaveRecord(record)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package momentumservices
import (
"fmt"
consts "momentum/momentum-core/momentum-config"
tree "momentum/momentum-core/momentum-tree"

"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
)
Expand All @@ -31,54 +29,6 @@ func NewRepositoryService(dao *daos.Dao, appService *ApplicationService) *Reposi
return repositoryService
}

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

appRecords, err := rs.applicationService.SyncApplicationsFromDisk(n, record)
if err != 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 _, applicationRecord := range appRecords {

appRecord, err := rs.dao.FindRecordById(consts.TABLE_APPLICATIONS_NAME, applicationRecord.Id)
if err != nil {
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, 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, nil, err
}

deployments = append(deployments, deploymentRec)
}
}
}

return record, appRecords, deployments, nil
}

func (rs *RepositoryService) FindForName(name string) (*models.Record, error) {

recs, err := rs.dao.FindRecordsByExpr(consts.TABLE_REPOSITORIES_NAME, dbx.NewExp(consts.TABLE_REPOSITORIES_FIELD_NAME+" = {:"+consts.TABLE_REPOSITORIES_FIELD_NAME+"}", dbx.Params{consts.TABLE_REPOSITORIES_FIELD_NAME: name}))
Expand Down
Loading

0 comments on commit b211dc0

Please sign in to comment.