Search in sources :

Example 16 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService in project ma-core-public by infiniteautomation.

the class MangoTestBase method createMockDataPoint.

protected DataPointVO createMockDataPoint(int id, String xid, String name, String deviceName, boolean enabled, int dataSourceId, String dataSourceXid, MangoPermission readPermission, MangoPermission setPermission, MockPointLocatorVO vo) {
    DataPointService service = Common.getBean(DataPointService.class);
    DataPointVO dp = new DataPointVO();
    dp.setId(id);
    dp.setXid(xid);
    dp.setName(name);
    dp.setDeviceName(deviceName);
    dp.setEnabled(enabled);
    dp.setPointLocator(vo);
    dp.setDataSourceId(dataSourceId);
    dp.setDataSourceXid(dataSourceXid);
    dp.setReadPermission(readPermission);
    dp.setSetPermission(setPermission);
    try {
        return service.insert(dp);
    } catch (ValidationException e) {
        StringBuilder failureMessage = new StringBuilder();
        for (ProcessMessage m : e.getValidationResult().getMessages()) {
            String messagePart = m.getContextKey() + " -> " + m.getContextualMessage().translate(Common.getTranslations()) + "\n";
            failureMessage.append(messagePart);
        }
        fail(failureMessage.toString());
        return null;
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Example 17 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService in project ma-core-public by infiniteautomation.

the class MangoTestBase method createMockDataPoint.

protected DataPointVO createMockDataPoint(MockDataSourceVO ds, Consumer<DataPointVO> customizer) {
    DataPointService service = Common.getBean(DataPointService.class);
    DataPointVO dp = new DataPointVO();
    dp.setName(UUID.randomUUID().toString());
    dp.setDeviceName(ds.getName());
    dp.setPointLocator(new MockPointLocatorVO(DataType.NUMERIC, true));
    dp.setDataSourceId(ds.getId());
    customizer.accept(dp);
    try {
        return service.insert(dp);
    } catch (ValidationException e) {
        fail(e.getValidationErrorMessage(Common.getTranslations()));
        return null;
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)

Example 18 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService in project ma-modules-public by infiniteautomation.

the class PointValueRestController method getStatistics.

@ApiOperation(value = "GET statistics for data point(s) over the given time range", notes = "From time inclusive, To time exclusive. Returns map of xid to Statistics object", response = PointValueTimeModel.class, responseContainer = "Map")
@RequestMapping(method = RequestMethod.GET, value = "/statistics/{xids}")
public Map<String, StreamingPointValueTimeModel> getStatistics(@ApiParam(value = "Point xids", required = true, allowMultiple = true) @PathVariable String[] xids, @ApiParam(value = "From time") @RequestParam(value = "from", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime from, @ApiParam(value = "To time") @RequestParam(value = "to", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime to, @ApiParam(value = "Time zone") @RequestParam(value = "timezone", required = false) String timezone, @ApiParam(value = "Date Time format pattern for timestamps as strings, if not included epoch milli number is used") @RequestParam(value = "dateTimeFormat", required = false) String dateTimeFormat, @ApiParam(value = "Fields to be included in the returned data, default is TIMESTAMP,VALUE") @RequestParam(required = false) PointValueField[] fields, Locale locale) {
    var points = Arrays.stream(xids).distinct().map(dataPointService::get).collect(Collectors.toUnmodifiableSet());
    var mapperBuilder = new StreamMapperBuilder().withDataPoints(points).withRollup(RollupEnum.ALL).withFields(fields).withDateTimeFormat(dateTimeFormat).withTimezone(timezone, from, to).withLocale(locale).withTimestampSource(TimestampSource.STATISTIC);
    var aggregateMapper = mapperBuilder.build(AggregateValueMapper::new);
    var rollupPeriod = Duration.between(from, to);
    return points.stream().collect(Collectors.toUnmodifiableMap(DataPointVO::getXid, point -> {
        var aggregateDao = dao.getAggregateDao();
        try (var stream = aggregateDao.query(point, from, to, null, rollupPeriod)) {
            return stream.map(aggregateMapper).findAny().orElseThrow();
        }
    }));
}
Also used : Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) ZonedDateTime(java.time.ZonedDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) ApiParam(io.swagger.annotations.ApiParam) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) Duration(java.time.Duration) Map(java.util.Map) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) PointValueTimeModel(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeModel) RuntimeManager(com.serotonin.m2m2.rt.RuntimeManager) XidTimeRangeQueryModel(com.infiniteautomation.mango.rest.latest.model.pointValue.query.XidTimeRangeQueryModel) RestController(org.springframework.web.bind.annotation.RestController) ZoneId(java.time.ZoneId) Stream(java.util.stream.Stream) DateUtils(com.serotonin.m2m2.util.DateUtils) TimestampSource(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.TimestampSource) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) StreamingMultiPointModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.StreamingMultiPointModel) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) DataType(com.serotonin.m2m2.DataType) PointValueTimeCacheControl(com.infiniteautomation.mango.rest.latest.model.pointValue.query.PointValueTimeCacheControl) MergingIterator(com.infiniteautomation.mango.db.iterators.MergingIterator) AggregateValueMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.AggregateValueMapper) TemporalAmount(java.time.temporal.TemporalAmount) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) Api(io.swagger.annotations.Api) TemporaryResourceWebSocketHandler(com.infiniteautomation.mango.rest.latest.temporaryResource.TemporaryResourceWebSocketHandler) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) PointValueField(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField) Common(com.serotonin.m2m2.Common) StreamingPointValueTimeModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.StreamingPointValueTimeModel) XidLatestQueryInfoModel(com.infiniteautomation.mango.rest.latest.model.pointValue.query.XidLatestQueryInfoModel) HttpStatus(org.springframework.http.HttpStatus) SystemSettingsDao(com.serotonin.m2m2.db.dao.SystemSettingsDao) RTException(com.serotonin.m2m2.rt.RTException) RollupEnum(com.infiniteautomation.mango.rest.latest.model.pointValue.RollupEnum) PointValueImportResult(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueImportResult) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) PathVariable(org.springframework.web.bind.annotation.PathVariable) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) PurgePointValuesResponseModel(com.infiniteautomation.mango.rest.latest.model.pointValue.PurgePointValuesResponseModel) LoggerFactory(org.slf4j.LoggerFactory) GenericRestException(com.infiniteautomation.mango.rest.latest.exception.GenericRestException) ExpandTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) ApiOperation(io.swagger.annotations.ApiOperation) StreamMapperBuilder(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.StreamMapperBuilder) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) Locale(java.util.Locale) SimplifyUtility(com.goebl.simplify.SimplifyUtility) ITime(com.serotonin.m2m2.view.stats.ITime) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) URI(java.net.URI) PointValueTimePointExtractor(com.infiniteautomation.mango.rest.pointextractor.PointValueTimePointExtractor) TemporaryResourceStatusUpdate(com.infiniteautomation.mango.rest.latest.temporaryResource.TemporaryResourceStatusUpdate) UnitConverter(javax.measure.converter.UnitConverter) TimePeriodType(com.infiniteautomation.mango.rest.latest.model.time.TimePeriodType) PurgeDataPointValuesModel(com.infiniteautomation.mango.rest.latest.model.pointValue.PurgeDataPointValuesModel) XidRollupTimeRangeQueryModel(com.infiniteautomation.mango.rest.latest.model.pointValue.query.XidRollupTimeRangeQueryModel) TimeZone(java.util.TimeZone) HttpHeaders(org.springframework.http.HttpHeaders) SetPointSource(com.serotonin.m2m2.rt.dataImage.SetPointSource) Collection(java.util.Collection) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ISO(org.springframework.format.annotation.DateTimeFormat.ISO) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) List(java.util.List) Environment(org.springframework.core.env.Environment) Entry(java.util.Map.Entry) Optional(java.util.Optional) TimeOrder(com.serotonin.m2m2.db.dao.pointvalue.TimeOrder) LegacyXidPointValueTimeModel(com.infiniteautomation.mango.rest.latest.model.pointValue.LegacyXidPointValueTimeModel) User(com.serotonin.m2m2.vo.User) TimestampGrouper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.TimestampGrouper) NotFoundRestException(com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TemporaryResourceStatus(com.infiniteautomation.mango.rest.latest.temporaryResource.TemporaryResource.TemporaryResourceStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) Function(java.util.function.Function) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) DefaultStreamMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.DefaultStreamMapper) TruncateTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.TruncateTimePeriodAdjuster) FireEvents(com.serotonin.m2m2.rt.dataImage.DataPointRT.FireEvents) ServerErrorException(com.infiniteautomation.mango.rest.latest.exception.ServerErrorException) Nullable(org.checkerframework.checker.nullness.qual.Nullable) TemporaryResource(com.infiniteautomation.mango.rest.latest.temporaryResource.TemporaryResource) Logger(org.slf4j.Logger) MangoTaskTemporaryResourceManager(com.infiniteautomation.mango.rest.latest.temporaryResource.MangoTaskTemporaryResourceManager) LegacyPointValueTimeModel(com.infiniteautomation.mango.rest.latest.model.pointValue.LegacyPointValueTimeModel) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) ResponseEntity(org.springframework.http.ResponseEntity) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) Comparator(java.util.Comparator) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) AggregateValueMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.AggregateValueMapper) StreamMapperBuilder(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.StreamMapperBuilder) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService in project ma-modules-public by infiniteautomation.

the class MaintenanceEventsRestController method exportQuery.

@ApiOperation(value = "Export formatted for Configuration Import by supplying an RQL query", notes = "User must have read permission")
@RequestMapping(method = RequestMethod.GET, value = "/export", produces = MediaTypes.SEROTONIN_JSON_VALUE)
public Map<String, JsonStreamedArray> exportQuery(HttpServletRequest request, @AuthenticationPrincipal PermissionHolder user) {
    ASTNode rql = RQLUtils.parseRQLtoAST(request.getQueryString());
    Map<String, JsonStreamedArray> export = new HashMap<>();
    if (permissionService.hasPermission(user, dataSourcePermissionDefinition.getPermission())) {
        export.put("maintenanceEvents", new StreamedSeroJsonVORqlQuery<>(service, rql, null, null, null));
    } else {
        export.put("maintenanceEvents", new StreamedSeroJsonVORqlQuery<>(service, rql, null, null, null, item -> {
            if (item.getDataPoints().size() > 0) {
                DataPointPermissionsCheckCallback callback = new DataPointPermissionsCheckCallback(user, true, permissionService, dataPointService);
                dao.getPoints(item.getId(), callback);
                if (!callback.hasPermission())
                    return false;
            }
            if (item.getDataSources().size() > 0) {
                DataSourcePermissionsCheckCallback callback = new DataSourcePermissionsCheckCallback(user, true, permissionService);
                dao.getDataSources(item.getId(), callback);
                if (!callback.hasPermission())
                    return false;
            }
            return true;
        }));
    }
    return export;
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) RequestParam(org.springframework.web.bind.annotation.RequestParam) BiFunction(java.util.function.BiFunction) StreamedVORqlQueryWithTotal(com.infiniteautomation.mango.rest.latest.model.StreamedVORqlQueryWithTotal) Autowired(org.springframework.beans.factory.annotation.Autowired) ApiParam(io.swagger.annotations.ApiParam) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) RestModelMapper(com.infiniteautomation.mango.rest.latest.model.RestModelMapper) PatchVORequestBody(com.infiniteautomation.mango.rest.latest.patch.PatchVORequestBody) MaintenanceEventsService(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService) ApiOperation(io.swagger.annotations.ApiOperation) Map(java.util.Map) URI(java.net.URI) DataPointPermissionsCheckCallback(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService.DataPointPermissionsCheckCallback) HttpHeaders(org.springframework.http.HttpHeaders) RQLUtils(com.infiniteautomation.mango.util.RQLUtils) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) EventInstanceService(com.infiniteautomation.mango.spring.service.EventInstanceService) Field(org.jooq.Field) RestController(org.springframework.web.bind.annotation.RestController) EventQueryByMaintenanceEventRql(com.infiniteautomation.mango.rest.latest.model.EventQueryByMaintenanceEventRql) JsonStreamedArray(com.serotonin.json.type.JsonStreamedArray) DataSourcePermissionDefinition(com.serotonin.m2m2.module.definitions.permissions.DataSourcePermissionDefinition) List(java.util.List) DataSourcePermissionsCheckCallback(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService.DataSourcePermissionsCheckCallback) StreamedSeroJsonVORqlQuery(com.infiniteautomation.mango.rest.latest.model.StreamedSeroJsonVORqlQuery) ASTNode(net.jazdw.rql.parser.ASTNode) EventTableRqlMappings(com.infiniteautomation.mango.rest.latest.EventsRestController.EventTableRqlMappings) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) EventQueryByMaintenanceCriteria(com.infiniteautomation.mango.rest.latest.model.EventQueryByMaintenanceCriteria) MaintenanceEventType(com.serotonin.m2m2.maintenanceEvents.MaintenanceEventType) MaintenanceEventModel(com.infiniteautomation.mango.rest.latest.model.MaintenanceEventModel) MaintenanceEventVO(com.serotonin.m2m2.maintenanceEvents.MaintenanceEventVO) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) Function(java.util.function.Function) Events(com.infiniteautomation.mango.db.tables.Events) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestBody(org.springframework.web.bind.annotation.RequestBody) HttpServletRequest(javax.servlet.http.HttpServletRequest) StreamedArrayWithTotal(com.infiniteautomation.mango.rest.latest.model.StreamedArrayWithTotal) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) Api(io.swagger.annotations.Api) MaintenanceEventDao(com.serotonin.m2m2.maintenanceEvents.MaintenanceEventDao) EventInstanceVO(com.serotonin.m2m2.vo.event.EventInstanceVO) EventInstanceModel(com.infiniteautomation.mango.rest.latest.model.event.EventInstanceModel) Consumer(java.util.function.Consumer) HttpStatus(org.springframework.http.HttpStatus) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) MediaTypes(com.serotonin.m2m2.web.MediaTypes) ResponseEntity(org.springframework.http.ResponseEntity) StreamedArray(com.infiniteautomation.mango.rest.latest.model.StreamedArray) HashMap(java.util.HashMap) ASTNode(net.jazdw.rql.parser.ASTNode) DataPointPermissionsCheckCallback(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService.DataPointPermissionsCheckCallback) DataSourcePermissionsCheckCallback(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService.DataSourcePermissionsCheckCallback) JsonStreamedArray(com.serotonin.json.type.JsonStreamedArray) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with DataPointService

use of com.infiniteautomation.mango.spring.service.DataPointService in project ma-modules-public by infiniteautomation.

the class MaintenanceEventType method getEventPermission.

@Override
public MangoPermission getEventPermission(Map<String, Object> context, PermissionService service) {
    DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
    DataPointService dataPointService = Common.getBean(DataPointService.class);
    MaintenanceEventsService maintenanceEventService = Common.getBean(MaintenanceEventsService.class);
    Set<Role> allRequired = new HashSet<>();
    try {
        MaintenanceEventVO vo = maintenanceEventService.get(maintenanceId);
        try {
            for (int dsId : vo.getDataSources()) {
                MangoPermission read = dataSourceService.getReadPermission(dsId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
        try {
            for (int dpId : vo.getDataPoints()) {
                MangoPermission read = dataPointService.getReadPermission(dpId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
    } catch (NotFoundException e) {
    // Ignore all of it
    }
    if (allRequired.size() == 0) {
        return MangoPermission.superadminOnly();
    } else {
        return MangoPermission.requireAllRoles(allRequired);
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) MaintenanceEventsService(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService) Role(com.serotonin.m2m2.vo.role.Role) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) HashSet(java.util.HashSet)

Aggregations

DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)33 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)31 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)20 List (java.util.List)20 Common (com.serotonin.m2m2.Common)19 Collectors (java.util.stream.Collectors)19 MangoTestBase (com.serotonin.m2m2.MangoTestBase)18 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)18 Test (org.junit.Test)18 Role (com.serotonin.m2m2.vo.role.Role)17 Set (java.util.Set)17 RunAs (com.infiniteautomation.mango.spring.components.RunAs)16 QueryBuilder (com.serotonin.m2m2.db.dao.QueryBuilder)16 Assert.assertEquals (org.junit.Assert.assertEquals)16 Assert.assertTrue (org.junit.Assert.assertTrue)16 Before (org.junit.Before)16 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)12 MockPointLocatorVO (com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)10 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)6 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)6