Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,28 @@

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.apache.tsfile.viewer.dto.ChartDataRequest;
import org.apache.tsfile.viewer.dto.ChartDataResponse;
import org.apache.tsfile.viewer.dto.DataPreviewRequest;
import org.apache.tsfile.viewer.dto.DataPreviewResponse;
import org.apache.tsfile.viewer.dto.ExportFormat;
import org.apache.tsfile.viewer.exception.AccessDeniedException;
import org.apache.tsfile.viewer.exception.QueryTimeoutException;
import org.apache.tsfile.viewer.exception.TsFileNotFoundException;
import org.apache.tsfile.viewer.service.DataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;

/**
Expand All @@ -47,6 +52,7 @@
*
* <ul>
* <li>POST /api/data/preview - Paginated data preview with filtering
* <li>POST /api/data/export - Streaming export of all rows matching the filters
* <li>POST /api/data/query - Chart data with aggregation
* </ul>
*
Expand Down Expand Up @@ -80,6 +86,60 @@ public ResponseEntity<DataPreviewResponse> previewData(
return ResponseEntity.ok(response);
}

/**
* Exports every row matching the given filters as a downloadable CSV or JSON file.
*
* <p>The request body is the preview request, so filtering behaves exactly like the preview; its
* {@code limit}/{@code offset} are ignored and the response is streamed page by page, which means
* an export is neither limited to the current preview page nor to {@code
* tsfile.query.max-result-size}.
*
* @param request the data preview request carrying the filters to apply
* @param format {@code csv} or {@code json}
* @param response the response the export is streamed to
*/
@PostMapping("/export")
public void exportData(
@RequestBody @Valid DataPreviewRequest request,
@RequestParam(name = "format", defaultValue = "csv") String format,
HttpServletResponse response)
throws TsFileNotFoundException, AccessDeniedException, IOException {
ExportFormat exportFormat = ExportFormat.from(format);
logger.debug("Exporting data for fileId={} as {}", request.getFileId(), exportFormat);

// Resolve the file (and the CSV columns) before committing the response, so an unknown fileId
// (404) or a forbidden path (403) still yields the regular JSON error body instead of a broken
// download.
DataService.ExportPlan plan = dataService.prepareExport(request, exportFormat);

response.setContentType(exportFormat.getContentType());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, exportFormat.getContentDisposition());

long startTime = System.currentTimeMillis();
try {
long rows = dataService.exportData(plan, response.getOutputStream());
logger.info(
"Exported {} rows as {} for fileId={} in {} ms",
rows,
exportFormat,
request.getFileId(),
System.currentTimeMillis() - startTime);
} catch (IOException e) {
// Part of the body may already be on the wire (client gone, disk error while reading), so the
// response can only be turned into an error when nothing has been committed yet.
logger.error(
"Export failed for fileId={} as {} after {} ms",
request.getFileId(),
exportFormat,
System.currentTimeMillis() - startTime,
e);
if (!response.isCommitted()) {
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR, "Export failed: " + e.getMessage(), e);
}
}
}

/**
* Queries chart data from a TsFile with optional aggregation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class DataPreviewRequest {
private List<String> devices;
private List<String> measurements;
private ValueRange valueRange;
private List<AdvancedCondition> advancedConditions;

@Min(value = 1, message = "limit must be at least 1")
@Max(value = 1000, message = "limit must not exceed 1000")
Expand Down Expand Up @@ -99,6 +100,14 @@ public void setValueRange(ValueRange valueRange) {
this.valueRange = valueRange;
}

public List<AdvancedCondition> getAdvancedConditions() {
return advancedConditions;
}

public void setAdvancedConditions(List<AdvancedCondition> advancedConditions) {
this.advancedConditions = advancedConditions;
}

public int getLimit() {
return limit;
}
Expand Down Expand Up @@ -141,6 +150,10 @@ public FilterConditions toFilterConditions() {
builder.valueRange(valueRange);
}

if (advancedConditions != null && !advancedConditions.isEmpty()) {
builder.advancedConditions(advancedConditions);
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.tsfile.viewer.dto;

/**
* Downloadable representation of an exported data set.
*
* <p>Each constant carries the media type and the download file name, so the controller and the
* writer in the service layer agree on a single definition.
*/
public enum ExportFormat {
CSV("csv", "text/csv;charset=UTF-8", "data.csv"),
JSON("json", "application/json;charset=UTF-8", "data.json");

private final String parameterValue;
private final String contentType;
private final String fileName;

ExportFormat(String parameterValue, String contentType, String fileName) {
this.parameterValue = parameterValue;
this.contentType = contentType;
this.fileName = fileName;
}

/**
* Resolves the {@code format} request parameter.
*
* @param raw the raw parameter value, e.g. {@code csv}; blank means {@link #CSV}
* @return the matching format
* @throws IllegalArgumentException if the value matches no supported format (mapped to HTTP 400)
*/
public static ExportFormat from(String raw) {
if (raw == null || raw.isBlank()) {
return CSV;
}
String normalized = raw.trim();
for (ExportFormat format : values()) {
if (format.parameterValue.equalsIgnoreCase(normalized)) {
return format;
}
}
throw new IllegalArgumentException(
"Unsupported export format '" + raw + "'; expected 'csv' or 'json'");
}

/** Returns the {@code Content-Type} announced for the download response. */
public String getContentType() {
return contentType;
}

/** Returns the file name announced in the {@code Content-Disposition} response header. */
public String getFileName() {
return fileName;
}

/** Returns the {@code Content-Disposition} value that makes browsers save instead of render. */
public String getContentDisposition() {
return "attachment; filename=\"" + fileName + "\"";
}
}
Loading
Loading