2023-04-14 14:44:28 -04:00
|
|
|
#include "network.h"
|
2024-04-25 13:16:52 -04:00
|
|
|
|
2024-06-04 14:47:11 -04:00
|
|
|
#include "chat.h"
|
2023-06-22 15:44:49 -04:00
|
|
|
#include "chatlistmodel.h"
|
2024-04-25 13:16:52 -04:00
|
|
|
#include "download.h"
|
|
|
|
#include "llm.h"
|
|
|
|
#include "localdocs.h"
|
2024-06-04 14:47:11 -04:00
|
|
|
#include "localdocsmodel.h"
|
|
|
|
#include "modellist.h"
|
2023-06-28 20:42:40 -04:00
|
|
|
#include "mysettings.h"
|
2023-04-14 14:44:28 -04:00
|
|
|
|
2024-06-04 14:47:11 -04:00
|
|
|
#include "../gpt4all-backend/llmodel.h"
|
2024-04-25 13:16:52 -04:00
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
#include <QCoreApplication>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QGlobalStatic>
|
2023-05-02 16:24:06 -04:00
|
|
|
#include <QGuiApplication>
|
2023-04-14 14:44:28 -04:00
|
|
|
#include <QJsonArray>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QJsonDocument>
|
2023-04-14 14:44:28 -04:00
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QNetworkRequest>
|
2023-05-02 16:24:06 -04:00
|
|
|
#include <QScreen>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QSettings>
|
|
|
|
#include <QSize>
|
|
|
|
#include <QSslConfiguration>
|
|
|
|
#include <QSslSocket>
|
|
|
|
#include <QSysInfo>
|
|
|
|
#include <Qt>
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QtLogging>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QUuid>
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
|
|
|
#include <utility>
|
2023-04-14 14:44:28 -04:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
using namespace Qt::Literals::StringLiterals;
|
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
//#define DEBUG
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
static const char MIXPANEL_TOKEN[] = "ce362e568ddaee16ed243eaffb5860a2";
|
|
|
|
|
2023-05-02 20:31:17 -04:00
|
|
|
#if defined(Q_OS_MAC)
|
2024-04-25 13:16:52 -04:00
|
|
|
|
2023-05-02 16:24:06 -04:00
|
|
|
#include <sys/sysctl.h>
|
2024-06-24 18:49:23 -04:00
|
|
|
static QString getCPUModel()
|
|
|
|
{
|
2023-05-02 16:24:06 -04:00
|
|
|
char buffer[256];
|
|
|
|
size_t bufferlen = sizeof(buffer);
|
|
|
|
sysctlbyname("machdep.cpu.brand_string", &buffer, &bufferlen, NULL, 0);
|
2024-04-25 13:16:52 -04:00
|
|
|
return buffer;
|
2023-05-02 16:24:06 -04:00
|
|
|
}
|
2024-04-25 13:16:52 -04:00
|
|
|
|
|
|
|
#elif defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)
|
|
|
|
|
2024-04-26 11:22:24 -04:00
|
|
|
#ifndef _MSC_VER
|
2024-06-24 18:49:23 -04:00
|
|
|
static void get_cpuid(int level, int *regs)
|
|
|
|
{
|
2024-04-26 11:22:24 -04:00
|
|
|
asm volatile("cpuid" : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3]) : "0" (level) : "memory");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define get_cpuid(level, regs) __cpuid(regs, level)
|
|
|
|
#endif
|
2024-04-25 13:16:52 -04:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
static QString getCPUModel()
|
|
|
|
{
|
2024-04-26 11:22:24 -04:00
|
|
|
int regs[12];
|
2024-04-25 13:16:52 -04:00
|
|
|
|
|
|
|
// EAX=800000000h: Get Highest Extended Function Implemented
|
2024-04-26 11:22:24 -04:00
|
|
|
get_cpuid(0x80000000, regs);
|
2024-04-25 13:16:52 -04:00
|
|
|
if (regs[0] < 0x80000004)
|
|
|
|
return "(unknown)";
|
|
|
|
|
|
|
|
// EAX=800000002h-800000004h: Processor Brand String
|
2024-04-26 11:22:24 -04:00
|
|
|
get_cpuid(0x80000002, regs);
|
|
|
|
get_cpuid(0x80000003, regs + 4);
|
|
|
|
get_cpuid(0x80000004, regs + 8);
|
2024-04-25 13:16:52 -04:00
|
|
|
|
|
|
|
char str[sizeof(regs) + 1];
|
|
|
|
memcpy(str, regs, sizeof(regs));
|
|
|
|
str[sizeof(regs)] = 0;
|
|
|
|
|
|
|
|
return QString(str).trimmed();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static QString getCPUModel() { return "(non-x86)"; }
|
|
|
|
|
2023-05-02 16:24:06 -04:00
|
|
|
#endif
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
class MyNetwork: public Network { };
|
|
|
|
Q_GLOBAL_STATIC(MyNetwork, networkInstance)
|
|
|
|
Network *Network::globalInstance()
|
|
|
|
{
|
|
|
|
return networkInstance();
|
|
|
|
}
|
|
|
|
|
2024-07-25 10:02:52 -04:00
|
|
|
bool Network::isHttpUrlValid(QUrl url) {
|
|
|
|
if (!url.isValid())
|
|
|
|
return false;
|
|
|
|
QString scheme(url.scheme());
|
|
|
|
if (scheme != "http" && scheme != "https")
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
Network::Network()
|
|
|
|
: QObject{nullptr}
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
m_uniqueId = settings.value("uniqueId", generateUniqueId()).toString();
|
|
|
|
settings.setValue("uniqueId", m_uniqueId);
|
2024-04-25 13:16:52 -04:00
|
|
|
m_sessionId = generateUniqueId();
|
|
|
|
|
|
|
|
// allow sendMixpanel to be called from any thread
|
|
|
|
connect(this, &Network::requestMixpanel, this, &Network::sendMixpanel, Qt::QueuedConnection);
|
|
|
|
|
|
|
|
const auto *mySettings = MySettings::globalInstance();
|
|
|
|
connect(mySettings, &MySettings::networkIsActiveChanged, this, &Network::handleIsActiveChanged);
|
|
|
|
connect(mySettings, &MySettings::networkUsageStatsActiveChanged, this, &Network::handleUsageStatsActiveChanged);
|
|
|
|
|
|
|
|
m_hasSentOptIn = !Download::globalInstance()->isFirstStart() && mySettings->networkUsageStatsActive();
|
|
|
|
m_hasSentOptOut = !Download::globalInstance()->isFirstStart() && !mySettings->networkUsageStatsActive();
|
|
|
|
|
|
|
|
if (mySettings->networkIsActive())
|
2023-04-28 10:54:05 -04:00
|
|
|
sendHealth();
|
2023-04-24 17:52:19 -04:00
|
|
|
connect(&m_networkManager, &QNetworkAccessManager::sslErrors, this,
|
|
|
|
&Network::handleSslErrors);
|
2023-04-14 14:44:28 -04:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
// NOTE: this won't be useful until we make it possible to change this via the settings page
|
|
|
|
void Network::handleUsageStatsActiveChanged()
|
2023-04-14 14:44:28 -04:00
|
|
|
{
|
2024-04-25 13:16:52 -04:00
|
|
|
if (!MySettings::globalInstance()->networkUsageStatsActive())
|
|
|
|
m_sendUsageStats = false;
|
2023-04-14 14:44:28 -04:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
void Network::handleIsActiveChanged()
|
2023-04-28 10:54:05 -04:00
|
|
|
{
|
2024-04-25 13:16:52 -04:00
|
|
|
if (MySettings::globalInstance()->networkUsageStatsActive())
|
|
|
|
sendHealth();
|
2023-04-28 10:54:05 -04:00
|
|
|
}
|
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
QString Network::generateUniqueId() const
|
|
|
|
{
|
|
|
|
return QUuid::createUuid().toString(QUuid::WithoutBraces);
|
|
|
|
}
|
|
|
|
|
2023-04-23 21:05:38 -04:00
|
|
|
bool Network::packageAndSendJson(const QString &ingestId, const QString &json)
|
2023-04-14 14:44:28 -04:00
|
|
|
{
|
2023-06-28 20:42:40 -04:00
|
|
|
if (!MySettings::globalInstance()->networkIsActive())
|
2023-04-14 14:44:28 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
QJsonParseError err;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8(), &err);
|
|
|
|
if (err.error != QJsonParseError::NoError) {
|
|
|
|
qDebug() << "Couldn't parse: " << json << err.errorString();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_ASSERT(doc.isObject());
|
2023-06-22 15:44:49 -04:00
|
|
|
Q_ASSERT(ChatListModel::globalInstance()->currentChat());
|
2023-04-14 14:44:28 -04:00
|
|
|
QJsonObject object = doc.object();
|
|
|
|
object.insert("source", "gpt4all-chat");
|
2023-07-01 11:34:21 -04:00
|
|
|
object.insert("agent_id", ChatListModel::globalInstance()->currentChat()->modelInfo().filename());
|
2023-04-14 14:44:28 -04:00
|
|
|
object.insert("submitter_id", m_uniqueId);
|
2023-04-23 21:05:38 -04:00
|
|
|
object.insert("ingest_id", ingestId);
|
2023-04-14 14:44:28 -04:00
|
|
|
|
2023-06-28 20:42:40 -04:00
|
|
|
QString attribution = MySettings::globalInstance()->networkAttribution();
|
2023-04-14 14:44:28 -04:00
|
|
|
if (!attribution.isEmpty())
|
2023-04-28 10:54:05 -04:00
|
|
|
object.insert("network/attribution", attribution);
|
2023-04-14 14:44:28 -04:00
|
|
|
|
2023-07-01 11:34:21 -04:00
|
|
|
QString promptTemplate = ChatListModel::globalInstance()->currentChat()->modelInfo().promptTemplate();
|
2023-05-15 18:57:13 -04:00
|
|
|
object.insert("prompt_template", promptTemplate);
|
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
QJsonDocument newDoc;
|
|
|
|
newDoc.setObject(object);
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
2023-04-26 22:05:56 -04:00
|
|
|
printf("%s\n", qPrintable(newDoc.toJson(QJsonDocument::Indented)));
|
2023-04-14 14:44:28 -04:00
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
2023-04-26 22:05:56 -04:00
|
|
|
|
2023-04-23 21:05:38 -04:00
|
|
|
QUrl jsonUrl("https://api.gpt4all.io/v1/ingest/chat");
|
2023-04-14 14:44:28 -04:00
|
|
|
QNetworkRequest request(jsonUrl);
|
2023-04-24 00:05:06 -04:00
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
2023-05-14 20:11:10 -04:00
|
|
|
QByteArray body(newDoc.toJson(QJsonDocument::Compact));
|
2023-04-14 14:44:28 -04:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
QNetworkReply *jsonReply = m_networkManager.post(request, body);
|
2024-06-04 14:47:11 -04:00
|
|
|
connect(qGuiApp, &QCoreApplication::aboutToQuit, jsonReply, &QNetworkReply::abort);
|
2023-04-14 14:44:28 -04:00
|
|
|
connect(jsonReply, &QNetworkReply::finished, this, &Network::handleJsonUploadFinished);
|
|
|
|
m_activeUploads.append(jsonReply);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::handleJsonUploadFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *jsonReply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!jsonReply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_activeUploads.removeAll(jsonReply);
|
|
|
|
|
|
|
|
QVariant response = jsonReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
Q_ASSERT(response.isValid());
|
|
|
|
bool ok;
|
|
|
|
int code = response.toInt(&ok);
|
|
|
|
if (!ok)
|
2023-04-23 21:05:38 -04:00
|
|
|
qWarning() << "ERROR: ingest invalid response.";
|
2023-04-23 07:35:38 -04:00
|
|
|
if (code != 200) {
|
2023-04-23 21:05:38 -04:00
|
|
|
qWarning() << "ERROR: ingest response != 200 code:" << code;
|
2023-04-23 07:35:38 -04:00
|
|
|
sendHealth();
|
|
|
|
}
|
2023-04-14 14:44:28 -04:00
|
|
|
|
2023-06-02 15:46:41 -04:00
|
|
|
#if defined(DEBUG)
|
2023-04-14 14:44:28 -04:00
|
|
|
QByteArray jsonData = jsonReply->readAll();
|
|
|
|
QJsonParseError err;
|
2023-06-02 15:46:41 -04:00
|
|
|
|
2023-04-14 14:44:28 -04:00
|
|
|
QJsonDocument document = QJsonDocument::fromJson(jsonData, &err);
|
|
|
|
if (err.error != QJsonParseError::NoError) {
|
|
|
|
qDebug() << "ERROR: Couldn't parse: " << jsonData << err.errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-26 22:05:56 -04:00
|
|
|
printf("%s\n", qPrintable(document.toJson(QJsonDocument::Indented)));
|
2023-04-14 14:44:28 -04:00
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
jsonReply->deleteLater();
|
|
|
|
}
|
|
|
|
|
2023-04-24 17:52:19 -04:00
|
|
|
void Network::handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
|
|
|
|
{
|
|
|
|
QUrl url = reply->request().url();
|
2023-06-02 15:46:41 -04:00
|
|
|
for (const auto &e : errors)
|
2023-04-24 17:52:19 -04:00
|
|
|
qWarning() << "ERROR: Received ssl error:" << e.errorString() << "for" << url;
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:54:05 -04:00
|
|
|
void Network::sendOptOut()
|
|
|
|
{
|
|
|
|
QJsonObject properties;
|
2024-04-25 13:16:52 -04:00
|
|
|
properties.insert("token", MIXPANEL_TOKEN);
|
|
|
|
properties.insert("time", QDateTime::currentMSecsSinceEpoch());
|
2023-04-28 10:54:05 -04:00
|
|
|
properties.insert("distinct_id", m_uniqueId);
|
|
|
|
properties.insert("$insert_id", generateUniqueId());
|
|
|
|
|
|
|
|
QJsonObject event;
|
|
|
|
event.insert("event", "opt_out");
|
|
|
|
event.insert("properties", properties);
|
|
|
|
|
|
|
|
QJsonArray array;
|
|
|
|
array.append(event);
|
|
|
|
|
|
|
|
QJsonDocument doc;
|
|
|
|
doc.setArray(array);
|
2024-05-01 12:08:40 -04:00
|
|
|
emit requestMixpanel(doc.toJson(QJsonDocument::Compact));
|
2023-04-28 10:54:05 -04:00
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
printf("%s %s\n", qPrintable("opt_out"), qPrintable(doc.toJson(QJsonDocument::Indented)));
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-04-26 22:05:56 -04:00
|
|
|
void Network::sendStartup()
|
|
|
|
{
|
2024-04-25 13:16:52 -04:00
|
|
|
const auto *mySettings = MySettings::globalInstance();
|
|
|
|
Q_ASSERT(mySettings->isNetworkUsageStatsActiveSet());
|
|
|
|
if (!mySettings->networkUsageStatsActive()) {
|
|
|
|
// send a single opt-out per session after the user has made their selections,
|
|
|
|
// unless this is a normal start (same version) and the user was already opted out
|
|
|
|
if (!m_hasSentOptOut) {
|
|
|
|
sendOptOut();
|
|
|
|
m_hasSentOptOut = true;
|
|
|
|
}
|
2023-05-02 20:31:17 -04:00
|
|
|
return;
|
2024-04-25 13:16:52 -04:00
|
|
|
}
|
2023-05-02 20:31:17 -04:00
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
// only chance to enable usage stats is at the start of a new session
|
|
|
|
m_sendUsageStats = true;
|
|
|
|
|
|
|
|
const auto *display = QGuiApplication::primaryScreen();
|
|
|
|
trackEvent("startup", {
|
|
|
|
{"$screen_dpi", std::round(display->physicalDotsPerInch())},
|
2024-06-24 18:49:23 -04:00
|
|
|
{"display", u"%1x%2"_s.arg(display->size().width()).arg(display->size().height())},
|
2024-04-25 13:16:52 -04:00
|
|
|
{"ram", LLM::globalInstance()->systemTotalRAMInGB()},
|
|
|
|
{"cpu", getCPUModel()},
|
2024-05-02 16:09:41 -04:00
|
|
|
{"cpu_supports_avx2", LLModel::Implementation::cpuSupportsAVX2()},
|
2024-04-25 13:16:52 -04:00
|
|
|
{"datalake_active", mySettings->networkIsActive()},
|
|
|
|
});
|
|
|
|
sendIpify();
|
|
|
|
|
|
|
|
// mirror opt-out logic so the ratio can be used to infer totals
|
|
|
|
if (!m_hasSentOptIn) {
|
|
|
|
trackEvent("opt_in");
|
|
|
|
m_hasSentOptIn = true;
|
|
|
|
}
|
2023-05-02 20:31:17 -04:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
void Network::trackChatEvent(const QString &ev, QVariantMap props)
|
2023-05-08 08:31:35 -04:00
|
|
|
{
|
2024-04-25 13:16:52 -04:00
|
|
|
const auto &curChat = ChatListModel::globalInstance()->currentChat();
|
|
|
|
if (!props.contains("model"))
|
|
|
|
props.insert("model", curChat->modelInfo().filename());
|
2024-06-26 15:26:27 -04:00
|
|
|
props.insert("device_backend", curChat->deviceBackend());
|
2024-04-25 13:16:52 -04:00
|
|
|
props.insert("actualDevice", curChat->device());
|
|
|
|
props.insert("doc_collections_enabled", curChat->collectionList().count());
|
|
|
|
props.insert("doc_collections_total", LocalDocs::globalInstance()->localDocsModel()->rowCount());
|
|
|
|
props.insert("datalake_active", MySettings::globalInstance()->networkIsActive());
|
|
|
|
props.insert("using_server", ChatListModel::globalInstance()->currentChat()->isServer());
|
|
|
|
trackEvent(ev, props);
|
2023-05-08 08:31:35 -04:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
void Network::trackEvent(const QString &ev, const QVariantMap &props)
|
2023-04-26 22:05:56 -04:00
|
|
|
{
|
2024-04-25 13:16:52 -04:00
|
|
|
if (!m_sendUsageStats)
|
2023-04-26 22:05:56 -04:00
|
|
|
return;
|
|
|
|
|
2023-06-22 15:44:49 -04:00
|
|
|
Q_ASSERT(ChatListModel::globalInstance()->currentChat());
|
2023-04-26 22:05:56 -04:00
|
|
|
QJsonObject properties;
|
2024-04-25 13:16:52 -04:00
|
|
|
|
|
|
|
properties.insert("token", MIXPANEL_TOKEN);
|
|
|
|
if (!props.contains("time"))
|
|
|
|
properties.insert("time", QDateTime::currentMSecsSinceEpoch());
|
|
|
|
properties.insert("distinct_id", m_uniqueId); // effectively a device ID
|
2023-04-26 22:05:56 -04:00
|
|
|
properties.insert("$insert_id", generateUniqueId());
|
2024-04-25 13:16:52 -04:00
|
|
|
|
2023-04-26 22:05:56 -04:00
|
|
|
if (!m_ipify.isEmpty())
|
|
|
|
properties.insert("ip", m_ipify);
|
2023-05-02 16:24:06 -04:00
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
properties.insert("$os", QSysInfo::prettyProductName());
|
|
|
|
properties.insert("session_id", m_sessionId);
|
|
|
|
properties.insert("name", QCoreApplication::applicationName() + " v" + QCoreApplication::applicationVersion());
|
|
|
|
|
|
|
|
for (const auto &[key, value]: props.asKeyValueRange())
|
|
|
|
properties.insert(key, QJsonValue::fromVariant(value));
|
2023-05-02 20:31:17 -04:00
|
|
|
|
2023-04-26 22:05:56 -04:00
|
|
|
QJsonObject event;
|
|
|
|
event.insert("event", ev);
|
|
|
|
event.insert("properties", properties);
|
|
|
|
|
|
|
|
QJsonArray array;
|
|
|
|
array.append(event);
|
|
|
|
|
|
|
|
QJsonDocument doc;
|
|
|
|
doc.setArray(array);
|
2024-04-25 13:16:52 -04:00
|
|
|
emit requestMixpanel(doc.toJson(QJsonDocument::Compact));
|
2023-04-26 22:05:56 -04:00
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
printf("%s %s\n", qPrintable(ev), qPrintable(doc.toJson(QJsonDocument::Indented)));
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::sendIpify()
|
|
|
|
{
|
2024-04-25 13:16:52 -04:00
|
|
|
if (!m_sendUsageStats || !m_ipify.isEmpty())
|
2023-04-26 22:05:56 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
QUrl ipifyUrl("https://api.ipify.org");
|
|
|
|
QNetworkRequest request(ipifyUrl);
|
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
|
|
|
QNetworkReply *reply = m_networkManager.get(request);
|
2024-06-04 14:47:11 -04:00
|
|
|
connect(qGuiApp, &QCoreApplication::aboutToQuit, reply, &QNetworkReply::abort);
|
2023-04-26 22:05:56 -04:00
|
|
|
connect(reply, &QNetworkReply::finished, this, &Network::handleIpifyFinished);
|
|
|
|
}
|
|
|
|
|
2024-05-01 12:08:40 -04:00
|
|
|
void Network::sendMixpanel(const QByteArray &json)
|
2023-04-26 22:05:56 -04:00
|
|
|
{
|
|
|
|
QUrl trackUrl("https://api.mixpanel.com/track");
|
|
|
|
QNetworkRequest request(trackUrl);
|
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
QNetworkReply *trackReply = m_networkManager.post(request, json);
|
2024-06-04 14:47:11 -04:00
|
|
|
connect(qGuiApp, &QCoreApplication::aboutToQuit, trackReply, &QNetworkReply::abort);
|
2023-04-26 22:05:56 -04:00
|
|
|
connect(trackReply, &QNetworkReply::finished, this, &Network::handleMixpanelFinished);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::handleIpifyFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QVariant response = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
Q_ASSERT(response.isValid());
|
|
|
|
bool ok;
|
|
|
|
int code = response.toInt(&ok);
|
|
|
|
if (!ok)
|
|
|
|
qWarning() << "ERROR: ipify invalid response.";
|
|
|
|
if (code != 200)
|
|
|
|
qWarning() << "ERROR: ipify response != 200 code:" << code;
|
2024-03-06 17:14:54 -05:00
|
|
|
m_ipify = reply->readAll();
|
2023-04-26 22:05:56 -04:00
|
|
|
#if defined(DEBUG)
|
2024-03-06 17:14:54 -05:00
|
|
|
printf("ipify finished %s\n", qPrintable(m_ipify));
|
2023-04-26 22:05:56 -04:00
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
reply->deleteLater();
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
trackEvent("ipify_complete");
|
2023-04-26 22:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Network::handleMixpanelFinished()
|
|
|
|
{
|
|
|
|
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
|
|
|
|
if (!reply)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QVariant response = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
Q_ASSERT(response.isValid());
|
|
|
|
bool ok;
|
|
|
|
int code = response.toInt(&ok);
|
|
|
|
if (!ok)
|
|
|
|
qWarning() << "ERROR: track invalid response.";
|
|
|
|
if (code != 200)
|
|
|
|
qWarning() << "ERROR: track response != 200 code:" << code;
|
|
|
|
#if defined(DEBUG)
|
|
|
|
printf("mixpanel finished %s\n", qPrintable(reply->readAll()));
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
2023-04-23 21:05:38 -04:00
|
|
|
bool Network::sendConversation(const QString &ingestId, const QString &conversation)
|
2023-04-14 14:44:28 -04:00
|
|
|
{
|
2023-04-23 21:05:38 -04:00
|
|
|
return packageAndSendJson(ingestId, conversation);
|
2023-04-14 14:44:28 -04:00
|
|
|
}
|
2023-04-23 07:35:38 -04:00
|
|
|
|
|
|
|
void Network::sendHealth()
|
|
|
|
{
|
2023-04-23 21:05:38 -04:00
|
|
|
QUrl healthUrl("https://api.gpt4all.io/v1/health");
|
2023-04-23 07:35:38 -04:00
|
|
|
QNetworkRequest request(healthUrl);
|
2023-04-24 00:05:06 -04:00
|
|
|
QSslConfiguration conf = request.sslConfiguration();
|
|
|
|
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
request.setSslConfiguration(conf);
|
2023-04-23 07:35:38 -04:00
|
|
|
QNetworkReply *healthReply = m_networkManager.get(request);
|
2024-06-04 14:47:11 -04:00
|
|
|
connect(qGuiApp, &QCoreApplication::aboutToQuit, healthReply, &QNetworkReply::abort);
|
2023-04-23 07:35:38 -04:00
|
|
|
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)
|
2023-04-23 21:05:38 -04:00
|
|
|
qWarning() << "ERROR: health invalid response.";
|
2023-04-23 07:35:38 -04:00
|
|
|
if (code != 200) {
|
2023-04-23 21:05:38 -04:00
|
|
|
qWarning() << "ERROR: health response != 200 code:" << code;
|
2023-04-23 07:35:38 -04:00
|
|
|
emit healthCheckFailed(code);
|
2023-06-28 20:42:40 -04:00
|
|
|
MySettings::globalInstance()->setNetworkIsActive(false);
|
2023-04-23 07:35:38 -04:00
|
|
|
}
|
|
|
|
healthReply->deleteLater();
|
|
|
|
}
|