-
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
7 changed files
with
240 additions
and
6 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
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,89 @@ | ||
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, ok := v1.EnvironmentFromContext(ctx) | ||
if !ok { | ||
return errors.New("can't get environment from context") | ||
} | ||
|
||
// 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.Id}, | ||
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 | ||
} | ||
|
||
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
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