##// END OF EJS Templates
Pass variable metadata as parameters of a request in a provider
Alexandre Leroux -
r411:1232f5544b57
parent child
Show More
@@ -1,15 +1,18
1 #ifndef SCIQLOP_DATAPROVIDERPARAMETERS_H
1 #ifndef SCIQLOP_DATAPROVIDERPARAMETERS_H
2 #define SCIQLOP_DATAPROVIDERPARAMETERS_H
2 #define SCIQLOP_DATAPROVIDERPARAMETERS_H
3
3
4 #include "SqpDateTime.h"
4 #include "SqpDateTime.h"
5
5
6 /**
6 /**
7 * @brief The DataProviderParameters struct holds the information needed to retrieve data from a
7 * @brief The DataProviderParameters struct holds the information needed to retrieve data from a
8 * data provider
8 * data provider
9 * @sa IDataProvider
9 * @sa IDataProvider
10 */
10 */
11 struct DataProviderParameters {
11 struct DataProviderParameters {
12 /// Times for which retrieve data
12 QVector<SqpDateTime> m_Times;
13 QVector<SqpDateTime> m_Times;
14 /// Extra data that can be used by the provider to retrieve data
15 QVariantHash m_Data;
13 };
16 };
14
17
15 #endif // SCIQLOP_DATAPROVIDERPARAMETERS_H
18 #endif // SCIQLOP_DATAPROVIDERPARAMETERS_H
@@ -1,217 +1,218
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_VariableToIdentifier;
41 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifier;
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
50
51 VariableController::~VariableController()
51 VariableController::~VariableController()
52 {
52 {
53 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
53 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
54 << QThread::currentThread();
54 << QThread::currentThread();
55 this->waitForFinish();
55 this->waitForFinish();
56 }
56 }
57
57
58 VariableModel *VariableController::variableModel() noexcept
58 VariableModel *VariableController::variableModel() noexcept
59 {
59 {
60 return impl->m_VariableModel;
60 return impl->m_VariableModel;
61 }
61 }
62
62
63 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
63 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
64 {
64 {
65 return impl->m_VariableSelectionModel;
65 return impl->m_VariableSelectionModel;
66 }
66 }
67
67
68 void VariableController::setTimeController(TimeController *timeController) noexcept
68 void VariableController::setTimeController(TimeController *timeController) noexcept
69 {
69 {
70 impl->m_TimeController = timeController;
70 impl->m_TimeController = timeController;
71 }
71 }
72
72
73 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
73 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
74 {
74 {
75 if (!variable) {
75 if (!variable) {
76 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
76 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
77 return;
77 return;
78 }
78 }
79
79
80 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
80 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
81 // make some treatments before the deletion
81 // make some treatments before the deletion
82 emit variableAboutToBeDeleted(variable);
82 emit variableAboutToBeDeleted(variable);
83
83
84 // Deletes provider
84 // Deletes provider
85 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
85 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
86 qCDebug(LOG_VariableController())
86 qCDebug(LOG_VariableController())
87 << tr("Number of providers deleted for variable %1: %2")
87 << tr("Number of providers deleted for variable %1: %2")
88 .arg(variable->name(), QString::number(nbProvidersDeleted));
88 .arg(variable->name(), QString::number(nbProvidersDeleted));
89
89
90 // Clears cache
90 // Clears cache
91 impl->m_VariableCacheController->clear(variable);
91 impl->m_VariableCacheController->clear(variable);
92
92
93 // Deletes from model
93 // Deletes from model
94 impl->m_VariableModel->deleteVariable(variable);
94 impl->m_VariableModel->deleteVariable(variable);
95 }
95 }
96
96
97 void VariableController::deleteVariables(
97 void VariableController::deleteVariables(
98 const QVector<std::shared_ptr<Variable> > &variables) noexcept
98 const QVector<std::shared_ptr<Variable> > &variables) noexcept
99 {
99 {
100 for (auto variable : qAsConst(variables)) {
100 for (auto variable : qAsConst(variables)) {
101 deleteVariable(variable);
101 deleteVariable(variable);
102 }
102 }
103 }
103 }
104
104
105 void VariableController::createVariable(const QString &name, const QVariantHash &metadata,
105 void VariableController::createVariable(const QString &name, const QVariantHash &metadata,
106 std::shared_ptr<IDataProvider> provider) noexcept
106 std::shared_ptr<IDataProvider> provider) noexcept
107 {
107 {
108
108
109 if (!impl->m_TimeController) {
109 if (!impl->m_TimeController) {
110 qCCritical(LOG_VariableController())
110 qCCritical(LOG_VariableController())
111 << tr("Impossible to create variable: The time controller is null");
111 << tr("Impossible to create variable: The time controller is null");
112 return;
112 return;
113 }
113 }
114
114
115
115
116 /// @todo : for the moment :
116 /// @todo : for the moment :
117 /// - the provider is only used to retrieve data from the variable for its initialization, but
117 /// - the provider is only used to retrieve data from the variable for its initialization, but
118 /// it will be retained later
118 /// it will be retained later
119 /// - default data are generated for the variable, without taking into account the timerange set
119 /// - default data are generated for the variable, without taking into account the timerange set
120 /// in sciqlop
120 /// in sciqlop
121 auto dateTime = impl->m_TimeController->dateTime();
121 auto dateTime = impl->m_TimeController->dateTime();
122
122
123 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) {
123 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) {
124 auto identifier = QUuid::createUuid();
124 auto identifier = QUuid::createUuid();
125
125
126 // store the provider
126 // store the provider
127 impl->m_VariableToProviderMap[newVariable] = provider;
127 impl->m_VariableToProviderMap[newVariable] = provider;
128 impl->m_VariableToIdentifier[newVariable] = identifier;
128 impl->m_VariableToIdentifier[newVariable] = identifier;
129
129
130 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
130 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
131 QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache)
131 QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache)
132 {
132 {
133 if (auto variable = varW.lock()) {
133 if (auto variable = varW.lock()) {
134 auto varIdentifier = impl->m_VariableToIdentifier.at(variable);
134 auto varIdentifier = impl->m_VariableToIdentifier.at(variable);
135 if (varIdentifier == identifier) {
135 if (varIdentifier == identifier) {
136 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
136 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
137 variable->setDataSeries(dataSeriesAcquired);
137 variable->setDataSeries(dataSeriesAcquired);
138 }
138 }
139 }
139 }
140 };
140 };
141
141
142 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
142 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
143 this->onRequestDataLoading(newVariable, dateTime);
143 this->onRequestDataLoading(newVariable, dateTime);
144 }
144 }
145 }
145 }
146
146
147 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
147 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
148 {
148 {
149 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
149 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
150 << QThread::currentThread()->objectName();
150 << QThread::currentThread()->objectName();
151 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
151 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
152
152
153 for (const auto &selectedRow : qAsConst(selectedRows)) {
153 for (const auto &selectedRow : qAsConst(selectedRows)) {
154 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
154 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
155 selectedVariable->setDateTime(dateTime);
155 selectedVariable->setDateTime(dateTime);
156 this->onRequestDataLoading(selectedVariable, dateTime);
156 this->onRequestDataLoading(selectedVariable, dateTime);
157 }
157 }
158 }
158 }
159 }
159 }
160
160
161 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
161 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
162 {
162 {
163 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
163 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
164
164
165 auto end = impl->m_VariableToIdentifier.cend();
165 auto end = impl->m_VariableToIdentifier.cend();
166 auto it = std::find_if(impl->m_VariableToIdentifier.cbegin(), end, findReply);
166 auto it = std::find_if(impl->m_VariableToIdentifier.cbegin(), end, findReply);
167 if (it != end) {
167 if (it != end) {
168 impl->m_VariableModel->setDataProgress(it->first, progress);
168 impl->m_VariableModel->setDataProgress(it->first, progress);
169 }
169 }
170 }
170 }
171
171
172
172
173 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
173 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
174 const SqpDateTime &dateTime)
174 const SqpDateTime &dateTime)
175 {
175 {
176 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
176 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
177 << QThread::currentThread()->objectName();
177 << QThread::currentThread()->objectName();
178 // we want to load data of the variable for the dateTime.
178 // we want to load data of the variable for the dateTime.
179 // First we check if the cache contains some of them.
179 // First we check if the cache contains some of them.
180 // For the other, we ask the provider to give them.
180 // For the other, we ask the provider to give them.
181 if (variable) {
181 if (variable) {
182
182
183 auto dateTimeListNotInCache
183 auto dateTimeListNotInCache
184 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
184 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
185
185
186 if (!dateTimeListNotInCache.empty()) {
186 if (!dateTimeListNotInCache.empty()) {
187 // Ask the provider for each data on the dateTimeListNotInCache
187 // Ask the provider for each data on the dateTimeListNotInCache
188 auto identifier = impl->m_VariableToIdentifier.at(variable);
188 auto identifier = impl->m_VariableToIdentifier.at(variable);
189 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
189 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
190 identifier, DataProviderParameters{std::move(dateTimeListNotInCache)});
190 identifier,
191 DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()});
191 }
192 }
192 else {
193 else {
193 emit variable->updated();
194 emit variable->updated();
194 }
195 }
195 }
196 }
196 else {
197 else {
197 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
198 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
198 }
199 }
199 }
200 }
200
201
201
202
202 void VariableController::initialize()
203 void VariableController::initialize()
203 {
204 {
204 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
205 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
205 impl->m_WorkingMutex.lock();
206 impl->m_WorkingMutex.lock();
206 qCDebug(LOG_VariableController()) << tr("VariableController init END");
207 qCDebug(LOG_VariableController()) << tr("VariableController init END");
207 }
208 }
208
209
209 void VariableController::finalize()
210 void VariableController::finalize()
210 {
211 {
211 impl->m_WorkingMutex.unlock();
212 impl->m_WorkingMutex.unlock();
212 }
213 }
213
214
214 void VariableController::waitForFinish()
215 void VariableController::waitForFinish()
215 {
216 {
216 QMutexLocker locker{&impl->m_WorkingMutex};
217 QMutexLocker locker{&impl->m_WorkingMutex};
217 }
218 }
General Comments 0
You need to be logged in to leave comments. Login now