Commit 5a14b0f4 authored by Volker Krause's avatar Volker Krause
Browse files

Add client side code for importing sample data

parent b8d00fdb
......@@ -26,12 +26,14 @@
#include "timeaggregationmodel.h"
#include <model/datamodel.h>
#include <rest/restapi.h>
#include <core/sample.h>
#include <QFile>
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
#include <QNetworkReply>
#include <QSettings>
using namespace UserFeedback::Analyzer;
......@@ -102,6 +104,7 @@ AnalyticsView::~AnalyticsView()
void AnalyticsView::setRESTClient(RESTClient* client)
{
m_client = client;
m_dataModel->setRESTClient(client);
}
......@@ -171,10 +174,29 @@ void AnalyticsView::exportData()
const auto samples = m_dataModel->index(0, 0).data(DataModel::AllSamplesRole).value<QVector<Sample>>();
f.write(Sample::toJson(samples, m_dataModel->product()));
logMessage(tr("Sample data of %1 exported to %2.").arg(m_dataModel->product().name(), f.fileName()));
emit logMessage(tr("Sample data of %1 exported to %2.").arg(m_dataModel->product().name(), f.fileName()));
}
void AnalyticsView::importData()
{
// TODO
const auto fileName = QFileDialog::getOpenFileName(this, tr("Import Data"));
if (fileName.isEmpty())
return;
QFile f(fileName);
if (!f.open(QFile::ReadOnly)) {
QMessageBox::critical(this, tr("Import Failed"), tr("Could not open file: %1").arg(f.errorString()));
return;
}
const auto samples = Sample::fromJson(f.readAll(), m_dataModel->product());
if (samples.isEmpty()) {
QMessageBox::critical(this, tr("Import Failed"), tr("Selected file contains no valid data."));
return;
}
auto reply = RESTApi::addSamples(m_client, m_dataModel->product(), samples);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() == QNetworkReply::NoError)
emit logMessage(tr("Samples imported."));
});
}
......@@ -60,6 +60,7 @@ private:
std::unique_ptr<Ui::AnalyticsView> ui;
RESTClient *m_client = nullptr;
DataModel *m_dataModel;
TimeAggregationModel *m_timeAggregationModel;
QVector<QAbstractItemModel*> m_aggregationModels;
......
......@@ -19,6 +19,7 @@
#include "restclient.h"
#include <core/product.h>
#include <core/sample.h>
#include <core/survey.h>
using namespace UserFeedback::Analyzer;
......@@ -57,6 +58,12 @@ QNetworkReply* RESTApi::listSamples(RESTClient *client, const Product &p)
return client->get(QStringLiteral("data/") + p.name());
}
QNetworkReply* RESTApi::addSamples(RESTClient *client, const Product &p, const QVector<Sample> &samples)
{
Q_ASSERT(p.isValid());
return client->post(QStringLiteral("data/") + p.name(), Sample::toJson(samples, p));
}
QNetworkReply* RESTApi::listSurveys(RESTClient *client, const Product &p)
{
Q_ASSERT(p.isValid());
......
......@@ -19,12 +19,14 @@
#define USERFEEDBACK_ANALYZER_RESTAPI_H
class QNetworkReply;
template <typename T> class QVector;
namespace UserFeedback {
namespace Analyzer {
class Product;
class RESTClient;
class Sample;
class Survey;
/** C++ wrapper for the server-side API.
......@@ -58,6 +60,11 @@ namespace RESTApi
*/
QNetworkReply* listSamples(RESTClient *client, const Product &p);
/** Add the given samples to a product.
* @param p The product to add. Must be valid.
*/
QNetworkReply* addSamples(RESTClient *client, const Product &p, const QVector<Sample> &samples);
/** List all surveys for product @p p. */
QNetworkReply* listSurveys(RESTClient *client, const Product &p);
......
Supports Markdown
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