From a139863c5c3176496bf02cd73fc0a5f1bc14a725 Mon Sep 17 00:00:00 2001 From: Evgeny Gagua Date: Thu, 23 Nov 2023 02:27:06 -0800 Subject: [PATCH] Transfer MER box --- Projects/la_mojo/build | 2 +- lib/App/common/SQLite_dataBinder.cpp | 55 +++++++++++++++++++++++--- lib/App/common/SQLite_dataBinder.h | 3 +- lib/App/common/dataBinder_structures.h | 2 + lib/App/mojo/mojoKernel.cpp | 55 +++++++------------------- 5 files changed, 69 insertions(+), 48 deletions(-) diff --git a/Projects/la_mojo/build b/Projects/la_mojo/build index 59699e61..e30c9046 100755 --- a/Projects/la_mojo/build +++ b/Projects/la_mojo/build @@ -4,7 +4,7 @@ #make clean -e "CFG=Debug" #make -e "CFG=Debug" -j6 -make clean -e "CFG=Debug86" +#make clean -e "CFG=Debug86" make -e "CFG=Debug86" -j4 #make clean -e "CFG=Release86" diff --git a/lib/App/common/SQLite_dataBinder.cpp b/lib/App/common/SQLite_dataBinder.cpp index 660abac7..8aad818b 100644 --- a/lib/App/common/SQLite_dataBinder.cpp +++ b/lib/App/common/SQLite_dataBinder.cpp @@ -1643,6 +1643,9 @@ bool sqliteDBC::getEventSessionStatus( uint32_t nodeID ){ return rv; }; +/* +* The Last TS is from the current report (as latest) +*/ uint32_t sqliteDBC::getLastEventTS(){ uint32_t rv = 0; if( m_bRepIsConnected ){ @@ -1669,16 +1672,20 @@ uint32_t sqliteDBC::getLastEventTS(){ return rv; }; +/* +* The first TS is generaly must show a DAY of first registered event so TS fetched +* from general statistics +*/ uint32_t sqliteDBC::getFirstEventTS(){ - uint32_t rv = 0; - if( m_bRepIsConnected ){ + uint32_t rv = 0; + if( m_bStaIsConnected ){ char tQBuf[CTEXT]; AAssoc *result; - sprintf( tQBuf, "SELECT `ts` FROM `events` ORDER BY `ts`, `ts_ms` LIMIT 1" ); + sprintf( tQBuf, "SELECT `ts` FROM `ts` ORDER BY `ts` LIMIT 1" ); #ifdef DEBUG_ALL LOG(tQBuf); #endif - result = m_pDataReports->Query( tQBuf ); + result = m_pDataStatistics->Query( tQBuf ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc *cArr; @@ -1695,10 +1702,37 @@ uint32_t sqliteDBC::getFirstEventTS(){ return rv; }; -bool sqliteDBC::deleteReportDate( uint32_t ts ){ +bool sqliteDBC::deleteReportDate( uint32_t ts ){ + if( ts ){ + REPORT_DIRINFO tRDF = {0}; + fillRDF( ts, tRDF ); + if( deleteRDFDir( m_pDbReportBase, tRDF ) ){ + return deleteReportStat( ts ); + }; + }; return false; }; +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 ); + 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 ); + }; + sprintf( tBuff, "%s/%s", Base, RDF.YEAR ); + if( dirEmpty( tBuff ) ){ + sprintf( tBuff, "rm -f -R \"%s/%s\"", Base, RDF.YEAR ); + system( tBuff ); + }; + return true; +}; + DBC_LRF_DATA *sqliteDBC::getLRFData( uint32_t ID ){ DBC_LRF_DATA * tVal = NULL; if( m_bStoIsConnected ){ @@ -3038,14 +3072,23 @@ void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_ m_nSiteID = SiteID; char tQBuf[MAX_PATH_LEN]; +#ifdef DEFAULT_SERVER_ID + sprintf( tQBuf, "INSERT INTO `servers`(`id`,`site_id`,`identity`,`version_string`) VALUES (%d,%d,'%s','%s') ON CONFLICT(`id`) DO UPDATE SET `identity`='%s',`version_string`='%s'", + DEFAULT_SERVER_ID, SiteID, identity, _BUILD_STRING_, identity, _BUILD_STRING_ ); +#else sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'", SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ ); +#endif + #ifdef DEBUG_ALL LOG(tQBuf); #endif m_pDataStorage->SilentQuery( tQBuf ); - +#ifdef DEFAULT_SERVER_ID + m_nServerID = DEFAULT_SERVER_ID; +#else m_nServerID = getServerID( identity ); +#endif *ServID = m_nServerID; if( m_nServerID ) { diff --git a/lib/App/common/SQLite_dataBinder.h b/lib/App/common/SQLite_dataBinder.h index f13f8fdd..c6213856 100644 --- a/lib/App/common/SQLite_dataBinder.h +++ b/lib/App/common/SQLite_dataBinder.h @@ -88,6 +88,8 @@ class sqliteDBC: public dataBinder { virtual void deleteNIF( uint32_t NifID ); virtual void deleteNIS( uint32_t NisID ); 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 void deleteResource( uint32_t ResourceID ); virtual void deleteSensor( uint32_t SensorID ); @@ -102,7 +104,6 @@ class sqliteDBC: public dataBinder { virtual bool getEventSessionStatus( uint32_t nodeID ); virtual uint32_t getLastEventTS(); virtual uint32_t getFirstEventTS(); - virtual bool deleteReportDate( uint32_t ts ); virtual DBC_LRF_DATA * getLRFData( uint32_t ID ); virtual uint8_t getLRFDataStatus( uint32_t ID ); virtual PLGS_2D_PARAMETERS getLRFDetectionProfile( uint32_t lrfID, uint8_t profileID, bool master ); diff --git a/lib/App/common/dataBinder_structures.h b/lib/App/common/dataBinder_structures.h index 6f7d05f8..e4ac2551 100644 --- a/lib/App/common/dataBinder_structures.h +++ b/lib/App/common/dataBinder_structures.h @@ -13,6 +13,8 @@ #define HUMAN_FRIENDLY_JSON true +#define DEFAULT_SERVER_ID 1 + #define LIST_ALL_RECORDS 0 #define LIST_VALID_RECORDS 1 #define LIST_INVALID_RECORDS 2 diff --git a/lib/App/mojo/mojoKernel.cpp b/lib/App/mojo/mojoKernel.cpp index ccde3902..16b7808d 100644 --- a/lib/App/mojo/mojoKernel.cpp +++ b/lib/App/mojo/mojoKernel.cpp @@ -932,7 +932,9 @@ void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){ fileCopy(ptr->scratchPath, tB); chown( tB, "nobody", "nogroup" ); chmod( tB, 0664 ); +#ifdef DEBUG LOG("Video Saved"); +#endif free( tP ); } else { LOG_ERROR("Cannot Get Report Path from DB!"); @@ -949,6 +951,7 @@ 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 @@ -958,15 +961,16 @@ bool mojoKernel::validateReports( void ){ return false; }; if ( m_sSiteData.report_cleanup ) { - //if ( m_sSiteData.report_cleanup > m_sSiteData.report_backup ) { - // if ( m_sSiteData.preserve_empty ) { - // // Do needed backup - // }; - //}; + LOG("CHeck Old Report"); cleanUpReportDir( m_sSiteData.report_cleanup ); - } else { - if ( m_sSiteData.preserve_empty ) { - // Only Do Backup - No Cleanup + } + if ( m_sSiteData.preserve_empty ) { + LOG("CHeck Disk Report"); + float tT = 1.0f - ( 1.0f / ((float)m_sSiteData.preserve_empty)); + if( m_sSysInfo.disk > tT ){ + LOG("Clean Oldest Report"); + // delete some oldest report date + m_pDBC->deleteReportDate( m_pDBC->getLastEventTS() ); }; }; return true; @@ -1010,40 +1014,11 @@ void mojoKernel::backupReports( uint32_t days ){ void mojoKernel::cleanUpReportDir( uint32_t days ){ // Cleanup Current reports no Backup - REPORT_DIRINFO tRDI = {0}; uint32_t cTS = (uint32_t)time(NULL); cTS -= 86400 * days; - m_pDBC->fillRDF( cTS, tRDI ); - DBC_INT_LIST *TL = m_pDBC->listOlderReportStat( tRDI.YEAR, tRDI.MONTH, tRDI.DAY ); - if ( TL ) { - for ( DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++ ) { - REPORT_DIRINFO *tRDI = m_pDBC->getReportDirInfo( *idx ); - if ( tRDI ) { - char tB[MAX_PATH]; - char dB[MAX_PATH]; - sprintf( tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY ); - //char tPATH[MAX_PATH]; - //sprintf( tPATH, "%s/data/archive/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY ); - if ( fileExist( tB ) ) { - DIR *dir = opendir( tB ); - if( dir ){ - struct dirent *d; - while( ( d = readdir(dir) ) != NULL ) { - if ( strlen( d->d_name ) > 3 ) { - char tPATH[MAX_PATH]; - sprintf( tPATH, "%s/%s", tB, d->d_name ); - unlink( tPATH ); - }; - }; - closedir( dir ); - }; - if ( dirEmpty( tB ) ) unlink( tB ); - }; - free( tRDI ); - }; - m_pDBC->deleteReportStat( *idx ); - }; - delete( TL ); + uint32_t oTS = m_pDBC->getLastEventTS(); + if ( oTS <= cTS ) { + m_pDBC->deleteReportDate( oTS ); }; };