/*
 * Copyright 2019 Google LLC
 *
 * 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 com.google.solutions.df.log.aggregations.common;

import com.google.auto.value.AutoValue;
import org.apache.beam.sdk.io.TextIO;
import org.apache.beam.sdk.io.gcp.pubsub.PubsubIO;
import org.apache.beam.sdk.transforms.Flatten;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.Watch.Growth;
import org.apache.beam.sdk.values.PBegin;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.PCollectionList;
import org.apache.beam.sdk.values.Row;
import org.joda.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@AutoValue
public abstract class ReadFlowLogTransform extends PTransform<PBegin, PCollection<Row>> {
  public static final Logger LOG = LoggerFactory.getLogger(PredictTransform.class);

  public abstract String subscriber();

  public abstract String filePattern();

  public abstract Duration pollInterval();

  @AutoValue.Builder
  public abstract static class Builder {
    public abstract Builder setSubscriber(String topic);

    public abstract Builder setFilePattern(String filePattern);

    public abstract Builder setPollInterval(Duration pollInterval);

    public abstract ReadFlowLogTransform build();
  }

  public static Builder newBuilder() {
    return new AutoValue_ReadFlowLogTransform.Builder();
  }

  @Override
  public PCollection<Row> expand(PBegin input) {

    PCollection<String> fileRow =
        input.apply(
            "ReadFromGCS",
            TextIO.read().from(filePattern()).watchForNewFiles(pollInterval(), Growth.never()));

    PCollection<String> pubsubMessage =
        input.apply("ReadFromPubSub", PubsubIO.readStrings().fromSubscription(subscriber()));

    return PCollectionList.of(fileRow)
        .and(pubsubMessage)
        .apply(Flatten.<String>pCollections())
        .apply("FlowLogs Convert To Rows", new JsonToRowValidationTransform());
  }
}

@@ -923,16 +923,70 @@
 Growth;%0A
+import org.apache.beam.sdk.transforms.WithTimestamps;%0A
 import o
@@ -1183,24 +1183,54 @@
 e.Duration;%0A
+import org.joda.time.Instant;%0A
 import org.s
@@ -2109,16 +2109,29 @@
 nput
+%0A            
 .apply(%0A
     
@@ -2126,16 +2126,20 @@
 .apply(%0A
+    
         
@@ -2157,16 +2157,20 @@
 omGCS%22,%0A
+    
         
@@ -2252,24 +2252,117 @@
 th.never()))
+%0A            .apply(%22AssignEventTimestamp%22, WithTimestamps.of((String rec) -%3E Instant.now()))
 ;%0A%0A    PColl

add event timestamp