From 5c1233a84f5044a9a2cf53da0cf16547726f60d8 Mon Sep 17 00:00:00 2001 From: Evgeny Gagua Date: Thu, 30 Nov 2023 08:03:16 -0800 Subject: [PATCH] MER Final stage 1 --- lib/App/common/SQLite_dataBinder.cpp | 46 ++++++++++++++++++++-------- lib/App/common/SQLite_dataBinder.h | 2 +- lib/App/mojo/mojoKernel.cpp | 27 ++++++++++------ 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/lib/App/common/SQLite_dataBinder.cpp b/lib/App/common/SQLite_dataBinder.cpp index 8aad818b..3593b1f7 100644 --- a/lib/App/common/SQLite_dataBinder.cpp +++ b/lib/App/common/SQLite_dataBinder.cpp @@ -522,12 +522,17 @@ void sqliteDBC::deleteNetwork( uint32_t NetID ){ }; -bool sqliteDBC::deleteReportStat( uint32_t id ){ +bool sqliteDBC::deleteReportStat( REPORT_DIRINFO &RDF ){ if ( m_bStaIsConnected ) { char tQBuf[MAX_PATH_LEN]; - sprintf(tQBuf, "DELETE FROM `days` WHERE `ROWID`=%d", id ); + sprintf( tQBuf, "DELETE FROM `days` WHERE `year`=%s AND `month`=%s AND `day`=%s", RDF.YEAR, RDF.MONTH, RDF.DAY ); +#ifdef DEBUG_ALL + LOG( tQBuf ); +#endif m_pDataStatistics->SilentQuery( tQBuf ); + return true; }; + return false; }; void sqliteDBC::deleteResource( uint32_t ResourceID ){ @@ -1681,20 +1686,30 @@ uint32_t sqliteDBC::getFirstEventTS(){ if( m_bStaIsConnected ){ char tQBuf[CTEXT]; AAssoc *result; - sprintf( tQBuf, "SELECT `ts` FROM `ts` ORDER BY `ts` LIMIT 1" ); + sprintf( tQBuf, "SELECT `year`,`month`,`day` FROM `days` ORDER BY `year`,`month`,`day` LIMIT 1" ); #ifdef DEBUG_ALL LOG(tQBuf); #endif result = m_pDataStatistics->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { + REPORT_DIRINFO RDF = { 0 }; Assoc *cArr; Assoc::iterator it; cArr = result->at(0); - it = cArr->find("ts"); + it = cArr->find("year"); if( it != cArr->end() ){ - rv = atoi( it->second.c_str() ); + strncpy( RDF.YEAR, it->second.c_str(), 4 ); + }; + it = cArr->find("month"); + if( it != cArr->end() ){ + strncpy( RDF.MONTH, it->second.c_str(), 2 ); }; + it = cArr->find("day"); + if( it != cArr->end() ){ + strncpy( RDF.DAY, it->second.c_str(), 2 ); + }; + rv = getTsFromRDF( RDF ); }; }; deleteResult( result ); @@ -1706,8 +1721,10 @@ bool sqliteDBC::deleteReportDate( uint32_t ts ){ if( ts ){ REPORT_DIRINFO tRDF = {0}; fillRDF( ts, tRDF ); + LOG("Delete RDF Dir"); if( deleteRDFDir( m_pDbReportBase, tRDF ) ){ - return deleteReportStat( ts ); + LOG("Dir Deleted OK"); + return deleteReportStat( tRDF ); }; }; return false; @@ -1716,19 +1733,24 @@ bool sqliteDBC::deleteReportDate( uint32_t ts ){ bool sqliteDBC::deleteRDFDir( CSTR Base, REPORT_DIRINFO &RDF ){ char tBuff[MAX_PATH]; sprintf( tBuff, "%s/%s/%s/%s", Base, RDF.YEAR, RDF.MONTH, RDF.DAY ); + // No Checking if dir is empty - dir contents all we need to delete! if( fileExist( tBuff ) ){ sprintf( tBuff, "rm -f -R \"%s/%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH, RDF.DAY ); system( tBuff ); }; sprintf( tBuff, "%s/%s/%s", Base, RDF.YEAR, RDF.MONTH ); - if( dirEmpty( tBuff ) ){ - sprintf( tBuff, "rm -f -R \"%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH ); - system( tBuff ); + if( fileExist( tBuff ) ){ + if( dirEmpty( tBuff ) ){ + sprintf( tBuff, "rm -f -R \"%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH ); + system( tBuff ); + }; }; sprintf( tBuff, "%s/%s", Base, RDF.YEAR ); - if( dirEmpty( tBuff ) ){ - sprintf( tBuff, "rm -f -R \"%s/%s\"", Base, RDF.YEAR ); - system( tBuff ); + if( fileExist( tBuff ) ){ + if( dirEmpty( tBuff ) ){ + sprintf( tBuff, "rm -f -R \"%s/%s\"", Base, RDF.YEAR ); + system( tBuff ); + }; }; return true; }; diff --git a/lib/App/common/SQLite_dataBinder.h b/lib/App/common/SQLite_dataBinder.h index c6213856..261d0e21 100644 --- a/lib/App/common/SQLite_dataBinder.h +++ b/lib/App/common/SQLite_dataBinder.h @@ -90,7 +90,7 @@ class sqliteDBC: public dataBinder { virtual void deleteNetwork( uint32_t NetID ); virtual bool deleteRDFDir( CSTR Base, REPORT_DIRINFO &RDF ); virtual bool deleteReportDate( uint32_t ts ); - virtual bool deleteReportStat( uint32_t id ); + virtual bool deleteReportStat( REPORT_DIRINFO &RDF ); virtual void deleteResource( uint32_t ResourceID ); virtual void deleteSensor( uint32_t SensorID ); virtual void deleteUART( uint32_t UARTID ); diff --git a/lib/App/mojo/mojoKernel.cpp b/lib/App/mojo/mojoKernel.cpp index 16b7808d..a80cb513 100644 --- a/lib/App/mojo/mojoKernel.cpp +++ b/lib/App/mojo/mojoKernel.cpp @@ -951,7 +951,6 @@ void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){ }; bool mojoKernel::validateReports( void ){ - LOG("ValiReport"); DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( 1 ); if( tSiteData ) { // Check if settings has been changed in DB @@ -961,16 +960,18 @@ bool mojoKernel::validateReports( void ){ return false; }; if ( m_sSiteData.report_cleanup ) { - LOG("CHeck Old Report"); cleanUpReportDir( m_sSiteData.report_cleanup ); - } + }; if ( m_sSiteData.preserve_empty ) { - LOG("CHeck Disk Report"); - float tT = 1.0f - ( 1.0f / ((float)m_sSiteData.preserve_empty)); + float tT = 100.0f * ( 1.0f - ( 1.0f / ((float)m_sSiteData.preserve_empty))); +#ifdef DEBUG_EXTRA + char tB[128]; + sprintf( tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty ); + LOG( tB ); +#endif if( m_sSysInfo.disk > tT ){ - LOG("Clean Oldest Report"); // delete some oldest report date - m_pDBC->deleteReportDate( m_pDBC->getLastEventTS() ); + m_pDBC->deleteReportDate( m_pDBC->getFirstEventTS() ); }; }; return true; @@ -1013,11 +1014,17 @@ void mojoKernel::backupReports( uint32_t days ){ }; void mojoKernel::cleanUpReportDir( uint32_t days ){ - // Cleanup Current reports no Backup + // Cleanup Current reports no Backup + uint32_t oTS = m_pDBC->getFirstEventTS(); uint32_t cTS = (uint32_t)time(NULL); - cTS -= 86400 * days; - uint32_t oTS = m_pDBC->getLastEventTS(); + cTS -= 86400 * days; +#ifdef DEBUG_EXTRA + char tB[128]; + sprintf( tB, "Oldest %d border %d", oTS, cTS ); + LOG( tB ); +#endif if ( oTS <= cTS ) { + m_pDBC->deleteReportDate( oTS ); }; };