@@ -15,6 +15,7 @@ import (
1515 "go.uber.org/zap"
1616 "golang.org/x/sync/errgroup"
1717 "runtime"
18+ "strings"
1819 "time"
1920)
2021
@@ -85,7 +86,8 @@ func (s Sync) Sync(ctx context.Context, subject *common.SyncSubject) error {
8586
8687 actual , dbErrs := s .db .YieldAll (
8788 ctx , subject .FactoryForDelta (),
88- s .db .BuildSelectStmt (NewScopedEntity (subject .Entity (), e .Meta ()), subject .Entity ().Fingerprint ()), e .Meta (),
89+ s .db .BuildSelectStmt (NewScopedEntity (subject .Entity (), e .Meta ()), subject .Entity ().Fingerprint ()),
90+ true , e .Meta (),
8991 )
9092 // Let errors from DB cancel our group.
9193 com .ErrgroupReceive (g , dbErrs )
@@ -106,6 +108,8 @@ func (s Sync) ApplyDelta(ctx context.Context, delta *Delta) error {
106108 g , ctx := errgroup .WithContext (ctx )
107109 stat := getCounterForEntity (delta .Subject .Entity ())
108110
111+ var subjectType = delta .Subject .Entity ()
112+
109113 // Create
110114 if len (delta .Create ) > 0 {
111115 s .logger .Infof ("Inserting %d items of type %s" , len (delta .Create ), utils .Key (utils .Name (delta .Subject .Entity ()), ' ' ))
@@ -128,9 +132,31 @@ func (s Sync) ApplyDelta(ctx context.Context, delta *Delta) error {
128132 entities = delta .Create .Entities (ctx )
129133 }
130134
135+ var slaTrailEntities chan contracts.Entity
136+ onSuccessHandlers := []OnSuccess [contracts.Entity ]{
137+ OnSuccessIncrement [contracts.Entity ](stat ),
138+ }
139+
140+ switch subjectType .(type ) {
141+ case * v1.Host , * v1.Service :
142+ slaTrailEntities = make (chan contracts.Entity )
143+ onSuccessHandlers = append (onSuccessHandlers , OnSuccessSendTo [contracts.Entity ](slaTrailEntities ))
144+ }
145+
131146 g .Go (func () error {
132- return s .db .CreateStreamed (ctx , entities , OnSuccessIncrement [contracts.Entity ](stat ))
147+ if slaTrailEntities != nil {
148+ defer close (slaTrailEntities )
149+ }
150+
151+ return s .db .CreateStreamed (ctx , entities , onSuccessHandlers ... )
133152 })
153+
154+ if slaTrailEntities != nil {
155+ s .logger .Infof ("Inserting %d items of type %s sla history trails of type create" , len (delta .Create ), utils .Key (utils .Name (delta .Subject .Entity ()), ' ' ))
156+ g .Go (func () error {
157+ return s .db .CreateStreamed (ctx , CheckableToSlaTrailEntities (ctx , g , slaTrailEntities , "create" ))
158+ })
159+ }
134160 }
135161
136162 // Update
@@ -160,6 +186,40 @@ func (s Sync) ApplyDelta(ctx context.Context, delta *Delta) error {
160186 // Delete
161187 if len (delta .Delete ) > 0 {
162188 s .logger .Infof ("Deleting %d items of type %s" , len (delta .Delete ), utils .Key (utils .Name (delta .Subject .Entity ()), ' ' ))
189+ switch subjectType .(type ) {
190+ case * v1.Host , * v1.Service :
191+ g .Go (func () error {
192+ var entities <- chan contracts.Entity
193+ var columns interface {}
194+
195+ if _ , ok := subjectType .(* v1.Host ); ok {
196+ columns = & SlaHostHistoryTrailColumns {}
197+ } else {
198+ columns = & SlaServiceHistoryTrailColumns {}
199+ }
200+
201+ var placeholders []string
202+ for i := 0 ; i < len (delta .Delete ); i ++ {
203+ // scopes = append(scopes, id.(types.Binary))
204+ placeholders = append (placeholders , "?" )
205+ }
206+
207+ query := s .db .BuildSelectStmt (delta .Subject .Entity (), columns )
208+ if placeholders != nil {
209+ query += fmt .Sprintf (` WHERE id IN (%s)` , strings .Join (placeholders , `, ` ))
210+ }
211+
212+ s .logger .Infof ("Halloo QUERY: %s" , query )
213+
214+ var err <- chan error
215+ entities , err = s .db .YieldAll (ctx , delta .Subject .Factory (), query , false , delta .Delete .IDs ()... )
216+ com .ErrgroupReceive (g , err )
217+
218+ s .logger .Infof ("Inserting %d items of type %s sla history trails of type delete" , len (delta .Delete ), utils .Key (utils .Name (delta .Subject .Entity ()), ' ' ))
219+ return s .db .CreateStreamed (ctx , CheckableToSlaTrailEntities (ctx , g , entities , "delete" ))
220+ })
221+ }
222+
163223 g .Go (func () error {
164224 return s .db .Delete (ctx , delta .Subject .Entity (), delta .Delete .IDs (), OnSuccessIncrement [any ](stat ))
165225 })
@@ -187,7 +247,8 @@ func (s Sync) SyncCustomvars(ctx context.Context) error {
187247
188248 actualCvs , errs := s .db .YieldAll (
189249 ctx , cv .FactoryForDelta (),
190- s .db .BuildSelectStmt (NewScopedEntity (cv .Entity (), e .Meta ()), cv .Entity ().Fingerprint ()), e .Meta (),
250+ s .db .BuildSelectStmt (NewScopedEntity (cv .Entity (), e .Meta ()), cv .Entity ().Fingerprint ()),
251+ true , e .Meta (),
191252 )
192253 com .ErrgroupReceive (g , errs )
193254
@@ -199,7 +260,8 @@ func (s Sync) SyncCustomvars(ctx context.Context) error {
199260
200261 actualFlatCvs , errs := s .db .YieldAll (
201262 ctx , flatCv .FactoryForDelta (),
202- s .db .BuildSelectStmt (NewScopedEntity (flatCv .Entity (), e .Meta ()), flatCv .Entity ().Fingerprint ()), e .Meta (),
263+ s .db .BuildSelectStmt (NewScopedEntity (flatCv .Entity (), e .Meta ()), flatCv .Entity ().Fingerprint ()),
264+ true , e .Meta (),
203265 )
204266 com .ErrgroupReceive (g , errs )
205267
0 commit comments