Web Interface For Lite Argus Control modules
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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. /*
  3. * Report database files of a day folder.
  4. *
  5. * la_mojo writes one report database per local hour into the day folder:
  6. * data/reports/YYYY/MM/DD/report_HH.db (HH = 00..23)
  7. * Days recorded before that change have a single data/reports/YYYY/MM/DD/report.db
  8. * (the "legacy" file); a day during the upgrade can have both.
  9. *
  10. * Row ids (events, pictures, videos) are AUTOINCREMENT per file, so ids sent to the browser
  11. * carry the file ("slot") they belong to:
  12. * global id = slot * REPORT_ID_SLOT + rowid slot: 0..23 = hour file, 24 = legacy report.db
  13. * The browser treats ids as opaque values and sends them back unchanged; controllers decode them
  14. * with reportSlotOf() / reportRawId(). The largest id (24 * 10^7 + 9999999) fits a 32-bit int.
  15. */
  16. define('REPORT_ID_SLOT', 10000000);
  17. define('REPORT_LEGACY_SLOT', 24);
  18. define('REPORT_RAW_ID_MAX', REPORT_ID_SLOT - 1);
  19. function reportDayDir( $DS ){
  20. global $SYSPath;
  21. return $SYSPath . '/lib/controller/data/reports/' . $DS;
  22. }
  23. // Path of the report file of a slot (does not check existence)
  24. function reportSlotPath( $DS, $slot ){
  25. if( $slot == REPORT_LEGACY_SLOT ) return reportDayDir( $DS ) . '/report.db';
  26. return reportDayDir( $DS ) . sprintf( '/report_%02d.db', $slot );
  27. }
  28. // Existing report files of a day: array( slot => path ), legacy report.db first, then hours ascending
  29. function reportDayFiles( $DS ){
  30. $RV = array();
  31. $P = reportSlotPath( $DS, REPORT_LEGACY_SLOT );
  32. if( file_exists( $P ) ) $RV[REPORT_LEGACY_SLOT] = $P;
  33. for( $H = 0; $H < 24; $H++ ){
  34. $P = reportSlotPath( $DS, $H );
  35. if( file_exists( $P ) ) $RV[$H] = $P;
  36. };
  37. return $RV;
  38. }
  39. // Opens the report file of a slot; NULL if it does not exist (never creates a file)
  40. function reportOpenSlot( $DS, $slot ){
  41. if( $slot < 0 || $slot > REPORT_LEGACY_SLOT ) return NULL;
  42. $P = reportSlotPath( $DS, $slot );
  43. if( !file_exists( $P ) ) return NULL;
  44. return new SQLite_DB( $P, SQLITE3_OPEN_READWRITE );
  45. }
  46. function reportEncodeId( $slot, $id ){
  47. return intval( $slot ) * REPORT_ID_SLOT + intval( $id );
  48. }
  49. function reportSlotOf( $gid ){
  50. return intdiv( intval( $gid ), REPORT_ID_SLOT );
  51. }
  52. function reportRawId( $gid ){
  53. return intval( $gid ) % REPORT_ID_SLOT;
  54. }
  55. /*
  56. * Resolves a BID..EID range of global ids (always inside one report file) to
  57. * array( slot, raw_bid, raw_eid ). If EID points past the file of BID (e.g. "until the end"),
  58. * the range runs to the end of BID's file.
  59. */
  60. function reportResolveRange( $BID, $EID ){
  61. $slot = reportSlotOf( $BID > 0 ? $BID : $EID );
  62. $rb = ( reportSlotOf( $BID ) == $slot ) ? reportRawId( $BID ) : 0;
  63. $re = ( reportSlotOf( $EID ) == $slot ) ? reportRawId( $EID ) : REPORT_RAW_ID_MAX;
  64. return array( $slot, $rb, $re );
  65. }
  66. ?>