Search in sources :

Example 1 with LineChartElement

use of org.netxms.ui.eclipse.dashboard.widgets.LineChartElement 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)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 DateFormat (java.text.DateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 PartInitException (org.eclipse.ui.PartInitException)1 DciDataRow (org.netxms.client.datacollection.DciDataRow)1 ElementWidget (org.netxms.ui.eclipse.dashboard.widgets.ElementWidget)1 LineChartElement (org.netxms.ui.eclipse.dashboard.widgets.LineChartElement)1 DataCacheElement (org.netxms.ui.eclipse.dashboard.widgets.LineChartElement.DataCacheElement)1 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)1