summaryrefslogtreecommitdiffstats
path: root/pkg/icingadb/v1/history/downtime.go
blob: 99f93f6a9f76d52ef41484ac74153b4ae82c08b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package history

import (
	"database/sql/driver"
	"github.com/icinga/icingadb/pkg/contracts"
	"github.com/icinga/icingadb/pkg/types"
)

type DowntimeHistoryEntity struct {
	DowntimeId types.Binary `json:"downtime_id"`
}

// Fingerprint implements part of the contracts.Entity interface.
func (dhe DowntimeHistoryEntity) Fingerprint() contracts.Fingerprinter {
	return dhe
}

// ID implements part of the contracts.Entity interface.
func (dhe DowntimeHistoryEntity) ID() contracts.ID {
	return dhe.DowntimeId
}

// SetID implements part of the contracts.Entity interface.
func (dhe *DowntimeHistoryEntity) SetID(id contracts.ID) {
	dhe.DowntimeId = id.(types.Binary)
}

type DowntimeHistoryUpserter struct {
	CancelledBy      types.String    `json:"cancelled_by"`
	HasBeenCancelled types.Bool      `json:"has_been_cancelled"`
	CancelTime       types.UnixMilli `json:"cancel_time"`
}

// Upsert implements the contracts.Upserter interface.
func (dhu *DowntimeHistoryUpserter) Upsert() interface{} {
	return dhu
}

type DowntimeHistory struct {
	DowntimeHistoryEntity   `json:",inline"`
	HistoryTableMeta        `json:",inline"`
	DowntimeHistoryUpserter `json:",inline"`
	TriggeredById           types.Binary    `json:"triggered_by_id"`
	ParentId                types.Binary    `json:"parent_id"`
	EntryTime               types.UnixMilli `json:"entry_time"`
	Author                  string          `json:"author"`
	Comment                 string          `json:"comment"`
	IsFlexible              types.Bool      `json:"is_flexible"`
	FlexibleDuration        uint64          `json:"flexible_duration"`
	ScheduledStartTime      types.UnixMilli `json:"scheduled_start_time"`
	ScheduledEndTime        types.UnixMilli `json:"scheduled_end_time"`
	StartTime               types.UnixMilli `json:"start_time"`
	EndTime                 types.UnixMilli `json:"end_time"`
	ScheduledBy             types.String    `json:"scheduled_by"`
	TriggerTime             types.UnixMilli `json:"trigger_time"`
}

type HistoryDowntime struct {
	HistoryMeta       `json:",inline"`
	DowntimeHistoryId types.Binary `json:"downtime_id"`

	// Idea: read StartTime, CancelTime, EndTime and HasBeenCancelled from Redis
	// and let EventTime decide based on HasBeenCancelled which of the others to write to MySQL.
	// So EventTime doesn't have to be read from Redis (json:"-")
	// and the others don't have to be written to MySQL (db:"-").
	StartTime        types.UnixMilli   `json:"start_time" db:"-"`
	CancelTime       types.UnixMilli   `json:"cancel_time" db:"-"`
	EndTime          types.UnixMilli   `json:"end_time" db:"-"`
	HasBeenCancelled types.Bool        `json:"has_been_cancelled" db:"-"`
	EventTime        DowntimeEventTime `json:"-"`
}

// Init implements the contracts.Initer interface.
func (h *HistoryDowntime) Init() {
	h.EventTime.History = h
}

// TableName implements the contracts.TableNamer interface.
func (*HistoryDowntime) TableName() string {
	return "history"
}

type SlaHistoryDowntime struct {
	DowntimeHistoryEntity      `json:",inline"`
	HistoryTableMeta           `json:",inline"`
	SlaHistoryDowntimeUpserter `json:",inline"`
	DowntimeStart              types.UnixMilli `json:"start_time"`
	HasBeenCancelled           types.Bool      `json:"has_been_cancelled" db:"-"`
	CancelTime                 types.UnixMilli `json:"cancel_time" db:"-"`
	EndTime                    types.UnixMilli `json:"end_time" db:"-"`
}

// Init implements the contracts.Initer interface.
func (s *SlaHistoryDowntime) Init() {
	s.DowntimeEnd.History = s
}

type SlaHistoryDowntimeUpserter struct {
	DowntimeEnd SlaDowntimeEndTime `json:"-"`
}

// Upsert implements the contracts.Upserter interface.
func (h *SlaHistoryDowntimeUpserter) Upsert() interface{} {
	return h
}

type DowntimeEventTime struct {
	History *HistoryDowntime `db:"-"`
}

// Value implements the driver.Valuer interface.
// Supports SQL NULL.
func (et DowntimeEventTime) Value() (driver.Value, error) {
	if et.History == nil {
		return nil, nil
	}

	switch et.History.EventType {
	case "downtime_start":
		return et.History.StartTime.Value()
	case "downtime_end":
		if !et.History.HasBeenCancelled.Valid {
			return nil, nil
		}

		if et.History.HasBeenCancelled.Bool {
			return et.History.CancelTime.Value()
		} else {
			return et.History.EndTime.Value()
		}
	default:
		return nil, nil
	}
}

type SlaDowntimeEndTime struct {
	History *SlaHistoryDowntime `db:"-"`
}

// Value implements the driver.Valuer interface.
func (et SlaDowntimeEndTime) Value() (driver.Value, error) {
	if et.History.HasBeenCancelled.Valid && et.History.HasBeenCancelled.Bool {
		return et.History.CancelTime.Value()
	} else {
		return et.History.EndTime.Value()
	}
}

// Assert interface compliance.
var (
	_ contracts.Entity     = (*DowntimeHistoryEntity)(nil)
	_ contracts.Upserter   = (*DowntimeHistoryUpserter)(nil)
	_ UpserterEntity       = (*DowntimeHistory)(nil)
	_ contracts.Initer     = (*HistoryDowntime)(nil)
	_ contracts.TableNamer = (*HistoryDowntime)(nil)
	_ UpserterEntity       = (*HistoryDowntime)(nil)
	_ contracts.Initer     = (*SlaHistoryDowntime)(nil)
	_ UpserterEntity       = (*SlaHistoryDowntime)(nil)
	_ driver.Valuer        = DowntimeEventTime{}
	_ driver.Valuer        = SlaDowntimeEndTime{}
)