Commit d971549b authored by Volker Krause's avatar Volker Krause
Browse files

Implement numeric aggregation for arrays and maps too

This makes for example numeric aggregation for screen dpi work.
parent ef751806
......@@ -44,8 +44,19 @@ private slots:
{
NumericAggregationModel model;
ModelTest modelTest(&model);
model.setAggregationValue(QString());
model.setAggregationValue(QLatin1String("usageTime.value"));
model.setAggregation(AggregationElement());
AggregationElement aggr;
{
SchemaEntry entry;
entry.setDataType(SchemaEntry::Scalar);
entry.setName(QLatin1String("usageTime"));
aggr.setSchemaEntry(entry);
SchemaEntryElement elem;
elem.setName(QLatin1String("value"));
aggr.setSchemaEntryElement(elem);
aggr.setType(AggregationElement::Value);
}
model.setAggregation(aggr);
TimeAggregationModel timeModel;
model.setSourceModel(&timeModel);
......@@ -69,7 +80,18 @@ private slots:
{
NumericAggregationModel model;
ModelTest modelTest(&model);
model.setAggregationValue(QLatin1String("usageTime.value"));
AggregationElement aggr;
{
SchemaEntry entry;
entry.setDataType(SchemaEntry::Scalar);
entry.setName(QLatin1String("usageTime"));
aggr.setSchemaEntry(entry);
SchemaEntryElement elem;
elem.setName(QLatin1String("value"));
aggr.setSchemaEntryElement(elem);
aggr.setType(AggregationElement::Value);
}
model.setAggregation(aggr);
TimeAggregationModel timeModel;
model.setSourceModel(&timeModel);
......
......@@ -68,9 +68,19 @@
}
],
"type": "numeric"
},
{
"elements": [
{
"schemaEntry": "screens",
"schemaEntryElement": "dpi",
"type": "value"
}
],
"type": "numeric"
}
],
"name": "org.kde.UserFeedback.orwell",
"name": "org.kde.orwell",
"schema": [
{
"elements": [
......@@ -125,6 +135,10 @@
{
"name": "height",
"type": "int"
},
{
"name": "dpi",
"type": "int"
}
],
"name": "screens",
......
......@@ -53,7 +53,7 @@ QAbstractItemModel* NumericAggregator::timeAggregationModel()
m_model.reset(new NumericAggregationModel);
m_model->setSourceModel(sourceModel());
const auto e = aggregation().elements().at(0);
m_model->setAggregationValue(e.schemaEntry().name() + QLatin1Char('.') + e.schemaEntryElement().name());
m_model->setAggregation(e);
QObject::connect(m_model.get(), &QAbstractItemModel::modelReset, [this]() {
updateTimelineChart();
});
......
......@@ -36,9 +36,9 @@ void NumericAggregationModel::setSourceModel(QAbstractItemModel* model)
recompute();
}
void NumericAggregationModel::setAggregationValue(const QString& aggrValue)
void NumericAggregationModel::setAggregation(const AggregationElement& aggr)
{
m_aggrValue = aggrValue;
m_aggr = aggr;
recompute();
}
......@@ -108,7 +108,7 @@ void NumericAggregationModel::recompute()
m_data.clear();
m_maxValue = 0;
if (rowCount <= 0 || m_aggrValue.isEmpty()) {
if (rowCount <= 0 || !m_aggr.isValid()) {
endResetModel();
return;
}
......@@ -122,10 +122,8 @@ void NumericAggregationModel::recompute()
values.clear();
values.reserve(samples.size());
foreach (const auto &sample, samples) {
const auto v = sample.value(m_aggrValue).toDouble();
values.push_back(v);
}
foreach (const auto &sample, samples)
values += sampleValues(sample);
std::sort(values.begin(), values.end());
......@@ -143,3 +141,39 @@ void NumericAggregationModel::recompute()
endResetModel();
}
QVector<double> NumericAggregationModel::sampleValues(const Sample &s) const
{
switch (m_aggr.schemaEntry().dataType()) {
case SchemaEntry::Scalar:
{
if (m_aggr.type() != AggregationElement::Value)
return {};
return {s.value(m_aggr.schemaEntry().name() + QLatin1String(".") + m_aggr.schemaEntryElement().name()).toDouble()};
}
case SchemaEntry::List:
{
const auto l = s.value(m_aggr.schemaEntry().name()).value<QVariantList>();
if (m_aggr.type() == AggregationElement::Size)
return {(double)l.size()};
QVector<double> r;
r.reserve(l.size());
for (const auto &entry : l)
r.push_back(entry.toMap().value(m_aggr.schemaEntryElement().name()).toDouble());
return r;
}
case SchemaEntry::Map:
{
const auto m = s.value(m_aggr.schemaEntry().name()).toMap();
if (m_aggr.type() == AggregationElement::Size)
return {(double)m.size()};
QVector<double> r;
r.reserve(m.size());
for (auto it = m.begin(); it != m.end(); ++it)
r.push_back(it.value().toMap().value(m_aggr.schemaEntryElement().name()).toDouble());
return r;
}
}
return {};
}
......@@ -18,11 +18,15 @@
#ifndef USERFEEDBACK_CONSOLE_NUMERICAGGREGATIONMODEL_H
#define USERFEEDBACK_CONSOLE_NUMERICAGGREGATIONMODEL_H
#include <core/aggregationelement.h>
#include <QAbstractTableModel>
namespace UserFeedback {
namespace Console {
class Sample;
class NumericAggregationModel : public QAbstractTableModel
{
Q_OBJECT
......@@ -31,7 +35,7 @@ public:
~NumericAggregationModel();
void setSourceModel(QAbstractItemModel *model);
void setAggregationValue(const QString &aggrValue);
void setAggregation(const AggregationElement &aggr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
......@@ -40,9 +44,10 @@ public:
private:
void recompute();
QVector<double> sampleValues(const Sample &s) const;
QAbstractItemModel *m_sourceModel = nullptr;
QString m_aggrValue;
AggregationElement m_aggr;
struct Data {
double lowerExtreme = 0.0;
double lowerQuartile = 0.0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment