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