|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +#include "globals.h" |
| 6 | +#include "launchdurationreporter.h" |
| 7 | + |
| 8 | +#ifdef HAVE_DDE_API_EVENTLOGGER |
| 9 | +#include <dde-api/eventlogger.hpp> |
| 10 | +#endif |
| 11 | + |
| 12 | +#include <QDBusConnection> |
| 13 | +#include <QDBusInterface> |
| 14 | +#include <QDBusObjectPath> |
| 15 | +#include <QDBusReply> |
| 16 | +#include <QDateTime> |
| 17 | +#include <QJsonDocument> |
| 18 | +#include <QJsonObject> |
| 19 | +#include <QJsonArray> |
| 20 | +#include <QLoggingCategory> |
| 21 | +#include <QFile> |
| 22 | +#include <QProcess> |
| 23 | +#include <QThread> |
| 24 | +#include <QtConcurrent> |
| 25 | + |
| 26 | +Q_LOGGING_CATEGORY(launchDurationReporter, "org.deepin.dde.shell.dock.launchDurationReporter") |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +using dock::escapeToObjectPath; |
| 31 | + |
| 32 | +constexpr auto kAmService = "org.desktopspec.ApplicationManager1"; |
| 33 | +constexpr auto kApplicationIface = "org.desktopspec.ApplicationManager1.Application"; |
| 34 | +constexpr auto kInstanceIface = "org.desktopspec.ApplicationManager1.Instance"; |
| 35 | +constexpr int kLinglongCacheTTLSeconds = 1800; // 30 minutes |
| 36 | +constexpr int kDebCacheTTLSeconds = 1800; // 30 minutes |
| 37 | + |
| 38 | +struct InstanceInfo { |
| 39 | + QString instanceId; |
| 40 | + QString launchType; |
| 41 | +}; |
| 42 | + |
| 43 | +QList<InstanceInfo> queryInstances(const QString &desktopId) |
| 44 | +{ |
| 45 | + QList<InstanceInfo> result; |
| 46 | + auto appPath = QStringLiteral("/org/desktopspec/ApplicationManager1/%1").arg(escapeToObjectPath(desktopId)); |
| 47 | + |
| 48 | + QDBusInterface appIface(QString::fromUtf8(kAmService), |
| 49 | + appPath, |
| 50 | + QStringLiteral("org.freedesktop.DBus.Properties"), |
| 51 | + QDBusConnection::sessionBus()); |
| 52 | + appIface.setTimeout(5000); // 5 second timeout |
| 53 | + QDBusReply<QVariant> reply = appIface.call(QStringLiteral("Get"), |
| 54 | + QString::fromUtf8(kApplicationIface), |
| 55 | + QStringLiteral("Instances")); |
| 56 | + if (!reply.isValid()) { |
| 57 | + qCDebug(launchDurationReporter) << "[DockIconTiming] queryInstances failed for" << desktopId << ":" << reply.error().message(); |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + const auto paths = qdbus_cast<QList<QDBusObjectPath>>(reply.value()); |
| 62 | + for (const auto &path : paths) { |
| 63 | + QDBusInterface instIface(QString::fromUtf8(kAmService), |
| 64 | + path.path(), |
| 65 | + QStringLiteral("org.freedesktop.DBus.Properties"), |
| 66 | + QDBusConnection::sessionBus()); |
| 67 | + instIface.setTimeout(5000); // 5 second timeout |
| 68 | + |
| 69 | + InstanceInfo info; |
| 70 | + info.instanceId = path.path().section(QLatin1Char('/'), -1); |
| 71 | + |
| 72 | + auto launchTypeReply = instIface.call(QStringLiteral("Get"), |
| 73 | + QString::fromUtf8(kInstanceIface), |
| 74 | + QStringLiteral("LaunchType")); |
| 75 | + if (launchTypeReply.type() == QDBusMessage::ReplyMessage) { |
| 76 | + info.launchType = qdbus_cast<QDBusVariant>(launchTypeReply.arguments().constFirst()).variant().toString(); |
| 77 | + } |
| 78 | + if (info.launchType.isEmpty()) { |
| 79 | + info.launchType = QStringLiteral("unknown"); |
| 80 | + } |
| 81 | + |
| 82 | + result.append(info); |
| 83 | + } |
| 84 | + return result; |
| 85 | +} |
| 86 | + |
| 87 | +QHash<QString, QString> loadAllLinglongVersions() |
| 88 | +{ |
| 89 | + QHash<QString, QString> result; |
| 90 | + |
| 91 | + QProcess proc; |
| 92 | + proc.start(QStringLiteral("ll-cli"), {QStringLiteral("list"), QStringLiteral("--type"), QStringLiteral("app")}); |
| 93 | + if (!proc.waitForFinished(3000)) { |
| 94 | + qCWarning(launchDurationReporter) << "ll-cli list timeout"; |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + if (proc.exitCode() != 0) { |
| 99 | + qCWarning(launchDurationReporter) << "ll-cli list failed, exitCode:" << proc.exitCode(); |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + QString output = QString::fromUtf8(proc.readAllStandardOutput()); |
| 104 | + QStringList lines = output.split(QLatin1Char('\n'), Qt::SkipEmptyParts); |
| 105 | + |
| 106 | + // Skip header line |
| 107 | + for (int i = 1; i < lines.size(); ++i) { |
| 108 | + QStringList columns = lines[i].simplified().split(QLatin1Char(' ')); |
| 109 | + if (columns.size() >= 3) { |
| 110 | + QString name = columns[1]; // 名称 column |
| 111 | + QString version = columns[2]; // 版本 column |
| 112 | + if (!name.isEmpty()) { |
| 113 | + result.insert(name, version); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return result; |
| 119 | +} |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +namespace dock { |
| 124 | + |
| 125 | +LaunchDurationReporter::LaunchDurationReporter(QObject *parent) |
| 126 | + : QObject(parent) |
| 127 | +{ |
| 128 | + m_workerPool.setMaxThreadCount(1); |
| 129 | +} |
| 130 | + |
| 131 | +LaunchDurationReporter::~LaunchDurationReporter() |
| 132 | +{ |
| 133 | + m_workerPool.waitForDone(); |
| 134 | +} |
| 135 | + |
| 136 | +void LaunchDurationReporter::reportWindowAppeared(const QString &desktopId) |
| 137 | +{ |
| 138 | + if (desktopId.isEmpty()) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + auto future = QtConcurrent::run(&m_workerPool, [this, desktopId]() { |
| 143 | + // Start async D-Bus query in global thread pool |
| 144 | + auto dbusQueryFuture = QtConcurrent::run([this, desktopId]() { |
| 145 | + return queryInstances(desktopId); |
| 146 | + }); |
| 147 | + |
| 148 | + // Query package version and type in parallel |
| 149 | + QString version; |
| 150 | + QString pakType; |
| 151 | + |
| 152 | + // Check cache with proper TTL management |
| 153 | + { |
| 154 | + QMutexLocker locker(&m_cacheMutex); |
| 155 | + qint64 currentTime = QDateTime::currentSecsSinceEpoch(); |
| 156 | + |
| 157 | + // Refresh linglong cache if expired |
| 158 | + if ((currentTime - m_linglongCacheTime) > kLinglongCacheTTLSeconds) { |
| 159 | + m_linglongCache = loadAllLinglongVersions(); |
| 160 | + m_linglongCacheTime = currentTime; |
| 161 | + } |
| 162 | + |
| 163 | + // Check linglong cache first |
| 164 | + if (m_linglongCache.contains(desktopId)) { |
| 165 | + version = m_linglongCache.value(desktopId); |
| 166 | + pakType = QStringLiteral("linglong"); |
| 167 | + } |
| 168 | + // Check deb cache with per-entry TTL |
| 169 | + else if (m_debCache.contains(desktopId)) { |
| 170 | + const auto &entry = m_debCache.value(desktopId); |
| 171 | + if ((currentTime - entry.timestamp) <= kDebCacheTTLSeconds) { |
| 172 | + version = entry.version; |
| 173 | + pakType = entry.pakType; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // If not found or expired, query dpkg |
| 179 | + if (pakType.isEmpty()) { |
| 180 | + QProcess proc; |
| 181 | + proc.start(QStringLiteral("dpkg-query"), {QStringLiteral("-W"), QStringLiteral("-f=${Version}"), desktopId}); |
| 182 | + proc.waitForFinished(1000); |
| 183 | + if (proc.exitCode() == 0) { |
| 184 | + version = QString::fromUtf8(proc.readAllStandardOutput()).trimmed(); |
| 185 | + pakType = QStringLiteral("deb"); |
| 186 | + } else { |
| 187 | + qCDebug(launchDurationReporter) << "dpkg-query failed for" << desktopId << "exitCode:" << proc.exitCode(); |
| 188 | + pakType = QStringLiteral("unknown"); |
| 189 | + } |
| 190 | + |
| 191 | + // Cache the result in deb cache |
| 192 | + QMutexLocker locker(&m_cacheMutex); |
| 193 | + m_debCache.insert(desktopId, {version, pakType, QDateTime::currentSecsSinceEpoch()}); |
| 194 | + } |
| 195 | + |
| 196 | + // Wait for D-Bus query result |
| 197 | + dbusQueryFuture.waitForFinished(); |
| 198 | + auto instances = dbusQueryFuture.result(); |
| 199 | + |
| 200 | + QString uniqueId; |
| 201 | + QString launchType = QStringLiteral("unknown"); |
| 202 | + if (!instances.isEmpty()) { |
| 203 | + const auto &latest = instances.constLast(); |
| 204 | + uniqueId = latest.instanceId; |
| 205 | + launchType = latest.launchType; |
| 206 | + } |
| 207 | + |
| 208 | + if (uniqueId.isEmpty()) { |
| 209 | + return; |
| 210 | + } |
| 211 | + |
| 212 | + QMetaObject::invokeMethod(this, [this, desktopId, uniqueId, launchType, version, pakType]() { |
| 213 | + doReport(desktopId, uniqueId, launchType, version, pakType); |
| 214 | + }, Qt::QueuedConnection); |
| 215 | + }); |
| 216 | + Q_UNUSED(future) |
| 217 | +} |
| 218 | + |
| 219 | +void LaunchDurationReporter::doReport(const QString &desktopId, |
| 220 | + const QString &uniqueId, |
| 221 | + const QString &launchType, |
| 222 | + const QString &version, |
| 223 | + const QString &pakType) |
| 224 | +{ |
| 225 | +#ifdef HAVE_DDE_API_EVENTLOGGER |
| 226 | + DDE_EventLogger::EventLogger::instance().writeEventLog({ |
| 227 | + 1000610003, |
| 228 | + desktopId, |
| 229 | + QJsonObject{ |
| 230 | + {"app_name", desktopId}, |
| 231 | + {"launch_type", launchType}, |
| 232 | + {"app_version", version}, |
| 233 | + {"unique_id", uniqueId}, |
| 234 | + {"time", QDateTime::currentMSecsSinceEpoch()}, |
| 235 | + {"app_package_type", pakType}, |
| 236 | + }, |
| 237 | + }); |
| 238 | +#else |
| 239 | + Q_UNUSED(desktopId) |
| 240 | + Q_UNUSED(uniqueId) |
| 241 | + Q_UNUSED(launchType) |
| 242 | + Q_UNUSED(version) |
| 243 | + Q_UNUSED(pakType) |
| 244 | +#endif |
| 245 | +} |
| 246 | + |
| 247 | +} |
0 commit comments