// Copyright 2020 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
//
//     https://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.graphgeckos.dashboard.storage;

import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.StructuredQuery.OrderBy;
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
import com.google.cloud.datastore.TimestampValue;
import com.google.cloud.datastore.Query;
import com.google.cloud.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.Date;
import java.util.List;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate;

/**
 * Implementation of the DataRepository, with the purpose of providing I/O to the Google Cloud
 * Datastore. Each individual database entry is modeled by the {@link #BuildInfo BuildInfo} class.
 * The relevant fields for the database, which are described in that class are:
 *    - Kind: "revision"
 *    - Key: commit hash
 * The database indexes after all the BuildInfo fields, besides the {@code builders} list.
 * It is unfeasable to query after the list of builders, since it would not be possible to
 * replicate such a list without querying the database for it first, and the complexity
 * of the indexing and querying would be great, without any apparent value.
 *
 * Useful links:
 * - https://googleapis.dev/java/google-cloud-datastore/latest/index.html
 * - https://googleapis.dev/java/spring-cloud-gcp/1.2.2.RELEASE/index.html
 * - https://cloud.google.com/datastore/docs/concepts/
 * - https://spring.io/projects/spring-cloud-gcp#overview
 */
@SpringBootApplication
public class GCDataRepository implements DataRepository {
  /**
   * This class encapsulates all the parameters for the cleanup subroutine.
   * Also, provides an easy interface for getting the next timestamp before which
   * to delete entries. It can be used by multiple GCDataRepository instances.
   */
  private static class CleanupParameters {
    static final long initialDelay = 1;
    static final long period = 1;
    static final TimeUnit periodUnit = TimeUnit.MINUTES;
    static final int ttlTimeUnit = Calendar.MONTH;
    static final int ttl = -3;

    /**
     * This method provides a stateless way to retrieve the timestamp before which to
     * delete all older entries.
     *
     * @return Date object with the appropriate "delete before" timestamp.
     */
    static Date getEarliestAliveTime() {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(new Date());
      calendar.add(ttlTimeUnit, ttl);
      return calendar.getTime();
    }
  };

  private DatastoreTemplate storage;
  private final ScheduledExecutorService cleanScheduler = Executors.newScheduledThreadPool(1);

  /**
   * Constructs the GCDataRepository instance, and schedules the cleanup routine after the
   * parameters defined in {@link #CleanupParameters CleanupParameters}.
   */
  public GCDataRepository() {
    cleanScheduler.scheduleAtFixedRate(cleanup, CleanupParameters.initialDelay,
                                                CleanupParameters.period,
                                                CleanupParameters.periodUnit);
  }

  /**
   * {@inheritDoc}
   */
  public void createRevisionEntry(ParsedGitData entryData) throws IllegalArgumentException {
    if (entryData == null) {
      throw new IllegalArgumentException("entryData cannot be null");
    } else if (!entryData.validCreateData()) {
      throw new IllegalArgumentException("entryData does not contain all necessary fields");
    }

    if (getRevisionEntry(entryData.getCommitHash()) == null) {
      storage.save(new BuildInfo(entryData););
    }
  }

  /**
   * {@inheritDoc}
   */
  public void updateRevisionEntry(ParsedBuildbotData updateData) throws IllegalArgumentException {
    if (updateData == null) {
      throw new IllegalArgumentException("entryData cannot be null");
    } else if (updateData.validUpdateData()) {
      throw new IllegalArgumentException("updateData does not contain all necessary fields");
    }

    BuildInfo associatedEntity = getRevisionEntry(updateData.getCommitHash());

    if (associatedEntity != null) {
      associatedEntity.addBuilder(updateData.toBuilder());

      storage.save(associatedEntity);
    }
  }

  /**
   * {@inheritDoc}
   */
  public void deleteRevisionEntry(String commitHash) throws IllegalArgumentException {
    if (commitHash == null) {
      throw new IllegalArgumentException("commitHash cannot be null");
    }

    BuildInfo toBeDeleted = getRevisionEntry(commitHash);

    if (toBeDeleted != null) {
      storage.delete(toBeDeleted);
    }
  }

  /**
   * {@inheritDoc}
   */
  public Iterable<BuildInfo> getLastRevisionEntries(int number, int offset)
                                                         throws IllegalArgumentException {
    if (number < 0 || offset < 0) {
      throw new IllegalArgumentException("Both number and offset must be >= 0");
    }

    Query<Entity> query = Query.newEntityQueryBuilder()
                               .setKind("revision")
                               .setOrderBy(OrderBy.desc("timestamp"))
                               .setOffset(offset)
                               .setLimit(number)
                               .build();

    Iterable<BuildInfo> results = storage.query(query, BuildInfo.class).getIterable();

    List<BuildInfo> toBeReturned = new ArrayList<BuildInfo>();

    for (BuildInfo entity : results) {
      toBeReturned.add(entity);
    }

    return toBeReturned;
  }

  /**
   * Queries the database for a given entry, that has the Id set to the provided commitHash.
   * Used to separate the usage from the actual Spring Datastore implementation.
   * Returns null when nothing was found.
   */
  private BuildInfo getRevisionEntry(String commitHash) {
    return storage.findById(commitHash, BuildInfo.class);
  }

  /**
   * A subroutine created for running at fixed intervals, deleting older entries.
   * Queries all "revision" type entries, and deletes all which are older than a specified
   * amount of time. During this operation, the GCDataRepository can be queried, but
   * there is no guarantee of consistency when querying the entries that are in process
   * of removal.
   */
  private final Runnable cleanup = new Runnable() {
    public void run() {
      TimestampValue borderDate = TimestampValue.of(Timestamp.of(
                                  CleanupParameters.getEarliestAliveTime()));

      Query<Entity> query = Query.newEntityQueryBuilder()
                                 .setKind("revision")
                                 .setFilter(PropertyFilter.lt("timestamp", borderDate)).build();

      Iterable<Entity> results = storage.query(query, Entity.class).getIterable();

      for (Entity entity : results) {
        storage.delete(entity);
      }
    }
  };
}

@@ -2402,1514 +2402,42 @@
 %7B%0A  
-/**%0A   * This class encapsulates all the parameters for the cleanup subroutine.%0A   * Also, provides an easy interface for getting the next timestamp before which%0A   * to delete entries. It can be used by multiple GCDataRepository instances.%0A   */%0A  private static class CleanupParameters %7B%0A    static final long initialDelay = 1;%0A    static final long period = 1;%0A    static final TimeUnit periodUnit = TimeUnit.MINUTES;%0A    static final int ttlTimeUnit = Calendar.MONTH;%0A    static final int ttl = -3;%0A%0A    /**%0A     * This method provides a stateless way to retrieve the timestamp before which to%0A     * delete all older entries.%0A     *%0A     * @return Date object with the appropriate %22delete before%22 timestamp.%0A     */%0A    static Date getEarliestAliveTime() %7B%0A      Calendar calendar = Calendar.getInstance();%0A      calendar.setTime(new Date());%0A      calendar.add(ttlTimeUnit, ttl);%0A      return calendar.getTime();%0A    %7D%0A  %7D;%0A%0A  private DatastoreTemplate storage;%0A  private final ScheduledExecutorService cleanScheduler = Executors.newScheduledThreadPool(1);%0A%0A  /**%0A   * Constructs the GCDataRepository instance, and schedules the cleanup routine after the%0A   * parameters defined in %7B@link #CleanupParameters CleanupParameters%7D.%0A   */%0A  public GCDataRepository() %7B%0A    cleanScheduler.scheduleAtFixedRate(cleanup, CleanupParameters.initialDelay,%0A                                                CleanupParameters.period,%0A                                                CleanupParameters.periodUnit);%0A  %7D
+private DatastoreTemplate storage;
 %0A%0A  
@@ -5142,987 +5142,7 @@
 %0A  %7D
-%0A%0A  /**%0A   * A subroutine created for running at fixed intervals, deleting older entries.%0A   * Queries all %22revision%22 type entries, and deletes all which are older than a specified%0A   * amount of time. During this operation, the GCDataRepository can be queried, but%0A   * there is no guarantee of consistency when querying the entries that are in process%0A   * of removal.%0A   */%0A  private final Runnable cleanup = new Runnable() %7B%0A    public void run() %7B%0A      TimestampValue borderDate = TimestampValue.of(Timestamp.of(%0A                                  CleanupParameters.getEarliestAliveTime()));%0A%0A      Query%3CEntity%3E query = Query.newEntityQueryBuilder()%0A                                 .setKind(%22revision%22)%0A                                 .setFilter(PropertyFilter.lt(%22timestamp%22, borderDate)).build();%0A%0A      Iterable%3CEntity%3E results = storage.query(query, Entity.class).getIterable();%0A%0A      for (Entity entity : results) %7B%0A        storage.delete(entity);%0A      %7D%0A    %7D%0A  %7D;
 %0A%7D%0A

remove cleanup components