##// END OF EJS Templates
Merge branch 'feature/DownloadProgressRefactoring' into develop
perrinel -
r427:7dfa9848e171 merge
parent child
Show More
@@ -1,64 +1,70
1 #ifndef SCIQLOP_IDATAPROVIDER_H
1 #ifndef SCIQLOP_IDATAPROVIDER_H
2 #define SCIQLOP_IDATAPROVIDER_H
2 #define SCIQLOP_IDATAPROVIDER_H
3
3
4 #include <memory>
4 #include <memory>
5
5
6 #include <QObject>
6 #include <QObject>
7 #include <QUuid>
7 #include <QUuid>
8
8
9 #include <Common/MetaTypes.h>
9 #include <Common/MetaTypes.h>
10
10
11 #include <Data/SqpDateTime.h>
11 #include <Data/SqpDateTime.h>
12
12
13 class DataProviderParameters;
13 class DataProviderParameters;
14 class IDataSeries;
14 class IDataSeries;
15 class QNetworkReply;
15 class QNetworkReply;
16 class QNetworkRequest;
16 class QNetworkRequest;
17
17
18 /**
18 /**
19 * @brief The IDataProvider interface aims to declare a data provider.
19 * @brief The IDataProvider interface aims to declare a data provider.
20 *
20 *
21 * A data provider is an entity that generates data and returns it according to various parameters
21 * A data provider is an entity that generates data and returns it according to various parameters
22 * (time interval, product to retrieve the data, etc.)
22 * (time interval, product to retrieve the data, etc.)
23 *
23 *
24 * @sa IDataSeries
24 * @sa IDataSeries
25 */
25 */
26 class IDataProvider : public QObject {
26 class IDataProvider : public QObject {
27
27
28 Q_OBJECT
28 Q_OBJECT
29 public:
29 public:
30 virtual ~IDataProvider() noexcept = default;
30 virtual ~IDataProvider() noexcept = default;
31
31
32 /**
32 /**
33 * @brief requestDataLoading provide datas for the data identified by identifier and parameters
33 * @brief requestDataLoading provide datas for the data identified by identifier and parameters
34 */
34 */
35 virtual void requestDataLoading(QUuid identifier, const DataProviderParameters &parameters) = 0;
35 virtual void requestDataLoading(QUuid identifier, const DataProviderParameters &parameters) = 0;
36
36
37 /**
37 /**
38 * @brief requestDataAborting stop data loading of the data identified by identifier
38 * @brief requestDataAborting stop data loading of the data identified by identifier
39 */
39 */
40 virtual void requestDataAborting(QUuid identifier) = 0;
40 virtual void requestDataAborting(QUuid identifier) = 0;
41
41
42 signals:
42 signals:
43 /**
43 /**
44 * @brief dataProvided send dataSeries under dateTime and that corresponds of the data
44 * @brief dataProvided send dataSeries under dateTime and that corresponds of the data
45 * identified by identifier
45 * identified by identifier
46 */
46 */
47 void dataProvided(QUuid identifier, std::shared_ptr<IDataSeries> dateSerie,
47 void dataProvided(QUuid identifier, std::shared_ptr<IDataSeries> dateSerie,
48 const SqpDateTime &dateTime);
48 const SqpDateTime &dateTime);
49
49
50 /**
51 * @brief dataProvided send dataSeries under dateTime and that corresponds of the data
52 * identified by identifier
53 */
54 void dataProvidedProgress(QUuid identifier, double progress);
55
50
56
51 /**
57 /**
52 * @brief requestConstructed send a request for the data identified by identifier
58 * @brief requestConstructed send a request for the data identified by identifier
53 * @callback is the methode call by the reply of the request when it is finished.
59 * @callback is the methode call by the reply of the request when it is finished.
54 */
60 */
55 void requestConstructed(const QNetworkRequest &request, QUuid identifier,
61 void requestConstructed(const QNetworkRequest &request, QUuid identifier,
56 std::function<void(QNetworkReply *, QUuid)> callback);
62 std::function<void(QNetworkReply *, QUuid)> callback);
57 };
63 };
58
64
59 // Required for using shared_ptr in signals/slots
65 // Required for using shared_ptr in signals/slots
60 SCIQLOP_REGISTER_META_TYPE(IDATAPROVIDER_PTR_REGISTRY, std::shared_ptr<IDataProvider>)
66 SCIQLOP_REGISTER_META_TYPE(IDATAPROVIDER_PTR_REGISTRY, std::shared_ptr<IDataProvider>)
61 SCIQLOP_REGISTER_META_TYPE(IDATAPROVIDER_FUNCTION_REGISTRY,
67 SCIQLOP_REGISTER_META_TYPE(IDATAPROVIDER_FUNCTION_REGISTRY,
62 std::function<void(QNetworkReply *, QUuid)>)
68 std::function<void(QNetworkReply *, QUuid)>)
63
69
64 #endif // SCIQLOP_IDATAPROVIDER_H
70 #endif // SCIQLOP_IDATAPROVIDER_H
@@ -1,120 +1,133
1 #include "Network/NetworkController.h"
1 #include "Network/NetworkController.h"
2
2
3 #include <QMutex>
3 #include <QMutex>
4 #include <QNetworkAccessManager>
4 #include <QNetworkAccessManager>
5 #include <QNetworkReply>
5 #include <QNetworkReply>
6 #include <QNetworkRequest>
6 #include <QNetworkRequest>
7 #include <QReadWriteLock>
7 #include <QReadWriteLock>
8 #include <QThread>
8 #include <QThread>
9
9
10 #include <unordered_map>
10 #include <unordered_map>
11
11
12 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
12 Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController")
13
13
14 struct NetworkController::NetworkControllerPrivate {
14 struct NetworkController::NetworkControllerPrivate {
15 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
15 explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {}
16 QMutex m_WorkingMutex;
16 QMutex m_WorkingMutex;
17
17
18 QReadWriteLock m_Lock;
18 QReadWriteLock m_Lock;
19 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
19 std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId;
20 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
20 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
21
21
22 void lockRead() { m_Lock.lockForRead(); }
22 void lockRead() { m_Lock.lockForRead(); }
23 void lockWrite() { m_Lock.lockForWrite(); }
23 void lockWrite() { m_Lock.lockForWrite(); }
24 void unlock() { m_Lock.unlock(); }
24 void unlock() { m_Lock.unlock(); }
25 };
25 };
26
26
27 NetworkController::NetworkController(QObject *parent)
27 NetworkController::NetworkController(QObject *parent)
28 : QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
28 : QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)}
29 {
29 {
30 }
30 }
31
31
32 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
32 void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier,
33 std::function<void(QNetworkReply *, QUuid)> callback)
33 std::function<void(QNetworkReply *, QUuid)> callback)
34 {
34 {
35 auto reply = impl->m_AccessManager->get(request);
36 qCDebug(LOG_NetworkController()) << tr("NetworkController registered")
35 qCDebug(LOG_NetworkController()) << tr("NetworkController registered")
37 << QThread::currentThread() << reply;
36 << QThread::currentThread()->objectName();
37 auto reply = impl->m_AccessManager->get(request);
38
38
39 // Store the couple reply id
39 // Store the couple reply id
40 impl->lockWrite();
40 impl->lockWrite();
41 impl->m_NetworkReplyToVariableId[reply] = identifier;
41 impl->m_NetworkReplyToVariableId[reply] = identifier;
42 impl->unlock();
42 impl->unlock();
43
43
44 auto onReplyFinished = [reply, this, identifier, callback]() {
44 auto onReplyFinished = [reply, this, identifier, callback]() {
45
45
46 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
46 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished")
47 << QThread::currentThread() << reply;
47 << QThread::currentThread() << reply;
48 impl->lockRead();
48 impl->lockRead();
49 auto it = impl->m_NetworkReplyToVariableId.find(reply);
49 auto it = impl->m_NetworkReplyToVariableId.find(reply);
50 impl->unlock();
50 impl->unlock();
51 if (it != impl->m_NetworkReplyToVariableId.cend()) {
51 if (it != impl->m_NetworkReplyToVariableId.cend()) {
52 impl->lockWrite();
52 impl->lockWrite();
53 impl->m_NetworkReplyToVariableId.erase(reply);
53 impl->m_NetworkReplyToVariableId.erase(reply);
54 impl->unlock();
54 impl->unlock();
55 // Deletes reply
55 // Deletes reply
56 callback(reply, identifier);
56 callback(reply, identifier);
57 reply->deleteLater();
57 reply->deleteLater();
58
58
59 emit this->replyDownloadProgress(identifier, 0);
59 emit this->replyDownloadProgress(identifier, 0);
60 }
60 }
61
61
62 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished END")
62 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished END")
63 << QThread::currentThread() << reply;
63 << QThread::currentThread() << reply;
64 };
64 };
65
65
66 auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) {
66 auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) {
67
67
68 double progress = (bytesRead * 100.0) / totalBytes;
68 double progress = (bytesRead * 100.0) / totalBytes;
69 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress
69 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress
70 << QThread::currentThread() << reply;
70 << QThread::currentThread() << reply;
71 impl->lockRead();
71 impl->lockRead();
72 auto it = impl->m_NetworkReplyToVariableId.find(reply);
72 auto it = impl->m_NetworkReplyToVariableId.find(reply);
73 impl->unlock();
73 impl->unlock();
74 if (it != impl->m_NetworkReplyToVariableId.cend()) {
74 if (it != impl->m_NetworkReplyToVariableId.cend()) {
75 emit this->replyDownloadProgress(it->second, progress);
75 emit this->replyDownloadProgress(it->second, progress);
76 }
76 }
77 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END")
77 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END")
78 << QThread::currentThread() << reply;
78 << QThread::currentThread() << reply;
79 };
79 };
80
80
81
81
82 connect(reply, &QNetworkReply::finished, this, onReplyFinished);
82 connect(reply, &QNetworkReply::finished, this, onReplyFinished);
83 connect(reply, &QNetworkReply::downloadProgress, this, onReplyProgress);
83 connect(reply, &QNetworkReply::downloadProgress, this, onReplyProgress);
84 qCDebug(LOG_NetworkController()) << tr("NetworkController registered END")
85 << QThread::currentThread()->objectName() << reply;
84 }
86 }
85
87
86 void NetworkController::initialize()
88 void NetworkController::initialize()
87 {
89 {
88 qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread();
90 qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread();
89 impl->m_WorkingMutex.lock();
91 impl->m_WorkingMutex.lock();
90 impl->m_AccessManager = std::make_unique<QNetworkAccessManager>();
92 impl->m_AccessManager = std::make_unique<QNetworkAccessManager>();
93
94
95 auto onReplyErrors = [this](QNetworkReply *reply, const QList<QSslError> &errors) {
96
97 qCCritical(LOG_NetworkController()) << tr("NetworkAcessManager errors: ") << errors;
98
99 };
100
101
102 connect(impl->m_AccessManager.get(), &QNetworkAccessManager::sslErrors, this, onReplyErrors);
103
91 qCDebug(LOG_NetworkController()) << tr("NetworkController init END");
104 qCDebug(LOG_NetworkController()) << tr("NetworkController init END");
92 }
105 }
93
106
94 void NetworkController::finalize()
107 void NetworkController::finalize()
95 {
108 {
96 impl->m_WorkingMutex.unlock();
109 impl->m_WorkingMutex.unlock();
97 }
110 }
98
111
99 void NetworkController::onReplyCanceled(QUuid identifier)
112 void NetworkController::onReplyCanceled(QUuid identifier)
100 {
113 {
101 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
114 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
102 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled")
115 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled")
103 << QThread::currentThread();
116 << QThread::currentThread();
104
117
105
118
106 impl->lockRead();
119 impl->lockRead();
107 auto end = impl->m_NetworkReplyToVariableId.cend();
120 auto end = impl->m_NetworkReplyToVariableId.cend();
108 auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
121 auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply);
109 impl->unlock();
122 impl->unlock();
110 if (it != end) {
123 if (it != end) {
111 it->first->abort();
124 it->first->abort();
112 }
125 }
113 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END")
126 qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END")
114 << QThread::currentThread();
127 << QThread::currentThread();
115 }
128 }
116
129
117 void NetworkController::waitForFinish()
130 void NetworkController::waitForFinish()
118 {
131 {
119 QMutexLocker locker{&impl->m_WorkingMutex};
132 QMutexLocker locker{&impl->m_WorkingMutex};
120 }
133 }
@@ -1,238 +1,240
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableCacheController.h>
2 #include <Variable/VariableCacheController.h>
3 #include <Variable/VariableController.h>
3 #include <Variable/VariableController.h>
4 #include <Variable/VariableModel.h>
4 #include <Variable/VariableModel.h>
5
5
6 #include <Data/DataProviderParameters.h>
6 #include <Data/DataProviderParameters.h>
7 #include <Data/IDataProvider.h>
7 #include <Data/IDataProvider.h>
8 #include <Data/IDataSeries.h>
8 #include <Data/IDataSeries.h>
9 #include <Time/TimeController.h>
9 #include <Time/TimeController.h>
10
10
11 #include <QDateTime>
11 #include <QDateTime>
12 #include <QMutex>
12 #include <QMutex>
13 #include <QThread>
13 #include <QThread>
14 #include <QUuid>
14 #include <QUuid>
15 #include <QtCore/QItemSelectionModel>
15 #include <QtCore/QItemSelectionModel>
16
16
17 #include <unordered_map>
17 #include <unordered_map>
18
18
19 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
20
20
21 struct VariableController::VariableControllerPrivate {
21 struct VariableController::VariableControllerPrivate {
22 explicit VariableControllerPrivate(VariableController *parent)
22 explicit VariableControllerPrivate(VariableController *parent)
23 : m_WorkingMutex{},
23 : m_WorkingMutex{},
24 m_VariableModel{new VariableModel{parent}},
24 m_VariableModel{new VariableModel{parent}},
25 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
25 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
26 m_VariableCacheController{std::make_unique<VariableCacheController>()}
26 m_VariableCacheController{std::make_unique<VariableCacheController>()}
27 {
27 {
28 }
28 }
29
29
30 QMutex m_WorkingMutex;
30 QMutex m_WorkingMutex;
31 /// Variable model. The VariableController has the ownership
31 /// Variable model. The VariableController has the ownership
32 VariableModel *m_VariableModel;
32 VariableModel *m_VariableModel;
33 QItemSelectionModel *m_VariableSelectionModel;
33 QItemSelectionModel *m_VariableSelectionModel;
34
34
35
35
36 TimeController *m_TimeController{nullptr};
36 TimeController *m_TimeController{nullptr};
37 std::unique_ptr<VariableCacheController> m_VariableCacheController;
37 std::unique_ptr<VariableCacheController> m_VariableCacheController;
38
38
39 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
39 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
40 m_VariableToProviderMap;
40 m_VariableToProviderMap;
41 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
41 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
42 };
42 };
43
43
44 VariableController::VariableController(QObject *parent)
44 VariableController::VariableController(QObject *parent)
45 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
45 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
46 {
46 {
47 qCDebug(LOG_VariableController()) << tr("VariableController construction")
47 qCDebug(LOG_VariableController()) << tr("VariableController construction")
48 << QThread::currentThread();
48 << QThread::currentThread();
49
49
50 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
50 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
51 &VariableController::onAbortProgressRequested);
51 &VariableController::onAbortProgressRequested);
52 }
52 }
53
53
54 VariableController::~VariableController()
54 VariableController::~VariableController()
55 {
55 {
56 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
56 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
57 << QThread::currentThread();
57 << QThread::currentThread();
58 this->waitForFinish();
58 this->waitForFinish();
59 }
59 }
60
60
61 VariableModel *VariableController::variableModel() noexcept
61 VariableModel *VariableController::variableModel() noexcept
62 {
62 {
63 return impl->m_VariableModel;
63 return impl->m_VariableModel;
64 }
64 }
65
65
66 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
66 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
67 {
67 {
68 return impl->m_VariableSelectionModel;
68 return impl->m_VariableSelectionModel;
69 }
69 }
70
70
71 void VariableController::setTimeController(TimeController *timeController) noexcept
71 void VariableController::setTimeController(TimeController *timeController) noexcept
72 {
72 {
73 impl->m_TimeController = timeController;
73 impl->m_TimeController = timeController;
74 }
74 }
75
75
76 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
76 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
77 {
77 {
78 if (!variable) {
78 if (!variable) {
79 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
79 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
80 return;
80 return;
81 }
81 }
82
82
83 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
83 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
84 // make some treatments before the deletion
84 // make some treatments before the deletion
85 emit variableAboutToBeDeleted(variable);
85 emit variableAboutToBeDeleted(variable);
86
86
87 // Deletes identifier
87 // Deletes identifier
88 impl->m_VariableToIdentifierMap.erase(variable);
88 impl->m_VariableToIdentifierMap.erase(variable);
89
89
90 // Deletes provider
90 // Deletes provider
91 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
91 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
92 qCDebug(LOG_VariableController())
92 qCDebug(LOG_VariableController())
93 << tr("Number of providers deleted for variable %1: %2")
93 << tr("Number of providers deleted for variable %1: %2")
94 .arg(variable->name(), QString::number(nbProvidersDeleted));
94 .arg(variable->name(), QString::number(nbProvidersDeleted));
95
95
96 // Clears cache
96 // Clears cache
97 impl->m_VariableCacheController->clear(variable);
97 impl->m_VariableCacheController->clear(variable);
98
98
99 // Deletes from model
99 // Deletes from model
100 impl->m_VariableModel->deleteVariable(variable);
100 impl->m_VariableModel->deleteVariable(variable);
101 }
101 }
102
102
103 void VariableController::deleteVariables(
103 void VariableController::deleteVariables(
104 const QVector<std::shared_ptr<Variable> > &variables) noexcept
104 const QVector<std::shared_ptr<Variable> > &variables) noexcept
105 {
105 {
106 for (auto variable : qAsConst(variables)) {
106 for (auto variable : qAsConst(variables)) {
107 deleteVariable(variable);
107 deleteVariable(variable);
108 }
108 }
109 }
109 }
110
110
111 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
111 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
112 {
112 {
113 }
113 }
114
114
115 void VariableController::createVariable(const QString &name, const QVariantHash &metadata,
115 void VariableController::createVariable(const QString &name, const QVariantHash &metadata,
116 std::shared_ptr<IDataProvider> provider) noexcept
116 std::shared_ptr<IDataProvider> provider) noexcept
117 {
117 {
118
118
119 if (!impl->m_TimeController) {
119 if (!impl->m_TimeController) {
120 qCCritical(LOG_VariableController())
120 qCCritical(LOG_VariableController())
121 << tr("Impossible to create variable: The time controller is null");
121 << tr("Impossible to create variable: The time controller is null");
122 return;
122 return;
123 }
123 }
124
124
125 auto dateTime = impl->m_TimeController->dateTime();
125 auto dateTime = impl->m_TimeController->dateTime();
126
126
127 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) {
127 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) {
128 auto identifier = QUuid::createUuid();
128 auto identifier = QUuid::createUuid();
129
129
130 // store the provider
130 // store the provider
131 impl->m_VariableToProviderMap[newVariable] = provider;
131 impl->m_VariableToProviderMap[newVariable] = provider;
132 impl->m_VariableToIdentifierMap[newVariable] = identifier;
132 impl->m_VariableToIdentifierMap[newVariable] = identifier;
133
133
134 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
134 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
135 QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache)
135 QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache)
136 {
136 {
137 if (auto variable = varW.lock()) {
137 if (auto variable = varW.lock()) {
138 auto varIdentifier = impl->m_VariableToIdentifierMap.at(variable);
138 auto varIdentifier = impl->m_VariableToIdentifierMap.at(variable);
139 if (varIdentifier == identifier) {
139 if (varIdentifier == identifier) {
140 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
140 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
141 variable->setDataSeries(dataSeriesAcquired);
141 variable->setDataSeries(dataSeriesAcquired);
142 }
142 }
143 }
143 }
144 };
144 };
145
145
146 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
146 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
147 connect(provider.get(), &IDataProvider::dataProvidedProgress, this,
148 &VariableController::onVariableRetrieveDataInProgress);
147 this->onRequestDataLoading(newVariable, dateTime);
149 this->onRequestDataLoading(newVariable, dateTime);
148 }
150 }
149 }
151 }
150
152
151 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
153 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
152 {
154 {
153 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
155 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
154 << QThread::currentThread()->objectName();
156 << QThread::currentThread()->objectName();
155 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
157 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
156
158
157 for (const auto &selectedRow : qAsConst(selectedRows)) {
159 for (const auto &selectedRow : qAsConst(selectedRows)) {
158 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
160 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
159 selectedVariable->setDateTime(dateTime);
161 selectedVariable->setDateTime(dateTime);
160 this->onRequestDataLoading(selectedVariable, dateTime);
162 this->onRequestDataLoading(selectedVariable, dateTime);
161 }
163 }
162 }
164 }
163 }
165 }
164
166
165 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
167 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
166 {
168 {
167 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
169 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
168
170
169 auto end = impl->m_VariableToIdentifierMap.cend();
171 auto end = impl->m_VariableToIdentifierMap.cend();
170 auto it = std::find_if(impl->m_VariableToIdentifierMap.cbegin(), end, findReply);
172 auto it = std::find_if(impl->m_VariableToIdentifierMap.cbegin(), end, findReply);
171 if (it != end) {
173 if (it != end) {
172 impl->m_VariableModel->setDataProgress(it->first, progress);
174 impl->m_VariableModel->setDataProgress(it->first, progress);
173 }
175 }
174 }
176 }
175
177
176 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
178 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
177 {
179 {
178 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
180 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
179 << QThread::currentThread()->objectName();
181 << QThread::currentThread()->objectName();
180
182
181 auto it = impl->m_VariableToIdentifierMap.find(variable);
183 auto it = impl->m_VariableToIdentifierMap.find(variable);
182 if (it != impl->m_VariableToIdentifierMap.cend()) {
184 if (it != impl->m_VariableToIdentifierMap.cend()) {
183 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
185 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
184 }
186 }
185 else {
187 else {
186 qCWarning(LOG_VariableController())
188 qCWarning(LOG_VariableController())
187 << tr("Aborting progression of inexistant variable detected !!!")
189 << tr("Aborting progression of inexistant variable detected !!!")
188 << QThread::currentThread()->objectName();
190 << QThread::currentThread()->objectName();
189 }
191 }
190 }
192 }
191
193
192
194
193 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
195 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
194 const SqpDateTime &dateTime)
196 const SqpDateTime &dateTime)
195 {
197 {
196 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
198 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
197 << QThread::currentThread()->objectName();
199 << QThread::currentThread()->objectName();
198 // we want to load data of the variable for the dateTime.
200 // we want to load data of the variable for the dateTime.
199 // First we check if the cache contains some of them.
201 // First we check if the cache contains some of them.
200 // For the other, we ask the provider to give them.
202 // For the other, we ask the provider to give them.
201 if (variable) {
203 if (variable) {
202
204
203 auto dateTimeListNotInCache
205 auto dateTimeListNotInCache
204 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
206 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
205
207
206 if (!dateTimeListNotInCache.empty()) {
208 if (!dateTimeListNotInCache.empty()) {
207 // Ask the provider for each data on the dateTimeListNotInCache
209 // Ask the provider for each data on the dateTimeListNotInCache
208 auto identifier = impl->m_VariableToIdentifierMap.at(variable);
210 auto identifier = impl->m_VariableToIdentifierMap.at(variable);
209 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
211 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
210 identifier,
212 identifier,
211 DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()});
213 DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()});
212 }
214 }
213 else {
215 else {
214 emit variable->updated();
216 emit variable->updated();
215 }
217 }
216 }
218 }
217 else {
219 else {
218 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
220 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
219 }
221 }
220 }
222 }
221
223
222
224
223 void VariableController::initialize()
225 void VariableController::initialize()
224 {
226 {
225 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
227 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
226 impl->m_WorkingMutex.lock();
228 impl->m_WorkingMutex.lock();
227 qCDebug(LOG_VariableController()) << tr("VariableController init END");
229 qCDebug(LOG_VariableController()) << tr("VariableController init END");
228 }
230 }
229
231
230 void VariableController::finalize()
232 void VariableController::finalize()
231 {
233 {
232 impl->m_WorkingMutex.unlock();
234 impl->m_WorkingMutex.unlock();
233 }
235 }
234
236
235 void VariableController::waitForFinish()
237 void VariableController::waitForFinish()
236 {
238 {
237 QMutexLocker locker{&impl->m_WorkingMutex};
239 QMutexLocker locker{&impl->m_WorkingMutex};
238 }
240 }
@@ -1,147 +1,142
1 #include "SqpApplication.h"
1 #include "SqpApplication.h"
2
2
3 #include <Data/IDataProvider.h>
3 #include <Data/IDataProvider.h>
4 #include <DataSource/DataSourceController.h>
4 #include <DataSource/DataSourceController.h>
5 #include <Network/NetworkController.h>
5 #include <Network/NetworkController.h>
6 #include <QThread>
6 #include <QThread>
7 #include <Time/TimeController.h>
7 #include <Time/TimeController.h>
8 #include <Variable/Variable.h>
8 #include <Variable/Variable.h>
9 #include <Variable/VariableController.h>
9 #include <Variable/VariableController.h>
10 #include <Visualization/VisualizationController.h>
10 #include <Visualization/VisualizationController.h>
11
11
12 Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication")
12 Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication")
13
13
14 class SqpApplication::SqpApplicationPrivate {
14 class SqpApplication::SqpApplicationPrivate {
15 public:
15 public:
16 SqpApplicationPrivate()
16 SqpApplicationPrivate()
17 : m_DataSourceController{std::make_unique<DataSourceController>()},
17 : m_DataSourceController{std::make_unique<DataSourceController>()},
18 m_NetworkController{std::make_unique<NetworkController>()},
18 m_NetworkController{std::make_unique<NetworkController>()},
19 m_TimeController{std::make_unique<TimeController>()},
19 m_TimeController{std::make_unique<TimeController>()},
20 m_VariableController{std::make_unique<VariableController>()},
20 m_VariableController{std::make_unique<VariableController>()},
21 m_VisualizationController{std::make_unique<VisualizationController>()}
21 m_VisualizationController{std::make_unique<VisualizationController>()}
22 {
22 {
23 // /////////////////////////////// //
23 // /////////////////////////////// //
24 // Connections between controllers //
24 // Connections between controllers //
25 // /////////////////////////////// //
25 // /////////////////////////////// //
26
26
27 // VariableController <-> DataSourceController
27 // VariableController <-> DataSourceController
28 connect(m_DataSourceController.get(),
28 connect(m_DataSourceController.get(),
29 SIGNAL(variableCreationRequested(const QString &, const QVariantHash &,
29 SIGNAL(variableCreationRequested(const QString &, const QVariantHash &,
30 std::shared_ptr<IDataProvider>)),
30 std::shared_ptr<IDataProvider>)),
31 m_VariableController.get(),
31 m_VariableController.get(),
32 SLOT(createVariable(const QString &, const QVariantHash &,
32 SLOT(createVariable(const QString &, const QVariantHash &,
33 std::shared_ptr<IDataProvider>)));
33 std::shared_ptr<IDataProvider>)));
34
34
35 // VariableController <-> VisualizationController
35 // VariableController <-> VisualizationController
36 connect(m_VariableController.get(),
36 connect(m_VariableController.get(),
37 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)),
37 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)),
38 m_VisualizationController.get(),
38 m_VisualizationController.get(),
39 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)), Qt::DirectConnection);
39 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)), Qt::DirectConnection);
40
40
41
41
42 m_DataSourceController->moveToThread(&m_DataSourceControllerThread);
42 m_DataSourceController->moveToThread(&m_DataSourceControllerThread);
43 m_NetworkController->moveToThread(&m_NetworkControllerThread);
43 m_NetworkController->moveToThread(&m_NetworkControllerThread);
44 m_VariableController->moveToThread(&m_VariableControllerThread);
44 m_VariableController->moveToThread(&m_VariableControllerThread);
45 m_VisualizationController->moveToThread(&m_VisualizationControllerThread);
45 m_VisualizationController->moveToThread(&m_VisualizationControllerThread);
46
46
47
47
48 // Additionnal init
48 // Additionnal init
49 m_VariableController->setTimeController(m_TimeController.get());
49 m_VariableController->setTimeController(m_TimeController.get());
50 }
50 }
51
51
52 virtual ~SqpApplicationPrivate()
52 virtual ~SqpApplicationPrivate()
53 {
53 {
54 qCInfo(LOG_SqpApplication()) << tr("SqpApplicationPrivate destruction");
54 qCInfo(LOG_SqpApplication()) << tr("SqpApplicationPrivate destruction");
55 m_DataSourceControllerThread.quit();
55 m_DataSourceControllerThread.quit();
56 m_DataSourceControllerThread.wait();
56 m_DataSourceControllerThread.wait();
57
57
58 m_NetworkControllerThread.quit();
58 m_NetworkControllerThread.quit();
59 m_NetworkControllerThread.wait();
59 m_NetworkControllerThread.wait();
60
60
61 m_VariableControllerThread.quit();
61 m_VariableControllerThread.quit();
62 m_VariableControllerThread.wait();
62 m_VariableControllerThread.wait();
63
63
64 m_VisualizationControllerThread.quit();
64 m_VisualizationControllerThread.quit();
65 m_VisualizationControllerThread.wait();
65 m_VisualizationControllerThread.wait();
66 }
66 }
67
67
68 std::unique_ptr<DataSourceController> m_DataSourceController;
68 std::unique_ptr<DataSourceController> m_DataSourceController;
69 std::unique_ptr<VariableController> m_VariableController;
69 std::unique_ptr<VariableController> m_VariableController;
70 std::unique_ptr<TimeController> m_TimeController;
70 std::unique_ptr<TimeController> m_TimeController;
71 std::unique_ptr<NetworkController> m_NetworkController;
71 std::unique_ptr<NetworkController> m_NetworkController;
72 std::unique_ptr<VisualizationController> m_VisualizationController;
72 std::unique_ptr<VisualizationController> m_VisualizationController;
73 QThread m_DataSourceControllerThread;
73 QThread m_DataSourceControllerThread;
74 QThread m_NetworkControllerThread;
74 QThread m_NetworkControllerThread;
75 QThread m_VariableControllerThread;
75 QThread m_VariableControllerThread;
76 QThread m_VisualizationControllerThread;
76 QThread m_VisualizationControllerThread;
77 };
77 };
78
78
79
79
80 SqpApplication::SqpApplication(int &argc, char **argv)
80 SqpApplication::SqpApplication(int &argc, char **argv)
81 : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()}
81 : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()}
82 {
82 {
83 qCDebug(LOG_SqpApplication()) << tr("SqpApplication construction") << QThread::currentThread();
83 qCDebug(LOG_SqpApplication()) << tr("SqpApplication construction") << QThread::currentThread();
84
84
85 connect(&impl->m_DataSourceControllerThread, &QThread::started,
85 connect(&impl->m_DataSourceControllerThread, &QThread::started,
86 impl->m_DataSourceController.get(), &DataSourceController::initialize);
86 impl->m_DataSourceController.get(), &DataSourceController::initialize);
87 connect(&impl->m_DataSourceControllerThread, &QThread::finished,
87 connect(&impl->m_DataSourceControllerThread, &QThread::finished,
88 impl->m_DataSourceController.get(), &DataSourceController::finalize);
88 impl->m_DataSourceController.get(), &DataSourceController::finalize);
89
89
90 connect(&impl->m_NetworkControllerThread, &QThread::started, impl->m_NetworkController.get(),
90 connect(&impl->m_NetworkControllerThread, &QThread::started, impl->m_NetworkController.get(),
91 &NetworkController::initialize);
91 &NetworkController::initialize);
92 connect(&impl->m_NetworkControllerThread, &QThread::finished, impl->m_NetworkController.get(),
92 connect(&impl->m_NetworkControllerThread, &QThread::finished, impl->m_NetworkController.get(),
93 &NetworkController::finalize);
93 &NetworkController::finalize);
94
94
95 connect(&impl->m_VariableControllerThread, &QThread::started, impl->m_VariableController.get(),
95 connect(&impl->m_VariableControllerThread, &QThread::started, impl->m_VariableController.get(),
96 &VariableController::initialize);
96 &VariableController::initialize);
97 connect(&impl->m_VariableControllerThread, &QThread::finished, impl->m_VariableController.get(),
97 connect(&impl->m_VariableControllerThread, &QThread::finished, impl->m_VariableController.get(),
98 &VariableController::finalize);
98 &VariableController::finalize);
99
99
100 connect(&impl->m_VisualizationControllerThread, &QThread::started,
100 connect(&impl->m_VisualizationControllerThread, &QThread::started,
101 impl->m_VisualizationController.get(), &VisualizationController::initialize);
101 impl->m_VisualizationController.get(), &VisualizationController::initialize);
102 connect(&impl->m_VisualizationControllerThread, &QThread::finished,
102 connect(&impl->m_VisualizationControllerThread, &QThread::finished,
103 impl->m_VisualizationController.get(), &VisualizationController::finalize);
103 impl->m_VisualizationController.get(), &VisualizationController::finalize);
104
104
105 impl->m_DataSourceControllerThread.start();
105 impl->m_DataSourceControllerThread.start();
106 impl->m_NetworkControllerThread.start();
106 impl->m_NetworkControllerThread.start();
107 impl->m_VariableControllerThread.start();
107 impl->m_VariableControllerThread.start();
108 impl->m_VisualizationControllerThread.start();
108 impl->m_VisualizationControllerThread.start();
109
110 // Core connections:
111 // NetworkController <-> VariableController
112 connect(&sqpApp->networkController(), &NetworkController::replyDownloadProgress,
113 &sqpApp->variableController(), &VariableController::onVariableRetrieveDataInProgress);
114 }
109 }
115
110
116 SqpApplication::~SqpApplication()
111 SqpApplication::~SqpApplication()
117 {
112 {
118 }
113 }
119
114
120 void SqpApplication::initialize()
115 void SqpApplication::initialize()
121 {
116 {
122 }
117 }
123
118
124 DataSourceController &SqpApplication::dataSourceController() noexcept
119 DataSourceController &SqpApplication::dataSourceController() noexcept
125 {
120 {
126 return *impl->m_DataSourceController;
121 return *impl->m_DataSourceController;
127 }
122 }
128
123
129 NetworkController &SqpApplication::networkController() noexcept
124 NetworkController &SqpApplication::networkController() noexcept
130 {
125 {
131 return *impl->m_NetworkController;
126 return *impl->m_NetworkController;
132 }
127 }
133
128
134 TimeController &SqpApplication::timeController() noexcept
129 TimeController &SqpApplication::timeController() noexcept
135 {
130 {
136 return *impl->m_TimeController;
131 return *impl->m_TimeController;
137 }
132 }
138
133
139 VariableController &SqpApplication::variableController() noexcept
134 VariableController &SqpApplication::variableController() noexcept
140 {
135 {
141 return *impl->m_VariableController;
136 return *impl->m_VariableController;
142 }
137 }
143
138
144 VisualizationController &SqpApplication::visualizationController() noexcept
139 VisualizationController &SqpApplication::visualizationController() noexcept
145 {
140 {
146 return *impl->m_VisualizationController;
141 return *impl->m_VisualizationController;
147 }
142 }
@@ -1,134 +1,138
1 #include "AmdaProvider.h"
1 #include "AmdaProvider.h"
2 #include "AmdaDefs.h"
2 #include "AmdaDefs.h"
3 #include "AmdaResultParser.h"
3 #include "AmdaResultParser.h"
4
4
5 #include <Data/DataProviderParameters.h>
5 #include <Data/DataProviderParameters.h>
6 #include <Network/NetworkController.h>
6 #include <Network/NetworkController.h>
7 #include <SqpApplication.h>
7 #include <SqpApplication.h>
8 #include <Variable/Variable.h>
8 #include <Variable/Variable.h>
9
9
10 #include <QNetworkAccessManager>
10 #include <QNetworkAccessManager>
11 #include <QNetworkReply>
11 #include <QNetworkReply>
12 #include <QTemporaryFile>
12 #include <QTemporaryFile>
13 #include <QThread>
13 #include <QThread>
14
14
15 Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider")
15 Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider")
16
16
17 namespace {
17 namespace {
18
18
19 /// URL format for a request on AMDA server. The parameters are as follows:
19 /// URL format for a request on AMDA server. The parameters are as follows:
20 /// - %1: start date
20 /// - %1: start date
21 /// - %2: end date
21 /// - %2: end date
22 /// - %3: parameter id
22 /// - %3: parameter id
23 const auto AMDA_URL_FORMAT = QStringLiteral(
23 const auto AMDA_URL_FORMAT = QStringLiteral(
24 "http://amda.irap.omp.eu/php/rest/"
24 "http://amda.irap.omp.eu/php/rest/"
25 "getParameter.php?startTime=%1&stopTime=%2&parameterID=%3&sampling=60&outputFormat=ASCII&"
25 "getParameter.php?startTime=%1&stopTime=%2&parameterID=%3&sampling=60&outputFormat=ASCII&"
26 "timeFormat=ISO8601&gzip=0");
26 "timeFormat=ISO8601&gzip=0");
27
27
28 /// Dates format passed in the URL (e.g 2013-09-23T09:00)
28 /// Dates format passed in the URL (e.g 2013-09-23T09:00)
29 const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:ss");
29 const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:ss");
30
30
31 /// Formats a time to a date that can be passed in URL
31 /// Formats a time to a date that can be passed in URL
32 QString dateFormat(double sqpDateTime) noexcept
32 QString dateFormat(double sqpDateTime) noexcept
33 {
33 {
34 auto dateTime = QDateTime::fromMSecsSinceEpoch(sqpDateTime * 1000.);
34 auto dateTime = QDateTime::fromMSecsSinceEpoch(sqpDateTime * 1000.);
35 return dateTime.toString(AMDA_TIME_FORMAT);
35 return dateTime.toString(AMDA_TIME_FORMAT);
36 }
36 }
37
37
38 } // namespace
38 } // namespace
39
39
40 AmdaProvider::AmdaProvider()
40 AmdaProvider::AmdaProvider()
41 {
41 {
42 qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider")
42 qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider")
43 << QThread::currentThread();
43 << QThread::currentThread();
44 if (auto app = sqpApp) {
44 if (auto app = sqpApp) {
45 auto &networkController = app->networkController();
45 auto &networkController = app->networkController();
46 connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid,
46 connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid,
47 std::function<void(QNetworkReply *, QUuid)>)),
47 std::function<void(QNetworkReply *, QUuid)>)),
48 &networkController,
48 &networkController,
49 SLOT(onProcessRequested(QNetworkRequest, QUuid,
49 SLOT(onProcessRequested(QNetworkRequest, QUuid,
50 std::function<void(QNetworkReply *, QUuid)>)));
50 std::function<void(QNetworkReply *, QUuid)>)));
51
52
53 connect(&sqpApp->networkController(), SIGNAL(replyDownloadProgress(QUuid, double)), this,
54 SIGNAL(dataProvidedProgress(QUuid, double)));
51 }
55 }
52 }
56 }
53
57
54 void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters &parameters)
58 void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters &parameters)
55 {
59 {
56 // NOTE: Try to use multithread if possible
60 // NOTE: Try to use multithread if possible
57 const auto times = parameters.m_Times;
61 const auto times = parameters.m_Times;
58 const auto data = parameters.m_Data;
62 const auto data = parameters.m_Data;
59 for (const auto &dateTime : qAsConst(times)) {
63 for (const auto &dateTime : qAsConst(times)) {
60 retrieveData(token, dateTime, data);
64 retrieveData(token, dateTime, data);
61 }
65 }
62 }
66 }
63
67
64 void AmdaProvider::requestDataAborting(QUuid identifier)
68 void AmdaProvider::requestDataAborting(QUuid identifier)
65 {
69 {
66 if (auto app = sqpApp) {
70 if (auto app = sqpApp) {
67 auto &networkController = app->networkController();
71 auto &networkController = app->networkController();
68 networkController.onReplyCanceled(identifier);
72 networkController.onReplyCanceled(identifier);
69 }
73 }
70 }
74 }
71
75
72 void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data)
76 void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data)
73 {
77 {
74 // Retrieves product ID from data: if the value is invalid, no request is made
78 // Retrieves product ID from data: if the value is invalid, no request is made
75 auto productId = data.value(AMDA_XML_ID_KEY).toString();
79 auto productId = data.value(AMDA_XML_ID_KEY).toString();
76 if (productId.isNull()) {
80 if (productId.isNull()) {
77 qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id");
81 qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id");
78 return;
82 return;
79 }
83 }
80
84
81 // /////////// //
85 // /////////// //
82 // Creates URL //
86 // Creates URL //
83 // /////////// //
87 // /////////// //
84
88
85 auto startDate = dateFormat(dateTime.m_TStart);
89 auto startDate = dateFormat(dateTime.m_TStart);
86 auto endDate = dateFormat(dateTime.m_TEnd);
90 auto endDate = dateFormat(dateTime.m_TEnd);
87
91
88 auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)};
92 auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)};
89
93
90 auto tempFile = std::make_shared<QTemporaryFile>();
94 auto tempFile = std::make_shared<QTemporaryFile>();
91
95
92 // LAMBDA
96 // LAMBDA
93 auto httpDownloadFinished
97 auto httpDownloadFinished
94 = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept {
98 = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept {
95 Q_UNUSED(dataId);
99 Q_UNUSED(dataId);
96
100
97 if (tempFile) {
101 if (tempFile) {
98 auto replyReadAll = reply->readAll();
102 auto replyReadAll = reply->readAll();
99 if (!replyReadAll.isEmpty()) {
103 if (!replyReadAll.isEmpty()) {
100 tempFile->write(replyReadAll);
104 tempFile->write(replyReadAll);
101 }
105 }
102 tempFile->close();
106 tempFile->close();
103
107
104 // Parse results file
108 // Parse results file
105 if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) {
109 if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) {
106 emit dataProvided(token, dataSeries, dateTime);
110 emit dataProvided(token, dataSeries, dateTime);
107 }
111 }
108 else {
112 else {
109 /// @todo ALX : debug
113 /// @todo ALX : debug
110 }
114 }
111 }
115 }
112
116
113
117
114 };
118 };
115 auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply,
119 auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply,
116 QUuid dataId) noexcept {
120 QUuid dataId) noexcept {
117
121
118 auto downloadFileUrl = QUrl{QString{reply->readAll()}};
122 auto downloadFileUrl = QUrl{QString{reply->readAll()}};
119
123
120
124
121 // Executes request for downloading file //
125 // Executes request for downloading file //
122
126
123 // Creates destination file
127 // Creates destination file
124 if (tempFile->open()) {
128 if (tempFile->open()) {
125 // Executes request
129 // Executes request
126 emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished);
130 emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished);
127 }
131 }
128 };
132 };
129
133
130 // //////////////// //
134 // //////////////// //
131 // Executes request //
135 // Executes request //
132 // //////////////// //
136 // //////////////// //
133 emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda);
137 emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda);
134 }
138 }
General Comments 0
You need to be logged in to leave comments. Login now