CtaAdminTextFormatter.cpp 27.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*!
 * @project        The CERN Tape Archive (CTA)
 * @brief          Text formatter for CTA Admin command tool
 * @description    CTA Admin command using Google Protocol Buffers and XRootD SSI transport
 * @copyright      Copyright 2017 CERN
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
 *
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
 *
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <iostream>
#include <iomanip>
#include <cmdline/CtaAdminTextFormatter.hpp>
23
#include <common/checksum/ChecksumBlobSerDeser.hpp>
24
25
#include <common/dataStructures/DriveStatusSerDeser.hpp>
#include <common/dataStructures/MountTypeSerDeser.hpp>
26

27
28
#include "common/utils/utils.hpp"

29
30
namespace cta {
namespace admin {
31
  
32
33
34
35
/**
 ** Generic utility methods
 **/

36
37
std::string TextFormatter::doubleToStr(double value, char unit) {
  std::stringstream ss;
38
39
  ss << std::fixed << std::setprecision(1) << value;
  if(unit != '\0') ss << unit;
40
41
42
43
  return ss.str();
}


44
45
46
47
48
49
50
51
52
53
54
std::string TextFormatter::timeToStr(const time_t &unixtime) {
  struct tm timeTm;
  localtime_r(&unixtime, &timeTm);

  char timeStr[17]; // YYYY-MM-DD HH:MM
  strftime(timeStr, 17, "%F %R", &timeTm);

  return timeStr;
}


55
std::string TextFormatter::dataSizeToStr(uint64_t value) {
56
  const std::vector<char> suffix = { 'K', 'M', 'G', 'T', 'P', 'E' };
57
58
59
60

  // Simple case, values less than 1000 bytes don't take a suffix
  if(value < 1000) return std::to_string(value);

61
62
  // Find the correct scaling, starting at 1 KB and working up. I'm assuming we won't have zettabytes
  // or yottabytes of data in a tapepool anytime soon.
63
  int unit;
64
65
  uint64_t divisor;
  for(unit = 0, divisor = 1000; unit < 6 && value >= divisor*1000; divisor *= 1000, ++unit) ;
66
67
68

  // Convert to format like "3.1G"
  double val_d = static_cast<double>(value) / static_cast<double>(divisor);
69
  return doubleToStr(val_d, suffix[unit]);
70
71
}

72

73
74
75
void TextFormatter::flush() {
  if(m_outputBuffer.empty()) return;

76
77
78
79
80
81
  // Check if first line is a header requiring special formatting
  bool is_header = false;
  if(m_outputBuffer.front().size() == 1 && m_outputBuffer.front().front() == "HEADER") {
    is_header = true;
    m_outputBuffer.erase(m_outputBuffer.begin());
    if(m_outputBuffer.empty()) return;
82
83
    auto numCols = m_outputBuffer.front().size();
    m_colSize.resize(numCols);
84
85
  }

86
87
  // Calculate column widths
  for(auto &l : m_outputBuffer) {
88
    if(l.size() != m_colSize.size()) throw std::runtime_error("TextFormatter::flush(): incorrect number of columns");
89
    for(size_t c = 0; c < l.size(); ++c) {
90
      if(m_colSize.at(c) < l.at(c).size()) m_colSize[c] = l.at(c).size();
91
92
93
94
95
    }
  }

  // Output columns
  for(auto &l : m_outputBuffer) {
96
    if(is_header) { std::cout << TEXT_RED; }
97
    for(size_t c = 0; c < l.size(); ++c) {
98
99
100
101
      //We inserted a prefix to spot drive reasons output
      bool isReason = l.at(c).find(REASON_PREFIX) != std::string::npos;
      // flush right, except for paths and drive reasons, which are flush left
      auto flush = ((is_header && l.at(c) == "path") || (is_header && l.at(c) == "reason") || isReason ||
102
103
                     l.at(c).substr(0,1)  == "/") ? std::left : std::right;

104
105
      if(isReason)
        l.at(c).erase(0,strlen(REASON_PREFIX));
106
      std::cout << std::setfill(' ')
107
                << std::setw(m_colSize.at(c))
108
                << flush
109
110
                << (l.at(c).empty() ? "-" : l.at(c))
                << ' ';
111
    }
112
    if(is_header) { std::cout << TEXT_NORMAL; is_header = false; }
113
114
115
116
117
118
119
120
    std::cout << std::endl;
  }

  // Empty buffer
  m_outputBuffer.clear();
}


121
122
123
124
/**
 ** Output for specific commands
 **/

125
void TextFormatter::printAdminLsHeader() {
126
127
128
129
130
131
132
133
134
135
136
137
138
  push_back("HEADER");
  push_back(
    "user",
    "c.user",
    "c.host", 
    "c.time",
    "m.user",
    "m.host",
    "m.time",
    "comment"
  );
}

139
void TextFormatter::print(const AdminLsItem &adls_item) {
140
141
142
143
144
145
146
147
148
149
150
151
  push_back(
    adls_item.user(),
    adls_item.creation_log().username(),
    adls_item.creation_log().host(),
    timeToStr(adls_item.creation_log().time()),
    adls_item.last_modification_log().username(),
    adls_item.last_modification_log().host(),
    timeToStr(adls_item.last_modification_log().time()),
    adls_item.comment()
  );
}

152
void TextFormatter::printArchiveFileLsHeader() {
153
  push_back("HEADER");
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  push_back(
    "archive id",
    "copy no",
    "vid",
    "fseq",
    "block id",
    "instance",
    "disk id",
    "size",
    "checksum type",
    "checksum value",
    "storage class",
    "owner",
    "group",
    "creation time",
169
170
    "sc vid", // superceded
    "sc fseq",
171
172
173
174
    "path"
  );
}

175
void TextFormatter::print(const ArchiveFileLsItem &afls_item) {
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  using namespace cta::checksum;

  std::string checksumType("NONE");
  std::string checksumValue;

  ChecksumBlob csb;
  ProtobufToChecksumBlob(afls_item.af().csb(), csb);

  // Files can have multiple checksums of different types. Display only the first checksum here. All
  // checksums will be listed in JSON.
  if(!csb.empty()) {
    auto cs_it = csb.getMap().begin();
    checksumType = ChecksumTypeName.at(cs_it->first);
    checksumValue = "0x" + ChecksumBlob::ByteArrayToHex(cs_it->second);
  }

192
193
194
195
196
197
198
199
  push_back(
    afls_item.af().archive_id(),
    afls_item.copy_nb(),
    afls_item.tf().vid(),
    afls_item.tf().f_seq(),
    afls_item.tf().block_id(),
    afls_item.af().disk_instance(),
    afls_item.af().disk_id(),
200
    dataSizeToStr(afls_item.af().size()),
201
202
    checksumType,
    checksumValue,
203
    afls_item.af().storage_class(),
204
205
    afls_item.af().df().owner_id().uid(),
    afls_item.af().df().owner_id().gid(),
206
    timeToStr(afls_item.af().creation_time()),
207
208
    afls_item.tf().superseded_by_vid(),
    afls_item.tf().superseded_by_f_seq(),
209
210
    afls_item.af().df().path()
  );
211
212
}

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
void TextFormatter::printArchiveFileLsSummaryHeader() {
  push_back("HEADER");
  push_back(
    "total files",
    "total size"
  );
}

void TextFormatter::print(const ArchiveFileLsSummary &afls_summary)
{
  push_back(
    afls_summary.total_files(),
    dataSizeToStr(afls_summary.total_size())
  );
}

void TextFormatter::printArchiveRouteLsHeader() {
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  push_back("HEADER");
  push_back(
    "storage class",
    "copy number",
    "tapepool",
    "c.user",
    "c.host",
    "c.time",
    "m.user",
    "m.host",
    "m.time",
    "comment"
  );
}

245
void TextFormatter::print(const ArchiveRouteLsItem &arls_item) {
246
247
248
249
250
251
  push_back(
    arls_item.storage_class(),
    arls_item.copy_number(),
    arls_item.tapepool(),
    arls_item.creation_log().username(),
    arls_item.creation_log().host(),
252
    timeToStr(arls_item.creation_log().time()),
253
254
    arls_item.last_modification_log().username(),
    arls_item.last_modification_log().host(),
255
    timeToStr(arls_item.last_modification_log().time()),
256
257
258
259
    arls_item.comment()
  );
}

260
261

void TextFormatter::printDriveLsHeader() {
262
  push_back("HEADER");
263
  push_back(
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
    "library",
    "drive",
    "host",
    "desired",
    "request",
    "status",
    "since",
    "vid",
    "tapepool",
    "files",
    "data",
    "MB/s",
    "session",
    "priority",
    "activity",
279
280
    "age",
    "reason"
281
  );
282
283
}

284
void TextFormatter::print(const DriveLsItem &drls_item)
285
{
286
  //using namespace cta::common::dataStructures;
287
288
289
290
291
292
293
294
295
296
297

  const int DRIVE_TIMEOUT = 600; // Time after which a drive will be marked as STALE

  std::string driveStatusSince;
  std::string filesTransferredInSession;
  std::string bytesTransferredInSession;
  std::string latestBandwidth;
  std::string sessionId;
  std::string timeSinceLastUpdate;


298
  if(drls_item.drive_status() != DriveLsItem::UNKNOWN_DRIVE_STATUS) {
299
300
301
    driveStatusSince = std::to_string(drls_item.drive_status_since());
  }

302
  if(drls_item.drive_status() == DriveLsItem::TRANSFERRING) {
303
304
    filesTransferredInSession = std::to_string(drls_item.files_transferred_in_session());
    bytesTransferredInSession = dataSizeToStr(drls_item.bytes_transferred_in_session());
305
306
    double bandwidth = static_cast<double>(drls_item.latest_bandwidth())/static_cast<double>(1000000);
    latestBandwidth = doubleToStr(bandwidth,'\0');
307
308
  }

309
310
311
  if(drls_item.drive_status() != DriveLsItem::UP &&
     drls_item.drive_status() != DriveLsItem::DOWN &&
     drls_item.drive_status() != DriveLsItem::UNKNOWN_DRIVE_STATUS) {
312
313
314
315
316
317
    sessionId = std::to_string(drls_item.session_id());
  }

  timeSinceLastUpdate = std::to_string(drls_item.time_since_last_update()) +
    (drls_item.time_since_last_update() > DRIVE_TIMEOUT ? " [STALE]" : "");

318
  //If there is a reason, we only want to display the beginning
319
320
  std::string reason = REASON_PREFIX + cta::utils::postEllipsis(drls_item.reason(),NB_CHAR_REASON_DRIVE);
  
321
  push_back(
322
323
324
    drls_item.logical_library(),
    drls_item.drive_name(),
    drls_item.host(),
325
326
327
    (drls_item.desired_drive_state() == DriveLsItem::UP ? "Up" : "Down"),
    toString(ProtobufToMountType(drls_item.mount_type())),
    toString(ProtobufToDriveStatus(drls_item.drive_status())),
328
329
330
331
332
333
334
335
336
    driveStatusSince,
    drls_item.vid(),
    drls_item.tapepool(),
    filesTransferredInSession,
    bytesTransferredInSession,
    latestBandwidth,
    sessionId,
    drls_item.current_priority(),
    drls_item.current_activity(),
337
338
    timeSinceLastUpdate,
    reason
339
  );
340
341
}

342
void TextFormatter::printFailedRequestLsHeader() {
343
  push_back("HEADER");
344
345
346
347
348
349
350
351
  push_back(
    "request type",
    "copy no",
    "tapepool/vid",
    "requester",
    "group",
    "path"
  );
352
353
}

354
void TextFormatter::print(const FailedRequestLsItem &frls_item) {
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  std::string request_type;
  std::string tapepool_vid;

  switch(frls_item.request_type()) {
    case admin::RequestType::ARCHIVE_REQUEST:
      request_type = "archive";
      tapepool_vid = frls_item.tapepool();
      break;
    case admin::RequestType::RETRIEVE_REQUEST:
      request_type = "retrieve";
      tapepool_vid = frls_item.tf().vid();
      break;
    default:
      throw std::runtime_error("Unrecognised request type: " + std::to_string(frls_item.request_type()));
  }
370

371
372
373
374
375
376
377
378
379
  push_back(
    request_type,
    frls_item.copy_nb(),
    tapepool_vid,
    frls_item.requester().username(),
    frls_item.requester().groupname(),
    frls_item.af().df().path()
  );

380
381
  // Note: failure log messages are available in frls_item.failurelogs(). These are not currently
  //       displayed in the text output, only in JSON.
382
383
}

384
void TextFormatter::printFailedRequestLsSummaryHeader() {
385
  push_back("HEADER");
386
387
388
  push_back(
    "request type",
    "total files",
389
    "total size"
390
  );
391
392
}

393
void TextFormatter::print(const FailedRequestLsSummary &frls_summary) {
394
  std::string request_type =
395
396
    frls_summary.request_type() == RequestType::ARCHIVE_REQUEST  ? "archive" :
    frls_summary.request_type() == RequestType::RETRIEVE_REQUEST ? "retrieve" : "total";
397
398
399
400

  push_back(
    request_type,
    frls_summary.total_files(),
401
    dataSizeToStr(frls_summary.total_size())
402
  );
403
404
}

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
void TextFormatter::printGroupMountRuleLsHeader() {
  push_back("HEADER");
  push_back(
    "instance",
    "group",
    "policy",
    "c.user",
    "c.host",
    "c.time",
    "m.user",
    "m.host",
    "m.time",
    "comment"
  );
}

void TextFormatter::print(const GroupMountRuleLsItem &gmrls_item) {
  push_back(
    gmrls_item.disk_instance(),
    gmrls_item.group_mount_rule(),
    gmrls_item.mount_policy(),
    gmrls_item.creation_log().username(),
    gmrls_item.creation_log().host(),
    timeToStr(gmrls_item.creation_log().time()),
    gmrls_item.last_modification_log().username(),
    gmrls_item.last_modification_log().host(),
    timeToStr(gmrls_item.last_modification_log().time()),
    gmrls_item.comment()
  );
}

436
void TextFormatter::printListPendingArchivesHeader() {
437
  push_back("HEADER");
438
439
440
441
442
443
444
445
446
447
448
449
450
451
  push_back(
    "tapepool",
    "archive id",
    "storage class",
    "copy no",
    "disk id",
    "instance",
    "checksum type",
    "checksum value",
    "size",
    "user",
    "group",
    "path"
  );
452
453
}

454
void TextFormatter::print(const ListPendingArchivesItem &lpa_item) {
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
  using namespace cta::checksum;

  std::string checksumType("NONE");
  std::string checksumValue;

  ChecksumBlob csb;
  ProtobufToChecksumBlob(lpa_item.af().csb(), csb);

  // Files can have multiple checksums of different types. Display only the first checksum here. All
  // checksums will be listed in JSON.
  if(!csb.empty()) {
    auto cs_it = csb.getMap().begin();
    checksumType = ChecksumTypeName.at(cs_it->first);
    checksumValue = "0x" + ChecksumBlob::ByteArrayToHex(cs_it->second);
  }

471
472
473
474
475
476
477
  push_back(
    lpa_item.tapepool(),
    lpa_item.af().archive_id(),
    lpa_item.af().storage_class(),
    lpa_item.copy_nb(),
    lpa_item.af().disk_id(),
    lpa_item.af().disk_instance(),
478
479
    checksumType,
    checksumValue,
480
    dataSizeToStr(lpa_item.af().size()),
481
482
    lpa_item.af().df().owner_id().uid(),
    lpa_item.af().df().owner_id().gid(),
483
484
    lpa_item.af().df().path()
  );
485
486
}

487
void TextFormatter::printListPendingArchivesSummaryHeader() {
488
  push_back("HEADER");
489
490
491
492
493
  push_back(
    "tapepool",
    "total files",
    "total size"
  );
494
495
}

496
void TextFormatter::print(const ListPendingArchivesSummary &lpa_summary) {
497
498
499
  push_back(
    lpa_summary.tapepool(),
    lpa_summary.total_files(),
500
    dataSizeToStr(lpa_summary.total_size())
501
  );
502
503
}

504
void TextFormatter::printListPendingRetrievesHeader() {
505
  push_back("HEADER");
506
507
508
509
510
511
512
513
514
515
516
  push_back(
    "vid",
    "archive id",
    "copy no",
    "fseq",
    "block id",
    "size",
    "user",
    "group",
    "path"
  );
517
518
}

519
void TextFormatter::print(const ListPendingRetrievesItem &lpr_item) {
520
521
522
523
524
525
  push_back(
    lpr_item.tf().vid(),
    lpr_item.af().archive_id(),
    lpr_item.copy_nb(),
    lpr_item.tf().f_seq(),
    lpr_item.tf().block_id(),
526
    dataSizeToStr(lpr_item.af().size()),
527
528
    lpr_item.af().df().owner_id().uid(),
    lpr_item.af().df().owner_id().gid(),
529
530
    lpr_item.af().df().path()
  );
531
532
}

533
void TextFormatter::printListPendingRetrievesSummaryHeader() {
534
  push_back("HEADER");
535
536
537
538
539
  push_back(
    "vid",
    "total files",
    "total size"
  );
540
541
}

542
void TextFormatter::print(const ListPendingRetrievesSummary &lpr_summary) {
543
544
545
  push_back(
    lpr_summary.vid(),
    lpr_summary.total_files(),
546
    dataSizeToStr(lpr_summary.total_size())
547
  );
548
549
}

550
551
552
void TextFormatter::printLogicalLibraryLsHeader() {
  push_back("HEADER");
  push_back(
553
    "library",
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
    "disabled",
    "c.user",
    "c.host",
    "c.time",
    "m.user",
    "m.host",
    "m.time",
    "comment"
  );
}

void TextFormatter::print(const LogicalLibraryLsItem &llls_item) {
  push_back(
    llls_item.name(),
    llls_item.is_disabled(),
    llls_item.creation_log().username(),
    llls_item.creation_log().host(),
571
    timeToStr(llls_item.creation_log().time()),
572
573
    llls_item.last_modification_log().username(),
    llls_item.last_modification_log().host(),
574
    timeToStr(llls_item.last_modification_log().time()),
575
576
577
578
    llls_item.comment()
  );
}

579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
void TextFormatter::printMediaTypeLsHeader() {
  push_back("HEADER");
  push_back(
    "media type",
    "cartridge",
    "capacity",
    "primary density code",
    "secondary density code",
    "number of wraps",
    "min LPos",
    "max LPos",
    "c.user",
    "c.host",
    "c.time",
    "m.user",
    "m.host",
    "m.time",
    "comment"
  );
}

void TextFormatter::print(const MediaTypeLsItem &mtls_item) {
  push_back(
    mtls_item.name(),
    mtls_item.cartridge(),
    mtls_item.capacity(),
    mtls_item.primary_density_code(),
    mtls_item.secondary_density_code(),
    mtls_item.number_of_wraps(),
    mtls_item.min_lpos(),
    mtls_item.max_lpos(),
    mtls_item.creation_log().username(),
    mtls_item.creation_log().host(),
    timeToStr(mtls_item.creation_log().time()),
    mtls_item.last_modification_log().username(),
    mtls_item.last_modification_log().host(),
    timeToStr(mtls_item.last_modification_log().time()),
    mtls_item.comment()
  );
}

620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
void TextFormatter::printMountPolicyLsHeader() {
  push_back("HEADER");
  push_back(
    "mount policy",
    "a.priority",
    "a.minAge",
    "r.priority",
    "r.minAge",
    "max drives",
    "c.user",
    "c.host",
    "c.time",
    "m.user",
    "m.host",
    "m.time",
    "comment"
  );
}

void TextFormatter::print(const MountPolicyLsItem &mpls_item) {
  push_back(
    mpls_item.name(),
    mpls_item.archive_priority(),
    mpls_item.archive_min_request_age(),
    mpls_item.retrieve_priority(),
    mpls_item.retrieve_min_request_age(),
    mpls_item.max_drives_allowed(),
    mpls_item.creation_log().username(),
    mpls_item.creation_log().host(),
649
    timeToStr(mpls_item.creation_log().time()),
650
651
    mpls_item.last_modification_log().username(),
    mpls_item.last_modification_log().host(),
652
    timeToStr(mpls_item.last_modification_log().time()),
653
654
655
656
    mpls_item.comment()
  );
}

657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
void TextFormatter::printRepackLsHeader() {
  push_back("HEADER");
  push_back(
    "vid",
    "repackBufferURL",
    "userProvidedFiles",
    "totalFilesToRetrieve",
    "totalBytesToRetrieve",
    "totalFilesToArchive",
    "totalBytesToArchive",
    "retrievedFiles",
    "archivedFiles",
    "failedToRetrieveFiles",
    "failedToRetrieveBytes",
    "failedToArchiveFiles",
    "failedToArchiveBytes",
    "lastExpandedFSeq",
    "status"
  );
}

void TextFormatter::print(const RepackLsItem &rels_item) {
  push_back(
   rels_item.vid(),
   rels_item.repack_buffer_url(),
   rels_item.user_provided_files(),
   rels_item.total_files_to_retrieve(),
   dataSizeToStr(rels_item.total_bytes_to_retrieve()),
   rels_item.total_files_to_archive(),
   dataSizeToStr(rels_item.total_bytes_to_archive()),
   rels_item.retrieved_files(),
   rels_item.archived_files(),
   rels_item.failed_to_retrieve_files(),
   dataSizeToStr(rels_item.failed_to_retrieve_bytes()),
   rels_item.failed_to_archive_files(),
692
   dataSizeToStr(rels_item.failed_to_archive_bytes()),
693
694
695
696
697
   rels_item.last_expanded_fseq(),
   rels_item.status()
  );
}

698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
void TextFormatter::printRequesterMountRuleLsHeader() {
  push_back("HEADER");
  push_back(
    "instance",
    "username",
    "policy",
    "c.user",
    "c.host",
    "c.time",
    "m.user",
    "m.host",
    "m.time",
    "comment"
  );
}

void TextFormatter::print(const RequesterMountRuleLsItem &rmrls_item) {
  push_back(
    rmrls_item.disk_instance(),
    rmrls_item.requester_mount_rule(),
    rmrls_item.mount_policy(),
    rmrls_item.creation_log().username(),
    rmrls_item.creation_log().host(),
    timeToStr(rmrls_item.creation_log().time()),
    rmrls_item.last_modification_log().username(),
    rmrls_item.last_modification_log().host(),
    timeToStr(rmrls_item.last_modification_log().time()),
    rmrls_item.comment()
  );
}

729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
void TextFormatter::printShowQueuesHeader() {
  push_back("HEADER");
  push_back(
    "type",
    "tapepool",
    "logical library",
    "vid",
    "files queued",
    "data queued",
    "oldest age",
    "priority",
    "min age",
    "max drives",
    "cur. mounts",
    "cur. files",
    "cur. data",
    "MB/s",
    "next mounts",
    "tapes capacity",
    "files on tapes",
    "data on tapes",
    "full tapes",
    "empty tapes",
    "disabled tapes",
753
    "rdonly tapes",
754
755
756
757
758
759
760
761
762
    "writable tapes"
  );
}

void TextFormatter::print(const ShowQueuesItem &sq_item) {
  std::string priority;
  std::string minAge;
  std::string maxDrivesAllowed;

763
  if(sq_item.mount_type() == ARCHIVE_FOR_USER || sq_item.mount_type() == ARCHIVE_FOR_REPACK || 
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
     sq_item.mount_type() == RETRIEVE) {
    priority         = std::to_string(sq_item.priority());
    minAge           = std::to_string(sq_item.min_age());
    maxDrivesAllowed = std::to_string(sq_item.max_drives());
  }

  push_back(
    toString(ProtobufToMountType(sq_item.mount_type())),
    sq_item.tapepool(),
    sq_item.logical_library(),
    sq_item.vid(),
    sq_item.queued_files(),
    dataSizeToStr(sq_item.queued_bytes()),
    sq_item.oldest_age(),
    priority,
    minAge,
    maxDrivesAllowed,
    sq_item.cur_mounts(),
    sq_item.cur_files(),
    dataSizeToStr(sq_item.cur_bytes()),
    sq_item.bytes_per_second() / 1000000,
    sq_item.next_mounts(),
    dataSizeToStr(sq_item.tapes_capacity()),
    sq_item.tapes_files(),
    dataSizeToStr(sq_item.tapes_bytes()),
    sq_item.full_tapes(),
    sq_item.empty_tapes(),
    sq_item.disabled_tapes(),
792
    sq_item.rdonly_tapes(),
793
794
795
796
797
798
799
800
801
    sq_item.writable_tapes()
  );
}

void TextFormatter::printStorageClassLsHeader() {
  push_back("HEADER");
  push_back(
    "storage class",
    "number of copies",
802
    "vo",
803
804
805
806
807
808
809
810
811
812
813
814
815
816
    "c.user",
    "c.host",
    "c.time",
    "m.user",
    "m.host",
    "m.time",
    "comment"
  );
}

void TextFormatter::print(const StorageClassLsItem &scls_item) {
  push_back(
    scls_item.name(),
    scls_item.nb_copies(),
817
    scls_item.vo(),
818
819
    scls_item.creation_log().username(),
    scls_item.creation_log().host(),
820
    timeToStr(scls_item.creation_log().time()),
821
822
    scls_item.last_modification_log().username(),
    scls_item.last_modification_log().host(),
823
    timeToStr(scls_item.last_modification_log().time()),
824
825
826
827
    scls_item.comment()
  );
}

828
void TextFormatter::printTapeLsHeader() {
829
  push_back("HEADER");
830
831
832
833
  push_back(
    "vid",
    "media type",
    "vendor",
834
    "library",
835
836
    "tapepool",
    "vo",
837
    "encryption key name",
838
839
840
841
842
    "capacity",
    "occupancy",
    "last fseq",
    "full",
    "disabled",
843
844
    "rdonly",
    "from castor",
845
846
847
848
    "label drive",
    "label time",
    "last w drive",
    "last w time",
849
    "w mounts",
850
851
    "last r drive",
    "last r time",
852
    "r mounts",
853
854
855
856
857
    "c.user",
    "c.host",
    "c.time",
    "m.user",
    "m.host",
858
859
    "m.time",
    "comment"
860
  );
861
862
}

863
void TextFormatter::print(const TapeLsItem &tals_item) {
864
865
866
867
868
869
870
  push_back(
    tals_item.vid(),
    tals_item.media_type(),
    tals_item.vendor(),
    tals_item.logical_library(),
    tals_item.tapepool(),
    tals_item.vo(),
871
    tals_item.encryption_key_name(),
872
873
    dataSizeToStr(tals_item.capacity()),
    dataSizeToStr(tals_item.occupancy()),
874
875
876
    tals_item.last_fseq(),
    tals_item.full(),
    tals_item.disabled(),
877
    tals_item.rdonly(),
878
    tals_item.from_castor(),
879
880
881
882
    tals_item.has_label_log()        ? tals_item.label_log().drive()                  : "",
    tals_item.has_label_log()        ? timeToStr(tals_item.label_log().time())        : "",
    tals_item.has_last_written_log() ? tals_item.last_written_log().drive()           : "",
    tals_item.has_last_written_log() ? timeToStr(tals_item.last_written_log().time()) : "",
883
    tals_item.write_mount_count(),
884
885
    tals_item.has_last_read_log()    ? tals_item.last_read_log().drive()              : "",
    tals_item.has_last_read_log()    ? timeToStr(tals_item.last_read_log().time())    : "",
886
    tals_item.read_mount_count(),
887
888
    tals_item.creation_log().username(),
    tals_item.creation_log().host(),
889
    timeToStr(tals_item.creation_log().time()),
890
891
    tals_item.last_modification_log().username(),
    tals_item.last_modification_log().host(),
892
893
    timeToStr(tals_item.last_modification_log().time()),
    tals_item.comment()
894
895
  );
}
896

897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
void TextFormatter::printTapeFileLsHeader() {
  push_back("HEADER");
  push_back(
    "archive id",
    "copy no",
    "vid",
    "fseq",
    "block id",
    "instance",
    "disk id",
    "size",
    "checksum type",
    "checksum value",
    "storage class",
    "owner",
    "group",
    "creation time",
    "sc vid", // superceded
    "sc fseq",
    "path"
  );
}

void TextFormatter::print(const TapeFileLsItem &tfls_item) {
921
922
  // Files can have multiple checksums of different types. The tabular output will
  // display only the first checksum; the JSON output will list all checksums.
923
924
925
  std::string checksumType("NONE");
  std::string checksumValue;

926
927
928
929
  if(!tfls_item.af().checksum().empty()) {
    const google::protobuf::EnumDescriptor *descriptor = cta::common::ChecksumBlob::Checksum::Type_descriptor();
    std::string name = descriptor->FindValueByNumber(tfls_item.af().checksum().begin()->type())->name();
    checksumValue = "0x" + tfls_item.af().checksum().begin()->value();
930
931
932
933
  }

  push_back(
    tfls_item.af().archive_id(),
934
    tfls_item.tf().copy_nb(),
935
936
937
    tfls_item.tf().vid(),
    tfls_item.tf().f_seq(),
    tfls_item.tf().block_id(),
938
939
    tfls_item.df().disk_instance(),
    tfls_item.df().disk_id(),
940
941
942
943
    dataSizeToStr(tfls_item.af().size()),
    checksumType,
    checksumValue,
    tfls_item.af().storage_class(),
944
945
    tfls_item.df().owner_id().uid(),
    tfls_item.df().owner_id().gid(),
946
947
948
    timeToStr(tfls_item.af().creation_time()),
    tfls_item.tf().superseded_by_vid(),
    tfls_item.tf().superseded_by_f_seq(),
949
    tfls_item.df().path()
950
951
952
  );
}

953
void TextFormatter::printTapePoolLsHeader() {
954
  push_back("HEADER");
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
  push_back(
    "name",
    "vo",
    "#tapes",
    "#partial",
    "#phys files",
    "size",
    "used",
    "avail",
    "use%",
    "encrypt",
    "supply",
    "c.user",
    "c.host",
    "c.time",
    "m.user",
    "m.host",
    "m.time",
    "comment"
  );
975
976
}

977
void TextFormatter::print(const TapePoolLsItem &tpls_item)
978
{
979
980
981
982
983
984
985
986
987
988
989
990
  uint64_t avail = tpls_item.capacity_bytes() > tpls_item.data_bytes() ?
    tpls_item.capacity_bytes()-tpls_item.data_bytes() : 0; 

  double use_percent = tpls_item.capacity_bytes() > 0 ?
    (static_cast<double>(tpls_item.data_bytes())/static_cast<double>(tpls_item.capacity_bytes()))*100.0 : 0.0;

  push_back(
    tpls_item.name(),
    tpls_item.vo(),
    tpls_item.num_tapes(),
    tpls_item.num_partial_tapes(),
    tpls_item.num_physical_files(),
991
992
993
994
    dataSizeToStr(tpls_item.capacity_bytes()),
    dataSizeToStr(tpls_item.data_bytes()),
    dataSizeToStr(avail),
    doubleToStr(use_percent, '%'),
995
996
997
998
    tpls_item.encrypt(),
    tpls_item.supply(),
    tpls_item.created().username(),
    tpls_item.created().host(),
999
    timeToStr(tpls_item.created().time()),
1000
    tpls_item.modified().username(),