From 0ec770a1fa8f0cb2ece4981ca7db78e4be55d978 2017-07-13 08:28:16 From: Alexandre Leroux Date: 2017-07-13 08:28:16 Subject: [PATCH] Merge branch 'feature/AmdaProviderImprovements' into develop --- diff --git a/core/include/Data/DataProviderParameters.h b/core/include/Data/DataProviderParameters.h index 07df203..1ffc96b 100644 --- a/core/include/Data/DataProviderParameters.h +++ b/core/include/Data/DataProviderParameters.h @@ -9,7 +9,10 @@ * @sa IDataProvider */ struct DataProviderParameters { - SqpDateTime m_Time; + /// Times for which retrieve data + QVector m_Times; + /// Extra data that can be used by the provider to retrieve data + QVariantHash m_Data; }; #endif // SCIQLOP_DATAPROVIDERPARAMETERS_H diff --git a/core/include/Data/IDataProvider.h b/core/include/Data/IDataProvider.h index 2b4cc75..0f16848 100644 --- a/core/include/Data/IDataProvider.h +++ b/core/include/Data/IDataProvider.h @@ -30,10 +30,9 @@ public: virtual ~IDataProvider() noexcept = default; /** - * @brief requestDataLoading provide datas for the data identified by identifier for all - * SqpDateTime of dateTimeList + * @brief requestDataLoading provide datas for the data identified by identifier and parameters */ - virtual void requestDataLoading(QUuid identifier, const QVector &dateTimeList) = 0; + virtual void requestDataLoading(QUuid identifier, const DataProviderParameters ¶meters) = 0; signals: /** diff --git a/core/include/DataSource/DataSourceController.h b/core/include/DataSource/DataSourceController.h index 0fb897c..ce458d8 100644 --- a/core/include/DataSource/DataSourceController.h +++ b/core/include/DataSource/DataSourceController.h @@ -74,10 +74,12 @@ signals: /** * Signal emitted when a variable creation is asked for a product * @param variableName the name of the variable + * @param variableMetadata the metadata of the variable * @param variableProvider the provider that will be used to retrieve the data of the variable * (can be null) */ void variableCreationRequested(const QString &variableName, + const QVariantHash &variableMetadata, std::shared_ptr variableProvider); private: diff --git a/core/include/DataSource/DataSourceItem.h b/core/include/DataSource/DataSourceItem.h index 950d3b8..684da09 100644 --- a/core/include/DataSource/DataSourceItem.h +++ b/core/include/DataSource/DataSourceItem.h @@ -25,7 +25,7 @@ public: static const QString NAME_DATA_KEY; explicit DataSourceItem(DataSourceItemType type, const QString &name); - explicit DataSourceItem(DataSourceItemType type, QHash data = {}); + explicit DataSourceItem(DataSourceItemType type, QVariantHash data = {}); /// @return the actions of the item as a vector QVector actions() const noexcept; @@ -60,7 +60,7 @@ public: QVariant data(const QString &key) const noexcept; /// Gets all data - const QHash &data() const noexcept; + QVariantHash data() const noexcept; bool isRoot() const noexcept; diff --git a/core/include/Variable/Variable.h b/core/include/Variable/Variable.h index 0232cb1..ed1d3d7 100644 --- a/core/include/Variable/Variable.h +++ b/core/include/Variable/Variable.h @@ -22,18 +22,18 @@ class Variable : public QObject { Q_OBJECT public: - explicit Variable(const QString &name, const QString &unit, const QString &mission, - const SqpDateTime &dateTime); + explicit Variable(const QString &name, const SqpDateTime &dateTime, + const QVariantHash &metadata = {}); QString name() const noexcept; - QString mission() const noexcept; - QString unit() const noexcept; SqpDateTime dateTime() const noexcept; void setDateTime(const SqpDateTime &dateTime) noexcept; /// @return the data of the variable, nullptr if there is no data IDataSeries *dataSeries() const noexcept; + QVariantHash metadata() const noexcept; + bool contains(const SqpDateTime &dateTime) const noexcept; bool intersect(const SqpDateTime &dateTime) const noexcept; bool isInside(const SqpDateTime &dateTime) const noexcept; diff --git a/core/include/Variable/VariableController.h b/core/include/Variable/VariableController.h index dc7ef23..2776c18 100644 --- a/core/include/Variable/VariableController.h +++ b/core/include/Variable/VariableController.h @@ -60,9 +60,11 @@ public slots: /** * Creates a new variable and adds it to the model * @param name the name of the new variable + * @param metadata the metadata of the new variable * @param provider the data provider for the new variable */ - void createVariable(const QString &name, std::shared_ptr provider) noexcept; + void createVariable(const QString &name, const QVariantHash &metadata, + std::shared_ptr provider) noexcept; /// Update the temporal parameters of every selected variable to dateTime void onDateTimeOnSelection(const SqpDateTime &dateTime); diff --git a/core/include/Variable/VariableModel.h b/core/include/Variable/VariableModel.h index 554c292..8e8f317 100644 --- a/core/include/Variable/VariableModel.h +++ b/core/include/Variable/VariableModel.h @@ -29,10 +29,11 @@ public: * Creates a new variable in the model * @param name the name of the new variable * @param dateTime the dateTime of the new variable + * @param metadata the metadata associated to the new variable * @return the pointer to the new variable */ - std::shared_ptr createVariable(const QString &name, - const SqpDateTime &dateTime) noexcept; + std::shared_ptr createVariable(const QString &name, const SqpDateTime &dateTime, + const QVariantHash &metadata) noexcept; /** * Deletes a variable from the model, if it exists diff --git a/core/src/DataSource/DataSourceController.cpp b/core/src/DataSource/DataSourceController.cpp index 6107311..0a3d1e3 100644 --- a/core/src/DataSource/DataSourceController.cpp +++ b/core/src/DataSource/DataSourceController.cpp @@ -88,13 +88,13 @@ void DataSourceController::setDataProvider(const QUuid &dataSourceUid, void DataSourceController::loadProductItem(const QUuid &dataSourceUid, const DataSourceItem &productItem) noexcept { - if (productItem.type() == DataSourceItemType::PRODUCT) { + if (productItem.type() == DataSourceItemType::PRODUCT + || productItem.type() == DataSourceItemType::COMPONENT) { /// Retrieves the data provider of the data source (if any) auto it = impl->m_DataProviders.find(dataSourceUid); auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr; - /// @todo retrieve timerange, and pass it to the signal - emit variableCreationRequested(productItem.name(), dataProvider); + emit variableCreationRequested(productItem.name(), productItem.data(), dataProvider); } else { qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product"); diff --git a/core/src/DataSource/DataSourceItem.cpp b/core/src/DataSource/DataSourceItem.cpp index 81dc552..48c1abd 100644 --- a/core/src/DataSource/DataSourceItem.cpp +++ b/core/src/DataSource/DataSourceItem.cpp @@ -6,7 +6,7 @@ const QString DataSourceItem::NAME_DATA_KEY = QStringLiteral("name"); struct DataSourceItem::DataSourceItemPrivate { - explicit DataSourceItemPrivate(DataSourceItemType type, QHash data) + explicit DataSourceItemPrivate(DataSourceItemType type, QVariantHash data) : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} { } @@ -14,16 +14,16 @@ struct DataSourceItem::DataSourceItemPrivate { DataSourceItem *m_Parent; std::vector > m_Children; DataSourceItemType m_Type; - QHash m_Data; + QVariantHash m_Data; std::vector > m_Actions; }; DataSourceItem::DataSourceItem(DataSourceItemType type, const QString &name) - : DataSourceItem{type, QHash{{NAME_DATA_KEY, name}}} + : DataSourceItem{type, QVariantHash{{NAME_DATA_KEY, name}}} { } -DataSourceItem::DataSourceItem(DataSourceItemType type, QHash data) +DataSourceItem::DataSourceItem(DataSourceItemType type, QVariantHash data) : impl{spimpl::make_unique_impl(type, std::move(data))} { } @@ -70,7 +70,7 @@ QVariant DataSourceItem::data(const QString &key) const noexcept return impl->m_Data.value(key); } -const QHash &DataSourceItem::data() const noexcept +QVariantHash DataSourceItem::data() const noexcept { return impl->m_Data; } diff --git a/core/src/Variable/Variable.cpp b/core/src/Variable/Variable.cpp index 2a2ba89..4a66066 100644 --- a/core/src/Variable/Variable.cpp +++ b/core/src/Variable/Variable.cpp @@ -9,27 +9,21 @@ Q_LOGGING_CATEGORY(LOG_Variable, "Variable") struct Variable::VariablePrivate { - explicit VariablePrivate(const QString &name, const QString &unit, const QString &mission, - const SqpDateTime &dateTime) - : m_Name{name}, - m_Unit{unit}, - m_Mission{mission}, - m_DateTime{dateTime}, - m_DataSeries{nullptr} + explicit VariablePrivate(const QString &name, const SqpDateTime &dateTime, + const QVariantHash &metadata) + : m_Name{name}, m_DateTime{dateTime}, m_Metadata{metadata}, m_DataSeries{nullptr} { } QString m_Name; - QString m_Unit; - QString m_Mission; SqpDateTime m_DateTime; // The dateTime available in the view and loaded. not the cache. + QVariantHash m_Metadata; std::unique_ptr m_DataSeries; }; -Variable::Variable(const QString &name, const QString &unit, const QString &mission, - const SqpDateTime &dateTime) - : impl{spimpl::make_unique_impl(name, unit, mission, dateTime)} +Variable::Variable(const QString &name, const SqpDateTime &dateTime, const QVariantHash &metadata) + : impl{spimpl::make_unique_impl(name, dateTime, metadata)} { } @@ -38,16 +32,6 @@ QString Variable::name() const noexcept return impl->m_Name; } -QString Variable::mission() const noexcept -{ - return impl->m_Mission; -} - -QString Variable::unit() const noexcept -{ - return impl->m_Unit; -} - SqpDateTime Variable::dateTime() const noexcept { return impl->m_DateTime; @@ -85,6 +69,11 @@ IDataSeries *Variable::dataSeries() const noexcept return impl->m_DataSeries.get(); } +QVariantHash Variable::metadata() const noexcept +{ + return impl->m_Metadata; +} + bool Variable::contains(const SqpDateTime &dateTime) const noexcept { return impl->m_DateTime.contains(dateTime); diff --git a/core/src/Variable/VariableController.cpp b/core/src/Variable/VariableController.cpp index 8611fb4..cd5aa59 100644 --- a/core/src/Variable/VariableController.cpp +++ b/core/src/Variable/VariableController.cpp @@ -102,7 +102,7 @@ void VariableController::deleteVariables( } } -void VariableController::createVariable(const QString &name, +void VariableController::createVariable(const QString &name, const QVariantHash &metadata, std::shared_ptr provider) noexcept { @@ -112,14 +112,9 @@ void VariableController::createVariable(const QString &name, return; } - - /// @todo : for the moment : - /// - the provider is only used to retrieve data from the variable for its initialization, but - /// it will be retained later - /// - default data are generated for the variable, without taking into account the timerange set - /// in sciqlop auto dateTime = impl->m_TimeController->dateTime(); - if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { + + if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) { auto identifier = QUuid::createUuid(); // store the provider @@ -186,7 +181,8 @@ void VariableController::onRequestDataLoading(std::shared_ptr variable // Ask the provider for each data on the dateTimeListNotInCache auto identifier = impl->m_VariableToIdentifier.at(variable); impl->m_VariableToProviderMap.at(variable)->requestDataLoading( - identifier, std::move(dateTimeListNotInCache)); + identifier, + DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()}); } else { emit variable->updated(); diff --git a/core/src/Variable/VariableModel.cpp b/core/src/Variable/VariableModel.cpp index af06461..637c4cb 100644 --- a/core/src/Variable/VariableModel.cpp +++ b/core/src/Variable/VariableModel.cpp @@ -60,14 +60,13 @@ VariableModel::VariableModel(QObject *parent) } std::shared_ptr VariableModel::createVariable(const QString &name, - const SqpDateTime &dateTime) noexcept + const SqpDateTime &dateTime, + const QVariantHash &metadata) noexcept { auto insertIndex = rowCount(); beginInsertRows({}, insertIndex, insertIndex); - /// @todo For the moment, the other data of the variable is initialized with default values - auto variable = std::make_shared(name, QStringLiteral("unit"), - QStringLiteral("mission"), dateTime); + auto variable = std::make_shared(name, dateTime, metadata); impl->m_Variables.push_back(variable); connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); diff --git a/core/tests/Variable/TestVariableCacheController.cpp b/core/tests/Variable/TestVariableCacheController.cpp index 6a6600d..92f886b 100644 --- a/core/tests/Variable/TestVariableCacheController.cpp +++ b/core/tests/Variable/TestVariableCacheController.cpp @@ -35,7 +35,7 @@ void TestVariableCacheController::testProvideNotInCacheDateTimeList() auto sqp2 = SqpDateTime{static_cast(ts2.toMSecsSinceEpoch()), static_cast(te2.toMSecsSinceEpoch())}; - auto var0 = std::make_shared("", "", "", sqp0); + auto var0 = std::make_shared("", sqp0); variableCacheController.addDateTime(var0, sqp0); variableCacheController.addDateTime(var0, sqp1); @@ -289,7 +289,7 @@ void TestVariableCacheController::testAddDateTime() static_cast(te03.toMSecsSinceEpoch())}; - auto var0 = std::make_shared("", "", "", sqp0); + auto var0 = std::make_shared("", sqp0); // First case: add the first interval to the variable :sqp0 diff --git a/gui/include/Visualization/operations/MenuBuilder.h b/gui/include/Visualization/operations/MenuBuilder.h index cb78fb7..c739162 100644 --- a/gui/include/Visualization/operations/MenuBuilder.h +++ b/gui/include/Visualization/operations/MenuBuilder.h @@ -25,11 +25,12 @@ public: void addAction(const QString &actionName, ActionFun actionFunction); /** - * Adds a new menu to the current menu + * Adds a new menu to the current menu and returns it * @param name the name of the menu * @param icon the icon of the menu (can be null) + * @returns the created menu, nullptr if it couldn't be created */ - void addMenu(const QString &name, const QIcon &icon = {}); + QMenu *addMenu(const QString &name, const QIcon &icon = {}); /// Adds a separator to the current menu. The separator is added only if the menu already /// contains entries diff --git a/gui/src/SqpApplication.cpp b/gui/src/SqpApplication.cpp index 0ec169f..04d8858 100644 --- a/gui/src/SqpApplication.cpp +++ b/gui/src/SqpApplication.cpp @@ -26,9 +26,11 @@ public: // VariableController <-> DataSourceController connect(m_DataSourceController.get(), - SIGNAL(variableCreationRequested(const QString &, std::shared_ptr)), + SIGNAL(variableCreationRequested(const QString &, const QVariantHash &, + std::shared_ptr)), m_VariableController.get(), - SLOT(createVariable(const QString &, std::shared_ptr))); + SLOT(createVariable(const QString &, const QVariantHash &, + std::shared_ptr))); // VariableController <-> VisualizationController connect(m_VariableController.get(), diff --git a/gui/src/Visualization/operations/GenerateVariableMenuOperation.cpp b/gui/src/Visualization/operations/GenerateVariableMenuOperation.cpp index e7190da..41592e1 100644 --- a/gui/src/Visualization/operations/GenerateVariableMenuOperation.cpp +++ b/gui/src/Visualization/operations/GenerateVariableMenuOperation.cpp @@ -21,7 +21,11 @@ struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate { void visitRootEnter() { // Creates the root menu - m_PlotMenuBuilder.addMenu(QObject::tr("Plot"), QIcon{":/icones/plot.png"}); + if (auto plotMenu + = m_PlotMenuBuilder.addMenu(QObject::tr("Plot"), QIcon{":/icones/plot.png"})) { + plotMenu->setEnabled(m_Variable && m_Variable->dataSeries() != nullptr); + } + m_UnplotMenuBuilder.addMenu(QObject::tr("Unplot"), QIcon{":/icones/unplot.png"}); } diff --git a/gui/src/Visualization/operations/MenuBuilder.cpp b/gui/src/Visualization/operations/MenuBuilder.cpp index 3d21803..cd45bde 100644 --- a/gui/src/Visualization/operations/MenuBuilder.cpp +++ b/gui/src/Visualization/operations/MenuBuilder.cpp @@ -12,13 +12,16 @@ MenuBuilder::MenuBuilder(QMenu *menu) } } -void MenuBuilder::addMenu(const QString &name, const QIcon &icon) +QMenu *MenuBuilder::addMenu(const QString &name, const QIcon &icon) { if (auto currMenu = currentMenu()) { - m_Menus.push(currMenu->addMenu(icon, name)); + auto menu = currMenu->addMenu(icon, name); + m_Menus.push(menu); + return menu; } else { qCCritical(LOG_MenuBuilder()) << QObject::tr("No current menu to attach the new menu"); + return nullptr; } } diff --git a/plugins/amda/include/AmdaDefs.h b/plugins/amda/include/AmdaDefs.h new file mode 100644 index 0000000..4189ea9 --- /dev/null +++ b/plugins/amda/include/AmdaDefs.h @@ -0,0 +1,16 @@ +#ifndef SCIQLOP_AMDADEFS_H +#define SCIQLOP_AMDADEFS_H + +#include + +// ////////////// // +// AMDA constants // +// ////////////// // + +// Relevant keys in JSON file +extern const QString AMDA_COMPONENT_KEY; +extern const QString AMDA_PRODUCT_KEY; +extern const QString AMDA_ROOT_KEY; +extern const QString AMDA_XML_ID_KEY; + +#endif // SCIQLOP_AMDADEFS_H diff --git a/plugins/amda/include/AmdaProvider.h b/plugins/amda/include/AmdaProvider.h index d6c0c59..d65573b 100644 --- a/plugins/amda/include/AmdaProvider.h +++ b/plugins/amda/include/AmdaProvider.h @@ -3,8 +3,6 @@ #include "AmdaGlobal.h" -#include - #include #include @@ -21,18 +19,10 @@ class SCIQLOP_AMDA_EXPORT AmdaProvider : public IDataProvider { public: explicit AmdaProvider(); - void requestDataLoading(QUuid token, const QVector &dateTimeList) override; + void requestDataLoading(QUuid token, const DataProviderParameters ¶meters) override; private: - void retrieveData(QUuid token, const DataProviderParameters ¶meters); - - class AmdaProviderPrivate; - spimpl::unique_impl_ptr impl; - - // private slots: - // void httpFinished(QNetworkReply *reply, QUuid dataId) noexcept; - // void httpDownloadFinished(QNetworkReply *reply, QUuid dataId) noexcept; - // void httpDownloadReadyRead(QNetworkReply *reply, QUuid dataId) noexcept; + void retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data); }; #endif // SCIQLOP_AMDAPROVIDER_H diff --git a/plugins/amda/src/AmdaDefs.cpp b/plugins/amda/src/AmdaDefs.cpp new file mode 100644 index 0000000..f90f076 --- /dev/null +++ b/plugins/amda/src/AmdaDefs.cpp @@ -0,0 +1,6 @@ +#include "AmdaDefs.h" + +const QString AMDA_COMPONENT_KEY = QStringLiteral("component"); +const QString AMDA_PRODUCT_KEY = QStringLiteral("parameter"); +const QString AMDA_ROOT_KEY = QStringLiteral("dataCenter"); +const QString AMDA_XML_ID_KEY = QStringLiteral("xml:id"); diff --git a/plugins/amda/src/AmdaParser.cpp b/plugins/amda/src/AmdaParser.cpp index f13f3ca..21a8cc4 100644 --- a/plugins/amda/src/AmdaParser.cpp +++ b/plugins/amda/src/AmdaParser.cpp @@ -1,4 +1,5 @@ #include "AmdaParser.h" +#include "AmdaDefs.h" #include @@ -11,18 +12,13 @@ Q_LOGGING_CATEGORY(LOG_AmdaParser, "AmdaParser") namespace { -// Significant keys of an AMDA's JSON file -const auto COMPONENT_KEY = QStringLiteral("component"); -const auto PRODUCT_KEY = QStringLiteral("parameter"); -const auto ROOT_KEY = QStringLiteral("dataCenter"); - /// Returns the correct item type according to the key passed in parameter DataSourceItemType itemType(const QString &key) noexcept { - if (key == PRODUCT_KEY) { + if (key == AMDA_PRODUCT_KEY) { return DataSourceItemType::PRODUCT; } - else if (key == COMPONENT_KEY) { + else if (key == AMDA_COMPONENT_KEY) { return DataSourceItemType::COMPONENT; } else { @@ -98,16 +94,16 @@ std::unique_ptr AmdaParser::readJson(const QString &filePath) no } auto jsonDocumentObject = jsonDocument.object(); - if (!jsonDocumentObject.contains(ROOT_KEY)) { + if (!jsonDocumentObject.contains(AMDA_ROOT_KEY)) { qCCritical(LOG_AmdaParser()) << QObject::tr( "Can't retrieve data source tree from file %1: the file is malformed (the key " "for the root element was not found (%2))") - .arg(filePath, ROOT_KEY); + .arg(filePath, AMDA_ROOT_KEY); return nullptr; } - auto rootValue = jsonDocumentObject.value(ROOT_KEY); + auto rootValue = jsonDocumentObject.value(AMDA_ROOT_KEY); if (!rootValue.isObject()) { qCCritical(LOG_AmdaParser()) << QObject::tr( diff --git a/plugins/amda/src/AmdaPlugin.cpp b/plugins/amda/src/AmdaPlugin.cpp index af18f76..f1f6436 100644 --- a/plugins/amda/src/AmdaPlugin.cpp +++ b/plugins/amda/src/AmdaPlugin.cpp @@ -1,4 +1,5 @@ #include "AmdaPlugin.h" +#include "AmdaDefs.h" #include "AmdaParser.h" #include "AmdaProvider.h" @@ -20,16 +21,30 @@ const auto JSON_FILE_PATH = QStringLiteral(":/samples/AmdaSample.json"); void associateActions(DataSourceItem &item, const QUuid &dataSourceUid) { - if (item.type() == DataSourceItemType::PRODUCT) { - auto itemName = item.name(); - - item.addAction(std::make_unique( - QObject::tr("Load %1 product").arg(itemName), - [itemName, dataSourceUid](DataSourceItem &item) { + auto addLoadAction = [&item, dataSourceUid](const QString &label) { + item.addAction( + std::make_unique(label, [dataSourceUid](DataSourceItem &item) { if (auto app = sqpApp) { app->dataSourceController().loadProductItem(dataSourceUid, item); } })); + }; + + const auto itemType = item.type(); + if (itemType == DataSourceItemType::PRODUCT) { + /// @todo : As for the moment we do not manage the loading of vectors, in the case of a + /// parameter, we update the identifier of download of the data: + /// - if the parameter has no component, the identifier remains the same + /// - if the parameter has at least one component, the identifier is that of the first + /// component (for example, "imf" becomes "imf (0)") + if (item.childCount() != 0) { + item.setData(AMDA_XML_ID_KEY, item.child(0)->data(AMDA_XML_ID_KEY)); + } + + addLoadAction(QObject::tr("Load %1 product").arg(item.name())); + } + else if (itemType == DataSourceItemType::COMPONENT) { + addLoadAction(QObject::tr("Load %1 component").arg(item.name())); } auto count = item.childCount(); diff --git a/plugins/amda/src/AmdaProvider.cpp b/plugins/amda/src/AmdaProvider.cpp index a7a6911..20be39b 100644 --- a/plugins/amda/src/AmdaProvider.cpp +++ b/plugins/amda/src/AmdaProvider.cpp @@ -1,4 +1,5 @@ #include "AmdaProvider.h" +#include "AmdaDefs.h" #include "AmdaResultParser.h" #include @@ -34,74 +35,77 @@ QString dateFormat(double sqpDateTime) noexcept return dateTime.toString(AMDA_TIME_FORMAT); } - } // namespace -struct AmdaProvider::AmdaProviderPrivate { - DataProviderParameters m_Params{}; - std::unique_ptr m_AccessManager{nullptr}; - QNetworkReply *m_Reply{nullptr}; - // std::unique_ptr m_File{nullptr}; - QUuid m_Token; -}; - -AmdaProvider::AmdaProvider() : impl{spimpl::make_unique_impl()} +AmdaProvider::AmdaProvider() { qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider") << QThread::currentThread(); if (auto app = sqpApp) { auto &networkController = app->networkController(); - connect(this, &AmdaProvider::requestConstructed, &networkController, - &NetworkController::onProcessRequested); + connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid, + std::function)), + &networkController, + SLOT(onProcessRequested(QNetworkRequest, QUuid, + std::function))); } } -void AmdaProvider::requestDataLoading(QUuid token, const QVector &dateTimeList) +void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) { // NOTE: Try to use multithread if possible - for (const auto &dateTime : dateTimeList) { - retrieveData(token, DataProviderParameters{dateTime}); + const auto times = parameters.m_Times; + const auto data = parameters.m_Data; + for (const auto &dateTime : qAsConst(times)) { + retrieveData(token, dateTime, data); } } -void AmdaProvider::retrieveData(QUuid token, const DataProviderParameters ¶meters) +void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data) { + // 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; + } + // /////////// // // Creates URL // // /////////// // - auto startDate = dateFormat(parameters.m_Time.m_TStart); - auto endDate = dateFormat(parameters.m_Time.m_TEnd); - auto productId = QStringLiteral("imf(0)"); + auto startDate = dateFormat(dateTime.m_TStart); + auto endDate = dateFormat(dateTime.m_TEnd); auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; auto tempFile = std::make_shared(); - // LAMBDA - auto httpDownloadFinished = [this, tempFile](QNetworkReply *reply, QUuid dataId) noexcept { - - if (tempFile) { - auto replyReadAll = reply->readAll(); - if (!replyReadAll.isEmpty()) { - tempFile->write(replyReadAll); - } - tempFile->close(); - - // Parse results file - if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { - emit dataProvided(impl->m_Token, dataSeries, impl->m_Params.m_Time); - } - else { - /// @todo ALX : debug - } - } - - // Deletes reply - reply->deleteLater(); - reply = nullptr; - }; + auto httpDownloadFinished + = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept { + Q_UNUSED(dataId); + + if (tempFile) { + auto replyReadAll = reply->readAll(); + if (!replyReadAll.isEmpty()) { + tempFile->write(replyReadAll); + } + tempFile->close(); + + // Parse results file + if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { + emit dataProvided(token, dataSeries, dateTime); + } + else { + /// @todo ALX : debug + } + } + + // Deletes reply + reply->deleteLater(); + reply = nullptr; + }; auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, QUuid dataId) noexcept { @@ -121,8 +125,5 @@ void AmdaProvider::retrieveData(QUuid token, const DataProviderParameters ¶m // //////////////// // // Executes request // // //////////////// // - - impl->m_Token = token; - impl->m_Params = parameters; emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); } diff --git a/plugins/mockplugin/include/CosinusProvider.h b/plugins/mockplugin/include/CosinusProvider.h index 41e7db4..1f04e38 100644 --- a/plugins/mockplugin/include/CosinusProvider.h +++ b/plugins/mockplugin/include/CosinusProvider.h @@ -14,13 +14,12 @@ Q_DECLARE_LOGGING_CATEGORY(LOG_CosinusProvider) */ class SCIQLOP_MOCKPLUGIN_EXPORT CosinusProvider : public IDataProvider { public: - void requestDataLoading(QUuid token, const QVector &dateTimeList) override; + void requestDataLoading(QUuid token, const DataProviderParameters ¶meters) override; private: /// @sa IDataProvider::retrieveData() - std::shared_ptr retrieveData(const DataProviderParameters ¶meters) const; - std::shared_ptr retrieveDataSeries(const SqpDateTime &dateTime); + std::shared_ptr retrieveData(const SqpDateTime &dateTime) const; }; #endif // SCIQLOP_COSINUSPROVIDER_H diff --git a/plugins/mockplugin/src/CosinusProvider.cpp b/plugins/mockplugin/src/CosinusProvider.cpp index f3fab7d..0fd5537 100644 --- a/plugins/mockplugin/src/CosinusProvider.cpp +++ b/plugins/mockplugin/src/CosinusProvider.cpp @@ -10,11 +10,8 @@ Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider") -std::shared_ptr -CosinusProvider::retrieveData(const DataProviderParameters ¶meters) const +std::shared_ptr CosinusProvider::retrieveData(const SqpDateTime &dateTime) const { - auto dateTime = parameters.m_Time; - auto dataIndex = 0; // Gets the timerange from the parameters @@ -38,13 +35,14 @@ CosinusProvider::retrieveData(const DataProviderParameters ¶meters) const return scalarSeries; } -void CosinusProvider::requestDataLoading(QUuid token, const QVector &dateTimeList) +void CosinusProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) { qCDebug(LOG_CosinusProvider()) << "CosinusProvider::requestDataLoading" << QThread::currentThread()->objectName(); // NOTE: Try to use multithread if possible - for (const auto &dateTime : dateTimeList) { - auto scalarSeries = this->retrieveData(DataProviderParameters{dateTime}); + const auto times = parameters.m_Times; + for (const auto &dateTime : qAsConst(times)) { + auto scalarSeries = this->retrieveData(dateTime); emit dataProvided(token, scalarSeries, dateTime); } }