Search in sources :

Example 1 with DataPoints

use of com.infiniteautomation.mango.db.tables.DataPoints in project ma-core-public by infiniteautomation.

the class EventInstanceDao method queryDataPointEventCountsByRQL.

/**
 * @param conditions supplied by {@link #createEventCountsConditions(net.jazdw.rql.parser.ASTNode)}
 * @param from from timestamp
 * @param to to timestamp
 * @param user user executing the query
 * @param callback result consumer
 */
public void queryDataPointEventCountsByRQL(@NonNull ConditionSortLimitWithTagKeys conditions, @Nullable Long from, @Nullable Long to, @NonNull PermissionHolder user, @NonNull Consumer<AlarmPointTagCount> callback) {
    Table<Record6<Integer, String, Integer, Integer, Long, Long>> eventCountsTable = eventCountsTable(from, to, user);
    Map<String, Field<String>> tagFields = conditions.getTagFields();
    SelectJoinStep<Record> joinStep = create.select(eventCountsFields.values()).select(tagFields.values()).from(eventCountsTable).leftOuterJoin(dataPoints).on(dataPoints.id.equal(eventCountsTable.field(table.typeRef1)));
    SelectConditionStep<Record> conditionStep = dataPointTagsDao.joinTags(joinStep, dataPoints.id, tagFields).where(conditions.getCondition());
    Select<Record> select = applySortLimitOffset(conditionStep, conditions);
    try (Stream<Record> stream = select.stream()) {
        stream.map(record -> {
            AlarmPointTagCount result = new AlarmPointTagCount();
            result.setXid(record.get(dataPoints.xid));
            result.setName(record.get(dataPoints.name));
            result.setDeviceName(record.get(dataPoints.deviceName));
            result.setMessage(readTranslatableMessage(record.get(eventCountsTable.field(table.message))));
            result.setAlarmLevel(AlarmLevels.fromValue(record.get(eventCountsTable.field(table.alarmLevel))));
            result.setCount(record.get(eventCountsTable.field(count)));
            result.setLatestActiveTs(record.get(eventCountsTable.field(latestActive)));
            result.setLatestRtnTs(record.get(eventCountsTable.field(latestRtn)));
            Map<String, String> tags = new HashMap<>();
            for (Entry<String, Field<String>> entry : tagFields.entrySet()) {
                String value = record.get(entry.getValue());
                if (value != null) {
                    tags.put(entry.getKey(), value);
                }
            }
            result.setTags(tags);
            return result;
        }).forEach(callback);
    }
}
Also used : UserCommentVO(com.serotonin.m2m2.vo.comment.UserCommentVO) DSL(org.jooq.impl.DSL) EventTypeNames(com.serotonin.m2m2.rt.event.type.EventType.EventTypeNames) MissingEventType(com.serotonin.m2m2.rt.event.type.MissingEventType) Table(org.jooq.Table) Autowired(org.springframework.beans.factory.annotation.Autowired) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Condition(org.jooq.Condition) DataPoints(com.infiniteautomation.mango.db.tables.DataPoints) Record1(org.jooq.Record1) Locale(java.util.Locale) Map(java.util.Map) ConditionSortLimitWithTagKeys(com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys) Repository(org.springframework.stereotype.Repository) SortOrder(org.jooq.SortOrder) Record6(org.jooq.Record6) Select(org.jooq.Select) ReturnCause(com.serotonin.m2m2.rt.event.ReturnCause) Name(org.jooq.Name) LazyInitSupplier(com.infiniteautomation.mango.util.LazyInitSupplier) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Field(org.jooq.Field) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) ModuleRegistry(com.serotonin.m2m2.module.ModuleRegistry) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) List(java.util.List) Stream(java.util.stream.Stream) UserComments(com.infiniteautomation.mango.db.tables.UserComments) ASTNode(net.jazdw.rql.parser.ASTNode) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) Entry(java.util.Map.Entry) SelectConnectByStep(org.jooq.SelectConnectByStep) RQLToConditionWithTagKeys(com.infiniteautomation.mango.db.query.RQLToConditionWithTagKeys) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels) SelectSelectStep(org.jooq.SelectSelectStep) SelectJoinStep(org.jooq.SelectJoinStep) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Users(com.infiniteautomation.mango.db.tables.Users) ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit) HashMap(java.util.HashMap) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) Function(java.util.function.Function) RQLOperation(com.infiniteautomation.mango.db.query.RQLOperation) Events(com.infiniteautomation.mango.db.tables.Events) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) SelectConditionStep(org.jooq.SelectConditionStep) RQLToCondition(com.infiniteautomation.mango.db.query.RQLToCondition) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) RQLSubSelectCondition(com.infiniteautomation.mango.db.query.RQLSubSelectCondition) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Record(org.jooq.Record) Common(com.serotonin.m2m2.Common) EventInstanceVO(com.serotonin.m2m2.vo.event.EventInstanceVO) Consumer(java.util.function.Consumer) DaoDependencies(com.infiniteautomation.mango.spring.DaoDependencies) AlarmPointTagCount(com.infiniteautomation.mango.spring.service.EventInstanceService.AlarmPointTagCount) SortField(org.jooq.SortField) EventsRecord(com.infiniteautomation.mango.db.tables.records.EventsRecord) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) Collections(java.util.Collections) EventsSuperadminViewPermissionDefinition(com.serotonin.m2m2.module.definitions.permissions.EventsSuperadminViewPermissionDefinition) AlarmPointTagCount(com.infiniteautomation.mango.spring.service.EventInstanceService.AlarmPointTagCount) Field(org.jooq.Field) SortField(org.jooq.SortField) Entry(java.util.Map.Entry) Record6(org.jooq.Record6) Record(org.jooq.Record) EventsRecord(com.infiniteautomation.mango.db.tables.records.EventsRecord) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with DataPoints

use of com.infiniteautomation.mango.db.tables.DataPoints in project ma-core-public by infiniteautomation.

the class PointValueDaoSQL method topPointHistoryCounts.

@Override
public List<PointHistoryCount> topPointHistoryCounts(int limit) {
    PointValueDao.validateLimit(limit);
    DataPoints points = DataPoints.DATA_POINTS;
    DataSources dataSources = DataSources.DATA_SOURCES;
    Field<Integer> count = DSL.count().as("count");
    return create.select(count).select(dataPointDao.getSelectFields()).from(pv).innerJoin(points).on(points.id.eq(pv.dataPointId)).leftJoin(dataSources).on(dataSources.id.eq(points.dataSourceId)).groupBy(points.id, dataSources.name, dataSources.xid, dataSources.dataSourceType).orderBy(count.desc()).limit(limit).fetch(record -> {
        DataPointVO point = dataPointDao.mapRecord(record);
        dataPointDao.loadRelationalData(point);
        return new PointHistoryCount(point, record.get(count));
    });
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PointHistoryCount(com.serotonin.m2m2.vo.bean.PointHistoryCount) DataSources(com.infiniteautomation.mango.db.tables.DataSources) DataPoints(com.infiniteautomation.mango.db.tables.DataPoints)

Example 3 with DataPoints

use of com.infiniteautomation.mango.db.tables.DataPoints in project ma-core-public by infiniteautomation.

the class DataPointDaoTest method testDeletePointsAndSeriesIds.

@Test
public void testDeletePointsAndSeriesIds() {
    // Insert some points that won't go away for this test
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        dao.insert(point);
    }
    // Insert points to delete
    List<IDataPoint> points = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        dao.insert(point);
        points.add(point);
    }
    DatabaseProxy proxy = Common.getBean(DatabaseProxy.class);
    DataPoints dataPoints = DataPoints.DATA_POINTS;
    TimeSeries timeSeries = TimeSeries.TIME_SERIES;
    // Delete the data points but not the series id
    for (IDataPoint point : points) {
        dao.delete((DataPointVO) point);
    }
    // Ensure the series Ids are gone via the delete post relational
    for (IDataPoint point : points) {
        assertEquals(0, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
    // delete the orphaned series ids (none)
    assertEquals(0, dao.deleteOrphanedTimeSeries());
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TimeSeries(com.infiniteautomation.mango.db.tables.TimeSeries) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) ArrayList(java.util.ArrayList) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) DataPoints(com.infiniteautomation.mango.db.tables.DataPoints) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 4 with DataPoints

use of com.infiniteautomation.mango.db.tables.DataPoints in project ma-core-public by infiniteautomation.

the class DataPointDaoTest method testDeleteOrphanedSeries.

@Test
public void testDeleteOrphanedSeries() {
    // Insert some points that won't go away for this test
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        dao.insert(point);
    }
    // Insert points to delete
    List<IDataPoint> points = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        dao.insert(point);
        points.add(point);
    }
    DatabaseProxy proxy = Common.getBean(DatabaseProxy.class);
    DataPoints dataPoints = DataPoints.DATA_POINTS;
    TimeSeries timeSeries = TimeSeries.TIME_SERIES;
    // Delete the data points but not the series id
    for (IDataPoint point : points) {
        proxy.getContext().deleteFrom(dataPoints).where(dataPoints.id.eq(point.getId())).execute();
    }
    // Ensure the series Ids are still there
    for (IDataPoint point : points) {
        assertEquals(1, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
    // delete the orphaned series id and ensure it is gone
    assertEquals(5, dao.deleteOrphanedTimeSeries());
    // Ensure the series Id are gone
    for (IDataPoint point : points) {
        assertEquals(0, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TimeSeries(com.infiniteautomation.mango.db.tables.TimeSeries) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) ArrayList(java.util.ArrayList) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) DataPoints(com.infiniteautomation.mango.db.tables.DataPoints) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 5 with DataPoints

use of com.infiniteautomation.mango.db.tables.DataPoints in project ma-core-public by infiniteautomation.

the class DataPointDaoTest method testDeleteOrphanedSeriesUsingCustomSeriesId.

@Test
public void testDeleteOrphanedSeriesUsingCustomSeriesId() {
    DatabaseProxy proxy = Common.getBean(DatabaseProxy.class);
    DataPoints dataPoints = DataPoints.DATA_POINTS;
    TimeSeries timeSeries = TimeSeries.TIME_SERIES;
    int seriesIdCounter = 10;
    // Insert some points that won't go away for this test
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        proxy.getContext().insertInto(timeSeries).columns(timeSeries.id).values(seriesIdCounter).execute();
        point.setSeriesId(seriesIdCounter++);
        dao.insert(point);
    }
    // Insert points to delete
    List<IDataPoint> points = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        DataPointVO point = newVO();
        proxy.getContext().insertInto(timeSeries).columns(timeSeries.id).values(seriesIdCounter).execute();
        point.setSeriesId(seriesIdCounter++);
        dao.insert(point);
        points.add(point);
    }
    // Delete the data points but not the series id
    for (IDataPoint point : points) {
        proxy.getContext().deleteFrom(dataPoints).where(dataPoints.id.eq(point.getId())).execute();
    }
    // Ensure the series Ids are still there
    for (IDataPoint point : points) {
        assertEquals(1, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
    // delete the orphaned series id and ensure it is gone
    assertEquals(5, dao.deleteOrphanedTimeSeries());
    // Ensure the series Id are gone
    for (IDataPoint point : points) {
        assertEquals(0, proxy.getContext().selectCount().from(timeSeries).where(timeSeries.id.eq(point.getSeriesId())).fetchSingle().value1().intValue());
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TimeSeries(com.infiniteautomation.mango.db.tables.TimeSeries) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) ArrayList(java.util.ArrayList) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) DataPoints(com.infiniteautomation.mango.db.tables.DataPoints) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Aggregations

DataPoints (com.infiniteautomation.mango.db.tables.DataPoints)5 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)4 ArrayList (java.util.ArrayList)4 TimeSeries (com.infiniteautomation.mango.db.tables.TimeSeries)3 DatabaseProxy (com.serotonin.m2m2.db.DatabaseProxy)3 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)3 Test (org.junit.Test)3 ConditionSortLimit (com.infiniteautomation.mango.db.query.ConditionSortLimit)1 ConditionSortLimitWithTagKeys (com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys)1 RQLOperation (com.infiniteautomation.mango.db.query.RQLOperation)1 RQLSubSelectCondition (com.infiniteautomation.mango.db.query.RQLSubSelectCondition)1 RQLToCondition (com.infiniteautomation.mango.db.query.RQLToCondition)1 RQLToConditionWithTagKeys (com.infiniteautomation.mango.db.query.RQLToConditionWithTagKeys)1 DataSources (com.infiniteautomation.mango.db.tables.DataSources)1 Events (com.infiniteautomation.mango.db.tables.Events)1 UserComments (com.infiniteautomation.mango.db.tables.UserComments)1 Users (com.infiniteautomation.mango.db.tables.Users)1 EventsRecord (com.infiniteautomation.mango.db.tables.records.EventsRecord)1 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)1 DaoDependencies (com.infiniteautomation.mango.spring.DaoDependencies)1