|
| 1 | +/* |
| 2 | + * Modifications Copyright (c) 2017-2021 Uber Technologies Inc. |
| 3 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 6 | + * use this file except in compliance with the License. A copy of the License is |
| 7 | + * located at |
| 8 | + * |
| 9 | + * http://aws.amazon.com/apache2.0 |
| 10 | + * |
| 11 | + * or in the "license" file accompanying this file. This file is distributed on |
| 12 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 13 | + * express or implied. See the License for the specific language governing |
| 14 | + * permissions and limitations under the License. |
| 15 | + */ |
| 16 | +package com.uber.cadence.internal.shadowing; |
| 17 | + |
| 18 | +import com.google.common.collect.Lists; |
| 19 | +import com.uber.cadence.worker.ShadowingOptions; |
| 20 | +import com.uber.cadence.worker.TimeFilter; |
| 21 | +import com.uber.cadence.worker.WorkflowStatus; |
| 22 | +import java.time.ZonedDateTime; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +public class QueryBuilder { |
| 27 | + public static QueryBuilder newQueryBuilder() { |
| 28 | + return new QueryBuilder(); |
| 29 | + } |
| 30 | + |
| 31 | + public static QueryBuilder newQueryBuilder(ShadowingOptions options) { |
| 32 | + return new QueryBuilder() |
| 33 | + .setWorkflowTypes(options.getWorkflowTypes()) |
| 34 | + .setWorkflowStartTime(options.getWorkflowStartTimeFilter()) |
| 35 | + .setWorkflowStatuses(options.getWorkflowStatuses()); |
| 36 | + } |
| 37 | + |
| 38 | + public QueryBuilder setWorkflowTypes(Collection<String> workflowTypes) { |
| 39 | + if (workflowTypes == null || workflowTypes.isEmpty()) { |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + Collection<String> types = |
| 44 | + workflowTypes |
| 45 | + .stream() |
| 46 | + .map((wfType) -> WORKFLOW_TYPE_PLACEHOLDER + wfType) |
| 47 | + .collect(Collectors.toList()); |
| 48 | + |
| 49 | + String query = String.join(OR_QUERY, types); |
| 50 | + this.appendPartialQuery(query); |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public QueryBuilder setWorkflowStatuses(Collection<WorkflowStatus> workflowStatuses) { |
| 55 | + if (workflowStatuses == null || workflowStatuses.isEmpty()) { |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + Collection<String> wfStatuses = Lists.newArrayListWithCapacity(workflowStatuses.size()); |
| 60 | + for (WorkflowStatus workflowStatus : workflowStatuses) { |
| 61 | + switch (workflowStatus) { |
| 62 | + case OPEN: |
| 63 | + wfStatuses.add(CLOSE_TIME_PLACEHOLDER + " = " + MISSING_QUERY); |
| 64 | + break; |
| 65 | + case CLOSED: |
| 66 | + wfStatuses.add(CLOSE_TIME_PLACEHOLDER + " != " + MISSING_QUERY); |
| 67 | + break; |
| 68 | + default: |
| 69 | + wfStatuses.add(WORKFLOW_STATUS_PLACEHOLDER + '"' + workflowStatus + '"'); |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + String query = String.join(OR_QUERY, wfStatuses); |
| 75 | + this.appendPartialQuery(query); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + public QueryBuilder setWorkflowStartTime(TimeFilter timeFilter) { |
| 80 | + if (timeFilter == null || timeFilter.isEmpty()) { |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + Collection<String> timerFilters = Lists.newArrayListWithCapacity(2); |
| 85 | + if (timeFilter.getMinTimestamp() != null) { |
| 86 | + timerFilters.add( |
| 87 | + START_TIME_PLACEHOLDER + " >= " + toNanoSeconds(timeFilter.getMinTimestamp())); |
| 88 | + } |
| 89 | + |
| 90 | + if (timeFilter.getMaxTimestamp() != null) { |
| 91 | + timerFilters.add( |
| 92 | + START_TIME_PLACEHOLDER + " <= " + toNanoSeconds(timeFilter.getMaxTimestamp())); |
| 93 | + } |
| 94 | + |
| 95 | + String query = String.join(AND_QUERY, timerFilters); |
| 96 | + this.appendPartialQuery(query); |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + public String build() { |
| 101 | + return this.stringBuffer.toString(); |
| 102 | + } |
| 103 | + |
| 104 | + private static final String OR_QUERY = " or "; |
| 105 | + private static final String AND_QUERY = " and "; |
| 106 | + private static final String LEFT_PARENTHESES = "("; |
| 107 | + private static final String RIGHT_PARENTHESES = ")"; |
| 108 | + private static final String MISSING_QUERY = "missing"; |
| 109 | + private static final String WORKFLOW_TYPE_PLACEHOLDER = "WorkflowType = "; |
| 110 | + private static final String WORKFLOW_STATUS_PLACEHOLDER = "CloseStatus = "; |
| 111 | + private static final String START_TIME_PLACEHOLDER = "StartTime"; |
| 112 | + private static final String CLOSE_TIME_PLACEHOLDER = "CloseTime"; |
| 113 | + private static final long TIMESTAMP_SCALE = 1_000_000_000L; |
| 114 | + private StringBuffer stringBuffer; |
| 115 | + |
| 116 | + private QueryBuilder() { |
| 117 | + this.stringBuffer = new StringBuffer(); |
| 118 | + } |
| 119 | + |
| 120 | + private void appendPartialQuery(String query) { |
| 121 | + if (query == null || query.length() == 0) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (this.stringBuffer.length() != 0) { |
| 126 | + this.stringBuffer.append(AND_QUERY); |
| 127 | + } |
| 128 | + |
| 129 | + this.stringBuffer.append(LEFT_PARENTHESES); |
| 130 | + this.stringBuffer.append(query); |
| 131 | + this.stringBuffer.append(RIGHT_PARENTHESES); |
| 132 | + } |
| 133 | + |
| 134 | + protected static long toNanoSeconds(ZonedDateTime time) { |
| 135 | + return time.toEpochSecond() * TIMESTAMP_SCALE + time.getNano(); |
| 136 | + } |
| 137 | +} |
0 commit comments