Search in sources :

Example 26 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStartAndEndFirst.

@Test
public void testQueryUsingNameAndStartAndEndFirst() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOrdering(Ordering.ASCENDING);
    criteria.setItemName(getItemName());
    criteria.setBeginDate(beforeStore);
    criteria.setEndDate(afterStore1);
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    Iterator<HistoricItem> iterator = iterable.iterator();
    HistoricItem actual1 = iterator.next();
    assertFalse(iterator.hasNext());
    assertStateEquals(getFirstItemState(), actual1.getState());
    assertTrue(actual1.getTimestamp().before(afterStore1));
    assertTrue(actual1.getTimestamp().after(beforeStore));
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Test(org.junit.Test)

Example 27 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStartNoMatch.

@Test
public void testQueryUsingNameAndStartNoMatch() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setItemName(getItemName());
    criteria.setBeginDate(afterStore2);
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    assertFalse(iterable.iterator().hasNext());
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Test(org.junit.Test)

Example 28 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStartAndEndWithEQOperator.

@Test
public void testQueryUsingNameAndStartAndEndWithEQOperator() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOperator(Operator.EQ);
    criteria.setState(getFirstItemState());
    criteria.setItemName(getItemName());
    criteria.setBeginDate(beforeStore);
    criteria.setEndDate(afterStore2);
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    Iterator<HistoricItem> iterator = iterable.iterator();
    HistoricItem actual1 = iterator.next();
    assertFalse(iterator.hasNext());
    assertStateEquals(getFirstItemState(), actual1.getState());
    assertTrue(actual1.getTimestamp().before(afterStore1));
    assertTrue(actual1.getTimestamp().after(beforeStore));
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Test(org.junit.Test)

Example 29 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class JdbcPostgresqlDAO method doGetHistItemFilterQuery.

@Override
public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount, String table, String name) {
    String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name);
    logger.debug("JDBC::doGetHistItemFilterQuery sql={}", sql);
    List<Object[]> m = Yank.queryObjectArrays(sql, null);
    List<HistoricItem> items = new ArrayList<HistoricItem>();
    for (int i = 0; i < m.size(); i++) {
        items.add(new JdbcItem(item.getName(), getState(item, m.get(i)[1]), objectAsDate(m.get(i)[0])));
    }
    return items;
}
Also used : ArrayList(java.util.ArrayList) JdbcItem(org.openhab.persistence.jdbc.model.JdbcItem) HistoricItem(org.openhab.core.persistence.HistoricItem)

Example 30 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class JdbcPersistenceService method query.

/**
     * Queries the {@link PersistenceService} for data with a given filter
     * criteria
     *
     * @param filter
     *            the filter to apply to the query
     * @return a time series of items
     */
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    if (!checkDBAccessability()) {
        logger.warn("JDBC::query: database not connected, query aborted for item '{}'", filter.getItemName());
        return Collections.emptyList();
    }
    if (itemRegistry == null) {
        logger.error("JDBC::query: itemRegistry == null. Ignore and give up!");
        return Collections.emptyList();
    }
    // Get the item name from the filter
    // Also get the Item object so we can determine the type
    Item item = null;
    String itemName = filter.getItemName();
    logger.debug("JDBC::query: item is {}", itemName);
    try {
        item = itemRegistry.getItem(itemName);
    } catch (ItemNotFoundException e1) {
        logger.error("JDBC::query: unable to get item for itemName: '{}'. Ignore and give up!", itemName);
        return Collections.emptyList();
    }
    if (item instanceof GroupItem) {
        // For Group Item is BaseItem needed to get correct Type of Value.
        item = GroupItem.class.cast(item).getBaseItem();
        logger.debug("JDBC::query: item is instanceof GroupItem '{}'", itemName);
        if (item == null) {
            logger.debug("JDBC::query: BaseItem of GroupItem is null. Ignore and give up!");
            return Collections.emptyList();
        }
        if (item instanceof GroupItem) {
            logger.debug("JDBC::query: BaseItem of GroupItem is a GroupItem too. Ignore and give up!");
            return Collections.emptyList();
        }
    }
    String table = sqlTables.get(itemName);
    if (table == null) {
        logger.warn("JDBC::query: unable to find table for query, no data in database for item '{}'. Current number of tables in the database: {}", itemName, sqlTables.size());
        // if enabled, table will be created immediately
        if (item != null) {
            logger.warn("JDBC::query: try to generate the table for item '{}'", itemName);
            table = getTable(item);
        } else {
            logger.warn("JDBC::query: no way to generate the table for item '{}'", itemName);
            return Collections.emptyList();
        }
    }
    long timerStart = System.currentTimeMillis();
    List<HistoricItem> items = new ArrayList<HistoricItem>();
    items = getHistItemFilterQuery(filter, conf.getNumberDecimalcount(), table, item);
    logger.debug("JDBC::query: query for {} returned {} rows in {} ms", item.getName(), items.size(), System.currentTimeMillis() - timerStart);
    // Success
    errCnt = 0;
    return items;
}
Also used : GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) HistoricItem(org.openhab.core.persistence.HistoricItem) ArrayList(java.util.ArrayList) GroupItem(org.openhab.core.items.GroupItem) HistoricItem(org.openhab.core.persistence.HistoricItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Aggregations

HistoricItem (org.openhab.core.persistence.HistoricItem)34 FilterCriteria (org.openhab.core.persistence.FilterCriteria)19 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)12 Item (org.openhab.core.items.Item)6 State (org.openhab.core.types.State)5 Date (java.util.Date)4 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)4 DecimalType (org.openhab.core.library.types.DecimalType)4 JdbcItem (org.openhab.persistence.jdbc.model.JdbcItem)3 Calendar (java.util.Calendar)2 GroupItem (org.openhab.core.items.GroupItem)2 ColorItem (org.openhab.core.library.items.ColorItem)2 ContactItem (org.openhab.core.library.items.ContactItem)2 DateTimeItem (org.openhab.core.library.items.DateTimeItem)2 DimmerItem (org.openhab.core.library.items.DimmerItem)2 NumberItem (org.openhab.core.library.items.NumberItem)2 RollershutterItem (org.openhab.core.library.items.RollershutterItem)2 SwitchItem (org.openhab.core.library.items.SwitchItem)2 DateTimeType (org.openhab.core.library.types.DateTimeType)2