-
Notifications
You must be signed in to change notification settings - Fork 23
/
componentcfg.go
113 lines (101 loc) · 3.42 KB
/
componentcfg.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
/*
* === This file is part of ALICE O² ===
*
* Copyright 2020-2021 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
* Author: George Raduta <george.raduta@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 componentcfg
import (
"errors"
"strconv"
"strings"
"time"
apricotpb "github.com/AliceO2Group/Control/apricot/protos"
)
const (
ConfigComponentsPath = "o2/components/"
)
const(
SEPARATOR = "/"
SEPARATOR_RUNE = '/'
)
// Checks whether the input string is a valid Consul path element on its own
func IsInputSingleValidWord(input string) bool {
return !strings.Contains(input, "/") && !strings.Contains(input, "@")
}
// Method to parse a timestamp in the specified format
func GetTimestampInFormat(timestamp string, timeFormat string)(string, error){
timeStampAsInt, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
return "", errors.New("unable to identify timestamp")
}
tm := time.Unix(timeStampAsInt, 0)
return tm.Format(timeFormat), nil
}
// Method to return the latest timestamp for a specified component & entry
// If no keys were passed an error and code exit 3 will be returned
func GetLatestTimestamp(keys []string, p *Query)(timestamp string, err error) {
keyPrefix := p.AbsoluteWithoutTimestamp()
if len(keys) == 0 {
err = errors.New("no keys found")
return "", err
}
var maxTimeStamp uint64
for _, key := range keys {
componentTimestamp, err := strconv.ParseUint(strings.TrimPrefix(key, keyPrefix + "/"), 10, 64)
if err == nil {
if componentTimestamp > maxTimeStamp {
maxTimeStamp = componentTimestamp
}
}
}
if maxTimeStamp == 0 {
return "", nil
}
return strconv.FormatUint(maxTimeStamp, 10), nil
}
// keys must be a slice containing all the full keys in /o2/components
func GetComponentsMapFromKeysList(keys []string) map[string]bool {
var componentsMap = make(map[string]bool)
for _,value := range keys {
value := strings.TrimPrefix(value, ConfigComponentsPath)
component := strings.Split(value, "/" )[0]
componentsMap[component] = true
}
return componentsMap
}
func GetEntriesMapOfComponentFromKeysList(component string, runtype apricotpb.RunType, rolename string, keys []string) map[string]bool {
var entriesMap = make(map[string]bool)
runtypeString, ok := apricotpb.RunType_name[int32(runtype)]
if !ok {
runtypeString = "ANY" // this should never happen
}
for _,value := range keys {
value := strings.TrimPrefix(value, ConfigComponentsPath)
parts := strings.Split(value, "/" )
if parts[0] == component &&
parts[1] == runtypeString &&
parts[2] == rolename {
entriesMap[parts[3]] = true
}
}
return entriesMap
}