-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathGenerate.php
More file actions
173 lines (157 loc) Β· 4.7 KB
/
Generate.php
File metadata and controls
173 lines (157 loc) Β· 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\UserUsageReport\Command;
use OCA\UserUsageReport\Reports\AllUsers;
use OCA\UserUsageReport\Reports\SingleUser;
use OCP\IUserManager;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
/**
* @psalm-api
*/
#[AsCommand(
name: 'usage-report:generate',
description: 'Generate usage report',
hidden: false,
)]
class Generate extends Command {
protected SingleUser $single;
protected AllUsers $all;
protected IUserManager $userManager;
public function __construct(SingleUser $single, AllUsers $all, IUserManager $userManager) {
parent::__construct();
$this->single = $single;
$this->all = $all;
$this->userManager = $userManager;
}
protected function configure(): void {
$this
->setHelp(
'Print usage information for a given or all users.' . "\n"
. "\n"
. "Example (with --verbose to show the header):\n"
. "\n"
. '"user-id","date as \'c\'","assigned quota (5 GB)","used quota (500 MB)",number of files,number of shares,number of uploads,number of downloads' . "\n"
. '"admin","2017-09-18T09:00:01+00:00",5368709120,786432000,1024,23,1400,5678'
)
->addArgument(
'user-id',
InputArgument::OPTIONAL,
'User to generate the report for, if none is given the report is generated for all users'
)
->addOption(
'field-separator',
'',
InputOption::VALUE_REQUIRED,
'Separator for the fields in the list (Only used when output format is CSV)',
','
)
->addOption(
'date-format',
'',
InputOption::VALUE_REQUIRED,
'Date format of the entries (see http://php.net/manual/en/function.date.php for more information)',
'c'
)
->addOption(
'last-login',
'',
InputOption::VALUE_NONE,
'Should the last login date be included in the report'
)
->addOption(
'display-name',
'',
InputOption::VALUE_NONE,
'Should the display name be included in the report'
)
->addOption(
'output',
'',
InputOption::VALUE_REQUIRED,
'Output format (csv or json)',
'csv'
)
->addOption(
'output-file',
'O',
InputOption::VALUE_REQUIRED,
'Output file for stdout'
)
;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$outputFile = $input->getOption('output-file');
if ($outputFile) {
$stream = fopen($outputFile, 'w');
if ($stream) {
$streamOutput = new StreamOutput($stream, $output->getVerbosity(), null, $output->getFormatter());
} else {
$output->writeln('<error>Unable to open ' . $outputFile . '.</error>');
return 1;
}
} else {
$streamOutput = $output;
}
$userId = $input->getArgument('user-id');
if ($userId) {
if (!$this->userManager->userExists($userId)) {
$output->writeln('<error>User with ID "' . $userId . '" could not be found.</error>');
return 1;
}
}
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$separator = $input->getOption('field-separator');
$header = [];
$header['user_id'] = '';
$data = '"user-id"' . $separator;
if ($input->getOption('display-name')) {
$header['display_name'] = '';
$data .= '"display name"' . $separator;
}
$header['date'] = '';
$data .= '"date as \'' . $input->getOption('date-format') . '\'"' . $separator;
if ($input->getOption('last-login')) {
$header['login'] = '';
$data .= '"last login date as \'' . $input->getOption('date-format') . '\'"' . $separator;
}
$header['quota'] = '';
$data .= '"assigned quota (5 GB)"' . $separator;
$header['used'] = '';
$data .= '"used quota (500 MB)"' . $separator;
$header['files'] = 0;
$data .= 'number of files' . $separator;
$header['shares'] = 0;
$data .= 'number of shares' . $separator;
$header['uploads'] = 0;
$data .= 'number of uploads' . $separator;
$header['downloads'] = 0;
$data .= 'number of downloads';
if ($input->getOption('output') === 'csv') {
$streamOutput->writeln($data);
} else {
$streamOutput->writeln(json_encode($header, JSON_THROW_ON_ERROR));
}
}
if ($input->getArgument('user-id')) {
$this->single->printReport($input, $streamOutput, $userId);
} else {
$this->all->printReport($input, $output, $streamOutput);
}
return 0;
}
}