Search in sources :

Example 1 with ResponseContainer

use of org.netxms.websvc.json.ResponseContainer in project netxms by netxms.

the class SummaryTableAdHoc method create.

/* (non-Javadoc)
    * @see org.netxms.websvc.handlers.AbstractHandler#create(org.json.JSONObject)
    */
@Override
protected Object create(JSONObject data) throws Exception {
    NXCSession session = getSession();
    if (!session.isObjectsSynchronized())
        session.syncObjects();
    String objectFilter = JsonTools.getStringFromJson(data, "baseObject", null);
    log.debug("POST adhoc summaryTable: baseObject = " + objectFilter);
    JSONArray columnFilter = JsonTools.getJsonArrayFromJson(data, "columns", null);
    if (objectFilter == null || objectFilter.isEmpty() || columnFilter == null) {
        log.warn("POST adhoc summaryTable: no DciSummaryTableColumn table or no value for BaseObject");
        return createErrorResponse(RCC.INVALID_ARGUMENT);
    }
    long baseObjectId;
    try {
        baseObjectId = Long.parseLong(objectFilter);
    } catch (NumberFormatException ex) {
        AbstractObject obj = session.findObjectByName(objectFilter);
        if (obj != null)
            baseObjectId = obj.getObjectId();
        else
            baseObjectId = 0;
    }
    List<DciSummaryTableColumn> columns = new ArrayList<DciSummaryTableColumn>();
    for (int i = 0; i < columnFilter.length(); i++) {
        JSONObject obj = columnFilter.getJSONObject(i);
        columns.add(new DciSummaryTableColumn(JsonTools.getStringFromJson(obj, "columnName", ""), JsonTools.getStringFromJson(obj, "dciName", ""), JsonTools.getBooleanFromJson(obj, "isRegexp", false) ? DciSummaryTableColumn.REGEXP_MATCH : 0));
    }
    AggregationFunction agrFunc = JsonTools.getEnumFromJson(data, AggregationFunction.class, "aggregationFunction", null);
    Date startDate;
    Date endDate;
    long date = JsonTools.getLongFromJson(data, "startDate", -1);
    startDate = date > 0 ? new Date(date * 1000) : null;
    date = JsonTools.getLongFromJson(data, "endDate", -1);
    endDate = date > 0 ? new Date(date * 1000) : null;
    // end date
    boolean multiInstance = JsonTools.getBooleanFromJson(data, "multiInstance", true);
    Table table = session.queryAdHocDciSummaryTable(baseObjectId, columns, agrFunc, startDate, endDate, multiInstance);
    // create json
    JSONObject root = new JSONObject();
    JSONArray columnList = new JSONArray();
    JSONArray rowList = new JSONArray();
    String[] names = table.getColumnDisplayNames();
    for (int i = 0; i < names.length; i++) columnList.put(names[i]);
    root.put("columns", columnList);
    TableRow[] rows = table.getAllRows();
    for (int i = 0; i < rows.length; i++) {
        JSONArray row = new JSONArray();
        for (int j = 0; j < rows[i].size(); j++) row.put(rows[i].get(j).getValue());
        rowList.put(row);
    }
    root.put("rows", rowList);
    return new ResponseContainer("table", root);
}
Also used : NXCSession(org.netxms.client.NXCSession) Table(org.netxms.client.Table) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Date(java.util.Date) AggregationFunction(org.netxms.client.constants.AggregationFunction) JSONObject(org.json.JSONObject) AbstractObject(org.netxms.client.objects.AbstractObject) TableRow(org.netxms.client.TableRow) DciSummaryTableColumn(org.netxms.client.datacollection.DciSummaryTableColumn) ResponseContainer(org.netxms.websvc.json.ResponseContainer)

Example 2 with ResponseContainer

use of org.netxms.websvc.json.ResponseContainer in project netxms by netxms.

the class HistoricalData method get.

/* (non-Javadoc)
    * @see org.netxms.websvc.handlers.AbstractHandler#get(java.lang.String)
    */
@Override
protected Object get(String id, Map<String, String> query) throws Exception {
    NXCSession session = getSession();
    AbstractObject obj = getObject();
    long dciId = 0;
    try {
        dciId = Long.parseLong(id);
    } catch (NumberFormatException e) {
        dciId = session.dciNameToId(obj.getObjectId(), id);
    }
    if (obj == null || dciId == 0 || !(obj instanceof DataCollectionTarget))
        throw new NXCException(RCC.INVALID_OBJECT_ID);
    String timeFrom = query.get("from");
    String timeTo = query.get("to");
    String timeInteval = query.get("timeInterval");
    String itemCount = query.get("itemCount");
    DciData data = null;
    if (timeFrom != null || timeTo != null) {
        data = session.getCollectedData(obj.getObjectId(), dciId, new Date(parseLong(timeFrom, 0) * 1000), new Date(parseLong(timeTo, System.currentTimeMillis() / 1000) * 1000), parseInt(itemCount, 0), false);
    } else if (timeInteval != null) {
        Date now = new Date();
        long from = now.getTime() - parseLong(timeInteval, 0) * 1000;
        data = session.getCollectedData(obj.getObjectId(), dciId, new Date(from), new Date(), parseInt(itemCount, 0), false);
    } else if (itemCount != null) {
        data = session.getCollectedData(obj.getObjectId(), dciId, null, null, parseInt(itemCount, 0), false);
    } else {
        Date now = new Date();
        // one hour
        long from = now.getTime() - 3600000;
        data = session.getCollectedData(obj.getObjectId(), dciId, new Date(from), now, parseInt(itemCount, 0), false);
    }
    return new ResponseContainer("values", data);
}
Also used : NXCSession(org.netxms.client.NXCSession) AbstractObject(org.netxms.client.objects.AbstractObject) DataCollectionTarget(org.netxms.client.objects.DataCollectionTarget) DciData(org.netxms.client.datacollection.DciData) ResponseContainer(org.netxms.websvc.json.ResponseContainer) NXCException(org.netxms.client.NXCException) Date(java.util.Date)

Example 3 with ResponseContainer

use of org.netxms.websvc.json.ResponseContainer in project netxms by netxms.

the class Alarms method getCollection.

/* (non-Javadoc)
    * @see org.netxms.websvc.handlers.AbstractHandler#getCollection(org.json.JSONObject)
    */
@Override
protected Object getCollection(Map<String, String> query) throws Exception {
    NXCSession session = getSession();
    Collection<Alarm> alarms = session.getAlarms().values();
    String queryGuid = query.get("objectGuid");
    if (queryGuid != null) {
        UUID objectGuid = UUID.fromString(queryGuid);
        if (!session.isObjectsSynchronized())
            session.syncObjects();
        AbstractObject object = session.findObjectByGUID(objectGuid);
        if (object == null)
            throw new NXCException(RCC.INVALID_OBJECT_ID);
        Iterator<Alarm> iterator = alarms.iterator();
        while (iterator.hasNext()) {
            Alarm alarm = iterator.next();
            if (alarm.getSourceObjectId() != object.getObjectId()) {
                iterator.remove();
            }
        }
    }
    return new ResponseContainer("alarms", alarms);
}
Also used : NXCSession(org.netxms.client.NXCSession) Alarm(org.netxms.client.events.Alarm) AbstractObject(org.netxms.client.objects.AbstractObject) UUID(java.util.UUID) ResponseContainer(org.netxms.websvc.json.ResponseContainer) NXCException(org.netxms.client.NXCException)

Example 4 with ResponseContainer

use of org.netxms.websvc.json.ResponseContainer in project netxms by netxms.

the class Objects method getCollection.

/* (non-Javadoc)
    * @see org.netxms.websvc.handlers.AbstractHandler#getCollection(org.json.JSONObject)
    */
@Override
protected Object getCollection(Map<String, String> query) throws Exception {
    NXCSession session = getSession();
    if (!session.isObjectsSynchronized())
        session.syncObjects();
    List<AbstractObject> objects = session.getAllObjects();
    String areaFilter = query.get("area");
    String classFilter = query.get("class");
    String nameFilter = query.get("name");
    Map<String, String> customAttributes = null;
    for (String k : query.keySet()) {
        if (!k.startsWith("@"))
            continue;
        if (customAttributes == null)
            customAttributes = new HashMap<String, String>();
        customAttributes.put(k.substring(1), query.get(k));
    }
    if ((areaFilter != null) || (classFilter != null) || (customAttributes != null) || (nameFilter != null)) {
        double[] area = null;
        if (areaFilter != null) {
            String[] parts = areaFilter.split(",");
            if (parts.length == 4) {
                try {
                    area = new double[4];
                    for (int i = 0; i < 4; i++) area[i] = Double.parseDouble(parts[i]);
                } catch (NumberFormatException e) {
                    log.warn("Invalid area filter " + areaFilter);
                }
            } else {
                log.warn("Invalid area filter " + areaFilter);
            }
        }
        String[] classes = null;
        if ((classFilter != null) && !classFilter.isEmpty()) {
            classes = classFilter.split(",");
        }
        Iterator<AbstractObject> it = objects.iterator();
        while (it.hasNext()) {
            AbstractObject o = it.next();
            // Filter by name
            if ((nameFilter != null) && !nameFilter.isEmpty() && !Glob.matchIgnoreCase(nameFilter, o.getObjectName())) {
                it.remove();
                continue;
            }
            // Filter by class
            if (classes != null) {
                boolean match = false;
                for (String c : classes) {
                    if (o.getObjectClassName().equalsIgnoreCase(c)) {
                        match = true;
                        break;
                    }
                }
                if (!match) {
                    it.remove();
                    continue;
                }
            }
            // Filter by geographical area
            if (area != null) {
                if (!o.getGeolocation().isWithinArea(area[0], area[1], area[2], area[3])) {
                    it.remove();
                    continue;
                }
            }
            // Filter by custom attribute
            if (customAttributes != null) {
                boolean match = true;
                for (Entry<String, String> e : customAttributes.entrySet()) {
                    String value = o.getCustomAttributes().get(e.getKey());
                    if ((value == null) || !Glob.matchIgnoreCase(e.getValue(), value)) {
                        match = false;
                        break;
                    }
                }
                if (!match) {
                    it.remove();
                    continue;
                }
            }
        }
    }
    return new ResponseContainer("objects", objects);
}
Also used : NXCSession(org.netxms.client.NXCSession) HashMap(java.util.HashMap) AbstractObject(org.netxms.client.objects.AbstractObject) ResponseContainer(org.netxms.websvc.json.ResponseContainer)

Aggregations

NXCSession (org.netxms.client.NXCSession)4 AbstractObject (org.netxms.client.objects.AbstractObject)4 ResponseContainer (org.netxms.websvc.json.ResponseContainer)4 Date (java.util.Date)2 NXCException (org.netxms.client.NXCException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 Table (org.netxms.client.Table)1 TableRow (org.netxms.client.TableRow)1 AggregationFunction (org.netxms.client.constants.AggregationFunction)1 DciData (org.netxms.client.datacollection.DciData)1 DciSummaryTableColumn (org.netxms.client.datacollection.DciSummaryTableColumn)1 Alarm (org.netxms.client.events.Alarm)1 DataCollectionTarget (org.netxms.client.objects.DataCollectionTarget)1