-
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.
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 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,79 @@ | ||
package icingadb | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"github.com/icinga/icingadb/pkg/com" | ||
"github.com/icinga/icingadb/pkg/contracts" | ||
v1 "github.com/icinga/icingadb/pkg/icingadb/v1" | ||
"github.com/icinga/icingadb/pkg/types" | ||
"github.com/icinga/icingadb/pkg/utils" | ||
"github.com/pkg/errors" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type SlaHistoryTrail struct { | ||
v1.EntityWithoutChecksum `json:",inline"` | ||
EnvironmentId types.Binary `json:"environment_id"` | ||
ObjectType string `json:"object_type"` | ||
HostId types.Binary `json:"host_id"` | ||
ServiceId types.Binary `json:"service_id"` | ||
EventTime types.UnixMilli `json:"event_time"` | ||
EventType string `json:"event_type"` | ||
} | ||
|
||
func CheckableToSlaTrailEntities(ctx context.Context, checkables <-chan contracts.Entity, eventType string) (<-chan contracts.Entity, <-chan error) { | ||
entities := make(chan contracts.Entity) | ||
g, ctx := errgroup.WithContext(ctx) | ||
|
||
g.Go(func() error { | ||
defer close(entities) | ||
|
||
for { | ||
select { | ||
case checkable, ok := <-checkables: | ||
if !ok { | ||
return nil | ||
} | ||
|
||
id, err := generateBinaryId() | ||
if err != nil { | ||
return errors.Wrap(err, "can't generate sla history trail ID") | ||
} | ||
|
||
entity := &SlaHistoryTrail{ | ||
EntityWithoutChecksum: v1.EntityWithoutChecksum{ | ||
IdMeta: v1.IdMeta{Id: id}, | ||
}, | ||
ObjectType: utils.Name(checkable), | ||
EventTime: types.UnixMilli{}, | ||
EventType: eventType, | ||
} | ||
|
||
switch ptr := checkable.(type) { | ||
case *v1.Host: | ||
entity.HostId = ptr.Id | ||
entity.EnvironmentId = ptr.EnvironmentId | ||
case *v1.Service: | ||
entity.HostId = ptr.HostId | ||
entity.ServiceId = ptr.Id | ||
entity.EnvironmentId = ptr.EnvironmentId | ||
} | ||
|
||
entities <- entity | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} | ||
}) | ||
|
||
return entities, com.WaitAsync(g) | ||
} | ||
|
||
// GenerateBinaryId generates a 20 byte length random id | ||
func generateBinaryId() (types.Binary, error) { | ||
id := make([]byte, 20) | ||
_, err := rand.Read(id) | ||
|
||
return id, err | ||
} |
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