Search in sources :

Example 6 with DciDataRow

use of org.netxms.client.datacollection.DciDataRow in project netxms by netxms.

the class NXCSession method getPredictedData.

/**
 * Get predicted DCI data from server.
 *
 * @param nodeId     Node ID
 * @param dciId      DCI ID
 * @param from       Start of time range
 * @param to         End of time range
 * @return DCI data set
 * @throws IOException  if socket I/O error occurs
 * @throws NXCException if NetXMS server returns an error or operation was timed out
 */
public DciData getPredictedData(long nodeId, long dciId, Date from, Date to) throws IOException, NXCException {
    NXCPMessage msg = newMessage(NXCPCodes.CMD_GET_PREDICTED_DATA);
    msg.setFieldInt32(NXCPCodes.VID_OBJECT_ID, (int) nodeId);
    msg.setFieldInt32(NXCPCodes.VID_DCI_ID, (int) dciId);
    DciData data = new DciData(nodeId, dciId);
    int rowsReceived;
    int timeFrom = (int) (from.getTime() / 1000);
    int timeTo = (int) (to.getTime() / 1000);
    do {
        msg.setMessageId(requestId.getAndIncrement());
        msg.setFieldInt32(NXCPCodes.VID_TIME_FROM, timeFrom);
        msg.setFieldInt32(NXCPCodes.VID_TIME_TO, timeTo);
        sendMessage(msg);
        waitForRCC(msg.getMessageId());
        NXCPMessage response = waitForMessage(NXCPCodes.CMD_DCI_DATA, msg.getMessageId());
        if (!response.isBinaryMessage())
            throw new NXCException(RCC.INTERNAL_ERROR);
        rowsReceived = parseDataRows(response.getBinaryData(), data);
        if (rowsReceived == MAX_DCI_DATA_ROWS) {
            // Rows goes in newest to oldest order, so if we need to
            // retrieve additional data, we should update timeTo limit
            DciDataRow row = data.getLastValue();
            if (row != null) {
                // There should be only one value per second, so we set
                // last row's timestamp - 1 second as new boundary
                timeTo = (int) (row.getTimestamp().getTime() / 1000) - 1;
            }
        }
    } while ((rowsReceived == MAX_DCI_DATA_ROWS) && (timeTo > timeFrom));
    return data;
}
Also used : DciDataRow(org.netxms.client.datacollection.DciDataRow) NXCPMessage(org.netxms.base.NXCPMessage) DciData(org.netxms.client.datacollection.DciData) ConnectionPoint(org.netxms.client.topology.ConnectionPoint) AccessPoint(org.netxms.client.objects.AccessPoint)

Example 7 with DciDataRow

use of org.netxms.client.datacollection.DciDataRow in project netxms by netxms.

the class NXCSession method parseDataRows.

/**
 * Parse data from raw message CMD_DCI_DATA.
 * This method is intended for calling by client internal methods only. It made public to
 * allow access from client extensions.
 *
 * @param input Raw data
 * @param data  Data object to add rows to
 * @return number of received data rows
 */
public int parseDataRows(final byte[] input, DciData data) {
    final NXCPDataInputStream inputStream = new NXCPDataInputStream(input);
    int rows = 0;
    DciDataRow row = null;
    try {
        // DCI ID
        inputStream.skipBytes(4);
        rows = inputStream.readInt();
        final DataType dataType = DataType.getByValue(inputStream.readInt());
        data.setDataType(dataType);
        // padding
        inputStream.skipBytes(4);
        for (int i = 0; i < rows; i++) {
            // convert to milliseconds
            long timestamp = inputStream.readUnsignedInt() * 1000;
            Object value;
            switch(dataType) {
                case INT32:
                    value = new Long(inputStream.readInt());
                    break;
                case UINT32:
                case COUNTER32:
                    value = new Long(inputStream.readUnsignedInt());
                    break;
                case INT64:
                case UINT64:
                case COUNTER64:
                    // padding
                    inputStream.skipBytes(4);
                    value = new Long(inputStream.readLong());
                    break;
                case FLOAT:
                    // padding
                    inputStream.skipBytes(4);
                    value = new Double(inputStream.readDouble());
                    break;
                case STRING:
                    StringBuilder sb = new StringBuilder(256);
                    int count;
                    for (count = MAX_DCI_STRING_VALUE_LENGTH; count > 0; count--) {
                        char ch = inputStream.readChar();
                        if (ch == 0) {
                            count--;
                            break;
                        }
                        sb.append(ch);
                    }
                    inputStream.skipBytes(count * 2);
                    value = sb.toString();
                    break;
                default:
                    value = null;
                    break;
            }
            if (timestamp > 0) {
                row = new DciDataRow(new Date(timestamp), value);
                data.addDataRow(row);
            } else {
                // raw value for previous entry
                if (row != null)
                    row.setRawValue(value);
            }
        }
    } catch (IOException e) {
    }
    inputStream.close();
    return rows;
}
Also used : DciDataRow(org.netxms.client.datacollection.DciDataRow) AtomicLong(java.util.concurrent.atomic.AtomicLong) DataType(org.netxms.client.constants.DataType) DataCollectionObject(org.netxms.client.datacollection.DataCollectionObject) NetworkMapObject(org.netxms.client.maps.elements.NetworkMapObject) WinPerfObject(org.netxms.client.datacollection.WinPerfObject) AbstractUserObject(org.netxms.client.users.AbstractUserObject) AbstractObject(org.netxms.client.objects.AbstractObject) EventObject(org.netxms.client.events.EventObject) UnknownObject(org.netxms.client.objects.UnknownObject) GenericObject(org.netxms.client.objects.GenericObject) IOException(java.io.IOException) ConnectionPoint(org.netxms.client.topology.ConnectionPoint) AccessPoint(org.netxms.client.objects.AccessPoint) Date(java.util.Date) NXCPDataInputStream(org.netxms.base.NXCPDataInputStream)

Example 8 with DciDataRow

use of org.netxms.client.datacollection.DciDataRow in project netxms by netxms.

the class DashboardView method exportLineChartValues.

/**
 * Export all line chart values as CSV
 */
private void exportLineChartValues() {
    final List<DataCacheElement> data = new ArrayList<DataCacheElement>();
    for (ElementWidget w : dbc.getElementWidgets()) {
        if (!(w instanceof LineChartElement))
            continue;
        for (DataCacheElement d : ((LineChartElement) w).getDataCache()) {
            data.add(d);
        }
    }
    final DateFormat dfDate = RegionalSettings.getDateFormat();
    final DateFormat dfTime = RegionalSettings.getTimeFormat();
    final DateFormat dfDateTime = RegionalSettings.getDateTimeFormat();
    new ConsoleJob(Messages.get().DashboardView_ExportLineChartData, this, Activator.PLUGIN_ID, null) {

        @Override
        protected void runInternal(IProgressMonitor monitor) throws Exception {
            // $NON-NLS-1$
            boolean doInterpolation = session.getPublicServerVariableAsBoolean("DashboardDataExportEnableInterpolation");
            // Build combined time series
            // Time stamps in series are reversed - latest value first
            List<Date> combinedTimeSeries = new ArrayList<Date>();
            for (DataCacheElement d : data) {
                for (DciDataRow r : d.data.getValues()) {
                    int i;
                    for (i = 0; i < combinedTimeSeries.size(); i++) {
                        if (combinedTimeSeries.get(i).getTime() == r.getTimestamp().getTime())
                            break;
                        if (combinedTimeSeries.get(i).getTime() < r.getTimestamp().getTime()) {
                            combinedTimeSeries.add(i, r.getTimestamp());
                            break;
                        }
                    }
                    if (i == combinedTimeSeries.size())
                        combinedTimeSeries.add(r.getTimestamp());
                }
            }
            List<Double[]> combinedData = new ArrayList<Double[]>(data.size());
            // insert missing values
            for (DataCacheElement d : data) {
                Double[] ySeries = new Double[combinedTimeSeries.size()];
                int combinedIndex = 0;
                double lastValue = 0;
                long lastTimestamp = 0;
                DciDataRow[] values = d.data.getValues();
                for (int i = 0; i < values.length; i++) {
                    Date currentTimestamp = values[i].getTimestamp();
                    double currentValue = values[i].getValueAsDouble();
                    long currentCombinedTimestamp = combinedTimeSeries.get(combinedIndex).getTime();
                    while (currentCombinedTimestamp > currentTimestamp.getTime()) {
                        if ((lastTimestamp != 0) && doInterpolation) {
                            // do linear interpolation for missed value
                            ySeries[combinedIndex] = lastValue + (currentValue - lastValue) * ((double) (lastTimestamp - currentCombinedTimestamp) / (double) (lastTimestamp - currentTimestamp.getTime()));
                        } else {
                            ySeries[combinedIndex] = null;
                        }
                        combinedIndex++;
                        currentCombinedTimestamp = combinedTimeSeries.get(combinedIndex).getTime();
                    }
                    ySeries[combinedIndex++] = currentValue;
                    lastTimestamp = currentTimestamp.getTime();
                    lastValue = currentValue;
                }
                combinedData.add(ySeries);
            }
            final File tmpFile = File.createTempFile("ExportDashboardCSV_" + DashboardView.this.hashCode(), "_" + System.currentTimeMillis());
            BufferedWriter writer = new BufferedWriter(new FileWriter(tmpFile));
            try {
                // $NON-NLS-1$ //$NON-NLS-2$
                writer.write("# " + dashboard.getObjectName() + " " + dfDateTime.format(new Date()));
                writer.newLine();
                // $NON-NLS-1$
                writer.write("DATE,TIME");
                for (DataCacheElement d : data) {
                    writer.write(',');
                    writer.write(d.name);
                }
                writer.newLine();
                for (int i = combinedTimeSeries.size() - 1; i >= 0; i--) {
                    Date d = combinedTimeSeries.get(i);
                    writer.write(dfDate.format(d));
                    writer.write(',');
                    writer.write(dfTime.format(d));
                    for (Double[] values : combinedData) {
                        writer.write(',');
                        if (values[i] != null) {
                            double v = values[i];
                            if (Math.abs(v) > 0.001)
                                // $NON-NLS-1$
                                writer.write(String.format("%.3f", v));
                            else
                                writer.write(Double.toString(v));
                        }
                    }
                    writer.newLine();
                }
            } finally {
                writer.close();
            }
            DownloadServiceHandler.addDownload(tmpFile.getName(), dashboard.getObjectName() + ".csv", tmpFile, "text/csv");
            runInUIThread(new Runnable() {

                @Override
                public void run() {
                    DownloadServiceHandler.startDownload(tmpFile.getName());
                }
            });
        }

        @Override
        protected String getErrorMessage() {
            return Messages.get().DashboardView_8;
        }
    }.start();
}
Also used : ElementWidget(org.netxms.ui.eclipse.dashboard.widgets.ElementWidget) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) PartInitException(org.eclipse.ui.PartInitException) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) LineChartElement(org.netxms.ui.eclipse.dashboard.widgets.LineChartElement) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) DciDataRow(org.netxms.client.datacollection.DciDataRow) DataCacheElement(org.netxms.ui.eclipse.dashboard.widgets.LineChartElement.DataCacheElement) DateFormat(java.text.DateFormat) ArrayList(java.util.ArrayList) List(java.util.List) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) File(java.io.File)

Example 9 with DciDataRow

use of org.netxms.client.datacollection.DciDataRow in project netxms by netxms.

the class ComparisonChartElement method refreshData.

/**
 * Refresh graph's data
 */
protected void refreshData(final ChartDciConfig[] dciList) {
    if (updateInProgress)
        return;
    updateInProgress = true;
    ConsoleJob job = new ConsoleJob(Messages.get().ComparisonChartElement_JobTitle, viewPart, Activator.PLUGIN_ID, Activator.PLUGIN_ID) {

        @Override
        protected void runInternal(IProgressMonitor monitor) throws Exception {
            final DciData[] data = new DciData[dciList.length];
            for (int i = 0; i < dciList.length; i++) {
                if (dciList[i].type == ChartDciConfig.ITEM)
                    data[i] = session.getCollectedData(dciList[i].nodeId, dciList[i].dciId, null, null, 1, false);
                else
                    data[i] = session.getCollectedTableData(dciList[i].nodeId, dciList[i].dciId, dciList[i].instance, dciList[i].column, null, null, 1);
            }
            final Threshold[][] thresholds;
            if (updateThresholds) {
                thresholds = new Threshold[dciList.length][];
                for (int i = 0; i < dciList.length; i++) {
                    thresholds[i] = session.getThresholds(dciList[i].nodeId, dciList[i].dciId);
                }
            } else {
                thresholds = null;
            }
            runInUIThread(new Runnable() {

                @Override
                public void run() {
                    if (!((Widget) chart).isDisposed()) {
                        for (int i = 0; i < data.length; i++) {
                            DciDataRow lastValue = data[i].getLastValue();
                            chart.updateParameter(i, (lastValue != null) ? lastValue : new DciDataRow(new Date(), 0.0), data[i].getDataType(), false);
                            if (updateThresholds)
                                chart.updateParameterThresholds(i, thresholds[i]);
                        }
                        chart.refresh();
                        chart.clearErrors();
                    }
                    updateInProgress = false;
                }
            });
        }

        @Override
        protected String getErrorMessage() {
            return Messages.get().ComparisonChartElement_JobError;
        }

        @Override
        protected void jobFailureHandler() {
            updateInProgress = false;
            super.jobFailureHandler();
        }

        @Override
        protected IStatus createFailureStatus(final Exception e) {
            runInUIThread(new Runnable() {

                @Override
                public void run() {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    chart.addError(getErrorMessage() + " (" + e.getLocalizedMessage() + ")");
                }
            });
            return Status.OK_STATUS;
        }
    };
    job.setUser(false);
    job.start();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) DciDataRow(org.netxms.client.datacollection.DciDataRow) Widget(org.eclipse.swt.widgets.Widget) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) DciData(org.netxms.client.datacollection.DciData) Point(org.eclipse.swt.graphics.Point) Date(java.util.Date)

Example 10 with DciDataRow

use of org.netxms.client.datacollection.DciDataRow in project netxms by netxms.

the class GrafanaDataCollection method getGraphData.

/**
 * Get query data
 *
 * @param query
 * @return data
 * @throws Exception
 */
private JsonArray getGraphData(Map<String, String> query) throws Exception {
    JsonParser parser = new JsonParser();
    JsonElement element = parser.parse(query.get("targets"));
    if (!element.isJsonArray())
        return new JsonArray();
    JsonArray targets = element.getAsJsonArray();
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
    Date from = format.parse(query.get("from").substring(1, query.get("from").length() - 1));
    Date to = format.parse(query.get("to").substring(1, query.get("to").length() - 1));
    JsonObject root, dciTarget, dci;
    JsonArray result = new JsonArray();
    for (JsonElement e : targets) {
        if (!e.getAsJsonObject().has("dciTarget") || !e.getAsJsonObject().has("dci"))
            continue;
        dciTarget = e.getAsJsonObject().getAsJsonObject("dciTarget");
        dci = e.getAsJsonObject().getAsJsonObject("dci");
        if (dciTarget.size() == 0 || dci.size() == 0)
            continue;
        DciData data = getSession().getCollectedData(Long.parseLong(dciTarget.get("id").getAsString()), Long.parseLong(dci.get("id").getAsString()), from, to, 0, false);
        root = new JsonObject();
        JsonArray datapoints = new JsonArray();
        JsonArray datapoint;
        DciDataRow[] values = data.getValues();
        for (int i = values.length - 1; i >= 0; i--) {
            DciDataRow r = values[i];
            log.debug(r.toString());
            datapoint = new JsonArray();
            datapoint.add(r.getValueAsDouble());
            datapoint.add(r.getTimestamp().getTime());
            datapoints.add(datapoint);
        }
        if (e.getAsJsonObject().has("legend") && !e.getAsJsonObject().get("legend").getAsString().equals(""))
            root.addProperty("target", e.getAsJsonObject().get("legend").getAsString());
        else
            root.addProperty("target", dci.get("name").getAsString());
        root.add("datapoints", datapoints);
        result.add(root);
    }
    return result;
}
Also used : JsonArray(com.google.gson.JsonArray) DciDataRow(org.netxms.client.datacollection.DciDataRow) JsonElement(com.google.gson.JsonElement) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) JsonObject(com.google.gson.JsonObject) DciData(org.netxms.client.datacollection.DciData) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) JsonParser(com.google.gson.JsonParser)

Aggregations

DciDataRow (org.netxms.client.datacollection.DciDataRow)10 DciData (org.netxms.client.datacollection.DciData)6 Date (java.util.Date)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)4 PartInitException (org.eclipse.ui.PartInitException)3 AccessPoint (org.netxms.client.objects.AccessPoint)3 ConnectionPoint (org.netxms.client.topology.ConnectionPoint)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 DateFormat (java.text.DateFormat)2 List (java.util.List)2 Point (org.eclipse.swt.graphics.Point)2 NXCPMessage (org.netxms.base.NXCPMessage)2 GraphItem (org.netxms.client.datacollection.GraphItem)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1