9c7356caf79a71d02de0b0b0c48d0758068baaf1
[public/netxms.git] / src / server / nxapisrv / java / src / main / java / org / netxms / websvc / handlers / GrafanaAlarms.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2017 Raden Solutions
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.websvc.handlers;
20
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.Map;
24 import org.json.JSONException;
25 import org.netxms.client.NXCSession;
26 import org.netxms.client.events.Alarm;
27 import org.netxms.client.objects.AbstractObject;
28 import org.netxms.client.users.AbstractUserObject;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonObject;
31
32 public class GrafanaAlarms extends AbstractHandler
33 {
34 private String[] states = { "Outstanding", "Acknowledged", "Resolved", "Terminated" };
35
36 /* (non-Javadoc)
37 * @see org.netxms.websvc.handlers.AbstractHandler#getCollection(java.util.Map)
38 */
39 @Override
40 public Object getCollection(Map<String, String> query) throws Exception
41 {
42 NXCSession session = getSession();
43 if (!session.isObjectsSynchronized())
44 session.syncObjects();
45
46 if (query.containsKey("node") && query.get("node").equals("Select source"))
47 {
48
49 }
50 JsonObject root = new JsonObject();
51
52 JsonArray columns = new JsonArray();
53 columns.add(createColumn("Severity", true, true));
54 columns.add(createColumn("State", true, false));
55 columns.add(createColumn("Source", true, false));
56 columns.add(createColumn("Message", true, false));
57 columns.add(createColumn("Count", true, false));
58 columns.add(createColumn("Helpdesk ID", true, false));
59 columns.add(createColumn("Ack/Resolved By", true, false));
60 columns.add(createColumn("Created", true, false));
61 columns.add(createColumn("Last Change", true, false));
62 root.add("columns", columns);
63
64 JsonArray rows = new JsonArray();
65
66 JsonArray r = new JsonArray();
67 DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
68 AbstractObject object = null;
69 AbstractUserObject user = null;
70
71 Map<Long, Alarm> alarms = session.getAlarms();
72 for( Alarm a : alarms.values())
73 {
74 r.add(a.getCurrentSeverity().name());
75 r.add(states[a.getState()]);
76
77 object = getSession().findObjectById(a.getSourceObjectId());
78 if (object == null)
79 r.add(a.getSourceObjectId());
80 else
81 r.add(object.getObjectName());
82
83 r.add(a.getMessage());
84 r.add(a.getRepeatCount());
85 r.add(a.getHelpdeskReference());
86
87 user = getSession().findUserDBObjectById(a.getAckByUser());
88 if (user == null)
89 r.add("");
90 else
91 r.add(user.getName());
92
93 r.add(df.format(a.getCreationTime()));
94 r.add(df.format(a.getLastChangeTime()));
95 rows.add(r);
96 r = new JsonArray();
97 }
98
99 root.add("rows", rows);
100
101 root.addProperty("type", "table");
102
103 JsonArray wrapper = new JsonArray();
104 wrapper.add(root);
105 return wrapper;
106 }
107
108 /**
109 * @param name
110 * @param sort
111 * @param desc
112 * @return
113 * @throws JSONException
114 */
115 private static JsonObject createColumn(String name, boolean sort, boolean desc) throws JSONException
116 {
117 JsonObject column = new JsonObject();
118 column.addProperty("text", name);
119 column.addProperty("sort", sort);
120 column.addProperty("sort", desc);
121 return column;
122 }
123 }