@@ -14,19 +14,49 @@ class DatabaseCommand
1414 private $ databaseConnector ;
1515 private $ conn ;
1616 private $ statisticsTableName ;
17+ private $ detailedStatisticsTableName ;
1718 private $ identityProvidersMapTableName ;
1819 private $ serviceProvidersMapTableName ;
1920
2021 public function __construct ()
2122 {
2223 $ this ->databaseConnector = new DatabaseConnector ();
2324 $ this ->conn = $ this ->databaseConnector ->getConnection ();
24- assert ($ this ->conn != null );
25+ assert ($ this ->conn !== null );
2526 $ this ->statisticsTableName = $ this ->databaseConnector ->getStatisticsTableName ();
27+ $ this ->detailedStatisticsTableName = $ this ->databaseConnector ->getDetailedStatisticsTableName ();
2628 $ this ->identityProvidersMapTableName = $ this ->databaseConnector ->getIdentityProvidersMapTableName ();
2729 $ this ->serviceProvidersMapTableName = $ this ->databaseConnector ->getServiceProvidersMapTableName ();
2830 }
2931
32+ private function writeLogin ($ year , $ month , $ day , $ sourceIdp , $ service , $ user = null )
33+ {
34+ $ params = [
35+ 'year ' => $ year ,
36+ 'month ' => $ month ,
37+ 'day ' => $ day ,
38+ 'sourceIdp ' => $ sourceIdp ,
39+ 'service ' => $ service ,
40+ 'count ' => 1 ,
41+ ];
42+ $ table = $ this ->statisticsTableName ;
43+ if ($ user && $ this ->databaseConnector ->getDetailedDays () > 0 ) {
44+ // write also into aggregated statistics
45+ self ::writeLogin ($ year , $ month , $ day , $ sourceIdp , $ service );
46+ $ params ['user ' ] = $ user ;
47+ $ table = $ this ->detailedStatisticsTableName ;
48+ }
49+ $ fields = array_keys ($ params );
50+ $ placeholders = array_map (function ($ field ) {
51+ return ': ' . $ field ;
52+
53+ }, $ fields );
54+ $ query = "INSERT INTO " . $ table . " ( " . implode (', ' , $ fields ) . ") " .
55+ " VALUES ( " . implode (', ' , $ placeholders ) . ") ON DUPLICATE KEY UPDATE count = count + 1 " ;
56+
57+ return $ this ->conn ->write ($ query , $ params );
58+ }
59+
3060 public function insertLogin (&$ request , &$ date )
3161 {
3262 if (!in_array ($ this ->databaseConnector ->getMode (), ['PROXY ' , 'IDP ' , 'SP ' ])) {
@@ -59,11 +89,9 @@ public function insertLogin(&$request, &$date)
5989 " is empty and login log wasn't inserted into the database. "
6090 );
6191 } else {
62- if ($ this ->conn ->write (
63- "INSERT INTO " . $ this ->statisticsTableName . "(year, month, day, sourceIdp, service, count) " .
64- " VALUES (:year, :month, :day, :idp, :sp, '1') ON DUPLICATE KEY UPDATE count = count + 1 " ,
65- ['year ' =>$ year , 'month ' =>$ month , 'day ' =>$ day , 'idp ' =>$ idpEntityID , 'sp ' =>$ spEntityId ]
66- ) === false ) {
92+ $ idAttribute = $ this ->databaseConnector ->getUserIdAttribute ();
93+ $ userId = isset ($ request ['Attributes ' ][$ idAttribute ]) ? $ request ['Attributes ' ][$ idAttribute ][0 ] : null ;
94+ if ($ this ->writeLogin ($ year , $ month , $ day , $ idpEntityID , $ spEntityId , $ userId ) === false ) {
6795 Logger::error ("The login log wasn't inserted into table: " . $ this ->statisticsTableName . ". " );
6896 }
6997
@@ -197,17 +225,30 @@ public function getLoginCountPerIdp($days)
197225 return $ this ->conn ->read ($ query , $ params )->fetchAll (PDO ::FETCH_NUM );
198226 }
199227
200- private static function addDaysRange ($ days , &$ query , &$ params )
228+ private static function addDaysRange ($ days , &$ query , &$ params, $ not = false )
201229 {
202230 if ($ days != 0 ) { // 0 = all time
203231 if (stripos ($ query , "WHERE " ) === false ) {
204232 $ query .= "WHERE " ;
205233 } else {
206234 $ query .= "AND " ;
207235 }
208- $ query .= " CONCAT(year,'-',LPAD(month,2,'00'),'-',LPAD(day,2,'00')) " .
209- "BETWEEN CURDATE() - INTERVAL :days DAY AND CURDATE() " ;
236+ $ query .= " CONCAT(year,'-',LPAD(month,2,'00'),'-',LPAD(day,2,'00')) " ;
237+ if ($ not ) {
238+ $ query .= "NOT " ;
239+ }
240+ $ query .= "BETWEEN CURDATE() - INTERVAL :days DAY AND CURDATE() " ;
210241 $ params ['days ' ] = $ days ;
211242 }
212243 }
244+
245+ public function deleteOldDetailedStatistics ()
246+ {
247+ if ($ this ->databaseConnector ->getDetailedDays () > 0 ) {
248+ $ query = "DELETE FROM " . $ this ->detailedStatisticsTableName . " " ;
249+ $ params = [];
250+ self ::addDaysRange ($ this ->databaseConnector ->getDetailedDays (), $ query , $ params , true );
251+ return $ this ->conn ->write ($ query , $ params );
252+ }
253+ }
213254}
0 commit comments