-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write
create
& delete
sla events when initially syncing configs
- Loading branch information
Showing
4 changed files
with
233 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package icingadb | ||
|
||
import ( | ||
"context" | ||
"github.com/icinga/icingadb/pkg/contracts" | ||
v1 "github.com/icinga/icingadb/pkg/icingadb/v1" | ||
"github.com/icinga/icingadb/pkg/types" | ||
"github.com/pkg/errors" | ||
"golang.org/x/sync/errgroup" | ||
"time" | ||
) | ||
|
||
type SlaHistoryTrail struct { | ||
Id types.Int `json:"id" db:"-"` | ||
v1.EnvironmentMeta `json:",inline"` | ||
HostId types.Binary `json:"host_id"` | ||
ServiceId types.Binary `json:"service_id"` | ||
EventType string `json:"event_type"` | ||
EventTime types.UnixMilli `json:"event_time"` | ||
} | ||
|
||
// Fingerprint implements the contracts.Fingerprinter interface. | ||
func (sht SlaHistoryTrail) Fingerprint() contracts.Fingerprinter { | ||
return sht | ||
} | ||
|
||
// ID implements part of the contracts.IDer interface. | ||
func (sht SlaHistoryTrail) ID() contracts.ID { | ||
return sht.Id | ||
} | ||
|
||
// SetID implements part of the contracts.IDer interface. | ||
func (sht *SlaHistoryTrail) SetID(id contracts.ID) { | ||
sht.Id = id.(types.Int) | ||
} | ||
|
||
type SlaServiceHistoryTrailColumns struct { | ||
v1.EntityWithoutChecksum `json:",inline"` | ||
HostId types.Binary `json:"host_id"` | ||
} | ||
|
||
func CheckableToSlaTrailEntities(ctx context.Context, g *errgroup.Group, checkables <-chan contracts.Entity, eventType string) <-chan contracts.Entity { | ||
entities := make(chan contracts.Entity, 1) | ||
|
||
g.Go(func() error { | ||
defer close(entities) | ||
|
||
env, err := getEnvironmentId(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Use the same event time for all chackables | ||
now := time.Now() | ||
|
||
for { | ||
select { | ||
case checkable, ok := <-checkables: | ||
if !ok { | ||
return nil | ||
} | ||
|
||
entity := &SlaHistoryTrail{ | ||
EnvironmentMeta: v1.EnvironmentMeta{EnvironmentId: env}, | ||
EventType: eventType, | ||
EventTime: types.UnixMilli(now), | ||
} | ||
|
||
switch ptr := checkable.(type) { | ||
case *v1.Host: | ||
entity.HostId = ptr.Id | ||
case *v1.Service: | ||
entity.HostId = ptr.HostId | ||
entity.ServiceId = ptr.Id | ||
} | ||
|
||
entities <- entity | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} | ||
}) | ||
|
||
return entities | ||
} | ||
|
||
// HostIdsToSlaHistoryTrail transforms the IDs from the passed channel into sla history trail struct | ||
// and streams them into a returned channel. | ||
func HostIdsToSlaHistoryTrail(ctx context.Context, g *errgroup.Group, ids <-chan any, eventType string) <-chan contracts.Entity { | ||
entities := make(chan contracts.Entity, 1) | ||
g.Go(func() error { | ||
defer close(entities) | ||
|
||
env, err := getEnvironmentId(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Use the same event time for all hosts | ||
now := time.Now() | ||
|
||
for { | ||
select { | ||
case id, ok := <-ids: | ||
if !ok { | ||
return nil | ||
} | ||
|
||
entities <- &SlaHistoryTrail{ | ||
EnvironmentMeta: v1.EnvironmentMeta{EnvironmentId: env}, | ||
HostId: id.(types.Binary), | ||
EventType: eventType, | ||
EventTime: types.UnixMilli(now), | ||
} | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} | ||
}) | ||
|
||
return entities | ||
} | ||
|
||
// Get environment id from the given context | ||
func getEnvironmentId(ctx context.Context) (types.Binary, error) { | ||
env, ok := v1.EnvironmentFromContext(ctx) | ||
if !ok { | ||
return nil, errors.New("can't get environment from context") | ||
} | ||
|
||
return env.Id, nil | ||
} | ||
|
||
// Assert interface compliance. | ||
var ( | ||
_ contracts.Entity = (*SlaHistoryTrail)(nil) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters