/
query_builder.go
163 lines (143 loc) · 5.55 KB
/
query_builder.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
// Copyright (c) 2017-2021 Uber Technologies Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package internal
import (
"fmt"
"math"
"strings"
"time"
"go.uber.org/cadence/.gen/go/shared"
)
type (
// WorkflowStatus represents the status of a workflow
WorkflowStatus string
)
const (
// WorkflowStatusOpen is the WorkflowStatus for open workflows
WorkflowStatusOpen WorkflowStatus = "OPEN"
// WorkflowStatusClosed is the WorkflowStatus for closed workflows
WorkflowStatusClosed WorkflowStatus = "CLOSED"
// WorkflowStatusALL is the WorkflowStatus for all workflows
WorkflowStatusALL WorkflowStatus = "ALL"
)
var (
// WorkflowStatusCompleted is the WorkflowStatus for completed workflow
WorkflowStatusCompleted = WorkflowStatus(shared.WorkflowExecutionCloseStatusCompleted.String())
// WorkflowStatusFailed is the WorkflowStatus for failed workflows
WorkflowStatusFailed = WorkflowStatus(shared.WorkflowExecutionCloseStatusFailed.String())
// WorkflowStatusCanceled is the WorkflowStatus for canceled workflows
WorkflowStatusCanceled = WorkflowStatus(shared.WorkflowExecutionCloseStatusCanceled.String())
// WorkflowStatusTerminated is the WorkflowStatus for terminated workflows
WorkflowStatusTerminated = WorkflowStatus(shared.WorkflowExecutionCloseStatusTerminated.String())
// WorkflowStatusContinuedAsNew is the WorkflowStatus for continuedAsNew workflows
WorkflowStatusContinuedAsNew = WorkflowStatus(shared.WorkflowExecutionCloseStatusContinuedAsNew.String())
// WorkflowStatusTimedOut is the WorkflowStatus for timedout workflows
WorkflowStatusTimedOut = WorkflowStatus(shared.WorkflowExecutionCloseStatusTimedOut.String())
)
const (
keyWorkflowType = "WorkflowType"
keyCloseStatus = "CloseStatus"
keyStartTime = "StartTime"
keyCloseTime = "CloseTime"
)
var (
maxTimestamp = time.Unix(0, math.MaxInt64)
)
type (
// QueryBuilder builds visibility query
QueryBuilder interface {
WorkflowTypes([]string) QueryBuilder
WorkflowStatus([]WorkflowStatus) QueryBuilder
StartTime(time.Time, time.Time) QueryBuilder
Build() string
}
queryBuilderImpl struct {
builder strings.Builder
}
)
// NewQueryBuilder creates a new visibility QueryBuilder
func NewQueryBuilder() QueryBuilder {
return &queryBuilderImpl{}
}
func (q *queryBuilderImpl) WorkflowTypes(types []string) QueryBuilder {
workflowTypeQueries := make([]string, 0, len(types))
for _, workflowType := range types {
workflowTypeQueries = append(workflowTypeQueries, fmt.Sprintf(keyWorkflowType+` = "%v"`, workflowType))
}
q.appendPartialQuery(strings.Join(workflowTypeQueries, " or "))
return q
}
func (q *queryBuilderImpl) WorkflowStatus(statuses []WorkflowStatus) QueryBuilder {
workflowStatusQueries := make([]string, 0, len(statuses))
for _, status := range statuses {
var statusQuery string
switch status {
case WorkflowStatusOpen:
statusQuery = keyCloseTime + " = missing"
case WorkflowStatusClosed:
statusQuery = keyCloseTime + " != missing"
case WorkflowStatusALL:
// no query needed
return q
default:
statusQuery = keyCloseStatus + ` = "` + string(status) + `"`
}
workflowStatusQueries = append(workflowStatusQueries, statusQuery)
}
q.appendPartialQuery(strings.Join(workflowStatusQueries, " or "))
return q
}
func (q *queryBuilderImpl) StartTime(minStartTime, maxStartTime time.Time) QueryBuilder {
startTimeQueries := make([]string, 0, 2)
if !minStartTime.IsZero() {
startTimeQueries = append(startTimeQueries, fmt.Sprintf(keyStartTime+` >= %v`, minStartTime.UnixNano()))
}
if !maxStartTime.Equal(maxTimestamp) {
startTimeQueries = append(startTimeQueries, fmt.Sprintf(keyStartTime+` <= %v`, maxStartTime.UnixNano()))
}
q.appendPartialQuery(strings.Join(startTimeQueries, " and "))
return q
}
func (q *queryBuilderImpl) Build() string {
return q.builder.String()
}
func (q *queryBuilderImpl) appendPartialQuery(query string) {
if len(query) == 0 {
return
}
if q.builder.Len() != 0 {
q.builder.WriteString(" and ")
}
q.builder.WriteRune('(')
q.builder.WriteString(query)
q.builder.WriteRune(')')
}
// ToWorkflowStatus converts workflow status from string type to WorkflowStatus type
func ToWorkflowStatus(statusString string) (WorkflowStatus, error) {
status := WorkflowStatus(strings.ToUpper(statusString))
switch status {
case WorkflowStatusOpen, WorkflowStatusClosed, WorkflowStatusCompleted,
WorkflowStatusFailed, WorkflowStatusCanceled, WorkflowStatusTerminated,
WorkflowStatusContinuedAsNew, WorkflowStatusTimedOut, WorkflowStatusALL:
return status, nil
default:
return "", fmt.Errorf("unknown workflow status: %v", statusString)
}
}