Changeset View
Changeset View
Standalone View
Standalone View
src/service/plugins/sqlite/ResourceScoreCache.cpp
Show First 20 Lines • Show All 149 Lines • ▼ Show 20 Line(s) | |||||
150 | { | 150 | { | ||
151 | QDateTime lastUpdate; | 151 | QDateTime lastUpdate; | ||
152 | QDateTime firstUpdate; | 152 | QDateTime firstUpdate; | ||
153 | QDateTime currentTime = QDateTime::currentDateTime(); | 153 | QDateTime currentTime = QDateTime::currentDateTime(); | ||
154 | qreal score = 0; | 154 | qreal score = 0; | ||
155 | 155 | | |||
156 | DATABASE_TRANSACTION(*resourcesDatabase()); | 156 | DATABASE_TRANSACTION(*resourcesDatabase()); | ||
157 | 157 | | |||
158 | qDebug() << "Creating the cache for: " << d->resource; | 158 | qCDebug(KAMD_LOG_RESOURCES) << "Creating the cache for: " << d->resource; | ||
159 | 159 | | |||
160 | // This can fail if we have the cache already made | 160 | // This can fail if we have the cache already made | ||
161 | auto isCacheNew = Utils::exec( | 161 | auto isCacheNew = Utils::exec( | ||
162 | Utils::IgnoreError, Queries::self().createResourceScoreCacheQuery, | 162 | Utils::IgnoreError, Queries::self().createResourceScoreCacheQuery, | ||
163 | ":usedActivity", d->activity, | 163 | ":usedActivity", d->activity, | ||
164 | ":initiatingAgent", d->application, | 164 | ":initiatingAgent", d->application, | ||
165 | ":targettedResource", d->resource, | 165 | ":targettedResource", d->resource, | ||
166 | ":firstUpdate", currentTime.toTime_t() | 166 | ":firstUpdate", currentTime.toTime_t() | ||
167 | ); | 167 | ); | ||
168 | 168 | | |||
169 | // Getting the old score | 169 | // Getting the old score | ||
170 | Utils::exec( | 170 | Utils::exec( | ||
171 | Utils::FailOnError, Queries::self().getResourceScoreCacheQuery, | 171 | Utils::FailOnError, Queries::self().getResourceScoreCacheQuery, | ||
172 | ":usedActivity", d->activity, | 172 | ":usedActivity", d->activity, | ||
173 | ":initiatingAgent", d->application, | 173 | ":initiatingAgent", d->application, | ||
174 | ":targettedResource", d->resource | 174 | ":targettedResource", d->resource | ||
175 | ); | 175 | ); | ||
176 | 176 | | |||
177 | // Only and always one result | 177 | // Only and always one result | ||
178 | for (const auto &result: Queries::self().getResourceScoreCacheQuery) { | 178 | for (const auto &result: Queries::self().getResourceScoreCacheQuery) { | ||
179 | 179 | | |||
180 | lastUpdate.setTime_t(result["lastUpdate"].toUInt()); | 180 | lastUpdate.setTime_t(result["lastUpdate"].toUInt()); | ||
181 | firstUpdate.setTime_t(result["firstUpdate"].toUInt()); | 181 | firstUpdate.setTime_t(result["firstUpdate"].toUInt()); | ||
182 | 182 | | |||
183 | qDebug() << "Already in database? " << (!isCacheNew); | 183 | qCDebug(KAMD_LOG_RESOURCES) << "Already in database? " << (!isCacheNew); | ||
184 | qDebug() << " First update : " << firstUpdate; | 184 | qCDebug(KAMD_LOG_RESOURCES) << " First update : " << firstUpdate; | ||
185 | qDebug() << " Last update : " << lastUpdate; | 185 | qCDebug(KAMD_LOG_RESOURCES) << " Last update : " << lastUpdate; | ||
186 | 186 | | |||
187 | if (isCacheNew) { | 187 | if (isCacheNew) { | ||
188 | // If we haven't had the cache before, set the score to 0 | 188 | // If we haven't had the cache before, set the score to 0 | ||
189 | firstUpdate = currentTime; | 189 | firstUpdate = currentTime; | ||
190 | score = 0; | 190 | score = 0; | ||
191 | 191 | | |||
192 | } else { | 192 | } else { | ||
193 | // Adjusting the score depending on the time that passed since the | 193 | // Adjusting the score depending on the time that passed since the | ||
194 | // last update | 194 | // last update | ||
195 | score = result["cachedScore"].toReal(); | 195 | score = result["cachedScore"].toReal(); | ||
196 | score *= d->timeFactor(lastUpdate, currentTime); | 196 | score *= d->timeFactor(lastUpdate, currentTime); | ||
197 | } | 197 | } | ||
198 | } | 198 | } | ||
199 | 199 | | |||
200 | // Calculating the updated score | 200 | // Calculating the updated score | ||
201 | // We are processing all events since the last cache update | 201 | // We are processing all events since the last cache update | ||
202 | 202 | | |||
203 | qDebug() << "After the adjustment"; | 203 | qCDebug(KAMD_LOG_RESOURCES) << "After the adjustment"; | ||
204 | qDebug() << " Current score : " << score; | 204 | qCDebug(KAMD_LOG_RESOURCES) << " Current score : " << score; | ||
205 | qDebug() << " First update : " << firstUpdate; | 205 | qCDebug(KAMD_LOG_RESOURCES) << " First update : " << firstUpdate; | ||
206 | qDebug() << " Last update : " << lastUpdate; | 206 | qCDebug(KAMD_LOG_RESOURCES) << " Last update : " << lastUpdate; | ||
207 | 207 | | |||
208 | Utils::exec(Utils::FailOnError, Queries::self().getScoreAdditionQuery, | 208 | Utils::exec(Utils::FailOnError, Queries::self().getScoreAdditionQuery, | ||
209 | ":usedActivity", d->activity, | 209 | ":usedActivity", d->activity, | ||
210 | ":initiatingAgent", d->application, | 210 | ":initiatingAgent", d->application, | ||
211 | ":targettedResource", d->resource, | 211 | ":targettedResource", d->resource, | ||
212 | ":start", lastUpdate.toTime_t() | 212 | ":start", lastUpdate.toTime_t() | ||
213 | ); | 213 | ); | ||
214 | 214 | | |||
215 | uint lastEventStart = currentTime.toTime_t(); | 215 | uint lastEventStart = currentTime.toTime_t(); | ||
216 | 216 | | |||
217 | for (const auto &result: Queries::self().getScoreAdditionQuery) { | 217 | for (const auto &result: Queries::self().getScoreAdditionQuery) { | ||
218 | lastEventStart = result["start"].toUInt(); | 218 | lastEventStart = result["start"].toUInt(); | ||
219 | 219 | | |||
220 | const auto end = result["end"].toUInt(); | 220 | const auto end = result["end"].toUInt(); | ||
221 | const auto intervalLength = end - lastEventStart; | 221 | const auto intervalLength = end - lastEventStart; | ||
222 | 222 | | |||
223 | qDebug() << "Interval length is " << intervalLength; | 223 | qCDebug(KAMD_LOG_RESOURCES) << "Interval length is " << intervalLength; | ||
224 | 224 | | |||
225 | if (intervalLength == 0) { | 225 | if (intervalLength == 0) { | ||
226 | // We have an Accessed event - otherwise, this wouldn't be 0 | 226 | // We have an Accessed event - otherwise, this wouldn't be 0 | ||
227 | score += d->timeFactor(QDateTime::fromTime_t(end), currentTime); // like it is open for 1 minute | 227 | score += d->timeFactor(QDateTime::fromTime_t(end), currentTime); // like it is open for 1 minute | ||
228 | 228 | | |||
229 | } else { | 229 | } else { | ||
230 | score += d->timeFactor(QDateTime::fromTime_t(end), currentTime) * intervalLength / 60.0; | 230 | score += d->timeFactor(QDateTime::fromTime_t(end), currentTime) * intervalLength / 60.0; | ||
231 | 231 | | |||
232 | } | 232 | } | ||
233 | } | 233 | } | ||
234 | 234 | | |||
235 | qDebug() << " New score : " << score; | 235 | qCDebug(KAMD_LOG_RESOURCES) << " New score : " << score; | ||
236 | 236 | | |||
237 | // Updating the score | 237 | // Updating the score | ||
238 | 238 | | |||
239 | Utils::exec(Utils::FailOnError, Queries::self().updateResourceScoreCacheQuery, | 239 | Utils::exec(Utils::FailOnError, Queries::self().updateResourceScoreCacheQuery, | ||
240 | ":usedActivity", d->activity, | 240 | ":usedActivity", d->activity, | ||
241 | ":initiatingAgent", d->application, | 241 | ":initiatingAgent", d->application, | ||
242 | ":targettedResource", d->resource, | 242 | ":targettedResource", d->resource, | ||
243 | ":cachedScore", score, | 243 | ":cachedScore", score, | ||
244 | ":lastUpdate", lastEventStart | 244 | ":lastUpdate", lastEventStart | ||
245 | ); | 245 | ); | ||
246 | 246 | | |||
247 | // Notifying the world | 247 | // Notifying the world | ||
248 | qDebug() << "ResourceScoreUpdated:" | 248 | qCDebug(KAMD_LOG_RESOURCES) << "ResourceScoreUpdated:" | ||
249 | << d->activity | 249 | << d->activity | ||
250 | << d->application | 250 | << d->application | ||
251 | << d->resource | 251 | << d->resource | ||
252 | ; | 252 | ; | ||
253 | emit QMetaObject::invokeMethod(StatsPlugin::self(), | 253 | emit QMetaObject::invokeMethod(StatsPlugin::self(), | ||
254 | "ResourceScoreUpdated", | 254 | "ResourceScoreUpdated", | ||
255 | Qt::QueuedConnection, | 255 | Qt::QueuedConnection, | ||
256 | Q_ARG(QString, d->activity), | 256 | Q_ARG(QString, d->activity), | ||
257 | Q_ARG(QString, d->application), | 257 | Q_ARG(QString, d->application), | ||
258 | Q_ARG(QString, d->resource), | 258 | Q_ARG(QString, d->resource), | ||
259 | Q_ARG(double, score), | 259 | Q_ARG(double, score), | ||
260 | Q_ARG(uint, lastEventStart), | 260 | Q_ARG(uint, lastEventStart), | ||
261 | Q_ARG(uint, firstUpdate.toTime_t()) | 261 | Q_ARG(uint, firstUpdate.toTime_t()) | ||
262 | ); | 262 | ); | ||
263 | } | 263 | } |