~ubuntu-branches/ubuntu/trusty/libusermetrics/trusty-proposed

« back to all changes in this revision

Viewing changes to src/usermetricsservice/DBusDataSet.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Pete Woods, Ubuntu daily release
  • Date: 2013-07-02 02:02:52 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130702020252-53yuhnlabsq1cq8x
Tags: 1.0.1+13.10.20130702-0ubuntu1
[ Pete Woods ]
* Implement most of storage service and wire up input API.
* Wire up the output API to the storage service.

[ Ubuntu daily release ]
* Automatic snapshot from revision 80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Author: Pete Woods <pete.woods@canonical.com>
 
17
 */
 
18
 
 
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>
 
24
 
 
25
#include <QtCore/QByteArray>
 
26
#include <QtCore/QDataStream>
 
27
 
 
28
#include <QDjangoQuerySet.h>
 
29
 
 
30
using namespace std;
 
31
using namespace UserMetricsCommon;
 
32
using namespace UserMetricsService;
 
33
 
 
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) {
 
40
 
 
41
        // DBus setup
 
42
        m_dbusConnection.registerObject(m_path, this);
 
43
}
 
44
 
 
45
DBusDataSet::~DBusDataSet() {
 
46
        QDBusConnection connection(QDBusConnection::sessionBus());
 
47
        connection.unregisterObject(m_path);
 
48
}
 
49
 
 
50
void DBusDataSet::getData(DataSet &dataSet, QVariantList &data) {
 
51
        QDataStream dataStream(dataSet.data());
 
52
        dataStream >> data;
 
53
}
 
54
 
 
55
QVariantList DBusDataSet::data() const {
 
56
        DataSet dataSet;
 
57
        DataSet::findById(m_id, &dataSet);
 
58
 
 
59
        QVariantList data;
 
60
        getData(dataSet, data);
 
61
 
 
62
        return data;
 
63
}
 
64
 
 
65
void DBusDataSet::update(const QVariantList &data) {
 
66
        DataSet dataSet;
 
67
        DataSet::findById(m_id, &dataSet);
 
68
 
 
69
        QVariantList oldData;
 
70
        getData(dataSet, oldData);
 
71
 
 
72
        QDate currentDate(m_dateFactory->currentDate());
 
73
        int daysSinceLastUpdate(dataSet.lastUpdated().daysTo(currentDate));
 
74
 
 
75
        QVariantList newData(data);
 
76
 
 
77
        // if we are in this situation we do nothing
 
78
        // new: |4|5|6|7|8|9|0|
 
79
        // old:     |1|2|3|
 
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
 
85
                        // new: |6|7|8|9|0|
 
86
                        // old:     |1|2|3|4|5|
 
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) {
 
91
                                ++oldDataIt;
 
92
                        }
 
93
                        // append the rest of the data
 
94
                        for (; oldDataIt != oldData.constEnd(); ++oldDataIt) {
 
95
                                newData.append(*oldDataIt);
 
96
                        }
 
97
                } else {
 
98
                        // we are in this situation - there is a gap
 
99
                        // and we want the whole of the old data appending
 
100
                        // new: |6|7|8|9|0|
 
101
                        // old:             |1|2|3|4|5|
 
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(""));
 
107
                        }
 
108
                        // append the whole of the old data
 
109
                        newData.append(oldData);
 
110
                }
 
111
        }
 
112
 
 
113
        QByteArray byteArray;
 
114
        {
 
115
                QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
 
116
                dataStream << newData;
 
117
        }
 
118
 
 
119
        dataSet.setLastUpdated(currentDate);
 
120
        dataSet.setData(byteArray);
 
121
        if (!dataSet.save()) {
 
122
                throw logic_error("couldn't save data set");
 
123
        }
 
124
 
 
125
        QDateTime dateTime(currentDate);
 
126
        m_adaptor->updated(dateTime.toTime_t(), newData);
 
127
}
 
128
 
 
129
uint DBusDataSet::lastUpdated() const {
 
130
        const QDate &lastUpdated(lastUpdatedDate());
 
131
        QDateTime dateTime(lastUpdated);
 
132
        return dateTime.toTime_t();
 
133
}
 
134
 
 
135
QDate DBusDataSet::lastUpdatedDate() const {
 
136
        DataSet dataSet;
 
137
        DataSet::findById(m_id, &dataSet);
 
138
        return dataSet.lastUpdated();
 
139
}
 
140
 
 
141
int DBusDataSet::id() const {
 
142
        return m_id;
 
143
}
 
144
 
 
145
QString DBusDataSet::path() const {
 
146
        return m_path;
 
147
}
 
148
 
 
149
QString DBusDataSet::dataSource() const {
 
150
        return m_dataSource;
 
151
}