-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.go
202 lines (177 loc) · 5.08 KB
/
utils.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
package utils
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/redhatinsights/ros-ocp-backend/internal/config"
"github.com/redhatinsights/ros-ocp-backend/internal/logging"
"github.com/sirupsen/logrus"
)
var log *logrus.Entry = logging.GetLogger()
var cfg *config.Config = config.GetConfig()
func Setup_kruize_performance_profile() {
// This func needs to be revisited once kruize implements this API
// Refer - https://github.com/kruize/autotune/blob/mvp_demo/src/main/java/com/autotune/analyzer/Analyzer.java#L50
list_performance_profile_url := cfg.KruizeUrl + "/listPerformanceProfiles"
for i := 0; i < 5; i++ {
log.Infof("Fetching performance profile list")
response, err := http.Get(list_performance_profile_url)
if err != nil {
log.Errorf("An Error Occured %v \n", err)
} else {
defer response.Body.Close()
create_performance_profile_url := cfg.KruizeUrl + "/createPerformanceProfile"
postBody, err := os.ReadFile("./resource_optimization_openshift.json")
if err != nil {
log.Errorf("File reading error: %v \n", err)
os.Exit(1)
}
res, e := http.Post(create_performance_profile_url, "application/json", bytes.NewBuffer(postBody))
if e != nil {
log.Errorf("unable to create performance profile in kruize: %v \n", e)
}
defer res.Body.Close()
if res.StatusCode == 201 {
log.Infof("Performance profile created successfully")
return
}
if res.StatusCode == 409 {
log.Infof("Performance Profile already exist")
return
}
bodyBytes, _ := io.ReadAll(res.Body)
data := map[string]interface{}{}
if err := json.Unmarshal(bodyBytes, &data); err != nil {
log.Errorf("can not unmarshal response data: %v", err)
os.Exit(1)
}
}
log.Infof("sleeping for 10 Seconds")
time.Sleep(10 * time.Second)
}
}
func ReadCSVFromUrl(url string) ([][]string, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
reader := csv.NewReader(resp.Body)
data, err := reader.ReadAll()
if err != nil {
return nil, err
}
return data, nil
}
type uniqueTypes interface {
int | float64 | string
}
func unique[T uniqueTypes](x []T) []T {
keys := make(map[T]bool)
list := []T{}
for _, entry := range x {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func Convert2DarrayToMap(arr [][]string) []map[string]interface{} {
data := []map[string]interface{}{}
for i := 1; i < len(arr); i++ {
m := make(map[string]interface{})
for j := 0; j < len(arr[0]); j++ {
if metric, err := strconv.ParseFloat(arr[i][j], 64); err == nil {
m[arr[0][j]] = metric
} else {
m[arr[0][j]] = arr[i][j]
}
}
data = append(data, m)
}
return data
}
func ConvertDateToISO8601(date string) string {
const date_format = "2006-01-02 15:04:05 -0700 MST"
t, _ := time.Parse(date_format, date)
return t.Format("2006-01-02T15:04:05.000Z")
}
func ConvertStringToTime(data string) (time.Time, error) {
dateTime, err := time.Parse("2006-01-02 15:04:05 -0700 MST", data)
if err != nil {
return time.Time{}, fmt.Errorf("unable to convert string to time: %s", err)
}
return dateTime, nil
}
func ConvertISO8601StringToTime(data string) (time.Time, error) {
dateTime, err := time.Parse("2006-01-02T15:04:05.000Z", data)
if err != nil {
return time.Time{}, fmt.Errorf("unable to convert string to time: %s", err)
}
return dateTime, nil
}
func MaxIntervalEndTime(slice []string) (time.Time, error) {
var converted_date_slice []time.Time
for _, v := range slice {
formated_date, err := ConvertStringToTime(v)
if err != nil {
return time.Time{}, fmt.Errorf("unable to convert string to time in a slice: %s", err)
}
converted_date_slice = append(converted_date_slice, formated_date)
}
var max time.Time
max = converted_date_slice[0]
for _, ele := range converted_date_slice {
if max.Before(ele) {
max = ele
}
}
return max, nil
}
func findInStringSlice(str string, s []string) int {
for i, e := range s {
if e == str {
return i
}
}
return -1
}
func GenerateExperimentName(org_id, source_id, cluster_id, namespace, k8s_object_type, k8s_object_name string) string {
return fmt.Sprintf("%s|%s|%s|%s|%s|%s", org_id, source_id, cluster_id, namespace, k8s_object_type, k8s_object_name)
}
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func Start_prometheus_server() {
if cfg.PrometheusPort != "" {
log.Info("Starting prometheus http server")
http.Handle("/metrics", promhttp.Handler())
_ = http.ListenAndServe(fmt.Sprintf(":%s", cfg.PrometheusPort), nil)
}
}
func NeedRecommOnFirstOfMonth(dbDate time.Time, maxEndTime time.Time) bool {
if isItFirstOfMonth(maxEndTime) && getDate(maxEndTime).After(getDate(dbDate)) {
return true
}
return false
}
func getDate(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
}
func isItFirstOfMonth(d time.Time) bool {
_, _, day := d.Date()
return day == 1
}