Search in sources :

Example 1 with FilteredStreamWithTotal

use of com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal in project ma-modules-public by infiniteautomation.

the class JsonRestController method query.

@RequestMapping(method = RequestMethod.GET, value = "/query/{xid}")
public ArrayWithTotal<Stream<JsonNode>> query(@PathVariable String xid, HttpServletRequest request, ASTNode rql) throws UnsupportedEncodingException {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String pointer = path.endsWith("/") ? "/" : "";
    ArrayNode items = this.jsonDataService.valuesForDataAtPointer(xid, pointer);
    return new FilteredStreamWithTotal<>(items, new RQLFilterJsonNode(rql));
}
Also used : RQLFilterJsonNode(com.infiniteautomation.mango.db.query.pojo.RQLFilterJsonNode) FilteredStreamWithTotal(com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with FilteredStreamWithTotal

use of com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal in project ma-modules-public by infiniteautomation.

the class EventTypesRestController method queryAllEventTypes.

@ApiOperation(value = "Query all available event types", notes = "Not specific to any reference ids, results come back based on type/sub-type combinations", response = EventTypeVOModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET)
public StreamWithTotal<EventTypeVOModel<?, ?, ?>> queryAllEventTypes(@AuthenticationPrincipal PermissionHolder user, Translations translations, ASTNode query) {
    List<EventTypeVOModel<?, ?, ?>> models = new ArrayList<>();
    // Data Source
    DataSourceEventType dset = new DataSourceEventType(0, 0);
    DataSourceEventTypeModel dsetm = new DataSourceEventTypeModel(dset);
    dsetm.setAlarmLevel(null);
    EventTypeVOModel<?, ?, ?> dataSource = new EventTypeVOModel<DataSourceEventType, AbstractDataSourceModel<?>, String>(dsetm, new TranslatableMessage("eventHandlers.dataSourceEvents"), false, true, true);
    dataSource.setAlarmLevel(null);
    models.add(dataSource);
    // Data point
    DataPointEventType dpet = new DataPointEventType(0, 0);
    DataPointEventTypeModel dpetm = new DataPointEventTypeModel(dpet);
    EventTypeVOModel<?, ?, ?> dataPoint = new EventTypeVOModel<DataPointEventType, DataPointModel, AbstractPointEventDetectorModel<?>>(dpetm, new TranslatableMessage("eventHandlers.pointEventDetector"), false, true, true);
    dataPoint.setAlarmLevel(null);
    models.add(dataPoint);
    // Publisher
    PublisherEventType pet = new PublisherEventType(0, 0);
    PublisherEventTypeModel petm = new PublisherEventTypeModel(pet);
    EventTypeVOModel<?, ?, ?> publisher = new EventTypeVOModel<PublisherEventType, AbstractPublisherModel<?, ?>, String>(petm, new TranslatableMessage("eventHandlers.publisherEvents"), false, true, true);
    publisher.setAlarmLevel(null);
    models.add(publisher);
    // System
    // TODO Permissions
    // TODO since we some system event types support referenceId1 and 2 we can't be sure here
    SystemEventType set = new SystemEventType();
    SystemEventTypeModel setm = new SystemEventTypeModel(set);
    EventTypeVOModel<?, ?, ?> system = new EventTypeVOModel<>(setm, new TranslatableMessage("eventHandlers.systemEvents"), null, true, true, true);
    models.add(system);
    // Audit
    // TODO Permissions
    // TODO Audit Event Types will eventually support ref1 & 2
    AuditEventType aet = new AuditEventType();
    AuditEventTypeModel aetm = new AuditEventTypeModel(aet);
    EventTypeVOModel<?, ?, ?> audit = new EventTypeVOModel<>(aetm, new TranslatableMessage("eventHandlers.auditEvents"), null, true, false, false);
    models.add(audit);
    // Module defined
    for (EventTypeDefinition def : ModuleRegistry.getDefinitions(EventTypeDefinition.class)) {
        if (!def.hasCreatePermission(user, permissionService))
            continue;
        EventType et = def.createDefaultEventType();
        AbstractEventTypeModel<?, ?, ?> model = modelMapper.map(et, AbstractEventTypeModel.class, user);
        models.add(new EventTypeVOModel<>(model, new TranslatableMessage(def.getDescriptionKey()), def.supportsSubType(), def.supportsReferenceId1(), def.supportsReferenceId2()));
    }
    return new FilteredStreamWithTotal<>(models, query, translations);
}
Also used : DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) DataSourceEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.DataSourceEventTypeModel) PublisherEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.PublisherEventTypeModel) ArrayList(java.util.ArrayList) FilteredStreamWithTotal(com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) DataPointEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.DataPointEventTypeModel) AuditEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.AuditEventTypeModel) SystemEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.SystemEventTypeModel) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EventTypeVOModel(com.infiniteautomation.mango.rest.latest.model.event.EventTypeVOModel) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with FilteredStreamWithTotal

use of com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal in project ma-modules-public by infiniteautomation.

the class JsonRestController method queryAtPointer.

@RequestMapping(method = RequestMethod.GET, value = "/query/{xid}/**")
public ArrayWithTotal<Stream<JsonNode>> queryAtPointer(@PathVariable String xid, @ApiIgnore @RemainingPath String path, ASTNode rql) throws UnsupportedEncodingException {
    String pointer = "/" + path;
    ArrayNode items = this.jsonDataService.valuesForDataAtPointer(xid, pointer);
    return new FilteredStreamWithTotal<>(items, new RQLFilterJsonNode(rql));
}
Also used : RQLFilterJsonNode(com.infiniteautomation.mango.db.query.pojo.RQLFilterJsonNode) FilteredStreamWithTotal(com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with FilteredStreamWithTotal

use of com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal in project ma-modules-public by infiniteautomation.

the class EventsWebSocketHandler method handleTextMessage.

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
    try {
        JsonNode tree = this.jacksonMapper.readTree(message.getPayload());
        if (!WebSocketMessageType.REQUEST.messageTypeMatches(tree) || tree.get("requestType") == null) {
            return;
        }
        EventsWebsocketRequest request = this.jacksonMapper.treeToValue(tree, EventsWebsocketRequest.class);
        request.ensureValid();
        if (request instanceof EventsSubscriptionRequest) {
            EventsSubscriptionRequest subscription = (EventsSubscriptionRequest) request;
            Set<AlarmLevels> levels = subscription.getLevels();
            if (levels == null) {
                levels = Collections.emptySet();
            }
            Set<EventActionEnum> actions = subscription.getActions();
            if (actions == null) {
                actions = EnumSet.noneOf(EventActionEnum.class);
            }
            boolean emptySubscriptions = levels.isEmpty() || actions.isEmpty();
            synchronized (this.lock) {
                // Configure listener
                Boolean subscribed = (Boolean) session.getAttributes().get(SUBSCRIPTION_ATTRIBUTE);
                if (subscribed == null || subscribed == Boolean.FALSE) {
                    if (!emptySubscriptions) {
                        changeLevels(levels);
                        changeActions(actions);
                        initialize();
                        session.getAttributes().put(SUBSCRIPTION_ATTRIBUTE, Boolean.TRUE);
                    }
                } else {
                    if (emptySubscriptions) {
                        terminate();
                        session.getAttributes().put(SUBSCRIPTION_ATTRIBUTE, Boolean.FALSE);
                    } else {
                        changeActions(actions);
                        changeLevels(levels);
                    }
                }
            }
            EventsSubscriptionResponse response = new EventsSubscriptionResponse();
            if (subscription.isSendActiveSummary()) {
                List<UserEventLevelSummary> summaries = service.getActiveSummary();
                List<EventLevelSummaryModel> models = summaries.stream().map(s -> {
                    EventInstanceModel instanceModel = s.getLatest() != null ? modelMapper.map(s.getLatest(), EventInstanceModel.class, user) : null;
                    return new EventLevelSummaryModel(s.getAlarmLevel(), s.getCount(), instanceModel);
                }).collect(Collectors.toList());
                response.setActiveSummary(models);
            }
            if (subscription.isSendUnacknowledgedSummary()) {
                List<UserEventLevelSummary> summaries = service.getUnacknowledgedSummary();
                List<EventLevelSummaryModel> models = summaries.stream().map(s -> {
                    EventInstanceModel instanceModel = s.getLatest() != null ? modelMapper.map(s.getLatest(), EventInstanceModel.class, user) : null;
                    return new EventLevelSummaryModel(s.getAlarmLevel(), s.getCount(), instanceModel);
                }).collect(Collectors.toList());
                response.setUnacknowledgedSummary(models);
            }
            this.sendRawMessage(session, new WebSocketResponse<>(request.getSequenceNumber(), response));
        } else if (request instanceof EventsDataPointSummaryRequest) {
            EventsDataPointSummaryRequest query = (EventsDataPointSummaryRequest) request;
            WebSocketResponse<List<DataPointEventSummaryModel>> response = new WebSocketResponse<>(request.getSequenceNumber());
            Collection<DataPointEventLevelSummary> summaries = service.getDataPointEventSummaries(query.getDataPointXids());
            List<DataPointEventSummaryModel> models = summaries.stream().map(s -> new DataPointEventSummaryModel(s.getXid(), s.getCounts())).collect(Collectors.toList());
            response.setPayload(models);
            this.sendRawMessage(session, response);
        } else if (request instanceof AllActiveEventsRequest) {
            WebSocketResponse<List<EventInstanceModel>> response = new WebSocketResponse<>(request.getSequenceNumber());
            List<EventInstance> active = service.getAllActiveUserEvents();
            List<EventInstanceModel> models = new ArrayList<>(active.size());
            for (EventInstance vo : active) {
                models.add(modelMapper.map(vo, EventInstanceModel.class, user));
            }
            response.setPayload(models);
            this.sendRawMessage(session, response);
        } else if (request instanceof ActiveEventsQuery) {
            List<EventInstance> active = service.getAllActiveUserEvents();
            List<EventInstanceModel> models = new ArrayList<>(active.size());
            for (EventInstance vo : active) {
                models.add(modelMapper.map(vo, EventInstanceModel.class, user));
            }
            String query = ((ActiveEventsQuery) request).getQuery();
            ASTNode rql = RQLUtils.parseRQLtoAST(query);
            WebSocketResponse<ArrayWithTotal<Stream<EventInstanceModel>>> response = new WebSocketResponse<>(request.getSequenceNumber());
            response.setPayload(new FilteredStreamWithTotal<>(models, rql, user.getTranslations()));
            this.sendRawMessage(session, response);
        }
    } catch (NotFoundException e) {
        try {
            this.sendErrorMessage(session, MangoWebSocketErrorType.NOT_FOUND, e.getTranslatableMessage());
        } catch (Exception e1) {
            log.error(e.getMessage(), e);
        }
    } catch (PermissionException e) {
        try {
            this.sendErrorMessage(session, MangoWebSocketErrorType.PERMISSION_DENIED, e.getTranslatableMessage());
        } catch (Exception e1) {
            log.error(e.getMessage(), e);
        }
    } catch (Exception e) {
        try {
            this.sendErrorMessage(session, MangoWebSocketErrorType.SERVER_ERROR, new TranslatableMessage("rest.error.serverError", e.getMessage()));
        } catch (Exception e1) {
            log.error(e.getMessage(), e);
        }
    }
}
Also used : FilteredStreamWithTotal(com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) EventInstance(com.serotonin.m2m2.rt.event.EventInstance) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Id(com.fasterxml.jackson.annotation.JsonTypeInfo.Id) ArrayWithTotal(com.infiniteautomation.mango.rest.latest.model.ArrayWithTotal) DataPointEventSummaryModel(com.infiniteautomation.mango.rest.latest.model.event.DataPointEventSummaryModel) WebSocketSession(org.springframework.web.socket.WebSocketSession) CloseStatus(org.springframework.web.socket.CloseStatus) ArrayList(java.util.ArrayList) RestModelMapper(com.infiniteautomation.mango.rest.latest.model.RestModelMapper) UserEventListener(com.serotonin.m2m2.rt.event.UserEventListener) TextMessage(org.springframework.web.socket.TextMessage) As(com.fasterxml.jackson.annotation.JsonTypeInfo.As) DataPointEventLevelSummary(com.serotonin.m2m2.rt.event.DataPointEventLevelSummary) JsonNode(com.fasterxml.jackson.databind.JsonNode) Validatable(com.serotonin.m2m2.vo.Validatable) EnumSet(java.util.EnumSet) Logger(org.slf4j.Logger) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) Common(com.serotonin.m2m2.Common) Collection(java.util.Collection) EventInstanceModel(com.infiniteautomation.mango.rest.latest.model.event.EventInstanceModel) RQLUtils(com.infiniteautomation.mango.util.RQLUtils) Set(java.util.Set) EventInstanceService(com.infiniteautomation.mango.spring.service.EventInstanceService) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) UserEventLevelSummary(com.serotonin.m2m2.rt.event.UserEventLevelSummary) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) List(java.util.List) Stream(java.util.stream.Stream) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) ASTNode(net.jazdw.rql.parser.ASTNode) EventLevelSummaryModel(com.infiniteautomation.mango.rest.latest.model.event.EventLevelSummaryModel) EventActionEnum(com.infiniteautomation.mango.rest.latest.model.event.EventActionEnum) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels) Collections(java.util.Collections) User(com.serotonin.m2m2.vo.User) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) Type(com.fasterxml.jackson.annotation.JsonSubTypes.Type) EventInstance(com.serotonin.m2m2.rt.event.EventInstance) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) EventActionEnum(com.infiniteautomation.mango.rest.latest.model.event.EventActionEnum) EventLevelSummaryModel(com.infiniteautomation.mango.rest.latest.model.event.EventLevelSummaryModel) ArrayList(java.util.ArrayList) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) EventInstanceModel(com.infiniteautomation.mango.rest.latest.model.event.EventInstanceModel) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) FilteredStreamWithTotal(com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal) UserEventLevelSummary(com.serotonin.m2m2.rt.event.UserEventLevelSummary) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DataPointEventSummaryModel(com.infiniteautomation.mango.rest.latest.model.event.DataPointEventSummaryModel) Collection(java.util.Collection) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels)

Aggregations

FilteredStreamWithTotal (com.infiniteautomation.mango.rest.latest.model.FilteredStreamWithTotal)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 RQLFilterJsonNode (com.infiniteautomation.mango.db.query.pojo.RQLFilterJsonNode)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 ArrayList (java.util.ArrayList)2 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 Type (com.fasterxml.jackson.annotation.JsonSubTypes.Type)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 As (com.fasterxml.jackson.annotation.JsonTypeInfo.As)1 Id (com.fasterxml.jackson.annotation.JsonTypeInfo.Id)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayWithTotal (com.infiniteautomation.mango.rest.latest.model.ArrayWithTotal)1 RestModelMapper (com.infiniteautomation.mango.rest.latest.model.RestModelMapper)1 AuditEventTypeModel (com.infiniteautomation.mango.rest.latest.model.event.AuditEventTypeModel)1 DataPointEventSummaryModel (com.infiniteautomation.mango.rest.latest.model.event.DataPointEventSummaryModel)1 DataPointEventTypeModel (com.infiniteautomation.mango.rest.latest.model.event.DataPointEventTypeModel)1 DataSourceEventTypeModel (com.infiniteautomation.mango.rest.latest.model.event.DataSourceEventTypeModel)1 EventActionEnum (com.infiniteautomation.mango.rest.latest.model.event.EventActionEnum)1 EventInstanceModel (com.infiniteautomation.mango.rest.latest.model.event.EventInstanceModel)1