summaryrefslogtreecommitdiffstats
path: root/sinksh/syntax_modules/sink_stat.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sinksh/syntax_modules/sink_stat.cpp')
-rw-r--r--sinksh/syntax_modules/sink_stat.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/sinksh/syntax_modules/sink_stat.cpp b/sinksh/syntax_modules/sink_stat.cpp
new file mode 100644
index 0000000..06586d9
--- /dev/null
+++ b/sinksh/syntax_modules/sink_stat.cpp
@@ -0,0 +1,120 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
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 <QDebug>
21#include <QObject> // tr()
22#include <QTimer>
23#include <QDir>
24
25#include "common/resource.h"
26#include "common/storage.h"
27#include "common/domain/event.h"
28#include "common/domain/folder.h"
29#include "common/resourceconfig.h"
30#include "common/log.h"
31#include "common/storage.h"
32#include "common/definitions.h"
33
34#include "sinksh_utils.h"
35#include "state.h"
36#include "syntaxtree.h"
37
38namespace SinkStat
39{
40
41void statResources(const QStringList &resources, const State &state)
42{
43 qint64 total = 0;
44 for (const auto &resource : resources) {
45 Sink::Storage storage(Sink::storageLocation(), resource, Sink::Storage::ReadOnly);
46 auto transaction = storage.createTransaction(Sink::Storage::ReadOnly);
47
48 QList<QByteArray> databases = transaction.getDatabaseNames();
49 for (const auto &databaseName : databases) {
50 state.printLine(QObject::tr("Database: %1").arg(QString(databaseName)), 1);
51 auto db = transaction.openDatabase(databaseName);
52 qint64 size = db.getSize() / 1024;
53 state.printLine(QObject::tr("Size [kb]: %1").arg(size), 1);
54 total += size;
55 }
56 int diskUsage = 0;
57
58 QDir dir(Sink::storageLocation());
59 for (const auto &folder : dir.entryList(QStringList() << resource + "*")) {
60 diskUsage += Sink::Storage(Sink::storageLocation(), folder, Sink::Storage::ReadOnly).diskUsage();
61 }
62 auto size = diskUsage / 1024;
63 state.printLine(QObject::tr("Disk usage [kb]: %1").arg(size), 1);
64 }
65
66 state.printLine(QObject::tr("Total [kb]: %1").arg(total));
67}
68
69bool statAllResources(State &state)
70{
71 Sink::Query query;
72 query.liveQuery = false;
73 auto model = SinkshUtils::loadModel("resource", query);
74
75 //SUUUPER ugly, but can't think of a better way with 2 glasses of wine in me on Christmas day
76 static QStringList resources;
77 resources.clear();
78
79 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model](const QModelIndex &index, int start, int end) mutable {
80 for (int i = start; i <= end; i++) {
81 auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>();
82 resources << object->identifier();
83 }
84 });
85
86 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
87 if (roles.contains(Sink::Store::ChildrenFetchedRole)) {
88 statResources(resources, state);
89 state.commandFinished();
90 }
91 });
92
93 if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) {
94 return true;
95 }
96
97 return false;
98}
99
100bool stat(const QStringList &args, State &state)
101{
102 if (args.isEmpty()) {
103 return statAllResources(state);
104 }
105
106 statResources(args, state);
107 return false;
108}
109
110Syntax::List syntax()
111{
112 Syntax state("stat", QObject::tr("Shows database usage for the resources requested"), &SinkStat::stat, Syntax::EventDriven);
113 state.completer = &SinkshUtils::resourceCompleter;
114
115 return Syntax::List() << state;
116}
117
118REGISTER_SYNTAX(SinkStat)
119
120}