2
* Copyright (C) 2013 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Pete Woods <pete.woods@canonical.com>
19
#include <usermetricsservice/database/DataSet.h>
20
#include <usermetricsservice/DBusDataSet.h>
21
#include <usermetricsservice/DataSetAdaptor.h>
22
#include <libusermetricscommon/DateFactory.h>
23
#include <libusermetricscommon/DBusPaths.h>
25
#include <QtCore/QByteArray>
26
#include <QtCore/QDataStream>
28
#include <QDjangoQuerySet.h>
31
using namespace UserMetricsCommon;
32
using namespace UserMetricsService;
34
DBusDataSet::DBusDataSet(int id, const QString &dataSource,
35
QDBusConnection &dbusConnection,
36
QSharedPointer<DateFactory> dateFactory, QObject *parent) :
37
QObject(parent), m_dbusConnection(dbusConnection), m_adaptor(
38
new DataSetAdaptor(this)), m_dateFactory(dateFactory), m_id(id), m_path(
39
DBusPaths::dataSet(m_id)), m_dataSource(dataSource) {
42
m_dbusConnection.registerObject(m_path, this);
45
DBusDataSet::~DBusDataSet() {
46
QDBusConnection connection(QDBusConnection::sessionBus());
47
connection.unregisterObject(m_path);
50
void DBusDataSet::getData(DataSet &dataSet, QVariantList &data) {
51
QDataStream dataStream(dataSet.data());
55
QVariantList DBusDataSet::data() const {
57
DataSet::findById(m_id, &dataSet);
60
getData(dataSet, data);
65
void DBusDataSet::update(const QVariantList &data) {
67
DataSet::findById(m_id, &dataSet);
70
getData(dataSet, oldData);
72
QDate currentDate(m_dateFactory->currentDate());
73
int daysSinceLastUpdate(dataSet.lastUpdated().daysTo(currentDate));
75
QVariantList newData(data);
77
// if we are in this situation we do nothing
78
// new: |4|5|6|7|8|9|0|
80
// res: |4|5|6|7|8|9|0|
81
if (daysSinceLastUpdate + oldData.size() > newData.size()) {
82
if (daysSinceLastUpdate < newData.size()) {
83
// if we are in this situation - we need the
84
// protruding data from old
87
// res: |6|7|8|9|0|4|5|
88
auto oldDataIt(oldData.constBegin());
89
// wind forward until the data we want
90
for (int i(daysSinceLastUpdate); i < newData.size(); ++i) {
93
// append the rest of the data
94
for (; oldDataIt != oldData.constEnd(); ++oldDataIt) {
95
newData.append(*oldDataIt);
98
// we are in this situation - there is a gap
99
// and we want the whole of the old data appending
102
// res: |6|7|8|9|0|n|1|2|3|4|5|
103
const int daysToPad(daysSinceLastUpdate - newData.size());
104
// pad the data will null variants
105
for (int i(0); i < daysToPad; ++i) {
106
newData.append(QVariant(""));
108
// append the whole of the old data
109
newData.append(oldData);
113
QByteArray byteArray;
115
QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
116
dataStream << newData;
119
dataSet.setLastUpdated(currentDate);
120
dataSet.setData(byteArray);
121
if (!dataSet.save()) {
122
throw logic_error("couldn't save data set");
125
QDateTime dateTime(currentDate);
126
m_adaptor->updated(dateTime.toTime_t(), newData);
129
uint DBusDataSet::lastUpdated() const {
130
const QDate &lastUpdated(lastUpdatedDate());
131
QDateTime dateTime(lastUpdated);
132
return dateTime.toTime_t();
135
QDate DBusDataSet::lastUpdatedDate() const {
137
DataSet::findById(m_id, &dataSet);
138
return dataSet.lastUpdated();
141
int DBusDataSet::id() const {
145
QString DBusDataSet::path() const {
149
QString DBusDataSet::dataSource() const {