1 /*  $Id: ns_cmd_impl.cpp 478655 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 
41 BEGIN_NCBI_SCOPE
42 
PrintNetScheduleStats()43 int CGridCommandLineInterfaceApp::PrintNetScheduleStats()
44 {
45     if (IsOptionSet(eJobGroupInfo))
46         PrintNetScheduleStats_Generic(eNetScheduleStatJobGroups);
47     else if (IsOptionSet(eClientInfo))
48         PrintNetScheduleStats_Generic(eNetScheduleStatClients);
49     else if (IsOptionSet(eNotificationInfo))
50         PrintNetScheduleStats_Generic(eNetScheduleStatNotifications);
51     else if (IsOptionSet(eAffinityInfo))
52         PrintNetScheduleStats_Generic(eNetScheduleStatAffinities);
53     else if (IsOptionSet(eActiveJobCount) || IsOptionSet(eJobsByStatus)) {
54         if (!IsOptionSet(eQueue) &&
55                 (IsOptionSet(eAffinity) || IsOptionSet(eJobGroup))) {
56             fprintf(stderr, GRID_APP_NAME ": invalid option combination.\n");
57             return 2;
58         }
59 
60         if (IsOptionSet(eActiveJobCount)) {
61             CNetScheduleAdmin::TStatusMap st_map;
62 
63             m_NetScheduleAdmin.StatusSnapshot(st_map,
64                     m_Opts.affinity, m_Opts.job_group);
65 
66             printf(m_Opts.output_format == eHumanReadable ?
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=");
76                 cmd.append(NStr::PrintableString(m_Opts.affinity));
77             }
78 
79             if (!m_Opts.job_group.empty()) {
80                 cmd.append(" group=");
81                 cmd.append(NStr::PrintableString(m_Opts.job_group));
82             }
83 
84             m_NetScheduleAPI.GetService().PrintCmdOutput(cmd,
85                     NcbiCout, CNetService::eMultilineOutput);
86         } else {
87             CNetScheduleAdmin::TStatusMap st_map;
88 
89             m_NetScheduleAdmin.StatusSnapshot(st_map,
90                     m_Opts.affinity, m_Opts.job_group);
91 
92             const char* format;
93             const char* format_cont;
94 
95             if (m_Opts.output_format == eHumanReadable)
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 
103             ITERATE(CNetScheduleAdmin::TStatusMap, it, st_map) {
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)
115             m_NetScheduleAdmin.PrintServerStatistics(NcbiCout,
116                 IsOptionSet(eBrief) ? CNetScheduleAdmin::eStatisticsBrief :
117                     CNetScheduleAdmin::eStatisticsAll);
118         else {
119             CJsonNode result(CJsonNode::NewObjectNode());
120 
121             for (CNetServiceIterator it = m_NetScheduleAPI.GetService().Iterate(
122                     CNetService::eIncludePenalized); it; ++it)
123                 result.SetByKey((*it).GetServerAddress(),
124                         g_LegacyStatToJson(*it, !IsOptionSet(eBrief)));
125 
126             g_PrintJSON(stdout, result);
127         }
128     }
129 
130     return 0;
131 }
132 
PrintNetScheduleStats_Generic(ENetScheduleStatTopic topic)133 void CGridCommandLineInterfaceApp::PrintNetScheduleStats_Generic(
134         ENetScheduleStatTopic topic)
135 {
136     if (m_Opts.output_format == eJSON) {
137         CJsonNode result(CJsonNode::NewObjectNode());
138 
139         for (CNetServiceIterator it = m_NetScheduleAPI.GetService().Iterate(
140                 CNetService::eIncludePenalized); it; ++it)
141             result.SetByKey((*it).GetServerAddress(),
142                     g_GenericStatToJson(*it, topic, IsOptionSet(eVerbose)));
143 
144         g_PrintJSON(stdout, result);
145     } else {
146         string cmd(g_GetNetScheduleStatCommand(topic));
147 
148         if (IsOptionSet(eVerbose))
149             cmd.append(" VERBOSE");
150 
151         m_NetScheduleAPI.GetService().PrintCmdOutput(cmd,
152                 NcbiCout, CNetService::eMultilineOutput);
153     }
154 }
155 
ProcessJobMeta(const CNetScheduleKey & key)156 void CPrintJobInfo::ProcessJobMeta(const CNetScheduleKey& key)
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 
BeginJobEvent(const CTempString & event_header)165 void CPrintJobInfo::BeginJobEvent(const CTempString& event_header)
166 {
167     printf("[%.*s]\n", int(event_header.length()), event_header.data());
168 }
169 
ProcessJobEventField(const CTempString & attr_name,const string & attr_value)170 void CPrintJobInfo::ProcessJobEventField(const CTempString& attr_name,
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 
ProcessJobEventField(const CTempString & attr_name)177 void CPrintJobInfo::ProcessJobEventField(const CTempString& attr_name)
178 {
179     printf("    %.*s\n", int(attr_name.length()), attr_name.data());
180 }
181 
PrintXput(const string & data,const char * prefix)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,
208                     NStr::PrintableString(CTempString(raw_data.data(),
209                             MAX_VISIBLE_DATA_LENGTH)).c_str());
210     }
211 }
212 
ProcessJobInfoField(const CTempString & field_name,const CTempString & field_value)213 void CPrintJobInfo::ProcessJobInfoField(const CTempString& field_name,
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 
ProcessRawLine(const string & line)220 void CPrintJobInfo::ProcessRawLine(const string& line)
221 {
222     CGridCommandLineInterfaceApp::PrintLine(line.c_str());
223 }
224 
225 END_NCBI_SCOPE
226