use of java.time.temporal.ChronoUnit.MINUTES in project phoebus-olog by Olog.
the class TimeParser method parseTemporalAmount.
/**
* Parse a temporal amount like "1 month 2 days" or "1 day 20 seconds"
*
* <p>Provides either a time-based {@link Duration}
* or a calendar based {@link Period}.
*
* <p>A period of "1 months" does not have a well defined
* length in time because a months could have 28 to 31 days.
* When the user specifies "1 month" we assume that
* a time span between the same day in different months
* is requested.
* As soon as the time span includes a month or year,
* a {@link Period} is returned and the smaller units
* from hours down are ignored.
*
* <p>For time spans that only include days or less,
* a {@link Duration} is used.
*
* @param string Text
* @return {@link Duration} or {@link Period}
*/
public static TemporalAmount parseTemporalAmount(final String string) {
if (NOW.equalsIgnoreCase(string))
return Duration.ZERO;
final Matcher timeQuantityUnitsMatcher = timeQuantityUnitsPattern.matcher(string);
final Map<ChronoUnit, Integer> timeQuantities = new HashMap<>();
boolean use_period = false;
while (timeQuantityUnitsMatcher.find()) {
final double quantity = "".equals(timeQuantityUnitsMatcher.group(1)) ? 1.0 : Double.valueOf(timeQuantityUnitsMatcher.group(1));
final int full = (int) quantity;
final double fraction = quantity - full;
final String unit = timeQuantityUnitsMatcher.group(2).toLowerCase();
// -> We place fractional amounts in the next finer unit.
if (unit.startsWith("y")) {
timeQuantities.put(YEARS, full);
if (fraction > 0) {
final int next = (int) (fraction * 12 + 0.5);
timeQuantities.compute(MONTHS, (u, prev) -> prev == null ? next : prev + next);
}
use_period = true;
} else if (unit.startsWith("mo")) {
timeQuantities.compute(MONTHS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 4 * 7 + 0.5);
timeQuantities.compute(DAYS, (u, prev) -> prev == null ? next : prev + next);
}
use_period = true;
} else if (unit.startsWith("w")) {
timeQuantities.compute(WEEKS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 7 + 0.5);
timeQuantities.compute(DAYS, (u, prev) -> prev == null ? next : prev + next);
}
use_period = true;
} else if (unit.startsWith("mi")) {
timeQuantities.compute(MINUTES, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 60 + 0.5);
timeQuantities.compute(SECONDS, (u, prev) -> prev == null ? next : prev + next);
}
} else if (unit.startsWith("h")) {
timeQuantities.compute(HOURS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 60 + 0.5);
timeQuantities.compute(MINUTES, (u, prev) -> prev == null ? next : prev + next);
}
} else if (unit.startsWith("d")) {
timeQuantities.compute(DAYS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 24 + 0.5);
timeQuantities.compute(HOURS, (u, prev) -> prev == null ? next : prev + next);
}
} else if (unit.startsWith("s")) {
timeQuantities.compute(SECONDS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 1000 + 0.5);
timeQuantities.compute(MILLIS, (u, prev) -> prev == null ? next : prev + next);
}
} else if (unit.equals("ms")) {
timeQuantities.compute(MILLIS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 1000 + 0.5);
timeQuantities.compute(MICROS, (u, prev) -> prev == null ? next : prev + next);
}
}
}
if (use_period) {
Period result = Period.ZERO;
if (timeQuantities.containsKey(YEARS))
result = result.plusYears(timeQuantities.get(YEARS));
if (timeQuantities.containsKey(WEEKS))
result = result.plusDays(7 * timeQuantities.get(WEEKS));
if (timeQuantities.containsKey(MONTHS))
result = result.plusMonths(timeQuantities.get(MONTHS));
if (timeQuantities.containsKey(DAYS))
result = result.plusDays(timeQuantities.get(DAYS));
// Ignoring hours, min, .. because they're insignificant compared to weeks
return result;
} else {
Duration result = Duration.ofSeconds(0);
for (Entry<ChronoUnit, Integer> entry : timeQuantities.entrySet()) result = result.plus(entry.getValue(), entry.getKey());
return result;
}
}
use of java.time.temporal.ChronoUnit.MINUTES in project vertx-starter by vert-x3.
the class AnalyticsTest method projectPersisted.
@Test
void projectPersisted(Vertx vertx, VertxTestContext testContext) {
Instant createdOn = Instant.now().truncatedTo(MINUTES);
VertxProject vertxProject = new VertxProject().setId("should-not-persist").setGroupId("should-not-persist").setArtifactId("should-not-persist").setPackageName("should-not-persist").setCreatedOn(createdOn).setOperatingSystem("Other");
vertx.eventBus().publish(Topics.PROJECT_CREATED, vertxProject);
Checkpoint checkpoint = testContext.laxCheckpoint();
vertx.setPeriodic(20, id -> {
JsonObject query = new JsonObject();
client.find(AnalyticsService.COLLECTION_NAME, query, testContext.succeeding(list -> {
testContext.verify(() -> {
assertTrue(list.size() <= 1);
if (list.size() == 1) {
JsonObject document = list.get(0);
assertFalse(Stream.of("id", "groupId", "artifactId", "packageName").anyMatch(document::containsKey));
assertEquals(createdOn, document.getJsonObject("createdOn").getInstant("$date"));
assertEquals(vertxProject.getOperatingSystem(), document.getString("operatingSystem"));
checkpoint.flag();
assertTrue(vertx.cancelTimer(id));
}
});
}));
});
}
use of java.time.temporal.ChronoUnit.MINUTES in project phoebus by ControlSystemStudio.
the class TimeParser method parseTemporalAmount.
/**
* Parse a temporal amount like "1 month 2 days" or "1 day 20 seconds"
*
* <p>Provides either a time-based {@link Duration}
* or a calendar based {@link Period}.
*
* <p>A period of "1 months" does not have a well defined
* length in time because a months could have 28 to 31 days.
* When the user specifies "1 month" we assume that
* a time span between the same day in different months
* is requested.
* As soon as the time span includes a month or year,
* a {@link Period} is returned and the smaller units
* from hours down are ignored.
*
* <p>For time spans that only include days or less,
* a {@link Duration} is used.
*
* @param string Text
* @return {@link Duration} or {@link Period}
*/
public static TemporalAmount parseTemporalAmount(final String string) {
if (NOW.equalsIgnoreCase(string))
return Duration.ZERO;
final Matcher timeQuantityUnitsMatcher = timeQuantityUnitsPattern.matcher(string);
final Map<ChronoUnit, Integer> timeQuantities = new HashMap<>();
boolean use_period = false;
while (timeQuantityUnitsMatcher.find()) {
final double quantity = "".equals(timeQuantityUnitsMatcher.group(1)) ? 1.0 : Double.valueOf(timeQuantityUnitsMatcher.group(1));
final int full = (int) quantity;
final double fraction = quantity - full;
final String unit = timeQuantityUnitsMatcher.group(2).toLowerCase();
// -> We place fractional amounts in the next finer unit.
if (unit.startsWith("y")) {
timeQuantities.put(YEARS, full);
if (fraction > 0) {
final int next = (int) (fraction * 12 + 0.5);
timeQuantities.compute(MONTHS, (u, prev) -> prev == null ? next : prev + next);
}
use_period = true;
} else if (unit.startsWith("mo")) {
timeQuantities.compute(MONTHS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 4 * 7 + 0.5);
timeQuantities.compute(DAYS, (u, prev) -> prev == null ? next : prev + next);
}
use_period = true;
} else if (unit.startsWith("w")) {
timeQuantities.compute(WEEKS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 7 + 0.5);
timeQuantities.compute(DAYS, (u, prev) -> prev == null ? next : prev + next);
}
use_period = true;
} else if (unit.startsWith("mi")) {
timeQuantities.compute(MINUTES, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 60 + 0.5);
timeQuantities.compute(SECONDS, (u, prev) -> prev == null ? next : prev + next);
}
} else if (unit.startsWith("h")) {
timeQuantities.compute(HOURS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 60 + 0.5);
timeQuantities.compute(MINUTES, (u, prev) -> prev == null ? next : prev + next);
}
} else if (unit.startsWith("d")) {
timeQuantities.compute(DAYS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 24 + 0.5);
timeQuantities.compute(HOURS, (u, prev) -> prev == null ? next : prev + next);
}
} else if (unit.startsWith("s")) {
timeQuantities.compute(SECONDS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 1000 + 0.5);
timeQuantities.compute(MILLIS, (u, prev) -> prev == null ? next : prev + next);
}
} else if (unit.equals("ms")) {
timeQuantities.compute(MILLIS, (u, prev) -> prev == null ? full : prev + full);
if (fraction > 0) {
final int next = (int) (fraction * 1000 + 0.5);
timeQuantities.compute(MICROS, (u, prev) -> prev == null ? next : prev + next);
}
}
}
if (use_period) {
Period result = Period.ZERO;
if (timeQuantities.containsKey(YEARS))
result = result.plusYears(timeQuantities.get(YEARS));
if (timeQuantities.containsKey(WEEKS))
result = result.plusDays(7 * timeQuantities.get(WEEKS));
if (timeQuantities.containsKey(MONTHS))
result = result.plusMonths(timeQuantities.get(MONTHS));
if (timeQuantities.containsKey(DAYS))
result = result.plusDays(timeQuantities.get(DAYS));
// Ignoring hours, min, .. because they're insignificant compared to weeks
return result;
} else {
Duration result = Duration.ofSeconds(0);
for (Entry<ChronoUnit, Integer> entry : timeQuantities.entrySet()) result = result.plus(entry.getValue(), entry.getKey());
return result;
}
}
use of java.time.temporal.ChronoUnit.MINUTES in project flux-capacitor-client by flux-capacitor-io.
the class SchedulingInterceptor method interceptHandling.
@Override
public Function<DeserializingMessage, Object> interceptHandling(Function<DeserializingMessage, Object> function, Handler<DeserializingMessage> handler, String consumer) {
return m -> {
if (m.getMessageType() == MessageType.SCHEDULE) {
long deadline = millisFromIndex(m.getIndex());
Periodic periodic = Optional.ofNullable(handler.getMethod(m)).map(method -> method.getAnnotation(Periodic.class)).orElse(ReflectionUtils.getTypeAnnotation(m.getPayloadClass(), Periodic.class));
Object result;
Instant now = ofEpochMilli(deadline);
try {
result = function.apply(m);
} catch (Exception e) {
if (periodic != null && periodic.continueOnError()) {
reschedule(m, now.plusMillis(periodic.value()));
}
throw e;
}
if (result instanceof TemporalAmount) {
reschedule(m, now.plus((TemporalAmount) result));
} else if (result instanceof TemporalAccessor) {
reschedule(m, Instant.from((TemporalAccessor) result));
} else if (result instanceof Schedule) {
Schedule schedule = (Schedule) result;
reschedule(schedule.getPayload(), schedule.getMetadata(), schedule.getDeadline());
} else if (result != null) {
Metadata metadata = m.getMetadata();
Object nextPayload = result;
if (result instanceof Message) {
metadata = ((Message) result).getMetadata();
nextPayload = ((Message) result).getPayload();
}
if (nextPayload != null && m.getPayloadClass().isAssignableFrom(nextPayload.getClass())) {
if (periodic == null) {
Instant dispatched = m.getTimestamp();
Duration previousDelay = between(dispatched, now);
if (previousDelay.compareTo(Duration.ZERO) > 0) {
reschedule(nextPayload, metadata, now.plus(previousDelay));
} else {
log.info("Delay between the time this schedule was created and scheduled is <= 0, " + "rescheduling with delay of 1 minute");
reschedule(nextPayload, metadata, now.plus(Duration.of(1, MINUTES)));
}
} else {
reschedule(nextPayload, metadata, now.plusMillis(periodic.value()));
}
} else if (periodic != null) {
reschedule(m, now.plusMillis(periodic.value()));
}
} else if (periodic != null) {
reschedule(m, now.plusMillis(periodic.value()));
}
return result;
}
return function.apply(m);
};
}
use of java.time.temporal.ChronoUnit.MINUTES in project trino by trinodb.
the class TestDeltaLakePageSink method testPageSinkStats.
@Test
public void testPageSinkStats() throws Exception {
File tempDir = Files.createTempDir();
try {
DeltaLakeWriterStats stats = new DeltaLakeWriterStats();
String tablePath = tempDir.getAbsolutePath() + "/test_table";
ConnectorPageSink pageSink = createPageSink(new Path(tablePath), stats);
List<LineItemColumn> columns = ImmutableList.copyOf(LineItemColumn.values());
List<Type> columnTypes = columns.stream().map(LineItemColumn::getType).map(TestDeltaLakePageSink::getTrinoType).collect(toList());
PageBuilder pageBuilder = new PageBuilder(columnTypes);
long rows = 0;
for (LineItem lineItem : new LineItemGenerator(0.01, 1, 1)) {
if (rows >= NUM_ROWS) {
break;
}
rows++;
pageBuilder.declarePosition();
for (int i = 0; i < columns.size(); i++) {
LineItemColumn column = columns.get(i);
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i);
writeToBlock(blockBuilder, column, lineItem);
}
}
Page page = pageBuilder.build();
pageSink.appendPage(page);
JsonCodec<DataFileInfo> dataFileInfoCodec = new JsonCodecFactory().jsonCodec(DataFileInfo.class);
Collection<Slice> fragments = getFutureValue(pageSink.finish());
List<DataFileInfo> dataFileInfos = fragments.stream().map(Slice::getBytes).map(dataFileInfoCodec::fromJson).collect(toImmutableList());
assertEquals(dataFileInfos.size(), 1);
DataFileInfo dataFileInfo = dataFileInfos.get(0);
List<File> files = ImmutableList.copyOf(new File(tablePath).listFiles((dir, name) -> !name.endsWith(".crc")));
assertEquals(files.size(), 1);
File outputFile = files.get(0);
assertEquals(round(stats.getInputPageSizeInBytes().getAllTime().getMax()), page.getRetainedSizeInBytes());
assertEquals(dataFileInfo.getStatistics().getNumRecords(), Optional.of(rows));
assertEquals(dataFileInfo.getPartitionValues(), ImmutableList.of());
assertEquals(dataFileInfo.getSize(), outputFile.length());
assertEquals(dataFileInfo.getPath(), outputFile.getName());
Instant now = Instant.now();
assertTrue(dataFileInfo.getCreationTime() < now.toEpochMilli());
assertTrue(dataFileInfo.getCreationTime() > now.minus(1, MINUTES).toEpochMilli());
} finally {
deleteRecursively(tempDir.toPath(), ALLOW_INSECURE);
}
}
Aggregations