Commit 56ffa5e1 by yahan.li

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

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