-
Notifications
You must be signed in to change notification settings - Fork 23
/
structs.go
170 lines (151 loc) · 5.11 KB
/
structs.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
/*
* === This file is part of ALICE O² ===
*
* Copyright 2021-2024 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package dcs
import (
"fmt"
"strings"
dcspb "github.com/AliceO2Group/Control/core/integration/dcs/protos"
)
type PartitionInfo struct {
SorSuccessful bool
Detectors ECSDetectorInfoMap
}
type ECSDetectorInfo struct {
State string
Timestamp string
AllowedRunTypes []string
PfrAvailability string
SorAvailability string
}
func (d DCSDetectors) ToStringSlice() (sslice []string) {
if d == nil {
return
}
sslice = make([]string, len(d))
if len(d) == 0 {
return
}
for i, det := range d {
sslice[i] = det.String()
}
return
}
func (d DCSDetectors) EcsDetectorsSlice() (sslice []string) {
if d == nil {
return
}
sslice = make([]string, len(d))
if len(d) == 0 {
return
}
for i, det := range d {
sslice[i] = dcsToEcsDetector(det)
}
return
}
func (dsm DCSDetectorOpAvailabilityMap) ToStringMap() (sm map[string]string) {
sm = make(map[string]string)
for det, detState := range dsm {
sm[det.String()] = detState.String()
}
return
}
func (dsm DCSDetectorOpAvailabilityMap) EcsDetectorsMap() (sm map[string]string) {
sm = make(map[string]string)
for det, detState := range dsm {
sm[dcsToEcsDetector(det)] = detState.String()
}
return
}
func (dsm DCSDetectorOpAvailabilityMap) makeDetectorsByStateMap() map[dcspb.DetectorState]DCSDetectors {
detectorsByState := make(map[dcspb.DetectorState]DCSDetectors)
for det, detState := range dsm {
if _, ok := detectorsByState[detState]; !ok {
detectorsByState[detState] = make([]dcspb.Detector, 0)
}
detectorsByState[detState] = append(detectorsByState[detState], det)
}
return detectorsByState
}
// Returns true if the provided detectors are either all in conditionState or in NULL_STATE
func (dsm DCSDetectorOpAvailabilityMap) compatibleWithDCSOperation(conditionState dcspb.DetectorState) (bool, error) {
detectorsByState := dsm.makeDetectorsByStateMap()
if len(detectorsByState) == 0 {
return true, fmt.Errorf("no detectors provided")
}
detectorsInConditionState, thereAreDetectorsInConditionState := detectorsByState[conditionState]
detectorsInNullState, thereAreDetectorsInNullState := detectorsByState[dcspb.DetectorState_NULL_STATE]
if thereAreDetectorsInConditionState && (len(detectorsInConditionState) == len(dsm)) {
// all detectors are in conditionState
return true, nil
} else if thereAreDetectorsInConditionState && thereAreDetectorsInNullState && (len(detectorsInConditionState)+len(detectorsInNullState) == len(dsm)) {
// all detectors are either in conditionState or in NULL_STATE
return true, fmt.Errorf("detectors %s are in NULL_STATE", strings.Join(detectorsByState[dcspb.DetectorState_NULL_STATE].ToStringSlice(), ", "))
} else if thereAreDetectorsInNullState && (len(detectorsInNullState) == len(dsm)) {
// all detectors are in NULL_STATE
return true, fmt.Errorf("all detectors are in NULL_STATE")
} else {
// there are detectors in other states incompatible with conditionState
reportByState := make([]string, 0)
for state, detectors := range detectorsByState {
if state == conditionState {
continue
}
reportByState = append(reportByState,
fmt.Sprintf("%s in %s", strings.Join(detectors.ToStringSlice(), ", "), state.String()))
}
return false, fmt.Errorf("detectors are in incompatible states: %v", strings.Join(reportByState, "; "))
}
}
func fromDcsDetectorInfo(d *dcspb.DetectorInfo) ECSDetectorInfo {
return ECSDetectorInfo{
State: d.GetState().String(),
Timestamp: d.GetTimestamp(),
AllowedRunTypes: func(rts []dcspb.RunType) []string {
out := make([]string, len(rts))
for i, rt := range rts {
out[i] = rt.String()
}
return out
}(d.GetAllowedRunTypes()),
PfrAvailability: d.GetPfrAvailability().String(),
SorAvailability: d.GetSorAvailability().String(),
}
}
func (m DCSDetectorInfoMap) ToEcsDetectors() ECSDetectorInfoMap {
out := make(map[string]ECSDetectorInfo)
for k, v := range m {
out[dcsToEcsDetector(k)] = fromDcsDetectorInfo(v)
}
return out
}
func (m ECSDetectorInfoMap) Filtered(detectorList []string) ECSDetectorInfoMap {
out := make(ECSDetectorInfoMap)
for _, det := range detectorList {
if _, ok := m[det]; ok {
out[det] = m[det]
}
}
return out
}