File indexing completed on 2024-12-15 04:56:11

0001 /*
0002  *   Copyright (C) 2017 Christian Mollekopf <mollekopf@kolabsystems.com>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 
0020 #include "json.h"
0021 
0022 #include "../datasetdefinition.h"
0023 #include "../dataset.h"
0024 
0025 #include <QDir>
0026 #include <QObject>
0027 #include <QJsonDocument>
0028 #include <QJsonObject>
0029 #include <QJsonArray>
0030 #include <QDateTime>
0031 
0032 #include <iostream>
0033 
0034 namespace HAWD
0035 {
0036 
0037 Json::Json()
0038     : Module()
0039 {
0040     Syntax top("json", &Json::toJson);
0041     setSyntax(top);
0042     setDescription(QObject::tr("Prints a table from a dataset to json; you can provide a list of rows to output"));
0043 }
0044 
0045 bool Json::toJson(const QStringList &commands, State &state)
0046 {
0047     if (commands.isEmpty()) {
0048         std::cout << QObject::tr("print requires a dataset to be named").toStdString() << std::endl;
0049         return true;
0050     }
0051 
0052     const QString datasetName = commands.first();
0053 
0054     QDir project(state.projectPath());
0055     Dataset dataset(datasetName, state);
0056 
0057     if (!dataset.isValid()) {
0058         std::cout << QObject::tr("The dataset %1 could not be loaded; try checking it with the check command").arg(datasetName).toStdString() << std::endl;
0059         return true;
0060     }
0061 
0062     auto definition = state.datasetDefinition(datasetName);
0063 
0064     QJsonObject json;
0065     json.insert("dataset", datasetName);
0066     json.insert("name", definition.name());
0067     json.insert("description", definition.description());
0068 
0069     const auto columns = definition.columns();
0070 
0071     QJsonArray array;
0072     dataset.eachRow(
0073         [&](const Dataset::Row &row) {
0074             QJsonObject jsonRow;
0075             jsonRow.insert("timestamp", QJsonValue::fromVariant(row.timestamp()));
0076             jsonRow.insert("commit", row.commitHash());
0077             QJsonArray columnsArray;
0078             for (const auto &col : columns) {
0079                 QJsonObject columnObject;
0080                 columnObject.insert("unit", QJsonValue::fromVariant(col.second.unit()));
0081                 columnObject.insert("name", QJsonValue::fromVariant(col.first));
0082                 columnObject.insert("value", QJsonValue::fromVariant(row.value(col.first)));
0083                 columnsArray << columnObject;
0084             }
0085             jsonRow.insert("columns", columnsArray);
0086             array.append(jsonRow);
0087         });
0088     json.insert("rows", array);
0089     std::cout << QJsonDocument{json}.toJson().toStdString() << std::endl;
0090     return true;
0091 }
0092 
0093 } // namespace HAWD
0094