-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv2.go
790 lines (667 loc) · 21.3 KB
/
v2.go
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
package v2
import (
"errors"
"fmt"
"net/http"
"sort"
"time"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
apiErrors "github.com/stackmon/otc-status-dashboard/internal/api/errors"
"github.com/stackmon/otc-status-dashboard/internal/db"
)
type IncidentID struct {
ID int `json:"id" uri:"id" binding:"required,gte=0"`
}
type IncidentData struct {
Title string `json:"title" binding:"required"`
//TODO: this field only valid for incident creation (legacy), but it should be an additional field in DB.
Description string `json:"description,omitempty"`
// INCIDENT_IMPACTS = {
// 0: Impact(0, "maintenance", "Scheduled maintenance"),
// 1: Impact(1, "minor", "Minor incident (i.e. performance impact)"),
// 2: Impact(2, "major", "Major incident"),
// 3: Impact(3, "outage", "Service outage"),
// }
Impact *int `json:"impact" binding:"required,gte=0,lte=3"`
Components []int `json:"components" binding:"required"`
// Datetime format is standard: "2006-01-01T12:00:00Z"
StartDate time.Time `json:"start_date" binding:"required"`
EndDate *time.Time `json:"end_date,omitempty"`
System *bool `json:"system,omitempty"`
Updates []db.IncidentStatus `json:"updates,omitempty"`
}
type Incident struct {
IncidentID
IncidentData
}
func GetIncidentsHandler(dbInst *db.DB, logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger.Debug("retrieve incidents")
r, err := dbInst.GetIncidents()
if err != nil {
apiErrors.RaiseInternalErr(c, err)
return
}
incidents := make([]*Incident, len(r))
for i, inc := range r {
components := make([]int, len(inc.Components))
for ind, comp := range inc.Components {
components[ind] = int(comp.ID)
}
incidents[i] = &Incident{
IncidentID: IncidentID{int(inc.ID)},
IncidentData: IncidentData{
Title: *inc.Text,
Impact: inc.Impact,
Components: components,
StartDate: *inc.StartDate,
EndDate: inc.EndDate,
System: &inc.System,
Updates: inc.Statuses,
},
}
}
c.JSON(http.StatusOK, gin.H{"data": incidents})
}
}
func GetIncidentHandler(dbInst *db.DB, logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger.Debug("retrieve incident")
var incID IncidentID
if err := c.ShouldBindUri(&incID); err != nil {
apiErrors.RaiseBadRequestErr(c, err)
return
}
r, err := dbInst.GetIncident(incID.ID)
if err != nil {
if errors.Is(err, db.ErrDBIncidentDSNotExist) {
apiErrors.RaiseStatusNotFoundErr(c, apiErrors.ErrIncidentDSNotExist)
return
}
apiErrors.RaiseInternalErr(c, err)
return
}
c.JSON(http.StatusOK, toAPIIncident(r))
}
}
func toAPIIncident(inc *db.Incident) *Incident {
components := make([]int, len(inc.Components))
for i, comp := range inc.Components {
components[i] = int(comp.ID)
}
incData := IncidentData{
Title: *inc.Text,
Impact: inc.Impact,
Components: components,
StartDate: *inc.StartDate,
EndDate: inc.EndDate,
System: &inc.System,
Updates: inc.Statuses,
}
return &Incident{IncidentID{ID: int(inc.ID)}, incData}
}
// TODO: copy-paste from the legacy, it's implemented, but only for API. We should discuss about this functionality.
//
// Process component status update and open new incident if required:
//
// - current active maintenance for the component - do nothing
// - current active incident for the component - do nothing
// - current active incident NOT for the component - add component into
// the list of affected components
// - no active incidents - create new one
// - current active incident for the component and requested
// impact > current impact - run handling:
//
// If a component exists in an incident, but the requested
// impact is higher than the current one, then the component
// will be moved to another incident if it exists with the
// requested impact, otherwise a new incident will be created
// and the component will be moved to the new incident.
// If there is only one component in an incident, and an
// incident with the requested impact does not exist,
// then the impact of the incident will be changed to a higher
// one, otherwise the component will be moved to an existing
// incident with the requested impact, and the current incident
// will be closed by the system.
// The movement of a component and the closure of an incident
// will be reflected in the incident statuses.
//
// TODO: skip this check, will be redesigned after the new incident management
// PostIncidentHandler creates an incident.
func PostIncidentHandler(dbInst *db.DB, logger *zap.Logger) gin.HandlerFunc { //nolint:gocognit
return func(c *gin.Context) {
var incData IncidentData
if err := c.ShouldBindBodyWithJSON(&incData); err != nil {
apiErrors.RaiseBadRequestErr(c, err)
return
}
if err := validateIncidentCreation(incData); err != nil {
apiErrors.RaiseBadRequestErr(c, err)
return
}
log := logger.With(zap.Any("incident", incData))
log.Info("start to prepare for an incident creation")
components := make([]db.Component, len(incData.Components))
for i, comp := range incData.Components {
components[i] = db.Component{ID: uint(comp)}
}
if incData.System == nil {
var system bool
incData.System = &system
}
incIn := db.Incident{
Text: &incData.Title,
StartDate: &incData.StartDate,
EndDate: incData.EndDate,
Impact: incData.Impact,
System: *incData.System,
Components: components,
}
log.Info("get opened incidents")
openedIncidents, err := dbInst.GetIncidents(&db.IncidentsParams{IsOpened: true})
if err != nil {
apiErrors.RaiseInternalErr(c, err)
return
}
if err = createIncident(dbInst, log, &incIn, incData.Description); err != nil {
apiErrors.RaiseInternalErr(c, err)
return
}
if len(openedIncidents) == 0 || *incData.Impact == 0 {
if *incData.Impact == 0 {
log.Info("the incident is maintenance, finish the incident creation")
} else {
log.Info("no opened incidents, finish the incident creation")
}
result := make([]*ProcessComponentResp, len(incIn.Components))
for i, comp := range incIn.Components {
result[i] = &ProcessComponentResp{
ComponentID: int(comp.ID),
IncidentID: int(incIn.ID),
}
}
c.JSON(http.StatusOK, PostIncidentResp{Result: result})
return
}
log.Info("start to analyse component movement")
result := make([]*ProcessComponentResp, 0)
// holly shit, but it moved from original logic
for _, comp := range incIn.Components {
compResult := &ProcessComponentResp{
ComponentID: int(comp.ID),
}
for _, inc := range openedIncidents {
for _, incComp := range inc.Components {
if comp.ID == incComp.ID {
log.Info("found the component in the opened incident", zap.Any("component", comp), zap.Any("incident", inc))
var closeInc bool
if len(inc.Components) == 1 {
closeInc = true
}
incident, errRes := dbInst.MoveComponentFromOldToAnotherIncident(&comp, inc, &incIn, closeInc)
if errRes != nil {
apiErrors.RaiseInternalErr(c, err)
return
}
compResult.IncidentID = int(incident.ID)
}
}
}
if compResult.IncidentID == 0 {
log.Info("there are no any opened incidents for given component, return created incident")
compResult.IncidentID = int(incIn.ID)
}
result = append(result, compResult)
}
c.JSON(http.StatusOK, PostIncidentResp{Result: result})
}
}
type PostIncidentResp struct {
Result []*ProcessComponentResp `json:"result"`
}
type ProcessComponentResp struct {
ComponentID int `json:"component_id"`
IncidentID int `json:"incident_id,omitempty"`
Error string `json:"error,omitempty"`
}
func validateIncidentCreation(incData IncidentData) error {
if *incData.Impact != 0 && incData.EndDate != nil {
return apiErrors.ErrIncidentEndDateShouldBeEmpty
}
if len(incData.Updates) != 0 {
return apiErrors.ErrIncidentUpdatesShouldBeEmpty
}
return nil
}
func createIncident(dbInst *db.DB, log *zap.Logger, inc *db.Incident, description string) error {
log.Info("start to create an incident")
id, err := dbInst.SaveIncident(inc)
if err != nil {
return err
}
inc.ID = id
if *inc.Impact == 0 && description != "" {
log.Info("the incident is maintenance for component, add description")
inc.Statuses = append(inc.Statuses, db.IncidentStatus{
IncidentID: inc.ID,
// TODO: add another status for this action, legacy
Status: "description",
Text: description,
Timestamp: time.Now().UTC(),
})
err = dbInst.ModifyIncident(inc)
if err != nil {
return err
}
}
return nil
}
type PatchIncidentData struct {
Title *string `json:"title,omitempty"`
Impact *int `json:"impact,omitempty"`
Message string `json:"message" binding:"required"`
Status string `json:"status" binding:"required"`
UpdateDate time.Time `json:"update_date" binding:"required"`
StartDate *time.Time `json:"start_date,omitempty"`
EndDate *time.Time `json:"end_date,omitempty"`
}
func PatchIncidentHandler(dbInst *db.DB, logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger.Debug("update incident")
var incID IncidentID
if err := c.ShouldBindUri(&incID); err != nil {
apiErrors.RaiseBadRequestErr(c, err)
return
}
var incData PatchIncidentData
if err := c.ShouldBindBodyWithJSON(&incData); err != nil {
apiErrors.RaiseBadRequestErr(c, err)
return
}
storedIncident, err := dbInst.GetIncident(incID.ID)
if err != nil {
apiErrors.RaiseInternalErr(c, err)
return
}
if err = checkPatchData(&incData, storedIncident); err != nil {
apiErrors.RaiseBadRequestErr(c, err)
return
}
updateFields(&incData, storedIncident)
status := db.IncidentStatus{
IncidentID: storedIncident.ID,
Status: incData.Status,
Text: incData.Message,
Timestamp: incData.UpdateDate,
}
storedIncident.Statuses = append(storedIncident.Statuses, status)
err = dbInst.ModifyIncident(storedIncident)
if err != nil {
apiErrors.RaiseInternalErr(c, err)
return
}
if incData.Status == IncidentReopened {
err = dbInst.ReOpenIncident(storedIncident)
if err != nil {
apiErrors.RaiseInternalErr(c, err)
return
}
}
inc, errDB := dbInst.GetIncident(int(storedIncident.ID))
if errDB != nil {
apiErrors.RaiseInternalErr(c, errDB)
return
}
c.JSON(http.StatusOK, toAPIIncident(inc))
}
}
func checkPatchData(incoming *PatchIncidentData, stored *db.Incident) error {
if *stored.Impact == 0 {
if incoming.Impact != nil && *incoming.Impact != 0 {
return apiErrors.ErrIncidentPatchMaintenanceImpactForbidden
}
if _, ok := maintenanceStatuses[incoming.Status]; !ok {
return apiErrors.ErrIncidentPatchMaintenanceStatus
}
return nil
}
if stored.EndDate != nil {
if _, ok := incidentClosedStatuses[incoming.Status]; !ok {
return apiErrors.ErrIncidentPatchClosedStatus
}
if (incoming.StartDate != nil || incoming.EndDate != nil) && incoming.Status != IncidentChanged {
return apiErrors.ErrIncidentPatchClosedStatus
}
return nil
}
if (incoming.Impact != nil && *incoming.Impact != *stored.Impact) && incoming.Status != IncidentImpactChanged {
return apiErrors.ErrIncidentPatchImpactStatusWrong
}
if incoming.Impact != nil && *incoming.Impact != *stored.Impact && *incoming.Impact == 0 {
return apiErrors.ErrIncidentPatchImpactToMaintenanceForbidden
}
if _, ok := incidentOpenStatuses[incoming.Status]; !ok {
return apiErrors.ErrIncidentPatchStatus
}
if incoming.StartDate != nil {
return apiErrors.ErrIncidentPatchOpenedStartDate
}
return nil
}
func updateFields(income *PatchIncidentData, stored *db.Incident) {
if *stored.Impact == 0 || stored.EndDate != nil {
if income.StartDate != nil {
stored.StartDate = income.StartDate
}
if income.EndDate != nil {
stored.EndDate = income.EndDate
}
}
if income.Title != nil {
stored.Text = income.Title
}
if income.Impact != nil {
stored.Impact = income.Impact
}
if income.Status == IncidentReopened {
stored.EndDate = nil
}
if income.Status == IncidentResolved {
stored.EndDate = &income.UpdateDate
}
}
type Component struct {
ComponentID
Attributes []ComponentAttribute `json:"attributes"`
Name string `json:"name"`
}
type ComponentAvailability struct {
ComponentID
Name string `json:"name"`
Availability []MonthlyAvailability `json:"availability"`
Region string `json:"region"`
}
type ComponentID struct {
ID int `json:"id" uri:"id" binding:"required,gte=0"`
}
// ComponentAttribute provides additional attributes for component.
// Available list of possible attributes are:
// 1. type
// 2. region
// 3. category
// All of them are required for creation.
type ComponentAttribute struct {
Name string `json:"name"`
Value string `json:"value"`
}
var availableAttrs = map[string]struct{}{ //nolint:gochecknoglobals
"type": {},
"region": {},
"category": {},
}
type MonthlyAvailability struct {
Year int `json:"year"`
Month int `json:"month"` // Number of the month (1 - 12)
Percentage float64 `json:"percentage"` // Percent (0 - 100 / example: 95.23478)
}
func GetComponentsHandler(dbInst *db.DB, logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger.Debug("retrieve components")
r, err := dbInst.GetComponentsWithValues()
if err != nil {
apiErrors.RaiseInternalErr(c, err)
return
}
c.JSON(http.StatusOK, r)
}
}
func GetComponentHandler(dbInst *db.DB, logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger.Debug("retrieve component")
var compID ComponentID
if err := c.ShouldBindUri(&compID); err != nil {
apiErrors.RaiseBadRequestErr(c, apiErrors.ErrComponentInvalidFormat)
return
}
r, err := dbInst.GetComponent(compID.ID)
if err != nil {
if errors.Is(err, db.ErrDBComponentDSNotExist) {
apiErrors.RaiseStatusNotFoundErr(c, apiErrors.ErrComponentDSNotExist)
return
}
apiErrors.RaiseInternalErr(c, err)
return
}
c.JSON(http.StatusOK, r)
}
}
type PostComponentData struct {
Attributes []ComponentAttribute `json:"attrs" binding:"required"`
Name string `json:"name" binding:"required"`
}
// PostComponentHandler creates a new component.
func PostComponentHandler(dbInst *db.DB, logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger.Debug("create a component")
var component PostComponentData
if err := c.ShouldBindBodyWithJSON(&component); err != nil {
apiErrors.RaiseBadRequestErr(c, err)
return
}
if err := checkComponentAttrs(component.Attributes); err != nil {
apiErrors.RaiseBadRequestErr(c, err)
return
}
attrs := make([]db.ComponentAttr, len(component.Attributes))
for i, attr := range component.Attributes {
attrs[i] = db.ComponentAttr{
Name: attr.Name,
Value: attr.Value,
}
}
compDB := &db.Component{
Name: component.Name,
Attrs: attrs,
}
componentID, err := dbInst.SaveComponent(compDB)
if err != nil {
if errors.Is(err, db.ErrDBComponentExists) {
apiErrors.RaiseBadRequestErr(c, apiErrors.ErrComponentExist)
}
apiErrors.RaiseInternalErr(c, err)
return
}
c.JSON(http.StatusCreated, Component{
ComponentID: ComponentID{int(componentID)},
Attributes: component.Attributes,
Name: component.Name,
})
}
}
func checkComponentAttrs(attrs []ComponentAttribute) error {
//nolint:nolintlint,mnd
// Check total number of attributes
// this magic number will be changed in the next iteration
if len(attrs) != 3 {
return apiErrors.ErrComponentAttrInvalidFormat
}
// Track seen attribute names to detect duplicates
seen := make(map[string]bool)
// Verify all required attributes exist exactly once
for _, attr := range attrs {
if _, exists := availableAttrs[attr.Name]; !exists {
return apiErrors.ErrComponentAttrInvalidFormat
}
// Check for duplicate attributes
if seen[attr.Name] {
return apiErrors.ErrComponentAttrInvalidFormat
}
seen[attr.Name] = true
}
// Verify all required attributes were found
for requiredAttr := range availableAttrs {
if !seen[requiredAttr] {
return apiErrors.ErrComponentAttrInvalidFormat
}
}
return nil
}
func GetComponentsAvailabilityHandler(dbInst *db.DB, logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger.Debug("retrieve availability of components")
components, err := dbInst.GetComponentsWithIncidents()
if err != nil {
apiErrors.RaiseInternalErr(c, err)
return
}
availability := make([]*ComponentAvailability, len(components))
for index, comp := range components {
attrs := make([]ComponentAttribute, len(comp.Attrs))
for i, attr := range comp.Attrs {
attrs[i] = ComponentAttribute{
Name: attr.Name,
Value: attr.Value,
}
}
regionValue := ""
for _, attr := range attrs {
if attr.Name == "region" {
regionValue = attr.Value
break
}
}
incidents := make([]*Incident, len(comp.Incidents))
for i, inc := range comp.Incidents {
newInc := &Incident{
IncidentID: IncidentID{int(inc.ID)},
IncidentData: IncidentData{
Title: *inc.Text,
Impact: inc.Impact,
StartDate: *inc.StartDate,
EndDate: inc.EndDate,
Updates: nil,
},
}
incidents[i] = newInc
}
compAvailability, calcErr := calculateAvailability(&comp)
if calcErr != nil {
apiErrors.RaiseInternalErr(c, calcErr)
return
}
sortComponentAvailability(compAvailability)
availability[index] = &ComponentAvailability{
ComponentID: ComponentID{int(comp.ID)},
Region: regionValue,
Name: comp.Name,
Availability: compAvailability,
}
}
c.JSON(http.StatusOK, gin.H{"data": availability})
}
}
func sortComponentAvailability(availabilities []MonthlyAvailability) {
sort.Slice(availabilities, func(i, j int) bool {
if availabilities[i].Year == availabilities[j].Year {
return availabilities[i].Month > availabilities[j].Month
}
return availabilities[i].Year > availabilities[j].Year
})
}
// TODO: add filters for GET request
func calculateAvailability(component *db.Component) ([]MonthlyAvailability, error) {
const (
monthsInYear = 12
precisionFactor = 100000
fullPercentage = 100
availabilityMonths = 11
roundFactor = 0.5
)
if component == nil {
return nil, fmt.Errorf("component is nil")
}
if len(component.Incidents) == 0 {
return nil, nil
}
periodEndDate := time.Now()
// Get the current date and starting point (12 months ago)
// a year ago, including current the month
periodStartDate := time.Date(periodEndDate.Year(), periodEndDate.Month(),
1, 0, 0, 0, 0, time.UTC).AddDate(0, -availabilityMonths, 0)
monthlyDowntime := make([]float64, monthsInYear) // 12 months
for _, inc := range component.Incidents {
if inc.EndDate == nil || (*inc.Impact != 3 && *inc.Impact != 2) {
continue
}
// here we skip all incidents that are not correspond to our period
// if the incident started before availability period
// (as example the incident was started at 01:00 31/12 and finished at 02:00 01/01),
// we cut the beginning to the period start date, and do the same for the period ending
incidentStart, incidentEnd, valid := adjustIncidentPeriod(
*inc.StartDate,
*inc.EndDate,
periodStartDate,
periodEndDate,
)
if !valid {
continue
}
current := incidentStart
for current.Before(incidentEnd) {
monthStart := time.Date(current.Year(), current.Month(), 1, 0, 0, 0, 0, time.UTC)
monthEnd := monthStart.AddDate(0, 1, 0)
downtimeStart := maxTime(incidentStart, monthStart)
downtimeEnd := minTime(incidentEnd, monthEnd)
downtime := downtimeEnd.Sub(downtimeStart).Hours()
monthIndex := (downtimeStart.Year()-periodStartDate.Year())*monthsInYear +
int(downtimeStart.Month()-periodStartDate.Month())
if monthIndex >= 0 && monthIndex < len(monthlyDowntime) {
monthlyDowntime[monthIndex] += downtime
}
current = monthEnd
}
}
monthlyAvailability := make([]MonthlyAvailability, 0, monthsInYear)
for i := range [monthsInYear]int{} {
monthDate := periodStartDate.AddDate(0, i, 0)
totalHours := hoursInMonth(monthDate.Year(), int(monthDate.Month()))
availability := fullPercentage - (monthlyDowntime[i] / totalHours * fullPercentage)
availability = float64(int(availability*precisionFactor+roundFactor)) / precisionFactor
monthlyAvailability = append(monthlyAvailability, MonthlyAvailability{
Year: monthDate.Year(),
Month: int(monthDate.Month()),
Percentage: availability,
})
}
return monthlyAvailability, nil
}
func adjustIncidentPeriod(incidentStart, incidentEnd, periodStart, periodEnd time.Time) (time.Time, time.Time, bool) {
if incidentEnd.Before(periodStart) || incidentStart.After(periodEnd) {
return time.Time{}, time.Time{}, false
}
if incidentStart.Before(periodStart) {
incidentStart = periodStart
}
if incidentEnd.After(periodEnd) {
incidentEnd = periodEnd
}
return incidentStart, incidentEnd, true
}
func minTime(start, end time.Time) time.Time {
if start.Before(end) {
return start
}
return end
}
func maxTime(start, end time.Time) time.Time {
if start.After(end) {
return start
}
return end
}
func hoursInMonth(year int, month int) float64 {
firstDay := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
nextMonth := firstDay.AddDate(0, 1, 0)
return float64(nextMonth.Sub(firstDay).Hours())
}