summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-10-10 17:16:49 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-10-10 17:16:49 +0200
commit8952f17e278088be95094ddb8e81a09131c9154b (patch)
treeb9abd6bdcbf17501700e3e93650e91cfc0fa57a8
parent4c0fe06dc4f66309cc66d90692ea31c22d5246b1 (diff)
downloadsink-8952f17e278088be95094ddb8e81a09131c9154b.tar.gz
sink-8952f17e278088be95094ddb8e81a09131c9154b.zip
hawd json output module
-rw-r--r--tests/hawd/CMakeLists.txt1
-rw-r--r--tests/hawd/dataset.cpp22
-rw-r--r--tests/hawd/dataset.h4
-rw-r--r--tests/hawd/module.cpp2
-rw-r--r--tests/hawd/modules/json.cpp87
-rw-r--r--tests/hawd/modules/json.h37
6 files changed, 149 insertions, 4 deletions
diff --git a/tests/hawd/CMakeLists.txt b/tests/hawd/CMakeLists.txt
index 7546920..8c9b389 100644
--- a/tests/hawd/CMakeLists.txt
+++ b/tests/hawd/CMakeLists.txt
@@ -23,6 +23,7 @@ set(SRCS
23 modules/list.cpp 23 modules/list.cpp
24 modules/check.cpp 24 modules/check.cpp
25 modules/print.cpp 25 modules/print.cpp
26 modules/json.cpp
26) 27)
27 28
28add_library(lib${PROJECT_NAME} SHARED ${lib_SRCS}) 29add_library(lib${PROJECT_NAME} SHARED ${lib_SRCS})
diff --git a/tests/hawd/dataset.cpp b/tests/hawd/dataset.cpp
index fb2d7e6..62fa0d7 100644
--- a/tests/hawd/dataset.cpp
+++ b/tests/hawd/dataset.cpp
@@ -77,6 +77,11 @@ void Dataset::Row::setValue(const QString &col, const QVariant &value)
77 } 77 }
78} 78}
79 79
80QVariant Dataset::Row::value(const QString &col) const
81{
82 return m_data.value(col);
83}
84
80void Dataset::Row::annotate(const QString &note) 85void Dataset::Row::annotate(const QString &note)
81{ 86{
82 m_annotation = note; 87 m_annotation = note;
@@ -176,6 +181,19 @@ QString Dataset::tableHeaders(const QStringList &cols, int standardCols, const Q
176 return strings.join(seperator); 181 return strings.join(seperator);
177} 182}
178 183
184
185QString Dataset::Row::commitHash() const
186{
187 return m_commitHash;
188}
189
190QDateTime Dataset::Row::timestamp() const
191{
192 QDateTime dt;
193 dt.setMSecsSinceEpoch(m_key);
194 return dt;
195}
196
179QString Dataset::Row::toString(const QStringList &cols, int standardCols, const QString &seperator) const 197QString Dataset::Row::toString(const QStringList &cols, int standardCols, const QString &seperator) const
180{ 198{
181 if (m_data.isEmpty()) { 199 if (m_data.isEmpty()) {
@@ -185,9 +203,7 @@ QString Dataset::Row::toString(const QStringList &cols, int standardCols, const
185 QStringList strings; 203 QStringList strings;
186 204
187 if (standardCols & Timestamp) { 205 if (standardCols & Timestamp) {
188 QDateTime dt; 206 strings << timestamp().toString("yyMMdd:hhmmss").leftJustified(s_fieldWidth, ' ');
189 dt.setMSecsSinceEpoch(m_key);
190 strings << dt.toString("yyMMdd:hhmmss").leftJustified(s_fieldWidth, ' ');
191 } 207 }
192 208
193 if (standardCols & CommitHash) { 209 if (standardCols & CommitHash) {
diff --git a/tests/hawd/dataset.h b/tests/hawd/dataset.h
index bb2aae5..9660709 100644
--- a/tests/hawd/dataset.h
+++ b/tests/hawd/dataset.h
@@ -47,7 +47,9 @@ public:
47 Row(const Row &other); 47 Row(const Row &other);
48 Row &operator=(const Row &rhs); 48 Row &operator=(const Row &rhs);
49 void setValue(const QString &column, const QVariant &value); 49 void setValue(const QString &column, const QVariant &value);
50 QVariant value(const QString &column); 50 QVariant value(const QString &column) const;
51 QString commitHash() const;
52 QDateTime timestamp() const;
51 void annotate(const QString &note); 53 void annotate(const QString &note);
52 void setCommitHash(const QString &hash); 54 void setCommitHash(const QString &hash);
53 qint64 key() const; 55 qint64 key() const;
diff --git a/tests/hawd/module.cpp b/tests/hawd/module.cpp
index 7753b44..0fcbfc6 100644
--- a/tests/hawd/module.cpp
+++ b/tests/hawd/module.cpp
@@ -22,6 +22,7 @@
22#include "modules/list.h" 22#include "modules/list.h"
23#include "modules/check.h" 23#include "modules/check.h"
24#include "modules/print.h" 24#include "modules/print.h"
25#include "modules/json.h"
25 26
26#include <QCoreApplication> 27#include <QCoreApplication>
27 28
@@ -53,6 +54,7 @@ void Module::loadModules()
53 addModule(Check()); 54 addModule(Check());
54 addModule(CheckAll()); 55 addModule(CheckAll());
55 addModule(Print()); 56 addModule(Print());
57 addModule(Json());
56// addModule(Annotate()); 58// addModule(Annotate());
57// addModule(Remove()); 59// addModule(Remove());
58} 60}
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
diff --git a/tests/hawd/modules/json.h b/tests/hawd/modules/json.h
new file mode 100644
index 0000000..75f7171
--- /dev/null
+++ b/tests/hawd/modules/json.h
@@ -0,0 +1,37 @@
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#pragma once
21
22#include "module.h"
23
24namespace HAWD
25{
26
27class Json : public Module
28{
29public:
30 Json();
31
32private:
33 static bool toJson(const QStringList &commands, State &state);
34};
35
36} // namespace HAWD
37