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 
ENetScheduleStatTopic
Definition: util.hpp:41
Meaningful information encoded in the NetSchedule key.
void StatusSnapshot(TStatusMap &status_map, const string &affinity_token=kEmptyStr, const string &job_group=kEmptyStr)
Returns statuses for a given affinity token.
void SetByKey(const string &key, CJsonNode::TInstance value)
For a JSON object node, insert a new element or update an existing element.
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:62
CNetServiceIterator Iterate(EIterationMode mode=eSortByLoad)
const struct ncbi::grid::netcache::search::fields::KEY key
#define NcbiCout
Definition: ncbistre.hpp:398
#define GRID_APP_NAME
Definition: cgi2rcgi.cpp:61
void g_PrintJSON(FILE *output_stream, CJsonNode node, const char *indent)
Definition: util.cpp:865
unsigned id
Job id.
static void PrintLine(const string &line)
Definition: grid_cli.cpp:1722
CNetService GetService()
ESerialDataFormat format
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:795
string g_NetService_TryResolveHost(const string &ip_or_hostname)
Definition: util.cpp:186
virtual void ProcessJobEventField(const CTempString &attr_name, const string &attr_value)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
CTempString & assign(const char *src_str, size_type len)
Assign new values to the content of the a string.
Definition: tempstr.hpp:764
Definition: types.hpp:81
string g_GetNetScheduleStatCommand(ENetScheduleStatTopic topic)
Definition: util.cpp:130
string queue
Queue name, optional.
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:5412
struct CGridCommandLineInterfaceApp::SOptions m_Opts
static CJsonNode NewObjectNode()
Create a new JSON object node.
unsigned short port
TCP/IP port number.
void PrintCmdOutput(const string &cmd, CNcbiOstream &output_stream, ECmdOutputStyle output_style, CNetService::EIterationMode=CNetService::eSortByLoad)
virtual void ProcessJobInfoField(const CTempString &field_name, const CTempString &field_value)
virtual void ProcessRawLine(const string &line)
static string PrintableString(const CTempString str, TPrintableMode mode=fNewLine_Quote|fNonAscii_Passthru)
Get a printable version of the specified string.
Definition: ncbistr.cpp:3826
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1153
#define MAX_VISIBLE_DATA_LENGTH
Definition: ns_cmd_impl.cpp:38
virtual void ProcessJobMeta(const CNetScheduleKey &key)
void PrintXput(const string &data, const char *prefix)
else result
Definition: token2.c:20
CJsonNode g_GenericStatToJson(CNetServer server, ENetScheduleStatTopic topic, bool verbose)
Definition: util.cpp:135
CNetScheduleAdmin m_NetScheduleAdmin
Definition: grid_cli.hpp:447
string host
Server name.
#define _ASSERT
void PrintServerStatistics(CNcbiOstream &output_stream, EStatisticsOptions opt=eStatisticsBrief)
virtual void BeginJobEvent(const CTempString &event_header)
static const char * prefix[]
Definition: pcregrep.c:251
CJsonNode g_LegacyStatToJson(CNetServer server, bool verbose)
Definition: util.cpp:202
size_type length(void) const
Return the length of the represented array.
Definition: tempstr.hpp:360
const char * data(void) const
Return a pointer to the array represented.
Definition: tempstr.hpp:353
JSON node abstraction.
CNetScheduleAPIExt m_NetScheduleAPI
Definition: grid_cli.hpp:446
bool IsOptionSet(int option) const
Definition: grid_cli.hpp:418
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
void PrintNetScheduleStats_Generic(ENetScheduleStatTopic topic)
static char cmd[512]
Definition: bcp.c:20
Modified on Wed Nov 22 17:10:46 2017 by modify_doxy.py rev. 546573