mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
walk_graph_normalize.go
140 lines (125 loc) · 3.74 KB
/
walk_graph_normalize.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
// Copyright 2016 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dm
import (
"math"
"sort"
"github.com/xtgo/set"
"go.chromium.org/luci/common/errors"
)
const (
// DefaultLimitMaxDataSize is the default MaxDataSize value (16MB).
DefaultLimitMaxDataSize = 16 * 1024 * 1024
// MaxLimitMaxDataSize is the maximum MaxDataSize value (30MB).
MaxLimitMaxDataSize = 30 * 1024 * 1024
)
// MakeWalkGraphIncludeAll makes a new WalkGraphReq_Include which has all the
// boxes ticked. This should only be used when your application plans to dump
// the resulting graph query data to some logging/debugging trace for humans.
//
// If you don't plan on dumping it for humans, please set the Include options
// appropriately in order to avoid wasting bandwidth/cpu/datastore query time on
// the server (and draining your DM quotas unnecessarially).
func MakeWalkGraphIncludeAll() *WalkGraphReq_Include {
return &WalkGraphReq_Include{
Quest: &WalkGraphReq_Include_Options{
Ids: true,
Data: true,
},
Attempt: &WalkGraphReq_Include_Options{
Ids: true,
Data: true,
Result: true,
Abnormal: true,
Expired: true,
},
Execution: &WalkGraphReq_Include_Options{
Ids: true,
Data: true,
Result: true,
Abnormal: true,
Expired: true,
},
NumExecutions: math.MaxUint32,
FwdDeps: true,
BackDeps: true,
}
}
// Normalize returns an error iff the WalkGraphReq_Exclude is invalid.
func (e *WalkGraphReq_Exclude) Normalize() error {
if len(e.Quests) > 0 {
e.Quests = e.Quests[:set.Uniq(sort.StringSlice(e.Quests))]
}
return e.Attempts.Normalize()
}
// Normalize returns an error iff the WalkGraphReq is invalid.
func (w *WalkGraphReq) Normalize() error {
if w.Auth != nil {
if err := w.Auth.Normalize(); err != nil {
return err
}
}
if w.Query == nil {
return errors.New("must specify a Query")
}
if err := w.Query.Normalize(); err != nil {
return err
}
if w.Mode == nil {
w.Mode = &WalkGraphReq_Mode{}
}
if w.Limit != nil {
if w.Limit.MaxDepth < -1 {
return errors.New("limit.max_depth must be >= -1")
}
if w.Limit.GetMaxTime().AsDuration() < 0 {
return errors.New("limit.max_time must be positive")
}
} else {
w.Limit = &WalkGraphReq_Limit{}
}
if w.Limit.MaxDataSize == 0 {
w.Limit.MaxDataSize = DefaultLimitMaxDataSize
}
if w.Limit.MaxDataSize > MaxLimitMaxDataSize {
w.Limit.MaxDataSize = MaxLimitMaxDataSize
}
if w.Include == nil {
w.Include = &WalkGraphReq_Include{
Quest: &WalkGraphReq_Include_Options{},
Attempt: &WalkGraphReq_Include_Options{},
Execution: &WalkGraphReq_Include_Options{},
}
} else {
if w.Include.Quest == nil {
w.Include.Quest = &WalkGraphReq_Include_Options{}
} else if w.Include.Quest.Result || w.Include.Quest.Abnormal || w.Include.Quest.Expired {
return errors.New("include.quest does not support result, abnormal or expired")
}
if w.Include.Attempt == nil {
w.Include.Attempt = &WalkGraphReq_Include_Options{}
} else {
if w.Include.Attempt.Result {
w.Include.Attempt.Data = true
}
}
if w.Include.Execution == nil {
w.Include.Execution = &WalkGraphReq_Include_Options{}
}
}
if w.Exclude == nil {
w.Exclude = &WalkGraphReq_Exclude{}
}
return w.Exclude.Normalize()
}