use of org.ovirt.engine.core.compat.DateTime in project ovirt-engine by oVirt.
the class AuditLogConditionFieldAutoCompleterTest method testformatValueWithTime.
@Test
public void testformatValueWithTime() {
Pair<String, String> pair = new Pair<>();
IConditionFieldAutoCompleter comp = new AuditLogConditionFieldAutoCompleter();
Date date = new Date(72, 0, 12);
String dateString = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
pair.setSecond(dateString);
comp.formatValue("TIME", pair, false);
DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT);
assertEquals(quote(fmt.format(date)), pair.getSecond());
pair.setSecond("1");
comp.formatValue("TIME", pair, false);
// Try today
// SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yyyy" +
// " 23:59:59 ");
// Today begins at 00:00 - this is why we reset the DateTime object to midnight.
DateTime dt = new DateTime(new Date());
dt = dt.resetToMidnight();
assertEquals(quote(fmt.format(dt)), pair.getSecond());
// Try Yesterday
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, -1);
pair.setSecond("2");
comp.formatValue("TIME", pair, false);
// Yesterday (as any other day) begins at 00:00 - this is why we reset the DateTime object to midnight.
dt = new DateTime(cal.getTime());
dt = dt.resetToMidnight();
assertEquals(quote(fmt.format(dt)), pair.getSecond());
// Just going to test that this works
pair.setSecond("Wednesday");
comp.formatValue("TIME", pair, false);
assertFalse("Day should be transformed to a date", pair.getSecond().equals("Wednesday"));
}
use of org.ovirt.engine.core.compat.DateTime in project ovirt-engine by oVirt.
the class BaseConditionFieldAutoCompleter method formatValue.
@Override
public void formatValue(String fieldName, Pair<String, String> pair, boolean caseSensitive) {
if (fieldName == null) {
return;
}
if ("TIME".equals(fieldName) || "CREATIONDATE".equals(fieldName)) {
Date temp = DateUtils.parse(StringHelper.trim(pair.getSecond(), '\''));
DateTime result;
if (temp == null) {
result = dealWithDateEnum(pair.getSecond());
} else {
result = new DateTime(temp);
}
if (pair.getFirst() != null && pair.getFirst().equals("=")) {
pair.setFirst("between");
DateTime nextDay = result.addDays(1);
pair.setSecond(StringFormat.format("'%1$s' and '%2$s'", result.toString(DateUtils.getFormat(DateFormat.DEFAULT, DateFormat.SHORT)), nextDay.toString(DateUtils.getFormat(DateFormat.DEFAULT, DateFormat.SHORT))));
} else {
// ">" or "<"
// value.argvalue = String.format("'%1$s'", result);
pair.setSecond(StringFormat.format("'%1$s'", result.toString(DateUtils.getFormat(DateFormat.DEFAULT, DateFormat.SHORT))));
}
} else if ("TAG".equals(fieldName)) {
pair.setSecond(pair.getSecond().startsWith("N'") ? pair.getSecond().substring(2) : pair.getSecond());
if (pair.getFirst() != null && pair.getFirst().equals("=")) {
pair.setFirst("IN");
pair.setSecond(StringHelper.trim(pair.getSecond(), '\''));
Tags tag = tagsHandler.getTagByTagName(pair.getSecond());
if (tag != null) {
pair.setSecond(StringFormat.format("(%1$s)", tagsHandler.getTagNameAndChildrenNames(tag.getTagId())));
} else {
pair.setSecond(StringFormat.format("('%1$s')", Guid.Empty));
}
} else if (pair.getFirst() != null && (pair.getFirst().equals("LIKE") || pair.getFirst().equals("ILIKE"))) {
pair.setFirst("IN");
pair.setSecond(StringHelper.trim(pair.getSecond(), '\'').replace("%", "*"));
String IDs = tagsHandler.getTagNamesAndChildrenNamesByRegExp(pair.getSecond());
if (StringHelper.isNullOrEmpty(IDs)) {
pair.setSecond(StringFormat.format("('%1$s')", Guid.Empty));
} else {
pair.setSecond(StringFormat.format("(%1$s)", IDs));
}
}
}
}
use of org.ovirt.engine.core.compat.DateTime in project ovirt-engine by oVirt.
the class CommandEntityCleanupManager method init.
@PostConstruct
private void init() {
log.info("Start initializing {}", getClass().getSimpleName());
Calendar calendar = new GregorianCalendar();
Date commandEntityCleanupTime = Config.<DateTime>getValue(ConfigValues.CommandEntityCleanupTime);
calendar.setTimeInMillis(commandEntityCleanupTime.getTime());
String cronExpression = String.format("%d %d %d * * ?", calendar.get(Calendar.SECOND), calendar.get(Calendar.MINUTE), calendar.get(Calendar.HOUR_OF_DAY));
log.info("Setting command entity cleanup manager to run at: {}", cronExpression);
executor.schedule(this::cleanup, new EngineCronTrigger(cronExpression));
log.info("Finished initializing {}", getClass().getSimpleName());
}
use of org.ovirt.engine.core.compat.DateTime in project ovirt-engine by oVirt.
the class AutoStartVmsRunner method startFailedAutoStartVmsImpl.
private void startFailedAutoStartVmsImpl() {
LinkedList<AutoStartVmToRestart> vmsToRemove = new LinkedList<>();
final DateTime iterationStartTime = DateTime.getNow();
final Date nextTimeOfRetryToRun = iterationStartTime.addSeconds(RETRY_TO_RUN_AUTO_START_VM_INTERVAL);
final Date delayedTimeOfRetryToRun = iterationStartTime.addSeconds(DELAY_TO_RUN_AUTO_START_VM_INTERVAL);
for (AutoStartVmToRestart autoStartVmToRestart : autoStartVmsToRestart) {
// (we'll try again in the next iteration)
if (!autoStartVmToRestart.isTimeToRun(iterationStartTime)) {
continue;
}
Guid vmId = autoStartVmToRestart.getVmId();
if (isNextRunConfiguration(vmId)) {
// if the NextRun config exists then give the ProcessDownVmCommand time to apply it
log.debug("NextRun config found for '{}' vm, the RunVm will be delayed", vmId);
if (autoStartVmToRestart.delayNextTimeToRun(delayedTimeOfRetryToRun)) {
// The priority is to run the VM even if the NextRun fails to be applied
continue;
}
// Waiting for NextRun config is over, let's run the VM even with the non-applied Next-Run
log.warn("Failed to wait for the NextRun config to be applied on vm '{}', trying to run the VM anyway", vmId);
}
EngineLock runVmLock = createEngineLockForRunVm(vmId);
// acquired, skip for now and we'll try again in the next iteration
if (!acquireLock(runVmLock)) {
log.debug("Could not acquire lock for auto starting VM '{}'", vmId);
continue;
}
if (!isVmNeedsToBeAutoStarted(vmId)) {
// if the VM doesn't need to be auto started anymore, release the lock and
// remove the VM from the collection of VMs that should be auto started
releaseLock(runVmLock);
vmsToRemove.add(autoStartVmToRestart);
continue;
}
if (runVm(vmId, runVmLock)) {
// the VM reached WaitForLunch, so from now on this job is not responsible
// to auto start it, future failures will be detected by the monitoring
vmsToRemove.add(autoStartVmToRestart);
} else {
logFailedAttemptToRestartVm(vmId);
if (!autoStartVmToRestart.scheduleNextTimeToRun(nextTimeOfRetryToRun)) {
// if we could not schedule the next time to run the VM, it means
// that we reached the maximum number of tried so don't try anymore
vmsToRemove.add(autoStartVmToRestart);
logFailureToRestartVm(vmId);
}
}
}
autoStartVmsToRestart.removeAll(vmsToRemove);
}
use of org.ovirt.engine.core.compat.DateTime in project ovirt-engine by oVirt.
the class BaseConditionFieldAutoCompleter method dealWithDateEnum.
// private static final String DATE_FORMAT = "MMM dd,yyyy";
private static DateTime dealWithDateEnum(String value) {
DateTime formatedValue = new DateTime();
final Integer result = IntegerCompat.tryParse(StringHelper.trim(value, '\''));
if (result != null) {
DateEnumForSearch dateEnumVal = DateEnumForSearch.forValue(result);
switch(dateEnumVal) {
case Today:
formatedValue = DateTime.getNow();
break;
case Yesterday:
formatedValue = DateTime.getNow().addDays(-1);
break;
default:
break;
}
} else {
for (int i = -2; i > -8; i--) {
if (DateUtils.getDayOfWeek(i).equalsIgnoreCase(StringHelper.trim(value, '\''))) {
formatedValue = DateTime.getNow().addDays(i);
return formatedValue.resetToMidnight();
}
}
}
return formatedValue.resetToMidnight();
}
Aggregations