mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-07-25 14:02:06 +08:00
UI: Clear Model Cache (#1058)
* clear model cache * add cache size * move to model manager * fix handling for default model --------- Co-authored-by: DevTekVE <devtekve@gmail.com> Co-authored-by: James Vecellio-Grant <159560811+Discountchubbs@users.noreply.github.com>
This commit is contained in:
@@ -156,6 +156,7 @@ inline static std::unordered_map<std::string, uint32_t> keys = {
|
||||
|
||||
// Model Manager params
|
||||
{"ModelManager_ActiveBundle", PERSISTENT},
|
||||
{"ModelManager_ClearCache", CLEAR_ON_MANAGER_START},
|
||||
{"ModelManager_DownloadIndex", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
||||
{"ModelManager_LastSyncTime", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
|
||||
{"ModelManager_ModelsCache", PERSISTENT | BACKUP},
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <algorithm>
|
||||
#include <QJsonDocument>
|
||||
#include <QStyle>
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
#include <QDir>
|
||||
|
||||
#include "common/model.h"
|
||||
#include "selfdrive/ui/sunnypilot/qt/offroad/settings/models_panel.h"
|
||||
@@ -66,6 +68,11 @@ ModelsPanel::ModelsPanel(QWidget *parent) : QWidget(parent) {
|
||||
connect(uiStateSP(), &UIStateSP::uiUpdate, this, &ModelsPanel::updateLabels);
|
||||
list->addItem(currentModelLblBtn);
|
||||
|
||||
clearModelCacheBtn = new ButtonControlSP(tr("Clear Model Cache"), tr("CLEAR"), "", this);
|
||||
connect(clearModelCacheBtn, &ButtonControlSP::clicked, this, &ModelsPanel::clearModelCache);
|
||||
|
||||
list->addItem(clearModelCacheBtn);
|
||||
|
||||
// Create progress bars for downloads
|
||||
supercomboProgressBar = createProgressBar(this);
|
||||
QString supercomboType = tr("Driving Model");
|
||||
@@ -367,6 +374,8 @@ void ModelsPanel::updateLabels() {
|
||||
delay_control->setLabel(QString::number(value, 'f', 2) + "s");
|
||||
delay_control->showDescription();
|
||||
}
|
||||
|
||||
clearModelCacheBtn->setValue(QString::number(calculateCacheSize(), 'f', 2) + " MB");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -387,3 +396,32 @@ void ModelsPanel::showResetParamsDialog() {
|
||||
params.remove("LiveTorqueParameters");
|
||||
}
|
||||
}
|
||||
|
||||
void ModelsPanel::clearModelCache() {
|
||||
QString confirmMsg = tr("This will delete ALL downloaded models from the cache"
|
||||
"<br/><u>except the currently active model</u>."
|
||||
"<br/><br/>Are you sure you want to continue?");
|
||||
QString content("<body><h2 style=\"text-align: center;\">" + tr("Driving Model Selector") + "</h2><br>"
|
||||
"<p style=\"text-align: center; margin: 0 128px; font-size: 50px;\">" + confirmMsg + "</p></body>");
|
||||
if (showConfirmationDialog(
|
||||
content,
|
||||
tr("Clear Cache"))) {
|
||||
params.putBool("ModelManager_ClearCache", true);
|
||||
}
|
||||
}
|
||||
|
||||
double ModelsPanel::calculateCacheSize() {
|
||||
QFuture<qint64> future_ModelCacheSize = QtConcurrent::run([=]() {
|
||||
|
||||
QDir model_dir(QString::fromStdString(Path::model_root()));
|
||||
QFileInfoList model_files = model_dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
|
||||
qint64 totalSize = 0;
|
||||
for (const QFileInfo &model_file : model_files) {
|
||||
if (model_file.isFile()) {
|
||||
totalSize += model_file.size();
|
||||
}
|
||||
}
|
||||
return totalSize;
|
||||
});
|
||||
return static_cast<double>(future_ModelCacheSize) / (1024.0 * 1024.0);
|
||||
}
|
||||
|
||||
@@ -41,6 +41,8 @@ private:
|
||||
cereal::ModelManagerSP::Reader model_manager;
|
||||
cereal::ModelManagerSP::DownloadStatus download_status{};
|
||||
cereal::ModelManagerSP::DownloadStatus prev_download_status{};
|
||||
void clearModelCache();
|
||||
double calculateCacheSize();
|
||||
|
||||
bool canContinueOnMeteredDialog() {
|
||||
if (!is_metered) return true;
|
||||
@@ -75,5 +77,6 @@ private:
|
||||
QProgressBar *policyProgressBar;
|
||||
QFrame *policyFrame;
|
||||
Params params;
|
||||
ButtonControlSP *clearModelCacheBtn;
|
||||
|
||||
};
|
||||
|
||||
@@ -178,6 +178,10 @@ class ModelManagerSP:
|
||||
finally:
|
||||
self.params.put("ModelManager_DownloadIndex", "")
|
||||
|
||||
if self.params.get("ModelManager_ClearCache", block=False, encoding="utf-8"):
|
||||
self.clear_model_cache()
|
||||
self.params.remove("ModelManager_ClearCache")
|
||||
|
||||
self._report_status()
|
||||
rk.keep_time()
|
||||
|
||||
@@ -185,6 +189,31 @@ class ModelManagerSP:
|
||||
cloudlog.exception(f"Error in main thread: {str(e)}")
|
||||
rk.keep_time()
|
||||
|
||||
def clear_model_cache(self) -> None:
|
||||
"""
|
||||
Clears the model cache directory of all files except those in the active model bundle.
|
||||
"""
|
||||
|
||||
# Get list of files used by active model bundle
|
||||
active_files = []
|
||||
if self.active_bundle is not None: # When the default model is active
|
||||
for model in self.active_bundle.models:
|
||||
if hasattr(model, 'artifact') and model.artifact.fileName:
|
||||
active_files.append(model.artifact.fileName)
|
||||
if hasattr(model, 'metadata') and model.metadata.fileName:
|
||||
active_files.append(model.metadata.fileName)
|
||||
|
||||
# Remove all files except active ones
|
||||
model_dir = Paths.model_root()
|
||||
try:
|
||||
for filename in os.listdir(model_dir):
|
||||
if filename not in active_files:
|
||||
file_path = os.path.join(model_dir, filename)
|
||||
if os.path.isfile(file_path):
|
||||
os.remove(file_path)
|
||||
cloudlog.info("Model cache cleared, keeping active model files")
|
||||
except Exception as e:
|
||||
cloudlog.exception(f"Error clearing model cache: {str(e)}")
|
||||
|
||||
def main():
|
||||
ModelManagerSP().main_thread()
|
||||
|
||||
@@ -55,4 +55,8 @@ namespace Path {
|
||||
return "/dev/shm";
|
||||
#endif
|
||||
}
|
||||
|
||||
inline std::string model_root() {
|
||||
return Hardware::PC() ? Path::comma_home() + "/media/0/models" : "/data/media/0/models";
|
||||
}
|
||||
} // namespace Path
|
||||
|
||||
Reference in New Issue
Block a user