Statistics filters
parent
876675445d
commit
5d1a2fcf77
@ -1,40 +1,69 @@
|
|||||||
package org.koitharu.kotatsu.stats.data
|
package org.koitharu.kotatsu.stats.data
|
||||||
|
|
||||||
|
import android.database.sqlite.SQLiteQueryBuilder
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
import androidx.room.MapColumn
|
import androidx.room.MapColumn
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
|
import androidx.room.RawQuery
|
||||||
|
import androidx.room.Transaction
|
||||||
import androidx.room.Upsert
|
import androidx.room.Upsert
|
||||||
|
import androidx.sqlite.db.SimpleSQLiteQuery
|
||||||
|
import androidx.sqlite.db.SupportSQLiteQuery
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import org.koitharu.kotatsu.core.db.entity.MangaEntity
|
||||||
|
import org.koitharu.kotatsu.history.data.HistoryEntity
|
||||||
|
import org.koitharu.kotatsu.history.data.HistoryWithManga
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
interface StatsDao {
|
abstract class StatsDao {
|
||||||
|
|
||||||
@Query("SELECT * FROM stats ORDER BY started_at")
|
@Query("SELECT * FROM stats ORDER BY started_at")
|
||||||
suspend fun findAll(): List<StatsEntity>
|
abstract suspend fun findAll(): List<StatsEntity>
|
||||||
|
|
||||||
@Query("SELECT * FROM stats WHERE manga_id = :mangaId ORDER BY started_at")
|
@Query("SELECT * FROM stats WHERE manga_id = :mangaId ORDER BY started_at")
|
||||||
suspend fun findAll(mangaId: Long): List<StatsEntity>
|
abstract suspend fun findAll(mangaId: Long): List<StatsEntity>
|
||||||
|
|
||||||
@Query("SELECT IFNULL(SUM(pages),0) FROM stats WHERE manga_id = :mangaId")
|
@Query("SELECT IFNULL(SUM(pages),0) FROM stats WHERE manga_id = :mangaId")
|
||||||
suspend fun getReadPagesCount(mangaId: Long): Int
|
abstract suspend fun getReadPagesCount(mangaId: Long): Int
|
||||||
|
|
||||||
@Query("SELECT IFNULL(SUM(duration)/SUM(pages), 0) FROM stats WHERE manga_id = :mangaId")
|
@Query("SELECT IFNULL(SUM(duration)/SUM(pages), 0) FROM stats WHERE manga_id = :mangaId")
|
||||||
suspend fun getAverageTimePerPage(mangaId: Long): Long
|
abstract suspend fun getAverageTimePerPage(mangaId: Long): Long
|
||||||
|
|
||||||
@Query("SELECT IFNULL(SUM(duration)/SUM(pages), 0) FROM stats")
|
@Query("SELECT IFNULL(SUM(duration)/SUM(pages), 0) FROM stats")
|
||||||
suspend fun getAverageTimePerPage(): Long
|
abstract suspend fun getAverageTimePerPage(): Long
|
||||||
|
|
||||||
@Query("SELECT IFNULL(SUM(duration), 0) FROM stats WHERE manga_id = :mangaId")
|
@Query("SELECT IFNULL(SUM(duration), 0) FROM stats WHERE manga_id = :mangaId")
|
||||||
suspend fun getReadingTime(mangaId: Long): Long
|
abstract suspend fun getReadingTime(mangaId: Long): Long
|
||||||
|
|
||||||
@Query("SELECT IFNULL(SUM(duration), 0) FROM stats")
|
@Query("SELECT IFNULL(SUM(duration), 0) FROM stats")
|
||||||
suspend fun getTotalReadingTime(): Long
|
abstract suspend fun getTotalReadingTime(): Long
|
||||||
|
|
||||||
@Query("SELECT manga_id, SUM(duration) AS d FROM stats WHERE started_at >= :fromDate GROUP BY manga_id ORDER BY d DESC")
|
|
||||||
suspend fun getDurationStats(fromDate: Long): Map<@MapColumn("manga_id") Long, @MapColumn("d") Long>
|
|
||||||
|
|
||||||
@Query("DELETE FROM stats")
|
@Query("DELETE FROM stats")
|
||||||
suspend fun clear()
|
abstract suspend fun clear()
|
||||||
|
|
||||||
@Upsert
|
@Upsert
|
||||||
suspend fun upsert(entity: StatsEntity)
|
abstract suspend fun upsert(entity: StatsEntity)
|
||||||
|
|
||||||
|
suspend fun getDurationStats(fromDate: Long, isNsfw: Boolean?, favouriteCategories: Set<Long>): Map<MangaEntity, Long> {
|
||||||
|
val conditions = ArrayList<String>()
|
||||||
|
conditions.add("stats.started_at >= $fromDate")
|
||||||
|
if (favouriteCategories.isNotEmpty()) {
|
||||||
|
val ids = favouriteCategories.joinToString(",")
|
||||||
|
conditions.add("stats.manga_id IN (SELECT manga_id FROM favourites WHERE category_id IN ($ids))")
|
||||||
|
}
|
||||||
|
if (isNsfw != null) {
|
||||||
|
val flag = if (isNsfw) 1 else 0
|
||||||
|
conditions.add("manga.nsfw = $flag")
|
||||||
|
}
|
||||||
|
val where = conditions.joinToString(separator = " AND ")
|
||||||
|
val query = SimpleSQLiteQuery(
|
||||||
|
"SELECT manga.*, SUM(duration) AS d FROM stats LEFT JOIN manga ON manga.manga_id = stats.manga_id WHERE $where GROUP BY manga.manga_id ORDER BY d DESC",
|
||||||
|
)
|
||||||
|
return getDurationStatsImpl(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
@RawQuery
|
||||||
|
protected abstract fun getDurationStatsImpl(
|
||||||
|
query: SupportSQLiteQuery
|
||||||
|
): Map<@MapColumn("manga") MangaEntity, @MapColumn("d") Long>
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue