All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.computate.vertx.api.ApiRequest Maven / Gradle / Ivy

/*
 * Copyright (c) 2018-2022 Computate Limited Liability Company in Utah, USA. 
 *
 * This program and the accompanying materials are made available under the
 * terms of the GNU GENERAL PUBLIC LICENSE Version 3 which is available at
 * 
 * https://www.gnu.org/licenses/gpl-3.0.en.html
 * 
 * You may not propagate or modify a covered work except as expressly provided 
 * under this License. Any attempt otherwise to propagate or modify it is void, 
 * and will automatically terminate your rights under this License (including 
 * any patent licenses granted under the third paragraph of section 11).
 */
package org.computate.vertx.api;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;

import org.computate.search.wrap.Wrap;
import org.computate.vertx.config.ComputateConfigKeys;
import org.computate.vertx.request.ComputateSiteRequest;

/**
 * Keyword: classSimpleNameApiRequest
 */
public class ApiRequest extends ApiRequestGen {
	
	/**
	 * {@inheritDoc}
	 * Ignore: true
	 */
	protected void _siteRequest_(Wrap c) {}
	
	protected void _created(Wrap c) {
		c.o(ZonedDateTime.now(ZoneId.of(siteRequest_.getConfig().getString(ComputateConfigKeys.SITE_ZONE))));
	}

	protected void _rows(Wrap c) {
	}

	protected void _numFound(Wrap c) {
	}

	protected void _numPATCH(Wrap c) {
		c.o(0L);
	}

	protected void _uuid(Wrap c) {
		c.o(UUID.randomUUID().toString());
	}

	protected void _id(Wrap c) {
		c.o("PATCH-" + uuid);
	}

	protected void _pk(Wrap c) {
	}

	protected void _original(Wrap c) {
	}

	protected void _pks(List c) {
	}

	protected void _classes(List c) {
	}

	protected void _vars(List c) {
	}

	protected void _timeRemaining(Wrap w) {
		w.o(calculateTimeRemaining());
	}

	public String calculateTimeRemaining() {
		ZonedDateTime now = ZonedDateTime.now(ZoneId.of(siteRequest_.getConfig().getString(ComputateConfigKeys.SITE_ZONE)));
		Long timeDifferenceNow = ChronoUnit.SECONDS.between(created, now);
		Double ratio = ((double) numPATCH / numFound);
		Double remainingSeconds = ((double) timeDifferenceNow) / ratio - ((double) timeDifferenceNow);

		// Calculating the difference in Hours
		Long hours = ((Double) (remainingSeconds / 60 / 60)).longValue();

		// Calculating the difference in Minutes
		Long minutes = ((Double) ((remainingSeconds / 60) % 60)).longValue();

		// Calculating the difference in Seconds
		Long seconds = ((Double) (remainingSeconds % 60)).longValue();

		return (hours > 0L ? hours + " hours " : "") + (minutes > 0L ? minutes + " minutes " : "") + (hours == 0L && minutes <= 1L ? seconds + " seconds." : "");
	}
}