stat async update

feature/top10
Laurent 7 years ago
parent aa6edf150f
commit 2f15f72764
  1. 66
      app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt
  2. 41
      app/src/main/java/net/pokeranalytics/android/ui/fragment/StatsFragment.kt

@ -6,11 +6,10 @@ import io.realm.RealmConfiguration
import io.realm.RealmResults import io.realm.RealmResults
import io.realm.kotlin.where import io.realm.kotlin.where
import net.pokeranalytics.android.model.Limit import net.pokeranalytics.android.model.Limit
import net.pokeranalytics.android.model.realm.* import net.pokeranalytics.android.model.realm.Game
import net.pokeranalytics.android.model.realm.Currency import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.utils.Seed import net.pokeranalytics.android.model.utils.Seed
import net.pokeranalytics.android.util.PokerAnalyticsLogs import net.pokeranalytics.android.util.PokerAnalyticsLogs
import net.pokeranalytics.android.util.Preferences
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
@ -71,22 +70,38 @@ class PokerAnalyticsApplication : Application() {
private fun createFakeSessions() { private fun createFakeSessions() {
val buyinList = arrayListOf(100.0, 200.0, 300.0, 500.0, 1000.0, 2000.0)
val resultsList = arrayListOf(
-2500.0, -2000.0, -1500.0, -1000.0, -500.0, 200.0, 1000.0, 1500.0, 2500.0
)
Thread().run() {
try {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
val games = realm.where<Game>().findAll()
// Test endedSessions // Test endedSessions
val sessions = realm.where<Session>().findAll() val sessions = realm.where<Session>().findAll()
if (sessions.size < 10) { if (sessions.size < 10) {
val numberOfSessions = 50 val numberOfSessions = 10000
Timber.d("> Start creating ${numberOfSessions} fake sessions...") Timber.d("*** Start creating ${numberOfSessions} fake sessions...")
val s = Date() val s = Date()
realm.beginTransaction()
for (index in 0..numberOfSessions) { for (index in 0..numberOfSessions) {
realm.executeTransaction { if (index % 1000 == 999) {
Timber.d("****** committing at ${index} sessions...")
realm.commitTransaction()
realm.beginTransaction()
}
val session = Session.newInstance(realm, false) val session = Session.newInstance(realm, false)
// session.id = UUID.randomUUID().toString()
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
calendar.set( calendar.set(
@ -106,46 +121,33 @@ class PokerAnalyticsApplication : Application() {
session.startDate = startDate session.startDate = startDate
session.endDate = endDate session.endDate = endDate
// session.timeFrame?.let {
// // it.startDate = startDate
//// it.endDate = endDate
// it.setDate(startDate, endDate)
// }
// session.timeFrame = timeFrame
session.creationDate = startDate session.creationDate = startDate
session.limit = Limit.values().random().ordinal session.limit = Limit.values().random().ordinal
session.game = realm.where<Game>().findAll().random() session.game = games.random()
session.result?.let { result -> session.result?.let { result ->
result.buyin = arrayListOf(100, 200, 300, 500, 1000, 2000).random().toDouble() result.buyin = buyinList.random()
result.netResult = arrayListOf( result.netResult = resultsList.random()
-2500.0, -2000.0, -1500.0, -1000.0, -500.0, 200.0, 1000.0, 1500.0,
2500.0
).random()
} }
realm.copyToRealmOrUpdate(session)
}
} }
realm.commitTransaction()
val e = Date() val e = Date()
val duration = (e.time - s.time) / 1000.0 val duration = (e.time - s.time) / 1000.0
Timber.d("> ended in ${duration} seconds") Timber.d("*** ended in ${duration} seconds")
} }
realm.close()
// val sets = realm.where(SessionSet::class.java).findAll() } catch (e: Exception) {
// Timber.e(e)
// Timber.d("sets = ${sets.size}") }
//
// sets.forEach { set ->
// Timber.d("set sd = : ${set.timeFrame?.startDate}, ed = ${set.timeFrame?.endDate}")
// }
realm.close() }
} }

@ -6,7 +6,10 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_stats.* import kotlinx.android.synthetic.main.fragment_stats.*
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.* import net.pokeranalytics.android.calculus.*
import net.pokeranalytics.android.model.StatRepresentable import net.pokeranalytics.android.model.StatRepresentable
@ -20,8 +23,12 @@ import net.pokeranalytics.android.ui.view.rowrepresentable.HeaderRowRepresentabl
import net.pokeranalytics.android.util.NULL_TEXT import net.pokeranalytics.android.util.NULL_TEXT
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
import kotlin.coroutines.CoroutineContext
class StatsFragment : SessionObserverFragment(), StaticRowRepresentableDataSource { class StatsFragment : SessionObserverFragment(), StaticRowRepresentableDataSource, CoroutineScope {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main
private var rowRepresentables: ArrayList<RowRepresentable> = ArrayList() private var rowRepresentables: ArrayList<RowRepresentable> = ArrayList()
@ -87,7 +94,7 @@ class StatsFragment : SessionObserverFragment(), StaticRowRepresentableDataSourc
// Override // Override
override fun sessionsChanged() { override fun sessionsChanged() {
this.launchStatComputation() // this.launchStatComputation()
this.statsAdapter.notifyDataSetChanged() this.statsAdapter.notifyDataSetChanged()
} }
@ -97,16 +104,28 @@ class StatsFragment : SessionObserverFragment(), StaticRowRepresentableDataSourc
* Init data * Init data
*/ */
private fun initData() { private fun initData() {
this.statsAdapter = RowRepresentableAdapter(this)
this.launchStatComputation() this.launchStatComputation()
} }
private fun launchStatComputation() { private fun launchStatComputation() {
var results = listOf<ComputedResults>()
val s = Date() val s = Date()
Timber.d(">>> start...") Timber.d(">>> start...")
runBlocking {
GlobalScope.launch(context = this.coroutineContext) {
val results = compute()
showResults(results)
}
val e = Date()
val duration = (e.time - s.time) / 1000.0
Timber.d(">>> ended in ${duration} seconds")
}
private fun compute() : List<ComputedResults> {
val cgSessions = mutableListOf<Session>() val cgSessions = mutableListOf<Session>()
val tSessions = mutableListOf<Session>() val tSessions = mutableListOf<Session>()
@ -126,16 +145,13 @@ class StatsFragment : SessionObserverFragment(), StaticRowRepresentableDataSourc
val tStats: List<Stat> = listOf(Stat.NETRESULT, Stat.HOURLY_RATE, Stat.ROI, Stat.WIN_RATIO, Stat.NUMBER_OF_GAMES, Stat.AVERAGE_BUYIN) val tStats: List<Stat> = listOf(Stat.NETRESULT, Stat.HOURLY_RATE, Stat.ROI, Stat.WIN_RATIO, Stat.NUMBER_OF_GAMES, Stat.AVERAGE_BUYIN)
val tSessionGroup = SessionGroup(getString(R.string.tournament), tSessions, tStats) val tSessionGroup = SessionGroup(getString(R.string.tournament), tSessions, tStats)
results = Calculator.computeGroups(listOf(allSessionGroup, cgSessionGroup, tSessionGroup), Calculator.Options()) return Calculator.computeGroups(listOf(allSessionGroup, cgSessionGroup, tSessionGroup), Calculator.Options())
} }
val e = Date()
val duration = (e.time - s.time) / 1000.0
Timber.d(">>> ended in ${duration} seconds")
this.rowRepresentables = this.convertResultsIntoRepresentables(results) private fun showResults(results: List<ComputedResults>) {
this.statsAdapter = RowRepresentableAdapter(this) // StatsAdapter(this, null) this.rowRepresentables = this.convertResultsIntoRepresentables(results)
val viewManager = LinearLayoutManager(requireContext()) val viewManager = LinearLayoutManager(requireContext())
@ -144,7 +160,6 @@ class StatsFragment : SessionObserverFragment(), StaticRowRepresentableDataSourc
layoutManager = viewManager layoutManager = viewManager
adapter = statsAdapter adapter = statsAdapter
} }
} }
private fun convertResultsIntoRepresentables(results: List<ComputedResults>) : ArrayList<RowRepresentable> { private fun convertResultsIntoRepresentables(results: List<ComputedResults>) : ArrayList<RowRepresentable> {

Loading…
Cancel
Save