Search in sources :

Example 1 with AggregationFunction

use of org.netxms.client.constants.AggregationFunction 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)

Aggregations

ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 NXCSession (org.netxms.client.NXCSession)1 Table (org.netxms.client.Table)1 TableRow (org.netxms.client.TableRow)1 AggregationFunction (org.netxms.client.constants.AggregationFunction)1 DciSummaryTableColumn (org.netxms.client.datacollection.DciSummaryTableColumn)1 AbstractObject (org.netxms.client.objects.AbstractObject)1 ResponseContainer (org.netxms.websvc.json.ResponseContainer)1