Perform a health check on the datalake before we enable network.

This commit is contained in:
Adam Treat 2023-04-23 07:35:38 -04:00
parent 42eec8b64c
commit 876f0537b4
4 changed files with 61 additions and 18 deletions

View File

@ -192,11 +192,17 @@ Window {
} }
onClicked: { onClicked: {
featureComingSoon.open() if (Network.isActive)
// if (Network.isActive) Network.isActive = false
// Network.isActive = false else
// else networkDialog.open()
// networkDialog.open() }
}
Connections {
target: Network
function onHealthCheckFailed(code) {
healthCheckFailed.open();
} }
} }
@ -237,9 +243,9 @@ Window {
} }
PopupDialog { PopupDialog {
id: featureComingSoon id: healthCheckFailed
anchors.centerIn: parent anchors.centerIn: parent
text: qsTr("Feature coming soon!") text: qsTr("Connection to datalake failed.")
} }
Button { Button {
@ -278,12 +284,6 @@ Window {
copyEdit.selectAll() copyEdit.selectAll()
copyEdit.copy() copyEdit.copy()
copyMessage.open() copyMessage.open()
timer.start()
}
Timer {
id: timer
interval: 500; running: false; repeat: false
onTriggered: copyMessage.close()
} }
} }

View File

@ -24,11 +24,10 @@ Network::Network()
{ {
QSettings settings; QSettings settings;
settings.sync(); settings.sync();
m_isActive = settings.value("network/isActive", false).toBool();
m_uniqueId = settings.value("uniqueId", generateUniqueId()).toString(); m_uniqueId = settings.value("uniqueId", generateUniqueId()).toString();
settings.setValue("uniqueId", m_uniqueId); settings.setValue("uniqueId", m_uniqueId);
settings.sync(); settings.sync();
emit activeChanged(); setActive(settings.value("network/isActive", false).toBool());
} }
void Network::setActive(bool b) void Network::setActive(bool b)
@ -38,6 +37,8 @@ void Network::setActive(bool b)
settings.sync(); settings.sync();
m_isActive = b; m_isActive = b;
emit activeChanged(); emit activeChanged();
if (m_isActive)
sendHealth();
} }
QString Network::generateUniqueId() const QString Network::generateUniqueId() const
@ -101,8 +102,10 @@ void Network::handleJsonUploadFinished()
int code = response.toInt(&ok); int code = response.toInt(&ok);
if (!ok) if (!ok)
qWarning() << "ERROR: Invalid response."; qWarning() << "ERROR: Invalid response.";
if (code != 200) if (code != 200) {
qWarning() << "ERROR: response != 200 code:" << code; qWarning() << "ERROR: response != 200 code:" << code;
sendHealth();
}
QByteArray jsonData = jsonReply->readAll(); QByteArray jsonData = jsonReply->readAll();
QJsonParseError err; QJsonParseError err;
@ -124,3 +127,31 @@ bool Network::sendConversation(const QString &conversation)
{ {
return packageAndSendJson(conversation); return packageAndSendJson(conversation);
} }
void Network::sendHealth()
{
QUrl healthUrl("http://localhost/v1/health");
QNetworkRequest request(healthUrl);
QNetworkReply *healthReply = m_networkManager.get(request);
connect(healthReply, &QNetworkReply::finished, this, &Network::handleHealthFinished);
}
void Network::handleHealthFinished()
{
QNetworkReply *healthReply = qobject_cast<QNetworkReply *>(sender());
if (!healthReply)
return;
QVariant response = healthReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
Q_ASSERT(response.isValid());
bool ok;
int code = response.toInt(&ok);
if (!ok)
qWarning() << "ERROR: Invalid response.";
if (code != 200) {
qWarning() << "ERROR: response != 200 code:" << code;
emit healthCheckFailed(code);
setActive(false);
}
healthReply->deleteLater();
}

View File

@ -10,7 +10,6 @@ class Network : public QObject
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool isActive READ isActive WRITE setActive NOTIFY activeChanged) Q_PROPERTY(bool isActive READ isActive WRITE setActive NOTIFY activeChanged)
public: public:
static Network *globalInstance(); static Network *globalInstance();
bool isActive() const { return m_isActive; } bool isActive() const { return m_isActive; }
@ -21,11 +20,14 @@ public:
Q_SIGNALS: Q_SIGNALS:
void activeChanged(); void activeChanged();
void healthCheckFailed(int code);
private Q_SLOTS: private Q_SLOTS:
void handleHealthFinished();
void handleJsonUploadFinished(); void handleJsonUploadFinished();
private: private:
void sendHealth();
bool packageAndSendJson(const QString &json); bool packageAndSendJson(const QString &json);
private: private:

View File

@ -4,7 +4,7 @@ import QtQuick.Controls
import QtQuick.Layouts import QtQuick.Layouts
Dialog { Dialog {
id: copyMessage id: popupDialog
anchors.centerIn: parent anchors.centerIn: parent
modal: false modal: false
opacity: 0.9 opacity: 0.9
@ -29,4 +29,14 @@ Dialog {
exit: Transition { exit: Transition {
NumberAnimation { duration: 500; property: "opacity"; from: 1.0; to: 0.0 } NumberAnimation { duration: 500; property: "opacity"; from: 1.0; to: 0.0 }
} }
onOpened: {
timer.start()
}
Timer {
id: timer
interval: 500; running: false; repeat: false
onTriggered: popupDialog.close()
}
} }