View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.protocol.http.restv2.converters;
22  
23  import com.fasterxml.jackson.databind.node.ObjectNode;
24  import org.joda.time.DateTime;
25  import org.joda.time.Period;
26  import org.waarp.common.json.JsonHandler;
27  import org.waarp.openr66.protocol.localhandler.Monitoring;
28  
29  import static org.waarp.openr66.protocol.configuration.Configuration.*;
30  import static org.waarp.openr66.protocol.http.restv2.RestConstants.*;
31  
32  /**
33   * A POJO representing the general status of the R66 server.
34   */
35  public final class ServerStatusMaker {
36  
37    /**
38     * Date of the last time the server status was requested.
39     */
40    private static DateTime lastRun;
41  
42    /**
43     * Makes the default constructor of this utility class inaccessible.
44     */
45    private ServerStatusMaker() throws InstantiationException {
46      throw new InstantiationException(
47          getClass().getName() + " cannot be instantiated.");
48    }
49  
50    // ########################## PUBLIC METHODS ################################
51  
52    /**
53     * Creates an {@link ObjectNode} listing general information about the R66
54     * server. The {@link Period}
55     * parameters specifies the time period on which the information is
56     * collected.
57     *
58     * @param period the time period analysed
59     *
60     * @return the server status ObjectNode
61     */
62    public static ObjectNode exportAsJson(final Period period) {
63      final int seconds = period.toStandardSeconds().getSeconds();
64      final Monitoring mon = configuration.getMonitoring();
65      mon.run(seconds, true);
66      final ObjectNode server = JsonHandler.createObjectNode();
67  
68      server.put("serverName", serverName());
69      server.put("date", DateTime.now().toString());
70      server.put("lastRun", lastRun == null? null : lastRun.toString());
71      server.put("fromDate", DateTime.now().minus(period).toString());
72      server.put("secondsRunning", mon.secondsRunning);
73      server.put("networkConnections", mon.nbNetworkConnection);
74      server.put("nbThreads", mon.nbThread);
75      server.put("inBandwidth", mon.bandwidthIn);
76      server.put("outBandwidth", mon.bandwidthOut);
77  
78      final ObjectNode overall = server.putObject("overall");
79      overall.put("allTransfers", mon.nbCountStepAllTransfer);
80      overall.put("unknown", mon.nbCountInfoUnknown);
81      overall.put("notUpdated", mon.nbCountInfoNotUpdated);
82      overall.put("interrupted", mon.nbCountInfoInterrupted);
83      overall.put("toSubmit", mon.nbCountInfoToSubmit);
84      overall.put("inError", mon.nbCountInfoError);
85      overall.put("running", mon.nbCountInfoRunning);
86      overall.put("done", mon.nbCountInfoDone);
87      overall.put("runningIn", mon.nbInActiveTransfer);
88      overall.put("runningOut", mon.nbOutActiveTransfer);
89      overall.put("lastRunningIn",
90                  new DateTime(mon.lastInActiveTransfer).toString());
91      overall.put("lastRunningOut",
92                  new DateTime(mon.lastOutActiveTransfer).toString());
93      overall.put("allIn", mon.nbInTotalTransfer);
94      overall.put("allOut", mon.nbOutTotalTransfer);
95      overall.put("errorsIn", mon.nbInErrorTransfer);
96      overall.put("errorsOut", mon.nbOutErrorTransfer);
97  
98      final ObjectNode globalSteps = server.putObject("globalSteps");
99      globalSteps.put("noTask", mon.nbCountStepNotask);
100     globalSteps.put("preTask", mon.nbCountStepPretask);
101     globalSteps.put("transferTask", mon.nbCountStepTransfer);
102     globalSteps.put("postTask", mon.nbCountStepPosttask);
103     globalSteps.put("allDoneTask", mon.nbCountStepAllDone);
104     globalSteps.put("errorTask", mon.nbCountStepError);
105 
106     final ObjectNode runningSteps = server.putObject("runningSteps");
107     runningSteps.put("allRunning", mon.nbCountAllRunningStep);
108     runningSteps.put("running", mon.nbCountRunningStep);
109     runningSteps.put("initOK", mon.nbCountInitOkStep);
110     runningSteps.put("preProcessingOk", mon.nbCountPreProcessingOkStep);
111     runningSteps.put("transferOk", mon.nbCountTransferOkStep);
112     runningSteps.put("postProcessingOk", mon.nbCountPostProcessingOkStep);
113     runningSteps.put("completeOk", mon.nbCountCompleteOkStep);
114 
115     final ObjectNode errors = server.putObject("errors");
116     errors.put("connectionImpossible", mon.nbCountStatusConnectionImpossible);
117     errors.put("serverOverloaded", mon.nbCountStatusServerOverloaded);
118     errors.put("badAuthent", mon.nbCountStatusBadAuthent);
119     errors.put("externalOp", mon.nbCountStatusExternalOp);
120     errors.put("transferError", mon.nbCountStatusTransferError);
121     errors.put("md5Error", mon.nbCountStatusMD5Error);
122     errors.put("disconnection", mon.nbCountStatusDisconnection);
123     errors.put("finalOp", mon.nbCountStatusFinalOp);
124     errors.put("unimplemented", mon.nbCountStatusUnimplemented);
125     errors.put("internal", mon.nbCountStatusInternal);
126     errors.put("warning", mon.nbCountStatusWarning);
127     errors.put("queryAlreadyFinished", mon.nbCountStatusQueryAlreadyFinished);
128     errors.put("queryStillRunning", mon.nbCountStatusQueryStillRunning);
129     errors.put("unknownHost", mon.nbCountStatusNotKnownHost);
130     errors.put("remotelyUnknown", mon.nbCountStatusQueryRemotelyUnknown);
131     errors.put("commandNotFound", mon.nbCountStatusCommandNotFound);
132     errors.put("passThroughMode", mon.nbCountStatusPassThroughMode);
133     errors.put("remoteShutdown", mon.nbCountStatusRemoteShutdown);
134     errors.put("shutdown", mon.nbCountStatusShutdown);
135     errors.put("remoteError", mon.nbCountStatusRemoteError);
136     errors.put("stopped", mon.nbCountStatusStopped);
137     errors.put("canceled", mon.nbCountStatusCanceled);
138     errors.put("fileNotFound", mon.nbCountStatusFileNotFound);
139     errors.put("unknown", mon.nbCountStatusUnknown);
140 
141     lastRun = DateTime.now();
142 
143     return server;
144   }
145 }