Search in sources :

Example 6 with DateTime

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"));
}
Also used : DateFormat(java.text.DateFormat) Calendar(java.util.Calendar) Date(java.util.Date) DateTime(org.ovirt.engine.core.compat.DateTime) Pair(org.ovirt.engine.core.common.utils.Pair) Test(org.junit.Test)

Example 7 with DateTime

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));
            }
        }
    }
}
Also used : Date(java.util.Date) DateTime(org.ovirt.engine.core.compat.DateTime) Tags(org.ovirt.engine.core.common.businessentities.Tags)

Example 8 with DateTime

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());
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) EngineCronTrigger(org.ovirt.engine.core.common.utils.EngineCronTrigger) Date(java.util.Date) DateTime(org.ovirt.engine.core.compat.DateTime) PostConstruct(javax.annotation.PostConstruct)

Example 9 with DateTime

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);
}
Also used : Guid(org.ovirt.engine.core.compat.Guid) LinkedList(java.util.LinkedList) DateTime(org.ovirt.engine.core.compat.DateTime) Date(java.util.Date) EngineLock(org.ovirt.engine.core.utils.lock.EngineLock)

Example 10 with DateTime

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();
}
Also used : DateEnumForSearch(org.ovirt.engine.core.common.businessentities.DateEnumForSearch) DateTime(org.ovirt.engine.core.compat.DateTime)

Aggregations

DateTime (org.ovirt.engine.core.compat.DateTime)10 Date (java.util.Date)7 Calendar (java.util.Calendar)3 LinkedList (java.util.LinkedList)3 Test (org.junit.Test)3 GregorianCalendar (java.util.GregorianCalendar)2 PostConstruct (javax.annotation.PostConstruct)2 EngineCronTrigger (org.ovirt.engine.core.common.utils.EngineCronTrigger)2 DateFormat (java.text.DateFormat)1 DateEnumForSearch (org.ovirt.engine.core.common.businessentities.DateEnumForSearch)1 Tags (org.ovirt.engine.core.common.businessentities.Tags)1 Pair (org.ovirt.engine.core.common.utils.Pair)1 Guid (org.ovirt.engine.core.compat.Guid)1 EngineLock (org.ovirt.engine.core.utils.lock.EngineLock)1