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);
}
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);
}
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);
}
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);
}
Aggregations