summaryrefslogtreecommitdiffstats
path: root/tests/hawd/modules/json.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/hawd/modules/json.cpp')
-rw-r--r--tests/hawd/modules/json.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/tests/hawd/modules/json.cpp b/tests/hawd/modules/json.cpp
new file mode 100644
index 0000000..d08df49
--- /dev/null
+++ b/tests/hawd/modules/json.cpp
@@ -0,0 +1,87 @@
1/*
2 * Copyright (C) 2017 Christian Mollekopf <mollekopf@kolabsystems.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "json.h"
21
22#include "../datasetdefinition.h"
23#include "../dataset.h"
24
25#include <QDir>
26#include <QObject>
27#include <QJsonDocument>
28#include <QJsonObject>
29#include <QJsonArray>
30#include <QDateTime>
31
32#include <iostream>
33
34namespace HAWD
35{
36
37Json::Json()
38 : Module()
39{
40 Syntax top("json", &Json::toJson);
41 setSyntax(top);
42 setDescription(QObject::tr("Prints a table from a dataset to json; you can provide a list of rows to output"));
43}
44
45bool Json::toJson(const QStringList &commands, State &state)
46{
47 if (commands.isEmpty()) {
48 std::cout << QObject::tr("print requires a dataset to be named").toStdString() << std::endl;
49 return true;
50 }
51
52 const QString datasetName = commands.first();
53
54 QDir project(state.projectPath());
55 Dataset dataset(datasetName, state);
56
57 if (!dataset.isValid()) {
58 std::cout << QObject::tr("The dataset %1 could not be loaded; try checking it with the check command").arg(datasetName).toStdString() << std::endl;
59 return true;
60 }
61
62 auto definition = state.datasetDefinition(datasetName);
63
64 QJsonObject json;
65 json.insert("dataset", datasetName);
66 json.insert("description", definition.description());
67
68 const auto columns = definition.columns();
69
70 QJsonArray array;
71 dataset.eachRow(
72 [&](const Dataset::Row &row) {
73 QJsonObject jsonRow;
74 jsonRow.insert("timestamp", QJsonValue::fromVariant(row.timestamp()));
75 jsonRow.insert("commit", row.commitHash());
76 for (const auto &col : columns) {
77 jsonRow.insert(col.first, QJsonValue::fromVariant(row.value(col.first)));
78 }
79 array.append(jsonRow);
80 });
81 json.insert("rows", array);
82 std::cout << QJsonDocument{json}.toJson().toStdString() << std::endl;
83 return true;
84}
85
86} // namespace HAWD
87