You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.6 KiB
74 lines
2.6 KiB
<?php
|
|
/*
|
|
* Report database files of a day folder.
|
|
*
|
|
* la_mojo writes one report database per local hour into the day folder:
|
|
* data/reports/YYYY/MM/DD/report_HH.db (HH = 00..23)
|
|
* Days recorded before that change have a single data/reports/YYYY/MM/DD/report.db
|
|
* (the "legacy" file); a day during the upgrade can have both.
|
|
*
|
|
* Row ids (events, pictures, videos) are AUTOINCREMENT per file, so ids sent to the browser
|
|
* carry the file ("slot") they belong to:
|
|
* global id = slot * REPORT_ID_SLOT + rowid slot: 0..23 = hour file, 24 = legacy report.db
|
|
* The browser treats ids as opaque values and sends them back unchanged; controllers decode them
|
|
* with reportSlotOf() / reportRawId(). The largest id (24 * 10^7 + 9999999) fits a 32-bit int.
|
|
*/
|
|
define('REPORT_ID_SLOT', 10000000);
|
|
define('REPORT_LEGACY_SLOT', 24);
|
|
define('REPORT_RAW_ID_MAX', REPORT_ID_SLOT - 1);
|
|
|
|
function reportDayDir( $DS ){
|
|
global $SYSPath;
|
|
return $SYSPath . '/lib/controller/data/reports/' . $DS;
|
|
}
|
|
|
|
// Path of the report file of a slot (does not check existence)
|
|
function reportSlotPath( $DS, $slot ){
|
|
if( $slot == REPORT_LEGACY_SLOT ) return reportDayDir( $DS ) . '/report.db';
|
|
return reportDayDir( $DS ) . sprintf( '/report_%02d.db', $slot );
|
|
}
|
|
|
|
// Existing report files of a day: array( slot => path ), legacy report.db first, then hours ascending
|
|
function reportDayFiles( $DS ){
|
|
$RV = array();
|
|
$P = reportSlotPath( $DS, REPORT_LEGACY_SLOT );
|
|
if( file_exists( $P ) ) $RV[REPORT_LEGACY_SLOT] = $P;
|
|
for( $H = 0; $H < 24; $H++ ){
|
|
$P = reportSlotPath( $DS, $H );
|
|
if( file_exists( $P ) ) $RV[$H] = $P;
|
|
};
|
|
return $RV;
|
|
}
|
|
|
|
// Opens the report file of a slot; NULL if it does not exist (never creates a file)
|
|
function reportOpenSlot( $DS, $slot ){
|
|
if( $slot < 0 || $slot > REPORT_LEGACY_SLOT ) return NULL;
|
|
$P = reportSlotPath( $DS, $slot );
|
|
if( !file_exists( $P ) ) return NULL;
|
|
return new SQLite_DB( $P, SQLITE3_OPEN_READWRITE );
|
|
}
|
|
|
|
function reportEncodeId( $slot, $id ){
|
|
return intval( $slot ) * REPORT_ID_SLOT + intval( $id );
|
|
}
|
|
|
|
function reportSlotOf( $gid ){
|
|
return intdiv( intval( $gid ), REPORT_ID_SLOT );
|
|
}
|
|
|
|
function reportRawId( $gid ){
|
|
return intval( $gid ) % REPORT_ID_SLOT;
|
|
}
|
|
|
|
/*
|
|
* Resolves a BID..EID range of global ids (always inside one report file) to
|
|
* array( slot, raw_bid, raw_eid ). If EID points past the file of BID (e.g. "until the end"),
|
|
* the range runs to the end of BID's file.
|
|
*/
|
|
function reportResolveRange( $BID, $EID ){
|
|
$slot = reportSlotOf( $BID > 0 ? $BID : $EID );
|
|
$rb = ( reportSlotOf( $BID ) == $slot ) ? reportRawId( $BID ) : 0;
|
|
$re = ( reportSlotOf( $EID ) == $slot ) ? reportRawId( $EID ) : REPORT_RAW_ID_MAX;
|
|
return array( $slot, $rb, $re );
|
|
}
|
|
?>
|