use of org.netxms.ui.eclipse.dashboard.widgets.ElementWidget 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();
}
Aggregations