/
util.go
86 lines (73 loc) · 2.18 KB
/
util.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
package grpcserver
import (
"context"
"encoding/json"
"time"
"github.com/chef/automate/components/config-mgmt-service/backend"
"github.com/chef/automate/lib/grpc/auth_context"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
gp "github.com/golang/protobuf/ptypes/struct"
google_protobuf1 "github.com/golang/protobuf/ptypes/timestamp"
"github.com/sirupsen/logrus"
)
// messageArrayToListValue Casts a 'Proto Message Array' into a 'Proto ListValue'
func messageArrayToListValue(messages []proto.Message, list *gp.ListValue) error {
list.Values = make([]*gp.Value, len(messages))
for i, msg := range messages {
v := gp.Value{}
content, err := getMessageRawJSON(msg)
if err != nil {
return err
}
err = protoFromJSON(content, &v)
if err != nil {
return err
}
list.Values[i] = &v
}
return nil
}
func ToTime(timestamp *google_protobuf1.Timestamp) (time.Time, error) {
if timestamp == nil {
return time.Time{}, nil
}
return ptypes.Timestamp(timestamp)
}
func filterByProjects(ctx context.Context, filters map[string][]string) (map[string][]string, error) {
projectsFilter, err := auth_context.ProjectsFromIncomingContext(ctx)
if err != nil {
return nil, err
}
if auth_context.AllProjectsRequested(projectsFilter) {
return filters, nil
}
filters[backend.Project] = projectsFilter
return filters, nil
}
// DaysBetween get the calendar days between two timestamp
func DaysBetween(fromTime, toTime time.Time) int {
if fromTime.After(toTime) {
fromTime, toTime = toTime, fromTime
}
days := -fromTime.YearDay()
for year := fromTime.Year(); year < toTime.Year(); year++ {
days += time.Date(year, time.December, 31, 0, 0, 0, 0, time.UTC).YearDay()
}
days += toTime.YearDay()
return days
}
func LogQueryPartMin(indices string, partToPrint interface{}, name string) {
part, err := json.Marshal(partToPrint)
if err != nil {
logrus.Errorf("%s", err)
}
stringPart := string(part)
if stringPart == "null" {
stringPart = ""
} else {
stringPart = "\n" + stringPart
}
logrus.Debugf("\n------------------ %s-(start)--[%s]---------------%s \n------------------ %s-(end)-----------------------------------\n",
name, indices, stringPart, name)
}