healchow commented on code in PR #5126: URL: https://github.com/apache/inlong/pull/5126#discussion_r924301623
########## inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/rollback/TimeBasedFilterInterceptor.java: ########## @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.inlong.sort.standalone.rollback; + +import org.apache.commons.lang3.math.NumberUtils; +import org.apache.flume.Context; +import org.apache.flume.Event; +import org.apache.flume.interceptor.Interceptor; +import org.apache.inlong.sort.standalone.channel.ProfileEvent; +import org.apache.inlong.sort.standalone.utils.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * Interceptor that filters events selectively based on a configured time interval. + * Usually, it's used to roll back data in a certain time interval. + * It supports config either one of start, stop time or both of them. + * Multiple TimeBasedFilterInterceptor can be chained together to create more complex time intervals. + */ +public class TimeBasedFilterInterceptor implements Interceptor { + + private static final Logger logger = LoggerFactory + .getLogger(TimeBasedFilterInterceptor.class); + private final long startTime; + private final long stopTime; + + public TimeBasedFilterInterceptor(long startTime, long stopTime) { + this.startTime = startTime; + this.stopTime = stopTime; + } + + @Override + public void initialize() { + //no-op + } + + @Override + public Event intercept(Event event) { + long logTime; + if (event instanceof ProfileEvent) { + ProfileEvent profile = (ProfileEvent) event; + logTime = profile.getRawLogTime(); + } else { + logTime = NumberUtils.toLong(event.getHeaders().get(Constants.HEADER_KEY_MSG_TIME), + System.currentTimeMillis()); + } + + if (logTime > stopTime || logTime < startTime) { + return null; + } + return event; + } + + @Override + public List<Event> intercept(List<Event> events) { + return events.stream() + .map(this::intercept) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + @Override + public void close() { + //no-op + } + + public static class Builder implements Interceptor.Builder { Review Comment: Please add Java doc for class, thanks. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
