/** * Creates a map from column id to value according to the aggregation columns. * * @param row The table row. * @param table The table. * @return A map from column id to value according to the aggregation columns. */ private Map<String, Value> getValuesToAggregate(TableRow row, DataTable table) { Map<String, Value> result = Maps.newHashMap(); // The map is generated by looking for the values of the aggregation columns // in the table row. for (String columnId : aggregateColumns) { Value curValue = row.getCell(table.getColumnIndex(columnId)).getValue(); result.put(columnId, curValue); } return result; }
/** * Creates a path for the aggregation tree defined by a table row. * * @param row The table row. * @param table The table. * @param depth The depth of the desired path. * @return A path for the aggregation tree defined by the table row. */ public AggregationPath getRowPath(TableRow row, DataTable table, int depth) { AggregationPath result = new AggregationPath(); // The tree path is generated by looking for the values of the group-by // columns in the table row (in the correct order). for (int i = 0; i <= depth; i++) { String columnId = groupByColumns.get(i); Value curValue = row.getCell(table.getColumnIndex(columnId)).getValue(); result.add(curValue); } return result; }
private String generateDataTableJson(TimeChartData chartData) throws DataSourceException { DataTable data = new DataTable(); ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>(); cd.add(new ColumnDescription("date", ValueType.DATETIME, "Date")); for (String serie : chartData.getSeries()) { cd.add(new ColumnDescription(serie, ValueType.NUMBER, serie)); } data.addColumns(cd); try { for (TimeChartDataValue timeSerie : chartData.getValues()) { TableRow row = new TableRow(); GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone("GMT")); calendar.setTimeInMillis( TimeZone.getDefault().getOffset(timeSerie.getTimestamp().getTime()) + timeSerie.getTimestamp().getTime()); row.addCell(new TableCell(new DateTimeValue(calendar))); for (Double value : timeSerie.getValues()) { if (value != null) { row.addCell(value); } else { row.addCell(Value.getNullValueFromValueType(ValueType.NUMBER)); } } data.addRow(row); } } catch (TypeMismatchException e) { System.out.println("Invalid type!"); } Query query = new Query(); DataSourceParameters parameters = new DataSourceParameters(""); DataSourceRequest request = new DataSourceRequest(query, parameters, ULocale.UK); String generateResponse = DataSourceHelper.generateResponse(data, request); return generateResponse; }
/** * Constructs a table aggregator and aggregates the table. * * @param groupByColumns An ordered list of columns to group by. * @param aggregateColumns A set of columns to aggregate. * @param table The table. */ public TableAggregator( List<String> groupByColumns, Set<String> aggregateColumns, DataTable table) { this.groupByColumns = groupByColumns; this.aggregateColumns = aggregateColumns; tree = new AggregationTree(aggregateColumns, table); // Iterate over all rows and aggregate each row via the aggregation tree. for (TableRow row : table.getRows()) { tree.aggregate( getRowPath(row, table, groupByColumns.size() - 1), getValuesToAggregate(row, table)); } }
/** * Generates the data table. This servlet assumes a special parameter that contains the CSV URL * from which to load the data. */ @Override public DataTable generateDataTable(Query query, HttpServletRequest request) throws DataSourceException { if (!this.isAuthorized(request)) return null; int user_id = (Integer) request.getSession().getAttribute(AuthorizedAction.USER_ID); String prj_id = request.getParameter(Env.PROJECT_PARAM_NAME); String sub_id = request.getParameter(Env.SUBJECT_PARAM_NAME); String date = request.getParameter(Env.DATE_PARAM_NAME); String time = request.getParameter(Env.TIME_PARAM_NAME); boolean includeExtraFields = "1".equals(request.getParameter(Env.TYPE_PARAM_NAME)); logger.info( "ActivityResultServlet.generateDataTable: " + prj_id + "/" + sub_id + "/" + date + "/" + time + "/" + includeExtraFields); DataTable dataTable = new DataTable(); List<ColumnDescription> cds = new LinkedList<ColumnDescription>(); cds.add(new ColumnDescription("TIME", ValueType.NUMBER, "TIME")); cds.add(new ColumnDescription("ACTIVITY", ValueType.NUMBER, "ACTIVITY")); // Below 3 columns are algorithm specific if (includeExtraFields) { cds.add(new ColumnDescription("SMA", ValueType.NUMBER, "SMA")); cds.add(new ColumnDescription("SMA-H", ValueType.NUMBER, "SMA-H")); cds.add(new ColumnDescription("SMA-L", ValueType.NUMBER, "SMA-L")); } dataTable.addColumns(cds); List<TableRow> rows = new LinkedList<TableRow>(); try { ActData ad = ReportManager.getAct(prj_id, sub_id, date, Integer.parseInt(time)); double sma_h = Double.parseDouble(SettingManager.getValue(user_id, "sma_h")); double sma_l = Double.parseDouble(SettingManager.getValue(user_id, "sma_l")); for (int i = 0; i < ad.getAct().length; ++i) { // expend to 100Hz data to sync with raw signal for (int j = 0; j < 100; ++j) { TableRow tr = new TableRow(); tr.addCell((ad.getStart_second() + i) * 100 + j); tr.addCell(ad.getAct()[i].num_filt); // Below 3 columns are algorithm specific if (includeExtraFields) { tr.addCell(ad.getAct()[i].sma); tr.addCell(sma_h); tr.addCell(sma_l); } rows.add(tr); } } } catch (Exception e) { logger.error(e.getMessage(), e); throw new DataSourceException(ReasonType.OTHER, e.getMessage()); } logger.info("Return data (rows): " + rows.size() + " x " + rows.get(0).getCells().size()); dataTable.addRows(rows); return dataTable; }