@ -17,10 +17,13 @@ import java.util.*
class CorruptSessionSetException ( message : String ) : Exception ( message )
/ * *
* The manager is in charge of updating the concept of timeline ,
* representing the time frames where the user plays .
* The TimeManager pre - computes time related data :
* - SessionSet : All overlapping sessions are grouped into a SessionSet ,
* used to calculate the number of sessions and break durations
* - FlatTimeInterval : Sessions time intervals are breaked down into smaller intervals
* when overlapping occurs to get faster duration calculations
* /
object SessionSetManager {
object Time Manager {
var sessions : RealmResults < Session > ? = null
@ -55,29 +58,82 @@ object SessionSetManager {
sessions = realm . where ( Session :: class . java ) . findAllAsync ( )
sessions ?. addChangeListener { _ , _ ->
if ( this . start != null && this . end != null ) {
realm . executeTransactionAsync { asyncRealm ->
processSessions ( asyncRealm )
cleanUp ( )
}
if ( sessionIdsToProcess . isNotEmpty ( ) ) {
realm . executeTransactionAsync ( { asyncRealm ->
val sessions = sessionIdsToProcess . mapNotNull { asyncRealm . findById < Session > ( it ) }
sessionIdsToProcess . clear ( )
for ( session in sessions ) {
Timber . d ( " Session id = ${session.id} " )
Timber . d ( " Session time intervals count = ${session.flatTimeIntervals.size} " )
session . flatTimeIntervals . deleteAllFromRealm ( )
val fti = FlatTimeInterval ( )
session . flatTimeIntervals . add ( fti )
asyncRealm . insertOrUpdate ( session )
}
} , {
Timber . d ( " executeTransactionAsync onSuccess listener... " )
val timeIntervals = realm . where ( FlatTimeInterval :: class . java ) . findAll ( )
Timber . d ( " Total timeIntervals count = ${timeIntervals.size} " )
timeIntervals . forEach {
Timber . d ( " >>> Time interval session count = ${it.sessions?.size} , session id = ${it.sessions?.firstOrNull()?.id} " )
}
} , { } )
}
}
// sessions?.addChangeListener { _, _ ->
//
// Timber.d("...sessions change at ${Date().time}")
//
// val start = this.start
// val end = this.end
// if (start != null && end != null) {
//
// Timber.d("...process date changes from $start to $end")
//
// this.start = null
// this.end = null
//
// realm.executeTransactionAsync ({ asyncRealm ->
// processSessions(asyncRealm, start, end)
// cleanUp()
// }, {
// Timber.d(">>>>> ON SUCCESS")
//
// realm.where(FlatTimeInterval::class.java).findAll().forEach {
// Timber.d("######## sessions count = ${it.sessions?.size}")
// }
//
// }, {
// Timber.d("Transaction failed : $it")
// })
// }
// }
realm . close ( )
}
private fun cleanUp ( ) {
this . start = null
this . end = null
// this.sessionIdsToProcess.clear()
this . sessionIdsToProcess . clear ( )
}
private fun processSessions ( realm : Realm ) {
private fun processSessions ( realm : Realm , start : Date , end : Date ) {
Timber . d ( " ***** processSessions, process count = ${sessionIdsToProcess.size} " )
val start = this . start
val end = this . end
// val start = this. start
// val end = this. end
val sessions = sessionIdsToProcess . mapNotNull { realm . findById < Session > ( it ) }
for ( session in sessions ) {
@ -94,9 +150,7 @@ object SessionSetManager {
}
// FlatTimeIntervals
if ( start != null && end != null ) {
processFlatTimeInterval ( realm , sessions . toSet ( ) , start , end )
}
processFlatTimeInterval ( realm , sessions . toSet ( ) , start , end )
val ftis = realm . where ( FlatTimeInterval :: class . java ) . findAll ( )
Timber . d ( " *** FTIs count = ${ftis.size} " )
@ -306,16 +360,21 @@ object SessionSetManager {
private fun processFlatTimeInterval ( realm : Realm , changedSessions : Set < Session > , start : Date , end : Date ) {
Timber . d ( " *************************************************** ")
Timber . d ( " *** processFlatTimeInterval, from: $start , to $end " )
Timber . d ( " *************************************************** ")
// Timber.d(" ***************************************************")
// Timber.d("*** processFlatTimeInterval, from: $start, to $end" )
// Timber.d(" ***************************************************")
val sessions = matchingData < Session > ( realm , start , end )
Timber . d ( " *** matching sessions: ${sessions.size} " )
val intervalsStore = IntervalsStore ( sessions . toSet ( ) )
intervalsStore . processSessions ( changedSessions )
intervalsStore . intervals . forEach { it . deleteFromRealm ( ) }
Timber . d ( " *** sessions count = ${intervalsStore.sessions.size} " )
Timber . d ( " *** ftis to delete: ${intervalsStore.intervals.size} " )
for ( fti in intervalsStore . intervals ) {
fti . deleteFromRealm ( )
}
// intervalsStore.intervals.forEach { it.deleteFromRealm() }
val intervals = SessionInterval . intervalMap ( intervalsStore . sessions )
@ -333,17 +392,25 @@ object SessionSetManager {
( sd != null && ed != null && sd <= s && ed >= e )
}
if ( matchingSessions . isNotEmpty ( ) ) {
Timber . d ( " **** Create FTI: $s - $e ")
// Timber.d("**** Create FTI: $s - $e")
val fti = FlatTimeInterval ( )
fti . startDate = s
fti . endDate = e
matchingSessions . forEach { it . flatTimeIntervals . add ( fti ) }
for ( session in matchingSessions ) {
session . flatTimeIntervals . add ( fti )
realm . insertOrUpdate ( session )
}
realm . insertOrUpdate ( fti )
} else {
Timber . w ( " The FTI has no sessions " )
}
}
}
sessions . forEach {
Timber . d ( " ending process...session FTI count = ${it.flatTimeIntervals.size} " )
}
}
}
@ -366,7 +433,7 @@ class IntervalsStore(sessionSet: Set<Session>) {
fun processSessions ( sessions : Set < Session > ) {
this . sessions . addAll ( sessions )
for ( session in sessions ) {
Timber . d ( " PROCESS > s = ${session.startDate} / e = ${session.endDate} " )
// Timber.d("PROCESS > s = ${session.startDate} / e = ${session.endDate} " )
loadIntervals ( session )
}
}
@ -382,6 +449,7 @@ class IntervalsStore(sessionSet: Set<Session>) {
this . sessionIds . add ( session . id )
Timber . d ( " session FTI count = ${session.flatTimeIntervals.size} " )
for ( fti in session . flatTimeIntervals ) {
this . intervals . add ( fti )
@ -411,7 +479,7 @@ class SessionInterval(session: Session) {
this . start = session . startDate !!
this . end = session . endDate
Timber . d ( " INTERVAL init: s = $start , e = $end " )
// Timber.d("INTERVAL init: s = $start, e = $end" )
this . addSession ( session )
}
@ -454,9 +522,9 @@ class SessionInterval(session: Session) {
}
}
intervals . forEach {
Timber . d ( " s = ${it.start} , e = ${it.end} ")
}
// intervals.forEach {
// Timber.d("s = ${it.start}, e = ${it.end}")
// }
return intervals
}