Search in sources :

Example 21 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class SystemMetricsRestController method query.

@ApiOperation(value = "Get the current value for all System Metrics", notes = "TBD Add RQL Support to this endpoint")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<List<ValueMonitor<?>>> query(HttpServletRequest request) {
    RestProcessResult<List<ValueMonitor<?>>> result = new RestProcessResult<List<ValueMonitor<?>>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        // Check Permissions
        String permissions = SystemSettingsDao.getValue(internalMetricsPermission);
        if (Permissions.hasPermission(user, permissions)) {
            return result.createResponseEntity(Common.MONITORED_VALUES.getMonitors());
        } else {
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) List(java.util.List) ValueMonitor(com.infiniteautomation.mango.monitor.ValueMonitor) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 22 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class SystemMetricsRestController method get.

@ApiOperation(value = "Get the current value for one System Metric by its ID", notes = "")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/{id}")
public ResponseEntity<ValueMonitor<?>> get(@ApiParam(value = "Valid Monitor id", required = true, allowMultiple = false) @PathVariable String id, HttpServletRequest request) {
    RestProcessResult<ValueMonitor<?>> result = new RestProcessResult<ValueMonitor<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        // Check Permissions
        String permissions = SystemSettingsDao.getValue(internalMetricsPermission);
        if (Permissions.hasPermission(user, permissions)) {
            List<ValueMonitor<?>> values = Common.MONITORED_VALUES.getMonitors();
            ValueMonitor<?> value = null;
            for (ValueMonitor<?> v : values) {
                if (v.getId().equals(id)) {
                    value = v;
                    break;
                }
            }
            if (value != null)
                return result.createResponseEntity(value);
            else {
                result.addRestMessage(getDoesNotExistMessage());
            }
        } else {
            result.addRestMessage(getUnauthorizedMessage());
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ValueMonitor(com.infiniteautomation.mango.monitor.ValueMonitor) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class ThreadMonitorRestController method getThreads.

@ApiOperation(value = "Get all threads", notes = "Larger stack depth will slow this request")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<List<ThreadModel>> getThreads(HttpServletRequest request, @ApiParam(value = "Limit size of stack trace", allowMultiple = false, defaultValue = "10") @RequestParam(value = "stackDepth", defaultValue = "10") int stackDepth, @ApiParam(value = "Return as file", allowMultiple = false, defaultValue = "false") @RequestParam(value = "asFile", defaultValue = "false") boolean asFile, @ApiParam(value = "Order by this member", allowMultiple = false, required = false) @RequestParam(value = "orderBy", required = false) String orderBy) {
    RestProcessResult<List<ThreadModel>> result = new RestProcessResult<List<ThreadModel>>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        List<ThreadModel> models = new ArrayList<ThreadModel>();
        Thread[] allThreads = this.getAllThreads();
        ThreadMXBean manager = ManagementFactory.getThreadMXBean();
        for (Thread t : allThreads) {
            ThreadInfo info = manager.getThreadInfo(t.getId(), stackDepth);
            ThreadModel model;
            if (info != null)
                model = new ThreadModel(t.getId(), t.getPriority(), t.getName(), info, manager.getThreadCpuTime(t.getId()), manager.getThreadUserTime(t.getId()));
            else
                model = new ThreadModel(t.getId(), t.getPriority(), t.getName(), manager.getThreadCpuTime(t.getId()), manager.getThreadUserTime(t.getId()));
            models.add(model);
        }
        // Do we need to order this list?
        if (orderBy != null) {
            // Determine what to order by
            final ThreadModelProperty orderProperty = ThreadModelProperty.convert(orderBy);
            Collections.sort(models, new Comparator<ThreadModel>() {

                @Override
                public int compare(ThreadModel left, ThreadModel right) {
                    switch(orderProperty) {
                        case PRIORITY:
                            return left.getPriority() - right.getPriority();
                        case NAME:
                            return left.getName().compareTo(right.getName());
                        case CPU_TIME:
                            if (left.getCpuTime() > right.getCpuTime())
                                return 1;
                            else if ((left.getCpuTime() < right.getCpuTime()))
                                return -1;
                            else
                                return 0;
                        case USER_TIME:
                            if (left.getUserTime() > right.getUserTime())
                                return 1;
                            else if ((left.getUserTime() < right.getUserTime()))
                                return -1;
                            else
                                return 0;
                        case STATE:
                            return left.getState().compareTo(right.getState());
                        case LOCATION:
                        case ID:
                        default:
                            return (int) (left.getId() - right.getId());
                    }
                }
            });
        }
        if (asFile) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss_'threads.json'");
            String filename = sdf.format(new Date());
            result.addHeader("Content-Disposition", "inline;filename=" + filename);
            return result.createResponseEntity(models, MediaType.APPLICATION_OCTET_STREAM);
        } else
            return result.createResponseEntity(models);
    }
    return result.createResponseEntity();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ArrayList(java.util.ArrayList) Date(java.util.Date) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ThreadInfo(java.lang.management.ThreadInfo) ArrayList(java.util.ArrayList) List(java.util.List) ThreadModel(com.serotonin.m2m2.web.mvc.rest.v1.model.thread.ThreadModel) ThreadModelProperty(com.serotonin.m2m2.web.mvc.rest.v1.model.thread.ThreadModelProperty) SimpleDateFormat(java.text.SimpleDateFormat) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 24 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class UserAccessRestController method getDataSourceAccess.

@ApiOperation(value = "Get Data Source Access List", notes = "Returns a list of users and thier access")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "/data-source/{xid}")
public ResponseEntity<List<UserAccessModel>> getDataSourceAccess(@ApiParam(value = "Valid data point xid", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<List<UserAccessModel>> result = new RestProcessResult<List<UserAccessModel>>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> vo = DataSourceDao.instance.getByXid(xid);
        if (vo != null) {
            List<UserAccessModel> models = new ArrayList<UserAccessModel>();
            List<User> allUsers = UserDao.instance.getUsers();
            for (User mangoUser : allUsers) {
                if (Permissions.hasDataSourcePermission(mangoUser, vo)) {
                    models.add(new UserAccessModel(Permissions.ACCESS_TYPE_CODES.getCode(DataPointAccessTypes.DATA_SOURCE), new UserModel(mangoUser)));
                }
            }
            return result.createResponseEntity(models);
        }
    }
    return result.createResponseEntity();
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) UserAccessModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserAccessModel) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class UserAccessRestController method getDataPointAccess.

@ApiOperation(value = "Get Data Point Access List", notes = "Returns a list of users and thier access")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "/data-point/{xid}")
public ResponseEntity<List<UserAccessModel>> getDataPointAccess(@ApiParam(value = "Valid data point xid", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<List<UserAccessModel>> result = new RestProcessResult<List<UserAccessModel>>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo != null) {
            List<UserAccessModel> models = new ArrayList<UserAccessModel>();
            List<User> allUsers = UserDao.instance.getUsers();
            int accessType;
            for (User mangoUser : allUsers) {
                accessType = Permissions.getDataPointAccessType(mangoUser, vo);
                if (accessType != Permissions.DataPointAccessTypes.NONE) {
                    models.add(new UserAccessModel(Permissions.ACCESS_TYPE_CODES.getCode(accessType), new UserModel(mangoUser)));
                }
            }
            return result.createResponseEntity(models);
        }
    }
    return result.createResponseEntity();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) UserAccessModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserAccessModel) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)132 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)125 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)125 User (com.serotonin.m2m2.vo.User)113 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)33 ArrayList (java.util.ArrayList)30 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)29 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)28 List (java.util.List)27 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)23 ASTNode (net.jazdw.rql.parser.ASTNode)23 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)22 URI (java.net.URI)18 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)15 HashMap (java.util.HashMap)14 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)13 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)11 RTException (com.serotonin.m2m2.rt.RTException)11 DataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel)11 QueryDataPageStream (com.serotonin.m2m2.web.mvc.rest.v1.model.QueryDataPageStream)11