@@ -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 )
@@ -128,9 +130,31 @@ func (s Sync) ApplyDelta(ctx context.Context, delta *Delta) error {
128130 entities = delta .Create .Entities (ctx )
129131 }
130132
133+ var slaTrailEntities chan contracts.Entity
134+ onSuccessHandlers := []OnSuccess [contracts.Entity ]{
135+ OnSuccessIncrement [contracts.Entity ](stat ),
136+ }
137+
138+ switch delta .Subject .Entity ().(type ) {
139+ case * v1.Host , * v1.Service :
140+ slaTrailEntities = make (chan contracts.Entity )
141+ onSuccessHandlers = append (onSuccessHandlers , OnSuccessSendTo [contracts.Entity ](slaTrailEntities ))
142+ }
143+
131144 g .Go (func () error {
132- return s .db .CreateStreamed (ctx , entities , OnSuccessIncrement [contracts.Entity ](stat ))
145+ if slaTrailEntities != nil {
146+ defer close (slaTrailEntities )
147+ }
148+
149+ return s .db .CreateStreamed (ctx , entities , onSuccessHandlers ... )
133150 })
151+
152+ if slaTrailEntities != nil {
153+ 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 ()), ' ' ))
154+ g .Go (func () error {
155+ return s .db .CreateStreamed (ctx , CheckableToSlaTrailEntities (ctx , g , slaTrailEntities , "create" ))
156+ })
157+ }
134158 }
135159
136160 // Update
@@ -160,6 +184,40 @@ func (s Sync) ApplyDelta(ctx context.Context, delta *Delta) error {
160184 // Delete
161185 if len (delta .Delete ) > 0 {
162186 s .logger .Infof ("Deleting %d items of type %s" , len (delta .Delete ), utils .Key (utils .Name (delta .Subject .Entity ()), ' ' ))
187+ entity := delta .Subject .Entity ()
188+ switch entity .(type ) {
189+ case * v1.Host , * v1.Service :
190+ g .Go (func () error {
191+ s .logger .Infof ("Inserting %d items of type %s sla history trails of type delete" , len (delta .Delete ), utils .Key (utils .Name (entity ), ' ' ))
192+
193+ var entities <- chan contracts.Entity
194+ var columns interface {}
195+
196+ if _ , ok := entity .(* v1.Host ); ok {
197+ columns = & SlaHostHistoryTrailColumns {}
198+ } else {
199+ columns = & SlaServiceHistoryTrailColumns {}
200+ }
201+
202+ query := s .db .BuildSelectStmt (entity , columns )
203+ if len (delta .Delete ) == 1 {
204+ query += ` WHERE id = ?`
205+ } else {
206+ var placeholders []string
207+ for i := 0 ; i < len (delta .Delete ); i ++ {
208+ placeholders = append (placeholders , "?" )
209+ }
210+
211+ query += fmt .Sprintf (` WHERE id IN (%s)` , strings .Join (placeholders , `, ` ))
212+ }
213+ var err <- chan error
214+ entities , err = s .db .YieldAll (ctx , delta .Subject .Factory (), query , false , delta .Delete .IDs ()... )
215+ com .ErrgroupReceive (g , err )
216+
217+ return s .db .CreateStreamed (ctx , CheckableToSlaTrailEntities (ctx , g , entities , "delete" ))
218+ })
219+ }
220+
163221 g .Go (func () error {
164222 return s .db .Delete (ctx , delta .Subject .Entity (), delta .Delete .IDs (), OnSuccessIncrement [any ](stat ))
165223 })
@@ -187,7 +245,8 @@ func (s Sync) SyncCustomvars(ctx context.Context) error {
187245
188246 actualCvs , errs := s .db .YieldAll (
189247 ctx , cv .FactoryForDelta (),
190- s .db .BuildSelectStmt (NewScopedEntity (cv .Entity (), e .Meta ()), cv .Entity ().Fingerprint ()), e .Meta (),
248+ s .db .BuildSelectStmt (NewScopedEntity (cv .Entity (), e .Meta ()), cv .Entity ().Fingerprint ()),
249+ true , e .Meta (),
191250 )
192251 com .ErrgroupReceive (g , errs )
193252
@@ -199,7 +258,8 @@ func (s Sync) SyncCustomvars(ctx context.Context) error {
199258
200259 actualFlatCvs , errs := s .db .YieldAll (
201260 ctx , flatCv .FactoryForDelta (),
202- s .db .BuildSelectStmt (NewScopedEntity (flatCv .Entity (), e .Meta ()), flatCv .Entity ().Fingerprint ()), e .Meta (),
261+ s .db .BuildSelectStmt (NewScopedEntity (flatCv .Entity (), e .Meta ()), flatCv .Entity ().Fingerprint ()),
262+ true , e .Meta (),
203263 )
204264 com .ErrgroupReceive (g , errs )
205265
0 commit comments