Add getStateSummary method for diagnostics
Summary: Adds a more structured representation of state updates than a multiline string. Also changes the string form to omit [started] records. Reviewed By: danielbuechele Differential Revision: D9315503 fbshipit-source-id: 55b8f9572091fd42fe852c8d26a8f2a53f76c82a
This commit is contained in:
committed by
Facebook Github Bot
parent
e51b8c0742
commit
660da3f80e
@@ -23,6 +23,7 @@
|
|||||||
#include <Sonar/SonarConnection.h>
|
#include <Sonar/SonarConnection.h>
|
||||||
#include <Sonar/SonarResponder.h>
|
#include <Sonar/SonarResponder.h>
|
||||||
#include <Sonar/SonarStateUpdateListener.h>
|
#include <Sonar/SonarStateUpdateListener.h>
|
||||||
|
#include <Sonar/SonarState.h>
|
||||||
|
|
||||||
using namespace facebook;
|
using namespace facebook;
|
||||||
using namespace facebook::sonar;
|
using namespace facebook::sonar;
|
||||||
@@ -249,6 +250,21 @@ class JSonarPluginWrapper : public SonarPlugin {
|
|||||||
JSonarPluginWrapper(jni::global_ref<JSonarPlugin> plugin): jplugin(plugin) {}
|
JSonarPluginWrapper(jni::global_ref<JSonarPlugin> plugin): jplugin(plugin) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct JStateSummary : public jni::JavaClass<JStateSummary> {
|
||||||
|
public:
|
||||||
|
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/core/StateSummary;";
|
||||||
|
|
||||||
|
static jni::local_ref<JStateSummary> create() {
|
||||||
|
return newInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
void addEntry(std::string name, std::string state) {
|
||||||
|
static const auto method = javaClassStatic()->getMethod<void(std::string, std::string)>("addEntry");
|
||||||
|
return method(self(), name, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
class JSonarClient : public jni::HybridClass<JSonarClient> {
|
class JSonarClient : public jni::HybridClass<JSonarClient> {
|
||||||
public:
|
public:
|
||||||
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/android/SonarClientImpl;";
|
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/android/SonarClientImpl;";
|
||||||
@@ -265,6 +281,7 @@ class JSonarClient : public jni::HybridClass<JSonarClient> {
|
|||||||
makeNativeMethod("unsubscribe", JSonarClient::unsubscribe),
|
makeNativeMethod("unsubscribe", JSonarClient::unsubscribe),
|
||||||
makeNativeMethod("getPlugin", JSonarClient::getPlugin),
|
makeNativeMethod("getPlugin", JSonarClient::getPlugin),
|
||||||
makeNativeMethod("getState", JSonarClient::getState),
|
makeNativeMethod("getState", JSonarClient::getState),
|
||||||
|
makeNativeMethod("getStateSummary", JSonarClient::getStateSummary),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,6 +324,21 @@ class JSonarClient : public jni::HybridClass<JSonarClient> {
|
|||||||
return SonarClient::instance()->getState();
|
return SonarClient::instance()->getState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jni::global_ref<JStateSummary::javaobject> getStateSummary() {
|
||||||
|
auto summary = jni::make_global(JStateSummary::create());
|
||||||
|
auto elements = SonarClient::instance()->getStateElements();
|
||||||
|
for (auto&& element : elements) {
|
||||||
|
std::string status;
|
||||||
|
switch (element.state_) {
|
||||||
|
case State::in_progress: status = "IN_PROGRESS"; break;
|
||||||
|
case State::failed: status = "FAILED"; break;
|
||||||
|
case State::success: status = "SUCCESS"; break;
|
||||||
|
}
|
||||||
|
summary->addEntry(element.name_, status);
|
||||||
|
}
|
||||||
|
return summary;
|
||||||
|
}
|
||||||
|
|
||||||
jni::alias_ref<JSonarPlugin> getPlugin(const std::string& identifier) {
|
jni::alias_ref<JSonarPlugin> getPlugin(const std::string& identifier) {
|
||||||
auto plugin = SonarClient::instance()->getPlugin(identifier);
|
auto plugin = SonarClient::instance()->getPlugin(identifier);
|
||||||
if (plugin) {
|
if (plugin) {
|
||||||
@@ -347,7 +379,6 @@ class JSonarClient : public jni::HybridClass<JSonarClient> {
|
|||||||
private:
|
private:
|
||||||
friend HybridBase;
|
friend HybridBase;
|
||||||
std::shared_ptr<SonarStateUpdateListener> mStateListener = nullptr;
|
std::shared_ptr<SonarStateUpdateListener> mStateListener = nullptr;
|
||||||
|
|
||||||
JSonarClient() {}
|
JSonarClient() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import com.facebook.sonar.BuildConfig;
|
|||||||
import com.facebook.sonar.core.SonarClient;
|
import com.facebook.sonar.core.SonarClient;
|
||||||
import com.facebook.sonar.core.SonarPlugin;
|
import com.facebook.sonar.core.SonarPlugin;
|
||||||
import com.facebook.sonar.core.SonarStateUpdateListener;
|
import com.facebook.sonar.core.SonarStateUpdateListener;
|
||||||
|
import com.facebook.sonar.core.StateSummary;
|
||||||
|
|
||||||
@DoNotStrip
|
@DoNotStrip
|
||||||
class SonarClientImpl implements SonarClient {
|
class SonarClientImpl implements SonarClient {
|
||||||
@@ -65,4 +66,7 @@ class SonarClientImpl implements SonarClient {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public native String getState();
|
public native String getState();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public native StateSummary getStateSummary();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,4 +23,6 @@ public interface SonarClient {
|
|||||||
void unsubscribe();
|
void unsubscribe();
|
||||||
|
|
||||||
String getState();
|
String getState();
|
||||||
|
|
||||||
|
StateSummary getStateSummary();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.facebook.sonar.core;
|
||||||
|
|
||||||
|
public class StateElement {
|
||||||
|
private final String mName;
|
||||||
|
private final String mState;
|
||||||
|
public StateElement(String name, String state) {
|
||||||
|
mName = name;
|
||||||
|
mState = state;
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return mName;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.facebook.sonar.core;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class StateSummary {
|
||||||
|
|
||||||
|
public enum State {
|
||||||
|
IN_PROGRESS, SUCCESS, FAILED, UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class StateElement {
|
||||||
|
private final String mName;
|
||||||
|
private final State mState;
|
||||||
|
public StateElement(String name, State state) {
|
||||||
|
mName = name;
|
||||||
|
mState = state;
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return mName;
|
||||||
|
}
|
||||||
|
public State getState() {
|
||||||
|
return mState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final List<StateElement> mList = new ArrayList<>();
|
||||||
|
|
||||||
|
public void addEntry(String name, String state) {
|
||||||
|
State s;
|
||||||
|
try {
|
||||||
|
s = State.valueOf(state);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
s = State.UNKNOWN;
|
||||||
|
}
|
||||||
|
mList.add(new StateElement(name, s));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
#include "SonarState.h"
|
#include "SonarState.h"
|
||||||
#include "SonarStep.h"
|
#include "SonarStep.h"
|
||||||
#include "SonarWebSocketImpl.h"
|
#include "SonarWebSocketImpl.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
#include <android/log.h>
|
#include <android/log.h>
|
||||||
@@ -110,10 +111,8 @@ void SonarClient::refreshPlugins() {
|
|||||||
void SonarClient::onConnected() {
|
void SonarClient::onConnected() {
|
||||||
SONAR_LOG("SonarClient::onConnected");
|
SONAR_LOG("SonarClient::onConnected");
|
||||||
|
|
||||||
auto step = sonarState_->start("Connect");
|
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
connected_ = true;
|
connected_ = true;
|
||||||
step->complete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SonarClient::onDisconnected() {
|
void SonarClient::onDisconnected() {
|
||||||
@@ -216,6 +215,10 @@ std::string SonarClient::getState() {
|
|||||||
return sonarState_->getState();
|
return sonarState_->getState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<StateElement> SonarClient::getStateElements() {
|
||||||
|
return sonarState_->getStateElements();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace sonar
|
} // namespace sonar
|
||||||
} // namespace facebook
|
} // namespace facebook
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "SonarStep.h"
|
#include "SonarStep.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace facebook {
|
namespace facebook {
|
||||||
namespace sonar {
|
namespace sonar {
|
||||||
@@ -77,6 +78,8 @@ class SonarClient : public SonarWebSocket::Callbacks {
|
|||||||
|
|
||||||
std::string getState();
|
std::string getState();
|
||||||
|
|
||||||
|
std::vector<StateElement> getStateElements();
|
||||||
|
|
||||||
template <typename P>
|
template <typename P>
|
||||||
std::shared_ptr<P> getPlugin(const std::string& identifier) {
|
std::shared_ptr<P> getPlugin(const std::string& identifier) {
|
||||||
return std::static_pointer_cast<P>(getPlugin(identifier));
|
return std::static_pointer_cast<P>(getPlugin(identifier));
|
||||||
|
|||||||
@@ -8,34 +8,41 @@
|
|||||||
#include "SonarState.h"
|
#include "SonarState.h"
|
||||||
#include "SonarStateUpdateListener.h"
|
#include "SonarStateUpdateListener.h"
|
||||||
#include "SonarStep.h"
|
#include "SonarStep.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace facebook::sonar;
|
||||||
|
|
||||||
/* Class responsible for collecting state updates and combining them into a
|
/* Class responsible for collecting state updates and combining them into a
|
||||||
* view of the current state of the sonar client. */
|
* view of the current state of the sonar client. */
|
||||||
|
|
||||||
SonarState::SonarState() {
|
|
||||||
stateUpdates = "";
|
SonarState::SonarState(): log("") {}
|
||||||
}
|
|
||||||
void SonarState::setUpdateListener(
|
void SonarState::setUpdateListener(
|
||||||
std::shared_ptr<SonarStateUpdateListener> listener) {
|
std::shared_ptr<SonarStateUpdateListener> listener) {
|
||||||
mListener = listener;
|
mListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SonarState::started(std::string step) {
|
void SonarState::started(std::string step) {
|
||||||
stateUpdates = stateUpdates + "[Started] " + step + "\n";
|
if (stateMap.find(step) == stateMap.end()) {
|
||||||
|
insertOrder.push_back(step);
|
||||||
|
}
|
||||||
|
stateMap[step] = State::in_progress;
|
||||||
if (mListener) {
|
if (mListener) {
|
||||||
mListener->onUpdate();
|
mListener->onUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SonarState::success(std::string step) {
|
void SonarState::success(std::string step) {
|
||||||
stateUpdates = stateUpdates + "[Success] " + step + "\n";
|
log = log + "[Success] " + step + "\n";
|
||||||
|
stateMap[step] = State::success;
|
||||||
if (mListener) {
|
if (mListener) {
|
||||||
mListener->onUpdate();
|
mListener->onUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SonarState::failed(std::string step, std::string errorMessage) {
|
void SonarState::failed(std::string step, std::string errorMessage) {
|
||||||
stateUpdates = stateUpdates + "[Failed] " + step + "\n";
|
log = log + "[Failed] " + step + "\n";
|
||||||
|
stateMap[step] = State::failed;
|
||||||
if (mListener) {
|
if (mListener) {
|
||||||
mListener->onUpdate();
|
mListener->onUpdate();
|
||||||
}
|
}
|
||||||
@@ -45,7 +52,15 @@ void SonarState::failed(std::string step, std::string errorMessage) {
|
|||||||
// representation of the current state so the UI can show it in a more intuitive
|
// representation of the current state so the UI can show it in a more intuitive
|
||||||
// way
|
// way
|
||||||
std::string SonarState::getState() {
|
std::string SonarState::getState() {
|
||||||
return stateUpdates;
|
return log;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<StateElement> SonarState::getStateElements() {
|
||||||
|
std::vector<StateElement> v;
|
||||||
|
for (auto stepName : insertOrder) {
|
||||||
|
v.push_back(StateElement(stepName, stateMap[stepName]));
|
||||||
|
}
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<SonarStep> SonarState::start(std::string step_name) {
|
std::shared_ptr<SonarStep> SonarState::start(std::string step_name) {
|
||||||
|
|||||||
@@ -10,11 +10,27 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <Sonar/SonarStep.h>
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
class SonarStep;
|
class SonarStep;
|
||||||
class SonarStateUpdateListener;
|
class SonarStateUpdateListener;
|
||||||
|
|
||||||
|
namespace facebook {
|
||||||
|
namespace sonar {
|
||||||
|
|
||||||
|
enum State { success, in_progress, failed };
|
||||||
|
|
||||||
|
class StateElement {
|
||||||
|
public:
|
||||||
|
StateElement(std::string name, State state): name_(name), state_(state) {};
|
||||||
|
std::string name_;
|
||||||
|
State state_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class SonarState {
|
class SonarState {
|
||||||
friend SonarStep;
|
friend SonarStep;
|
||||||
|
|
||||||
@@ -22,6 +38,7 @@ class SonarState {
|
|||||||
SonarState();
|
SonarState();
|
||||||
void setUpdateListener(std::shared_ptr<SonarStateUpdateListener>);
|
void setUpdateListener(std::shared_ptr<SonarStateUpdateListener>);
|
||||||
std::string getState();
|
std::string getState();
|
||||||
|
std::vector<facebook::sonar::StateElement> getStateElements();
|
||||||
|
|
||||||
/* To record a state update, call start() with the name of the step to get a
|
/* To record a state update, call start() with the name of the step to get a
|
||||||
SonarStep object. Call complete on this to register it successful,
|
SonarStep object. Call complete on this to register it successful,
|
||||||
@@ -33,6 +50,9 @@ class SonarState {
|
|||||||
void success(std::string);
|
void success(std::string);
|
||||||
void failed(std::string, std::string);
|
void failed(std::string, std::string);
|
||||||
void started(std::string);
|
void started(std::string);
|
||||||
|
|
||||||
std::shared_ptr<SonarStateUpdateListener> mListener = nullptr;
|
std::shared_ptr<SonarStateUpdateListener> mListener = nullptr;
|
||||||
std::string stateUpdates;
|
std::string log;
|
||||||
|
std::vector<std::string> insertOrder;
|
||||||
|
std::map<std::string, facebook::sonar::State> stateMap;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "SonarWebSocketImpl.h"
|
#include "SonarWebSocketImpl.h"
|
||||||
|
#include "SonarStep.h"
|
||||||
#include <folly/String.h>
|
#include <folly/String.h>
|
||||||
#include <folly/futures/Future.h>
|
#include <folly/futures/Future.h>
|
||||||
#include <folly/io/async/SSLContext.h>
|
#include <folly/io/async/SSLContext.h>
|
||||||
@@ -224,11 +225,12 @@ void SonarWebSocketImpl::sendMessage(const folly::dynamic& message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool SonarWebSocketImpl::isCertificateExchangeNeeded() {
|
bool SonarWebSocketImpl::isCertificateExchangeNeeded() {
|
||||||
auto step = sonarState_->start("Check required certificates are present");
|
|
||||||
if (failedConnectionAttempts_ >= 2) {
|
if (failedConnectionAttempts_ >= 2) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto step = sonarState_->start("Check required certificates are present");
|
||||||
std::string caCert = loadStringFromFile(absoluteFilePath(SONAR_CA_FILE_NAME));
|
std::string caCert = loadStringFromFile(absoluteFilePath(SONAR_CA_FILE_NAME));
|
||||||
std::string clientCert =
|
std::string clientCert =
|
||||||
loadStringFromFile(absoluteFilePath(CLIENT_CERT_FILE_NAME));
|
loadStringFromFile(absoluteFilePath(CLIENT_CERT_FILE_NAME));
|
||||||
|
|||||||
Reference in New Issue
Block a user