NCBI C++ ToolKit
ns_cmd_impl.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ns_cmd_impl.cpp 68871 2015-09-11 13:36:32Z sadyrovr $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Dmitry Kazimirov
27  *
28  * File Description: NetSchedule-specific commands - implementation
29  * helpers.
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 
35 #include "ns_cmd_impl.hpp"
36 #include "util.hpp"
37 
38 #define MAX_VISIBLE_DATA_LENGTH 50
39 
40 
42 
44 {
47  else if (IsOptionSet(eClientInfo))
51  else if (IsOptionSet(eAffinityInfo))
54  if (!IsOptionSet(eQueue) &&
56  fprintf(stderr, GRID_APP_NAME ": invalid option combination.\n");
57  return 2;
58  }
59 
62 
65 
67  "Total number of Running and Pending jobs: %u\n" :
68  m_Opts.output_format == eRaw ? "%u\n" :
69  "{\n\t\"active_job_count\": %u\n}\n",
70  st_map["Running"] + st_map["Pending"]);
71  } else if (m_Opts.output_format == eRaw) {
72  string cmd = "STAT JOBS";
73 
74  if (!m_Opts.affinity.empty()) {
75  cmd.append(" aff=");
77  }
78 
79  if (!m_Opts.job_group.empty()) {
80  cmd.append(" group=");
82  }
83 
86  } else {
88 
91 
92  const char* format;
93  const char* format_cont;
94 
96  format_cont = format = "%s: %u\n";
97  else { // Output format is eJSON.
98  printf("{");
99  format = "\n\t\"%s\": %u";
100  format_cont = ",\n\t\"%s\": %u";
101  }
102 
104  if (it->second > 0 || it->first == "Total") {
105  printf(format, it->first.c_str(), it->second);
106  format = format_cont;
107  }
108  }
109 
110  if (m_Opts.output_format == eJSON)
111  printf("\n}\n");
112  }
113  } else {
114  if (m_Opts.output_format != eJSON)
118  else {
120 
123  result.SetByKey((*it).GetServerAddress(),
125 
126  g_PrintJSON(stdout, result);
127  }
128  }
129 
130  return 0;
131 }
132 
134  ENetScheduleStatTopic topic)
135 {
136  if (m_Opts.output_format == eJSON) {
138 
141  result.SetByKey((*it).GetServerAddress(),
143 
144  g_PrintJSON(stdout, result);
145  } else {
146  string cmd(g_GetNetScheduleStatCommand(topic));
147 
148  if (IsOptionSet(eVerbose))
149  cmd.append(" VERBOSE");
150 
153  }
154 }
155 
157 {
158  printf("server_address: %s:%hu\njob_id: %u\n",
159  g_NetService_TryResolveHost(key.host).c_str(), key.port, key.id);
160 
161  if (!key.queue.empty())
162  printf("queue_name: %s\n", key.queue.c_str());
163 }
164 
165 void CPrintJobInfo::BeginJobEvent(const CTempString& event_header)
166 {
167  printf("[%.*s]\n", int(event_header.length()), event_header.data());
168 }
169 
171  const string& attr_value)
172 {
173  printf(" %.*s: %s\n", int(attr_name.length()),
174  attr_name.data(), attr_value.c_str());
175 }
176 
178 {
179  printf(" %.*s\n", int(attr_name.length()), attr_name.data());
180 }
181 
182 void CPrintJobInfo::PrintXput(const string& data, const char* prefix)
183 {
184  _ASSERT(prefix);
185 
186  if (NStr::StartsWith(data, "K "))
187  printf("%s_storage: netcache, key=%s\n", prefix, data.c_str() + 2);
188  else {
189  const char* format;
190  CTempString raw_data;
191 
192  if (NStr::StartsWith(data, "D ")) {
193  format = "embedded";
194  raw_data.assign(data.data() + 2, data.length() - 2);
195  } else {
196  format = "raw";
197  raw_data = data;
198  }
199 
200  printf("%s_storage: %s, size=%lu\n", prefix,
201  format, (unsigned long) raw_data.length());
202 
203  if (data.length() <= MAX_VISIBLE_DATA_LENGTH)
204  printf("%s_%s_data: \"%s\"\n", format, prefix,
205  NStr::PrintableString(raw_data).c_str());
206  else
207  printf("%s_%s_data_preview: \"%s\"...\n", format, prefix,
209  MAX_VISIBLE_DATA_LENGTH)).c_str());
210  }
211 }
212 
214  const CTempString& field_value)
215 {
216  printf("%.*s: %.*s\n", (int) field_name.length(), field_name.data(),
217  (int) field_value.length(), field_value.data());
218 }
219 
220 void CPrintJobInfo::ProcessRawLine(const string& line)
221 {
223 }
224 
CJsonNode g_LegacyStatToJson(CNetServer server, bool verbose)
Definition: util.cpp:72
void g_PrintJSON(FILE *output_stream, CJsonNode node, const char *indent)
Definition: util.cpp:738
#define GRID_APP_NAME
Definition: cgi2rcgi.cpp:63
bool IsOptionSet(int option) const
Definition: grid_cli.hpp:421
struct CGridCommandLineInterfaceApp::SOptions m_Opts
CNetScheduleAdmin m_NetScheduleAdmin
Definition: grid_cli.hpp:450
void PrintNetScheduleStats_Generic(ENetScheduleStatTopic topic)
CNetScheduleAPIExt m_NetScheduleAPI
Definition: grid_cli.hpp:449
static void PrintLine(const string &line)
Definition: grid_cli.cpp:1775
JSON node abstraction.
static CJsonNode NewObjectNode()
Create a new JSON object node.
void PrintServerStatistics(CNcbiOstream &output_stream, EStatisticsOptions opt=eStatisticsBrief)
void StatusSnapshot(TStatusMap &status_map, const string &affinity_token=kEmptyStr, const string &job_group=kEmptyStr)
Returns statuses for a given affinity token.
void PrintCmdOutput(const string &cmd, CNcbiOstream &output_stream, ECmdOutputStyle output_style, CNetService::EIterationMode=CNetService::eSortByLoad)
CNetServiceIterator Iterate(EIterationMode mode=eSortByLoad)
void PrintXput(const string &data, const char *prefix)
virtual void ProcessJobMeta(const CNetScheduleKey &key)
virtual void ProcessRawLine(const string &line)
virtual void ProcessJobInfoField(const CTempString &field_name, const CTempString &field_value)
virtual void ProcessJobEventField(const CTempString &attr_name, const string &attr_value)
virtual void BeginJobEvent(const CTempString &event_header)
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
char data[12]
Definition: iconv.c:80
@ eHumanReadable
Definition: grid_cli.hpp:277
@ eAffinityInfo
Definition: grid_cli.hpp:214
@ eVerbose
Definition: grid_cli.hpp:220
@ eNotificationInfo
Definition: grid_cli.hpp:213
@ eAffinity
Definition: grid_cli.hpp:194
@ eBrief
Definition: grid_cli.hpp:221
@ eJobGroupInfo
Definition: grid_cli.hpp:211
@ eJobsByStatus
Definition: grid_cli.hpp:216
@ eActiveJobCount
Definition: grid_cli.hpp:215
@ eQueue
Definition: grid_cli.hpp:191
@ eClientInfo
Definition: grid_cli.hpp:212
@ eJobGroup
Definition: grid_cli.hpp:229
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
CNetService GetService()
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NcbiCout
Definition: ncbistre.hpp:543
static string PrintableString(const CTempString str, TPrintableMode mode=fNewLine_Quote|fNonAscii_Passthru)
Get a printable version of the specified string.
Definition: ncbistr.cpp:3944
CTempString & assign(const char *src_str, size_type len)
Assign new values to the content of the a string.
Definition: tempstr.hpp:733
const char * data(void) const
Return a pointer to the array represented.
Definition: tempstr.hpp:313
static bool StartsWith(const CTempString str, const CTempString start, ECase use_case=eCase)
Check if a string starts with a specified prefix value.
Definition: ncbistr.hpp:5406
size_type length(void) const
Return the length of the represented array.
Definition: tempstr.hpp:320
@ eRaw
Definition: types.hpp:81
string g_NetService_TryResolveHost(const string &ip_or_hostname)
Definition: util.cpp:186
const struct ncbi::grid::netcache::search::fields::KEY key
string g_GetNetScheduleStatCommand(ENetScheduleStatTopic topic)
ENetScheduleStatTopic
@ eNetScheduleStatClients
@ eNetScheduleStatNotifications
@ eNetScheduleStatJobGroups
@ eNetScheduleStatAffinities
CJsonNode g_GenericStatToJson(CNetServer server, ENetScheduleStatTopic topic, bool verbose)
static Format format
Definition: njn_ioutil.cpp:53
#define MAX_VISIBLE_DATA_LENGTH
Definition: ns_cmd_impl.cpp:38
Meaningful information encoded in the NetSchedule key.
#define _ASSERT
else result
Definition: token2.c:20
Modified on Fri Sep 20 14:57:32 2024 by modify_doxy.py rev. 669887