private List<MetricsBulletChartNVD3> getMetricsBulletChart(
      List<Integer> variableIds, Long timestampFrom, Long timestampTo) {
    ArrayList<MetricsBulletChartNVD3> bulletCharts = new ArrayList<MetricsBulletChartNVD3>();
    List<SensorVariable> sensorVariables = DaoUtils.getSensorVariableDao().getAll(variableIds);
    // Update from/to time
    MetricsDoubleTypeDevice queryInput =
        MetricsDoubleTypeDevice.builder()
            .timestampFrom(timestampFrom)
            .timestampTo(timestampTo)
            .build();
    for (SensorVariable sensorVariable : sensorVariables) {
      // Update sensor variable
      queryInput.setSensorVariable(sensorVariable);
      MetricsDoubleTypeDevice metric =
          DaoUtils.getMetricsDoubleTypeDeviceDao().getMinMaxAvg(queryInput);

      String unit = sensorVariable.getUnit() != "" ? " (" + sensorVariable.getUnit() + ")" : "";

      // If current value not available, do not allow any value
      if (sensorVariable.getValue() == null || metric.getMin() == null) {
        bulletCharts.add(
            MetricsBulletChartNVD3.builder()
                .id(sensorVariable.getId())
                .resourceName(
                    new ResourceModel(RESOURCE_TYPE.SENSOR_VARIABLE, sensorVariable)
                            .getResourceLessDetails()
                        + unit)
                .displayName(
                    sensorVariable.getSensor().getName()
                        + " >> "
                        + sensorVariable.getVariableType().getText()
                        + unit)
                .build());
      } else {
        bulletCharts.add(
            MetricsBulletChartNVD3.builder()
                .id(sensorVariable.getId())
                // .title(sensorVariable.getVariableType().getText())
                // .subtitle(sensorVariable.getUnit())
                .ranges(new Object[] {metric.getMin(), metric.getAvg(), metric.getMax()})
                .measures(new Object[] {sensorVariable.getValue()})
                .markers(new Object[] {sensorVariable.getPreviousValue()})
                .resourceName(
                    new ResourceModel(RESOURCE_TYPE.SENSOR_VARIABLE, sensorVariable)
                            .getResourceLessDetails()
                        + unit)
                .displayName(
                    sensorVariable.getSensor().getName()
                        + " >> "
                        + sensorVariable.getVariableType().getText()
                        + unit)
                .build());
      }
    }
    return bulletCharts;
  }
 private String getFileName(SensorVariable sensorVariable, AGGREGATION_TYPE aggrType) {
   Sensor sensor = DaoUtils.getSensorDao().getById(sensorVariable.getSensor().getId());
   StringBuilder builder = new StringBuilder();
   builder
       .append(sensor.getNode().getName())
       .append("_")
       .append(sensor.getName())
       .append("_Nid-")
       .append(sensor.getNode().getEui())
       .append("_Sid-")
       .append(sensor.getSensorId())
       .append("_")
       .append(sensorVariable.getVariableType().getText());
   if (sensorVariable.getMetricType() == METRIC_TYPE.DOUBLE) {
     builder.append("_").append("SampleType_").append(aggrType);
   }
   builder
       .append("_")
       .append(new SimpleDateFormat("yyyy-MMM-dd_hh-mm-ss").format(new Date()))
       .append(".csv");
   return builder.toString().replaceAll(" ", "_");
 }
  private ArrayList<MetricsChartDataGroupNVD3> getMetricsDataJsonNVD3WithChartType(
      List<Integer> variableIds, Long timestampFrom, Long timestampTo, String chartType) {

    // Get sensor variables
    List<SensorVariable> sensorVariables = null;
    if (!variableIds.isEmpty()) {
      sensorVariables = DaoUtils.getSensorVariableDao().getAll(variableIds);
    }

    // Return if no data available
    if (sensorVariables == null) {
      return new ArrayList<MetricsChartDataGroupNVD3>();
    }

    MetricsGraphSettings metricsGraphSettings =
        ObjectFactory.getAppProperties().getMetricsGraphSettings();
    ArrayList<MetricsChartDataGroupNVD3> finalData = new ArrayList<MetricsChartDataGroupNVD3>();

    SensorVariable yaxis1Variable = null;
    boolean initialLoadDone = false;
    String chartTypeInternal = null;

    ArrayList<MetricsChartDataNVD3> metricDataValues = new ArrayList<MetricsChartDataNVD3>();

    String unit = null;
    String unit2 = null;
    String chartInterpolate = null;
    boolean isMultiChart = false;

    for (SensorVariable sensorVariable : sensorVariables) {
      MetricsGraph metrics =
          metricsGraphSettings.getMetric(sensorVariable.getVariableType().getText());
      // Load initial settings
      if (!initialLoadDone) {
        if (CHART_TYPE.MULTI_CHART == CHART_TYPE.fromString(chartType)) {
          isMultiChart = true;
          yaxis1Variable = sensorVariable;
          chartTypeInternal = chartType;
        } else if (CHART_TYPE.LINE_CHART == CHART_TYPE.fromString(chartType)) {
          chartTypeInternal = chartType;
          chartInterpolate = metrics.getInterpolate();
        }
        initialLoadDone = true;
      }

      Integer yAxis = null;
      if (yaxis1Variable != null) {
        if (yaxis1Variable.getVariableType() == sensorVariable.getVariableType()) {
          yAxis = 1;
          unit = sensorVariable.getUnit();
        } else {
          yAxis = 2;
          unit2 = sensorVariable.getUnit();
        }
      } else {
        unit = sensorVariable.getUnit();
      }

      if (chartTypeInternal == null) {
        chartTypeInternal = metrics.getType();
      }

      String seriesName = null;
      if (isMultiChart) {
        seriesName =
            sensorVariable.getSensor().getName() + "-" + sensorVariable.getVariableType().getText();
      } else {
        seriesName = sensorVariable.getSensor().getName();
      }
      switch (sensorVariable.getMetricType()) {
        case DOUBLE:
          MetricsDoubleTypeDevice metricQueryDouble =
              MetricsDoubleTypeDevice.builder()
                  .timestampFrom(timestampFrom)
                  .timestampTo(timestampTo)
                  .sensorVariable(sensorVariable)
                  .build();
          List<MetricsDoubleTypeDevice> doubleMetrics =
              DaoUtils.getMetricsDoubleTypeDeviceDao().getAll(metricQueryDouble);
          ArrayList<Object> avgMetricDoubleValues = new ArrayList<Object>();
          for (MetricsDoubleTypeDevice metric : doubleMetrics) {
            if (isMultiChart) {
              avgMetricDoubleValues.add(
                  MetricsChartDataXY.builder().x(metric.getTimestamp()).y(metric.getAvg()).build());
            } else {
              avgMetricDoubleValues.add(new Object[] {metric.getTimestamp(), metric.getAvg()});
            }
          }
          if (!doubleMetrics.isEmpty()) {
            metricDataValues.add(
                MetricsChartDataNVD3.builder()
                    .key(seriesName)
                    .values(avgMetricDoubleValues)
                    .type(metrics.getSubType())
                    // .interpolate(metrics.getInterpolate())
                    .yAxis(yAxis)
                    .build()
                    .updateSubType(chartTypeInternal));
          }

          break;
        case BINARY:
          MetricsBinaryTypeDevice metricQueryBinary =
              MetricsBinaryTypeDevice.builder()
                  .timestampFrom(timestampFrom)
                  .timestampTo(timestampTo)
                  .sensorVariable(sensorVariable)
                  .build();
          List<MetricsBinaryTypeDevice> binaryMetrics =
              DaoUtils.getMetricsBinaryTypeDeviceDao().getAll(metricQueryBinary);
          ArrayList<Object> metricBinaryValues = new ArrayList<Object>();
          for (MetricsBinaryTypeDevice metric : binaryMetrics) {
            if (isMultiChart) {
              metricBinaryValues.add(
                  MetricsChartDataXY.builder()
                      .x(metric.getTimestamp())
                      .y(metric.getState() ? 1 : 0)
                      .build());
            } else {
              metricBinaryValues.add(
                  new Object[] {metric.getTimestamp(), metric.getState() ? 1 : 0});
            }
          }
          if (!binaryMetrics.isEmpty()) {
            metricDataValues.add(
                MetricsChartDataNVD3.builder()
                    .key(seriesName)
                    .values(metricBinaryValues)
                    .type(metrics.getSubType())
                    // .interpolate(metrics.getInterpolate())
                    .yAxis(yAxis)
                    .id(sensorVariable.getId())
                    .resourceName(
                        new ResourceModel(RESOURCE_TYPE.SENSOR_VARIABLE, sensorVariable)
                            .getResourceLessDetails())
                    .build()
                    .updateSubType(chartTypeInternal));
          }

          break;
        default:
          // no need to do anything here
          break;
      }
    }

    finalData.add(
        MetricsChartDataGroupNVD3.builder()
            .metricsChartDataNVD3(metricDataValues)
            .unit(unit)
            .unit2(unit2)
            .timeFormat(getTimeFormat(timestampFrom))
            .chartType(chartType)
            .chartInterpolate(chartInterpolate)
            .build());

    return finalData;
  }