Fix for arango report
This commit is contained in:
parent
5346f94e57
commit
29d6de5bf9
1 changed files with 118 additions and 54 deletions
|
|
@ -31,7 +31,7 @@ public class DownloadService {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DownloadService.class);
|
private static final Logger logger = LoggerFactory.getLogger(DownloadService.class);
|
||||||
|
|
||||||
public File download(BaseDownloadRequest selectedRequest, List<String> ids, RequestParameters parameters, Map<String, Boolean> validationResults) {
|
public File download(BaseDownloadRequest selectedRequest, List<String> ids, RequestParameters parameters, Map<String, Boolean> validationResults) throws SQLException {
|
||||||
LocalDate startDate = parameters.getStartDate();
|
LocalDate startDate = parameters.getStartDate();
|
||||||
LocalDate endDate = parameters.getEndDate();
|
LocalDate endDate = parameters.getEndDate();
|
||||||
if (selectedRequest instanceof SQLDownloadRequest) {
|
if (selectedRequest instanceof SQLDownloadRequest) {
|
||||||
|
|
@ -42,43 +42,65 @@ public class DownloadService {
|
||||||
throw new IllegalArgumentException("Unsupported request type: " + selectedRequest.getClass().getSimpleName());
|
throw new IllegalArgumentException("Unsupported request type: " + selectedRequest.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private File processAqlDownloadRequest(AQLDownloadRequest request, List<String> ids, LocalDate startDate, LocalDate endDate, Map<String, Boolean> validationResults) {
|
private File processAqlDownloadRequest(AQLDownloadRequest request, List<String> ids, LocalDate startDate, LocalDate endDate, Map<String, Boolean> validationResults) throws SQLException {
|
||||||
ArangoDatabase arangoDb = ArangoDBConnection.getConnection(request.getAqlConnectionParams());
|
try {
|
||||||
|
ArangoDatabase arangoDb = ArangoDBConnection.getConnection(request.getAqlConnectionParams());
|
||||||
|
|
||||||
Boolean emptyIdsAllowed = validationResults.get(ValidationService.IS_EMPTY_IDS_ALLOWED);
|
Boolean emptyIdsAllowed = validationResults.get(ValidationService.IS_EMPTY_IDS_ALLOWED);
|
||||||
Boolean emptyDatesAllowed = validationResults.get(ValidationService.IS_EMPTY_DATES_ALLOWED);
|
Boolean emptyDatesAllowed = validationResults.get(ValidationService.IS_EMPTY_DATES_ALLOWED);
|
||||||
|
|
||||||
|
List<Map<String, Object>> entities = executeSelectAqlRequest(
|
||||||
|
arangoDb,
|
||||||
|
request.getDownloadRequestEntitySelectorQuery(),
|
||||||
|
ids,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
emptyIdsAllowed,
|
||||||
|
emptyDatesAllowed
|
||||||
|
);
|
||||||
|
|
||||||
Map<String, Object> entities = executeSelectAqlRequest(arangoDb, request.getDownloadRequestEntitySelectorQuery(), ids, emptyIdsAllowed);
|
if (entities.isEmpty()) {
|
||||||
|
logger.warn("No entities found for main AQL request.");
|
||||||
|
throw new NoDownloadReportRecordsException(
|
||||||
|
"Отчет не может быть сгенерирован. Нет записей в базе для успешной генерации."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (entities.isEmpty()) {
|
return writeResultsToCsv(entities);
|
||||||
logger.warn("No entities found for main AQL request.");
|
|
||||||
throw new NoDownloadReportRecordsException("Отчет не может быть сгенерирован. Нет записей в базе для успешной генерации.");
|
} catch (ArangoDBException e) {
|
||||||
|
logger.error("Error connecting to ArangoDB or executing AQL query: {}", e.getMessage(), e);
|
||||||
|
throw new SQLException("Ошибка работы с базой данных. Попробуйте позже.", e);
|
||||||
|
} catch (NoDownloadReportRecordsException e) {
|
||||||
|
logger.warn("No records available for report generation: {}", e.getMessage());
|
||||||
|
throw e;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Unexpected error occurred during report generation: {}", e.getMessage(), e);
|
||||||
|
throw new RuntimeException("Произошла непредвиденная ошибка при генерации отчета.", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Map<String, Object>> results = new ArrayList<>();
|
// request.getAqlRequestCollections().forEach(collection -> {
|
||||||
request.getAqlRequestCollections().forEach(collection -> {
|
// String type = collection.getCollectionUrl();
|
||||||
String type = collection.getCollectionUrl();
|
// String entityType;
|
||||||
String entityType;
|
//
|
||||||
|
// if (Objects.equals(type, "applications")) {
|
||||||
|
// entityType = "applicationId";
|
||||||
|
// } else {
|
||||||
|
// entityType = type + "Id";
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Object entityIds = entities.get(entityType);
|
||||||
|
//
|
||||||
|
// if (entityIds instanceof String) {
|
||||||
|
// entityIds = Collections.singletonList((String) entityIds);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// String aqlQuery = buildAqlQuery(type, ids, collection.getDateAttribute(), startDate, endDate, emptyIdsAllowed, emptyDatesAllowed);
|
||||||
|
//
|
||||||
|
// results.addAll(executeAqlQuery(arangoDb, aqlQuery, (List<String>) entityIds, startDate, endDate, emptyIdsAllowed, emptyDatesAllowed));
|
||||||
|
// });
|
||||||
|
|
||||||
if (Objects.equals(type, "applications")) {
|
// return writeResultsToCsv(results);
|
||||||
entityType = "applicationId";
|
|
||||||
} else {
|
|
||||||
entityType = type + "Id";
|
|
||||||
}
|
|
||||||
|
|
||||||
Object entityIds = entities.get(entityType);
|
|
||||||
|
|
||||||
if (entityIds instanceof String) {
|
|
||||||
entityIds = Collections.singletonList((String) entityIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
String aqlQuery = buildAqlQuery(type, ids, collection.getDateAttribute(), startDate, endDate, emptyIdsAllowed, emptyDatesAllowed);
|
|
||||||
|
|
||||||
results.addAll(executeAqlQuery(arangoDb, aqlQuery, (List<String>) entityIds, startDate, endDate, emptyIdsAllowed, emptyDatesAllowed));
|
|
||||||
});
|
|
||||||
|
|
||||||
return writeResultsToCsv(results);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private File processSqlDownloadRequest(SQLDownloadRequest request, List<String> ids, LocalDate startDate, LocalDate endDate, Map<String, Boolean> validationResults) {
|
private File processSqlDownloadRequest(SQLDownloadRequest request, List<String> ids, LocalDate startDate, LocalDate endDate, Map<String, Boolean> validationResults) {
|
||||||
|
|
@ -114,35 +136,77 @@ public class DownloadService {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> executeSelectAqlRequest(ArangoDatabase arangoDb,
|
private List<Map<String, Object>> executeSelectAqlRequest(ArangoDatabase arangoDb,
|
||||||
String downloadRequestEntitySelectorQuery,
|
String downloadRequestEntitySelectorQuery,
|
||||||
List<String> ids, Boolean emptyIdsAllowed) {
|
List<String> ids, LocalDate startDate, LocalDate endDate, Boolean emptyIdsAllowed, Boolean emptyDatesAllowed) {
|
||||||
Map<String, Object> entities = new HashMap<>();
|
List<Map<String, Object>> results = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
Map<String, Object> bindVars = new HashMap<>();
|
try {
|
||||||
|
Map<String, Object> bindVars = new HashMap<>();
|
||||||
if (!emptyIdsAllowed) bindVars.put("ids", ids);
|
if (!emptyIdsAllowed && ids != null && !ids.isEmpty()) {
|
||||||
|
bindVars.put("ids", ids);
|
||||||
AqlQueryOptions aqlQueryOptions = new AqlQueryOptions();
|
}
|
||||||
|
if (!emptyDatesAllowed) {
|
||||||
try (ArangoCursor<Map> cursor = arangoDb.query(downloadRequestEntitySelectorQuery, Map.class, bindVars, aqlQueryOptions)) {
|
if (startDate != null) {
|
||||||
while (cursor.hasNext()) {
|
bindVars.put("startDate", startDate.toString());
|
||||||
Map<String, Object> result = cursor.next();
|
}
|
||||||
|
if (endDate != null) {
|
||||||
for (Map.Entry<String, Object> entry : result.entrySet()) {
|
bindVars.put("endDate", endDate.toString());
|
||||||
String key = entry.getKey();
|
|
||||||
Object entityValue = entry.getValue();
|
|
||||||
|
|
||||||
entities.put(key, entityValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
logger.error("Failed to execute AQL url", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return entities;
|
logger.info("Executing AQL query: {}\nWith bindVars: {}", downloadRequestEntitySelectorQuery, bindVars);
|
||||||
|
|
||||||
|
AqlQueryOptions aqlQueryOptions = new AqlQueryOptions();
|
||||||
|
try (ArangoCursor<Map> cursor = arangoDb.query(downloadRequestEntitySelectorQuery, Map.class, bindVars, aqlQueryOptions)) {
|
||||||
|
while (cursor.hasNext()) {
|
||||||
|
results.add(cursor.next());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (ArangoDBException e) {
|
||||||
|
logger.error("AQL query execution failed: {}\nError: {}", downloadRequestEntitySelectorQuery, e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
// Map<String, Object> entities = new HashMap<>();
|
||||||
|
//
|
||||||
|
// Map<String, Object> bindVars = new HashMap<>();
|
||||||
|
// if (!emptyIdsAllowed && ids != null && !ids.isEmpty()) {
|
||||||
|
// bindVars.put("ids", ids);
|
||||||
|
// }
|
||||||
|
// if (!emptyDatesAllowed) {
|
||||||
|
// if (startDate != null) {
|
||||||
|
// bindVars.put("startDate", startDate.toString());
|
||||||
|
// }
|
||||||
|
// if (endDate != null) {
|
||||||
|
// bindVars.put("endDate", endDate.toString());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// logger.info("Executing AQL query: {}\nWith bindVars: {}", aqlQuery, bindVars);
|
||||||
|
//
|
||||||
|
// AqlQueryOptions aqlQueryOptions = new AqlQueryOptions();
|
||||||
|
//
|
||||||
|
// try (ArangoCursor<Map> cursor = arangoDb.query(downloadRequestEntitySelectorQuery, Map.class, bindVars, aqlQueryOptions)) {
|
||||||
|
// while (cursor.hasNext()) {
|
||||||
|
// Map<String, Object> result = cursor.next();
|
||||||
|
//
|
||||||
|
// for (Map.Entry<String, Object> entry : result.entrySet()) {
|
||||||
|
// String key = entry.getKey();
|
||||||
|
// Object entityValue = entry.getValue();
|
||||||
|
//
|
||||||
|
// entities.put(key, entityValue);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// catch (Exception e) {
|
||||||
|
// logger.error("Failed to execute AQL url", e);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String buildAqlQuery(String collectionName, List<String> ids, String dateAttribute, LocalDate startDate, LocalDate endDate, Boolean emptyIdsAllowed, Boolean emptyDatesAllowed) {
|
private String buildAqlQuery(String collectionName, List<String> ids, String dateAttribute, LocalDate startDate, LocalDate endDate, Boolean emptyIdsAllowed, Boolean emptyDatesAllowed) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue