##// END OF EJS Templates
Implementation of progression
Implementation of progression

File last commit:

r606:7ea0025fca62 feature/SynchroIm...
r606:7ea0025fca62 feature/SynchroIm...
Show More
AmdaProvider.cpp
254 lines | 9.2 KiB | text/x-c | CppLexer
Alexandre Leroux
Inits Amda provider
r348 #include "AmdaProvider.h"
Alexandre Leroux
Amda provider update (2)...
r380 #include "AmdaDefs.h"
Alexandre Leroux
Amda provider (3)...
r351 #include "AmdaResultParser.h"
Alexandre Leroux
Uses DateUtils
r450 #include <Common/DateUtils.h>
Alexandre Leroux
Amda provider (1)...
r349 #include <Data/DataProviderParameters.h>
Modify the AmdaProvider to remove from it all network controller...
r358 #include <Network/NetworkController.h>
#include <SqpApplication.h>
#include <Variable/Variable.h>
Alexandre Leroux
Amda provider (1)...
r349
#include <QNetworkAccessManager>
#include <QNetworkReply>
Alexandre Leroux
Amda provider (2)...
r350 #include <QTemporaryFile>
Modify the AmdaProvider to remove from it all network controller...
r358 #include <QThread>
Alexandre Leroux
Inits Amda provider
r348
Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider")
Alexandre Leroux
Amda provider (1)...
r349 namespace {
/// URL format for a request on AMDA server. The parameters are as follows:
/// - %1: start date
/// - %2: end date
/// - %3: parameter id
const auto AMDA_URL_FORMAT = QStringLiteral(
"http://amda.irap.omp.eu/php/rest/"
Alexandre Leroux
Removes sampling in AMDA provider
r437 "getParameter.php?startTime=%1&stopTime=%2&parameterID=%3&outputFormat=ASCII&"
Alexandre Leroux
Amda provider (1)...
r349 "timeFormat=ISO8601&gzip=0");
/// Dates format passed in the URL (e.g 2013-09-23T09:00)
Log AMDA get url of the file and the download file
r406 const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss");
Alexandre Leroux
Amda provider (1)...
r349
Implementation of progression
r606 // struct AmdaProgression {
// QUuid acqIdentifier;
// std::map<QNetworkRequest, double> m_RequestId;
//};
Alexandre Leroux
Amda provider (1)...
r349 /// Formats a time to a date that can be passed in URL
Change SqpRange for SqpDateTime
r471 QString dateFormat(double sqpRange) noexcept
Alexandre Leroux
Amda provider (1)...
r349 {
Change SqpRange for SqpDateTime
r471 auto dateTime = DateUtils::dateTime(sqpRange);
Alexandre Leroux
Amda provider (1)...
r349 return dateTime.toString(AMDA_TIME_FORMAT);
}
Alexandre Leroux
Reads variable metadata to get the data type...
r533 AmdaResultParser::ValueType valueType(const QString &valueType)
{
if (valueType == QStringLiteral("scalar")) {
return AmdaResultParser::ValueType::SCALAR;
}
else if (valueType == QStringLiteral("vector")) {
return AmdaResultParser::ValueType::VECTOR;
}
else {
return AmdaResultParser::ValueType::UNKNOWN;
}
}
Alexandre Leroux
Amda provider (1)...
r349 } // namespace
Alexandre Leroux
Amda provider cleaning...
r376 AmdaProvider::AmdaProvider()
Alexandre Leroux
Inits Amda provider
r348 {
Alexandre Leroux
Minor refactoring...
r424 qCDebug(LOG_AmdaProvider()) << tr("AmdaProvider::AmdaProvider") << QThread::currentThread();
Modify the AmdaProvider to remove from it all network controller...
r358 if (auto app = sqpApp) {
auto &networkController = app->networkController();
Alexandre Leroux
Updates signal to be valid on Windows
r383 connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid,
std::function<void(QNetworkReply *, QUuid)>)),
&networkController,
SLOT(onProcessRequested(QNetworkRequest, QUuid,
std::function<void(QNetworkReply *, QUuid)>)));
Remove connection for progress from NC -> VC to NC -> Provider.
r391
Implementation of progression
r606 connect(&sqpApp->networkController(),
SIGNAL(replyDownloadProgress(QUuid, const QNetworkRequest &, double)), this,
SLOT(onReplyDownloadProgress(QUuid, const QNetworkRequest &, double)));
Modify the AmdaProvider to remove from it all network controller...
r358 }
Alexandre Leroux
Inits Amda provider
r348 }
Implementation of V5 acquisition
r510 void AmdaProvider::requestDataLoading(QUuid acqIdentifier, const DataProviderParameters &parameters)
Alexandre Leroux
Inits Amda provider
r348 {
// NOTE: Try to use multithread if possible
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r375 const auto times = parameters.m_Times;
Alexandre Leroux
Amda provider update (2)...
r380 const auto data = parameters.m_Data;
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r375 for (const auto &dateTime : qAsConst(times)) {
Add synchronization part of v5 acquisition
r511 this->retrieveData(acqIdentifier, dateTime, data);
Correction for MR
r517
Implementation of progression
r606
Add sleep to permits AMDA to wait 1 sec between each request
r587 // TORM when AMDA will support quick asynchrone request
QThread::msleep(1000);
Alexandre Leroux
Inits Amda provider
r348 }
}
Implementation of V5 acquisition
r510 void AmdaProvider::requestDataAborting(QUuid acqIdentifier)
Implement of the abort download process
r388 {
if (auto app = sqpApp) {
auto &networkController = app->networkController();
Implementation of V5 acquisition
r510 networkController.onReplyCanceled(acqIdentifier);
Implement of the abort download process
r388 }
}
Implementation of progression
r606 void AmdaProvider::onReplyDownloadProgress(QUuid acqIdentifier,
const QNetworkRequest &networkRequest, double progress)
{
qCCritical(LOG_AmdaProvider()) << tr("onReplyDownloadProgress") << progress;
auto acqIdToRequestProgressMapIt = m_AcqIdToRequestProgressMap.find(acqIdentifier);
if (acqIdToRequestProgressMapIt != m_AcqIdToRequestProgressMap.end()) {
qCCritical(LOG_AmdaProvider()) << tr("1 onReplyDownloadProgress") << progress;
auto requestPtr = &networkRequest;
auto findRequest
= [requestPtr](const auto &entry) { return requestPtr == entry.first.get(); };
auto &requestProgressMap = acqIdToRequestProgressMapIt->second;
auto requestProgressMapEnd = requestProgressMap.end();
auto requestProgressMapIt
= std::find_if(requestProgressMap.begin(), requestProgressMapEnd, findRequest);
if (requestProgressMapIt != requestProgressMapEnd) {
requestProgressMapIt->second = progress;
}
else {
qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve Request in progress");
}
}
acqIdToRequestProgressMapIt = m_AcqIdToRequestProgressMap.find(acqIdentifier);
if (acqIdToRequestProgressMapIt != m_AcqIdToRequestProgressMap.end()) {
qCCritical(LOG_AmdaProvider()) << tr("2 onReplyDownloadProgress") << progress;
double finalProgress = 0.0;
auto &requestProgressMap = acqIdToRequestProgressMapIt->second;
auto fraq = requestProgressMap.size();
for (auto requestProgress : requestProgressMap) {
finalProgress += requestProgress.second;
}
if (fraq > 0) {
finalProgress = finalProgress / fraq;
}
qCCritical(LOG_AmdaProvider()) << tr("2 onReplyDownloadProgress") << finalProgress;
emit dataProvidedProgress(acqIdentifier, finalProgress);
}
else {
emit dataProvidedProgress(acqIdentifier, 0.0);
}
}
Change SqpRange for SqpDateTime
r471 void AmdaProvider::retrieveData(QUuid token, const SqpRange &dateTime, const QVariantHash &data)
Alexandre Leroux
Amda provider (1)...
r349 {
Alexandre Leroux
Amda provider update (2)...
r380 // Retrieves product ID from data: if the value is invalid, no request is made
auto productId = data.value(AMDA_XML_ID_KEY).toString();
if (productId.isNull()) {
qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id");
return;
}
Implementation of V5 acquisition
r510 qCDebug(LOG_AmdaProvider()) << tr("AmdaProvider::retrieveData") << dateTime;
Alexandre Leroux
Amda provider update (2)...
r380
Alexandre Leroux
Reads variable metadata to get the data type...
r533 // Retrieves the data type that determines whether the expected format for the result file is
// scalar, vector...
auto productValueType = valueType(data.value(AMDA_DATA_TYPE_KEY).toString());
Alexandre Leroux
Amda provider (1)...
r349 // /////////// //
// Creates URL //
// /////////// //
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r375 auto startDate = dateFormat(dateTime.m_TStart);
auto endDate = dateFormat(dateTime.m_TEnd);
Alexandre Leroux
Amda provider (1)...
r349
auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)};
Correction for MR
r517 qCInfo(LOG_AmdaProvider()) << tr("TORM AmdaProvider::retrieveData url:") << url;
Modify the AmdaProvider to remove from it all network controller...
r358 auto tempFile = std::make_shared<QTemporaryFile>();
Alexandre Leroux
Amda provider (1)...
r349
Modify the AmdaProvider to remove from it all network controller...
r358 // LAMBDA
Alexandre Leroux
Reads variable metadata to get the data type...
r533 auto httpDownloadFinished = [this, dateTime, tempFile,
productValueType](QNetworkReply *reply, QUuid dataId) noexcept {
// Don't do anything if the reply was abort
if (reply->error() != QNetworkReply::OperationCanceledError) {
if (tempFile) {
auto replyReadAll = reply->readAll();
if (!replyReadAll.isEmpty()) {
tempFile->write(replyReadAll);
}
tempFile->close();
// Parse results file
if (auto dataSeries
= AmdaResultParser::readTxt(tempFile->fileName(), productValueType)) {
emit dataProvided(dataId, dataSeries, dateTime);
}
else {
/// @todo ALX : debug
}
}
Implementation of progression
r606 m_AcqIdToRequestProgressMap.erase(dataId);
Alexandre Leroux
Reads variable metadata to get the data type...
r533 }
};
Add implementation of abort impact on Amda plugin
r397 auto httpFinishedLambda
= [this, httpDownloadFinished, tempFile](QNetworkReply *reply, QUuid dataId) noexcept {
Alexandre Leroux
Amda provider (2)...
r350
Add implementation of abort impact on Amda plugin
r397 // Don't do anything if the reply was abort
if (reply->error() != QNetworkReply::OperationCanceledError) {
auto downloadFileUrl = QUrl{QString{reply->readAll()}};
Implement of the abort download process
r388
Correction for MR
r517 qCInfo(LOG_AmdaProvider())
<< tr("TORM AmdaProvider::retrieveData downloadFileUrl:") << downloadFileUrl;
Add implementation of abort impact on Amda plugin
r397 // Executes request for downloading file //
Modify the AmdaProvider to remove from it all network controller...
r358
Add implementation of abort impact on Amda plugin
r397 // Creates destination file
if (tempFile->open()) {
// Executes request
Implementation of progression
r606 auto request = std::make_shared<QNetworkRequest>(downloadFileUrl);
updateRequestProgress(dataId, request, 0.0);
emit requestConstructed(*request.get(), dataId, httpDownloadFinished);
Add implementation of abort impact on Amda plugin
r397 }
}
Implementation of progression
r606 else {
m_AcqIdToRequestProgressMap.erase(dataId);
}
Add implementation of abort impact on Amda plugin
r397 };
Modify the AmdaProvider to remove from it all network controller...
r358
// //////////////// //
// Executes request //
// //////////////// //
Implementation of progression
r606
auto request = std::make_shared<QNetworkRequest>(url);
updateRequestProgress(token, request, 0.0);
emit requestConstructed(*request.get(), token, httpFinishedLambda);
}
void AmdaProvider::updateRequestProgress(QUuid acqIdentifier,
std::shared_ptr<QNetworkRequest> request, double progress)
{
auto acqIdToRequestProgressMapIt = m_AcqIdToRequestProgressMap.find(acqIdentifier);
if (acqIdToRequestProgressMapIt != m_AcqIdToRequestProgressMap.end()) {
auto &requestProgressMap = acqIdToRequestProgressMapIt->second;
auto requestProgressMapIt = requestProgressMap.find(request);
if (requestProgressMapIt != requestProgressMap.end()) {
requestProgressMapIt->second = progress;
}
else {
acqIdToRequestProgressMapIt->second.insert(std::make_pair(request, progress));
}
}
else {
auto requestProgressMap = std::map<std::shared_ptr<QNetworkRequest>, double>{};
requestProgressMap.insert(std::make_pair(request, progress));
m_AcqIdToRequestProgressMap.insert(
std::make_pair(acqIdentifier, std::move(requestProgressMap)));
}
Alexandre Leroux
Inits Amda provider
r348 }