feat: 2-week select
This commit is contained in:
@@ -7,12 +7,13 @@ import androidx.room.TypeConverters
|
||||
@Database(
|
||||
entities = [
|
||||
GroupEntity::class,
|
||||
GroupPeriodEntity::class,
|
||||
ScheduleEntity::class,
|
||||
DayEntity::class,
|
||||
LessonEntity::class,
|
||||
PreferenceEntity::class
|
||||
],
|
||||
version = 2,
|
||||
version = 3,
|
||||
exportSchema = true
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
|
||||
@@ -25,4 +25,13 @@ interface GroupDao {
|
||||
|
||||
@Query("SELECT MAX(lastSyncedAt) FROM groups")
|
||||
suspend fun maxSyncedAt(): Long?
|
||||
|
||||
@Query("SELECT * FROM group_periods WHERE groupId = :groupId AND weekEnd >= :fromEpochDay ORDER BY weekStart")
|
||||
suspend fun getCurrentAndFuturePeriods(groupId: String, fromEpochDay: Long): List<GroupPeriodEntity>
|
||||
|
||||
@Query("DELETE FROM group_periods")
|
||||
suspend fun deleteAllPeriods()
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertPeriods(periods: List<GroupPeriodEntity>)
|
||||
}
|
||||
|
||||
@@ -8,9 +8,5 @@ data class GroupEntity(
|
||||
@PrimaryKey val id: String,
|
||||
val name: String,
|
||||
val faculty: String,
|
||||
val autumnFirst: Long?,
|
||||
val autumnSecond: Long?,
|
||||
val springFirst: Long?,
|
||||
val springSecond: Long?,
|
||||
val lastSyncedAt: Long
|
||||
)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package agzam.vyatsu.data.local
|
||||
|
||||
import agzam.vyatsu.domain.model.Season
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
// Все опубликованные на сайте 2-недельные периоды группы, включая ещё не начавшиеся.
|
||||
@Entity(
|
||||
tableName = "group_periods",
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = GroupEntity::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["groupId"],
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)
|
||||
],
|
||||
indices = [Index("groupId")]
|
||||
)
|
||||
data class GroupPeriodEntity(
|
||||
@PrimaryKey(autoGenerate = true) val id: Long = 0,
|
||||
val groupId: String,
|
||||
val season: Season,
|
||||
val weekStart: Long,
|
||||
val weekEnd: Long
|
||||
)
|
||||
@@ -2,12 +2,12 @@ package agzam.vyatsu.data.repository
|
||||
|
||||
import agzam.vyatsu.data.local.GroupDao
|
||||
import agzam.vyatsu.data.local.GroupEntity
|
||||
import agzam.vyatsu.data.local.GroupPeriodEntity
|
||||
import agzam.vyatsu.data.remote.GroupsParser
|
||||
import agzam.vyatsu.data.remote.VyatsuHttpClient
|
||||
import agzam.vyatsu.data.remote.VyatsuUrls
|
||||
import agzam.vyatsu.domain.ScheduleDates
|
||||
import agzam.vyatsu.domain.model.Group
|
||||
import agzam.vyatsu.domain.model.Season
|
||||
import agzam.vyatsu.domain.repository.GroupRepository
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@@ -45,26 +45,33 @@ class GroupRepositoryImpl(
|
||||
val parsed = withContext(Dispatchers.IO) {
|
||||
VyatsuHttpClient.openStream(VyatsuUrls.GROUPS_PAGE).use(GroupsParser::parseStream)
|
||||
}
|
||||
val entities = parsed.groups.map { group ->
|
||||
val range = parsed.dateRanges[group.id]
|
||||
val autumn = range?.get(Season.AUTUMN)?.lastOrNull()
|
||||
val spring = range?.get(Season.SPRING)?.lastOrNull()
|
||||
GroupEntity(
|
||||
id = group.id,
|
||||
name = group.name,
|
||||
faculty = group.faculty,
|
||||
autumnFirst = autumn?.first?.let(::epochDay),
|
||||
autumnSecond = autumn?.second?.let(::epochDay),
|
||||
springFirst = spring?.first?.let(::epochDay),
|
||||
springSecond = spring?.second?.let(::epochDay),
|
||||
lastSyncedAt = now()
|
||||
)
|
||||
val now = now()
|
||||
groupDao.insertAll(
|
||||
parsed.groups.map { group ->
|
||||
GroupEntity(
|
||||
id = group.id,
|
||||
name = group.name,
|
||||
faculty = group.faculty,
|
||||
lastSyncedAt = now
|
||||
)
|
||||
}
|
||||
)
|
||||
val periods = parsed.groups.flatMap { group ->
|
||||
parsed.dateRanges[group.id].orEmpty().flatMap { (season, ranges) ->
|
||||
ranges.map { range ->
|
||||
GroupPeriodEntity(
|
||||
groupId = group.id,
|
||||
season = season,
|
||||
weekStart = ScheduleDates.asDate(range.first).toEpochDay(),
|
||||
weekEnd = ScheduleDates.asDate(range.second).toEpochDay()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
groupDao.insertAll(entities)
|
||||
groupDao.deleteAllPeriods()
|
||||
groupDao.insertPeriods(periods)
|
||||
}
|
||||
|
||||
private fun epochDay(dateStr: String): Long = ScheduleDates.asDate(dateStr).toEpochDay()
|
||||
|
||||
private fun now(): Long = System.currentTimeMillis()
|
||||
|
||||
private fun GroupEntity.toDomain(): Group = Group(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package agzam.vyatsu.data.repository
|
||||
|
||||
import agzam.vyatsu.data.local.DayEntity
|
||||
import agzam.vyatsu.data.local.GroupPeriodEntity
|
||||
import agzam.vyatsu.data.local.LessonEntity
|
||||
import agzam.vyatsu.data.local.ScheduleDao
|
||||
import agzam.vyatsu.data.local.ScheduleEntity
|
||||
@@ -51,15 +52,38 @@ class ScheduleRepositoryImpl(
|
||||
Log.w(TAG, "sync: group $groupId not found in DB")
|
||||
return null
|
||||
}
|
||||
val season = ScheduleDates.currentSeason()
|
||||
val range = dateRangeFor(group, season) ?: run {
|
||||
Log.w(TAG, "sync: no date range for season $season (group ${group.id})")
|
||||
val today = ScheduleDates.moscowToday().toEpochDay()
|
||||
val periods = groupDao.getCurrentAndFuturePeriods(groupId, today)
|
||||
if (periods.isEmpty()) {
|
||||
Log.w(TAG, "sync: no current/future periods for group ${group.id}")
|
||||
return null
|
||||
}
|
||||
Log.d(TAG, "sync: season=$season range=[${range.first}, ${range.second}]")
|
||||
|
||||
return try {
|
||||
val pdfUrl = VyatsuUrls.groupPdfUrl(groupId, season, range)
|
||||
for (period in periods) {
|
||||
syncPeriod(group, period)
|
||||
}
|
||||
|
||||
scheduleDao.trim(groupId, MAX_PERIODS)
|
||||
|
||||
return getSchedule(groupId)
|
||||
}
|
||||
|
||||
private suspend fun syncPeriod(group: GroupEntity, period: GroupPeriodEntity) {
|
||||
val season = period.season
|
||||
val range = DateRange(
|
||||
LocalDate.ofEpochDay(period.weekStart).format(RANGE_FMT),
|
||||
LocalDate.ofEpochDay(period.weekEnd).format(RANGE_FMT)
|
||||
)
|
||||
val existing = scheduleDao.getSchedules(group.id)
|
||||
.firstOrNull { it.season == season && it.weekStart == period.weekStart }
|
||||
val isCurrent = ScheduleDates.moscowToday().toEpochDay() in period.weekStart..period.weekEnd
|
||||
if (existing != null && !isCurrent && now() - existing.lastSyncedAt < STALE_WINDOW_MS) {
|
||||
Log.d(TAG, "sync: period $range up to date, skip")
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
val pdfUrl = VyatsuUrls.groupPdfUrl(group.id, season, range)
|
||||
Log.d(TAG, "sync: downloading $pdfUrl")
|
||||
val bytes = VyatsuHttpClient.getBytes(pdfUrl)
|
||||
Log.d(TAG, "sync: downloaded ${bytes.size} bytes")
|
||||
@@ -67,29 +91,19 @@ class ScheduleRepositoryImpl(
|
||||
val days = PdfScheduleParser.parse(bytes)
|
||||
Log.d(TAG, "sync: parsed ${days.size} days")
|
||||
|
||||
val weekStart = ScheduleDates.asDate(range.first).toEpochDay()
|
||||
val weekEnd = ScheduleDates.asDate(range.second).toEpochDay()
|
||||
|
||||
val existing = scheduleDao.getSchedules(groupId)
|
||||
.firstOrNull { it.season == season && it.weekStart == weekStart }
|
||||
val scheduleId = if (existing != null) existing.id
|
||||
else scheduleDao.insertSchedule(
|
||||
val scheduleId = existing?.id ?: scheduleDao.insertSchedule(
|
||||
ScheduleEntity(
|
||||
groupId = groupId,
|
||||
groupId = group.id,
|
||||
season = season,
|
||||
weekStart = weekStart,
|
||||
weekEnd = weekEnd,
|
||||
weekStart = period.weekStart,
|
||||
weekEnd = period.weekEnd,
|
||||
lastSyncedAt = now()
|
||||
)
|
||||
)
|
||||
replaceDays(scheduleId, days)
|
||||
|
||||
scheduleDao.trim(groupId, MAX_PERIODS)
|
||||
|
||||
getSchedule(groupId)
|
||||
Log.d(TAG, "sync: period $range synced")
|
||||
} catch (e: Throwable) {
|
||||
Log.e(TAG, "sync: failed for $groupId (${e::class.simpleName}: ${e.message})", e)
|
||||
null
|
||||
Log.e(TAG, "sync: failed for ${group.id} period $range (${e::class.simpleName}: ${e.message})", e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,30 +201,13 @@ class ScheduleRepositoryImpl(
|
||||
teacher = teacher
|
||||
)
|
||||
|
||||
private fun dateRangeFor(group: GroupEntity, season: Season): DateRange? {
|
||||
return when (season) {
|
||||
Season.AUTUMN -> if (group.autumnFirst != null && group.autumnSecond != null) {
|
||||
DateRange(
|
||||
LocalDate.ofEpochDay(group.autumnFirst).format(RANGE_FMT),
|
||||
LocalDate.ofEpochDay(group.autumnSecond).format(RANGE_FMT)
|
||||
)
|
||||
} else null
|
||||
Season.SPRING -> if (group.springFirst != null && group.springSecond != null) {
|
||||
DateRange(
|
||||
LocalDate.ofEpochDay(group.springFirst).format(RANGE_FMT),
|
||||
LocalDate.ofEpochDay(group.springSecond).format(RANGE_FMT)
|
||||
)
|
||||
} else null
|
||||
}
|
||||
}
|
||||
|
||||
private fun LocalTime.toMinutesOfDay(): Int = hour * 60 + minute
|
||||
|
||||
private fun now(): Long = System.currentTimeMillis()
|
||||
|
||||
companion object {
|
||||
private const val STALE_WINDOW_MS = 12 * 60 * 60 * 1000L
|
||||
private const val MAX_PERIODS = 3
|
||||
private const val MAX_PERIODS = 24
|
||||
private const val TAG = "ScheduleRepo"
|
||||
private val RANGE_FMT = java.time.format.DateTimeFormatter.ofPattern("ddMMyyyy")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user