Commit 56ffa5e1 by yahan.li

1. 如果数据库表不存在,则重建表

2. 前台启时,如果权限导致程序起不来,控制台输出,进行提示
	修改:     RadomForestSAC/RamdomForestCalculate.cpp
	修改:     main.cpp
	修改:     package/E3/SigerCalculation
parent c230cbd0
...@@ -5,16 +5,25 @@ ...@@ -5,16 +5,25 @@
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <QFileInfo> #include <QFileInfo>
void delete_sac_data() void delete_excess_files(const char *path, const char *suffix, int threshold)
{ {
std::vector<std::string> vec;
int to_delete = 0;
int done = 0; int done = 0;
int len = 0; int len = 0;
int ret = 0; int ret = 0;
char pa[300]; char pa[512];
const char *name = NULL; const char *name = NULL;
DIR* dir = opendir("/home/pi/SigerTMS/stream/SAC"); if (!path || !suffix)
if (!dir) return; return;
int suffix_len = strlen(suffix);
DIR* dir = opendir(path);
if (!dir){
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"), "opendir {} failed", path);
return;
}
struct dirent* entry = NULL; struct dirent* entry = NULL;
while ((entry = readdir(dir)) != nullptr) { while ((entry = readdir(dir)) != nullptr) {
...@@ -22,57 +31,73 @@ void delete_sac_data() ...@@ -22,57 +31,73 @@ void delete_sac_data()
continue; continue;
name = entry->d_name; name = entry->d_name;
len = strlen(entry->d_name); len = strlen(name);
if (len>4 && !strcmp(&name[len-4], ".dat")) if (len>=suffix_len && !strcmp(&name[len-suffix_len], suffix))
{ {
ret = snprintf(pa, sizeof(pa)-1, "%s/%s", "/home/pi/SigerTMS/stream/SAC", name); if (path){
pa[ret] = 0; ret = snprintf(pa, sizeof(pa)-1, "%s/%s", path, name);
if (!unlink(entry->d_name)) pa[ret] = 0;
++done; vec.push_back(pa);
}
} }
} }
closedir(dir); closedir(dir);
if (done > 0) to_delete = static_cast<int>(vec.size());
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"done={}", done); if (to_delete > threshold)
{
for (int i=0; i<to_delete; ++i)
{
if (!unlink(vec[i].c_str()))
++done;
}
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"to_delete={} done={}", to_delete, done);
}
} }
void delete_excess_rf_models() bool checkDbHealth(const QString& path)
{ {
int to_delete = 0; static time_t last_time = 0;
int done = 0; time_t now = time(NULL);
if (last_time && (int)(now-last_time) < 600)
return true;
last_time = now;
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"), "[checkDbHealth] Cannot open file for reading");
return false;
}
DIR* dir = opendir("."); QByteArray header = file.read(16);
if (!dir) return; file.close();
struct dirent* entry = NULL; if (!header.startsWith("SQLite format 3")) {
while ((entry = readdir(dir)) != nullptr) { SPDLOG_LOGGER_DEBUG(spdlog::get("logger"), "[checkDbHealth] Not a valid SQLite database");
if ('.' == entry->d_name[0]) return false;
continue;
std::string filename = entry->d_name;
if (filename.find("rf_model.dat") != std::string::npos)
++to_delete;
} }
if (to_delete >= 1000) QString connName = QString("check_%1").arg(now);
{ auto db = QSqlDatabase::addDatabase("QSQLITE", connName);
rewinddir(dir); db.setDatabaseName(path);
while ((entry = readdir(dir)) != nullptr) {
if ('.' == entry->d_name[0])
continue;
std::string filename = entry->d_name; if (!db.open()){
if (filename.find("rf_model.dat") != std::string::npos){ QSqlDatabase::removeDatabase(connName);
if (!unlink(entry->d_name)) SPDLOG_LOGGER_DEBUG(spdlog::get("logger"), "[checkDbHealth] Cannot open db for reading");
++done; return false;
} }
}
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"to_delete={} done={}", to_delete, done); bool ok=false;
{
QSqlQuery q("SELECT 1 FROM sqlite_master WHERE type='table' AND name='RamdomForestFeatureValue' LIMIT 1", db);
ok = q.next();
} }
closedir(dir); db.close();
QSqlDatabase::removeDatabase(connName);
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"checkDbHealth {} succ", path.toStdString());
return ok;
} }
int pickPriorityValue(double xpred, double ypred, double zpred) int pickPriorityValue(double xpred, double ypred, double zpred)
...@@ -90,21 +115,16 @@ int pickPriorityValue(double xpred, double ypred, double zpred) ...@@ -90,21 +115,16 @@ int pickPriorityValue(double xpred, double ypred, double zpred)
return 0; // 都没有 return 0; // 都没有
} }
void CreatTable() void CheckcAndCreatTable()
{ {
QString dbPath = QString("/home/pi/SigerTMS/stream/SAC/%1.db") QString dbPath = QString("/home/pi/SigerTMS/stream/SAC/%1.db")
.arg("RamdomForestFeatureValue"); .arg("RamdomForestFeatureValue");
// 判断数据库文件是否存在 // 判断数据库文件是否存在
QFileInfo fi(dbPath); if (true == checkDbHealth(dbPath))
if (fi.exists()){ return;
if (fi.size() > 0)
return;
int ret = unlink(dbPath.toStdString().c_str());
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"delete {} for zero-size: {}", dbPath.toStdString(), ret);
}
system("sudo rm -rf /home/pi/SigerTMS/stream/SAC/RamdomForestFeatureValue*");
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"), "Database not exists. Creating..."); SPDLOG_LOGGER_DEBUG(spdlog::get("logger"), "Database not exists. Creating...");
// ---- 短连接:打开 -> 执行建表语句 -> 关闭 ---- // ---- 短连接:打开 -> 执行建表语句 -> 关闭 ----
...@@ -193,10 +213,10 @@ RamdomForestCalculate::RamdomForestCalculate(QObject *parent) ...@@ -193,10 +213,10 @@ RamdomForestCalculate::RamdomForestCalculate(QObject *parent)
m_tcpServerManager->startServer(2026); m_tcpServerManager->startServer(2026);
//2.创建数据库表 //2.创建数据库表
CreatTable(); CheckcAndCreatTable();
//3.训练初始模型 //3.训练初始模型
delete_excess_rf_models(); delete_excess_files(".", "rf_model.dat", 1000);
Retrain(); Retrain();
//GLS Test //GLS Test
...@@ -352,6 +372,7 @@ int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata) ...@@ -352,6 +372,7 @@ int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata)
if (!ok) { if (!ok) {
SPDLOG_LOGGER_ERROR(spdlog::get("logger"), "Insert failed!"); SPDLOG_LOGGER_ERROR(spdlog::get("logger"), "Insert failed!");
CheckcAndCreatTable();
} }
//dbFunc->addDataToQueue(data); //dbFunc->addDataToQueue(data);
...@@ -536,7 +557,7 @@ void RamdomForestCalculate::Retrain() ...@@ -536,7 +557,7 @@ void RamdomForestCalculate::Retrain()
struct timeval start,end; struct timeval start,end;
gettimeofday(&start, NULL); gettimeofday(&start, NULL);
delete_sac_data(); delete_excess_files("/home/pi/SigerTMS/stream/SAC", ".dat", 0);
RamdomForestCalculate::queryRecordsByLabel(dbPath, record); RamdomForestCalculate::queryRecordsByLabel(dbPath, record);
DealRecord(record, result); DealRecord(record, result);
m_rf_map.clear(); m_rf_map.clear();
......
...@@ -31,9 +31,8 @@ void initSpdlog() ...@@ -31,9 +31,8 @@ void initSpdlog()
} }
catch (const spdlog::spdlog_ex& ex) catch (const spdlog::spdlog_ex& ex)
{ {
qDebug() << "spdlog 初始化失败:" << ex.what();
} }
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment