-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* LectureRepository에 findAllRatings 추가 * evInfo 동기화 Batch 추가 * 강의평 추가/삭제/수정 시 snu4t에 정보 적용 * add reactive mongodb dependency * JpaPagingItemReader 사용 * MongoService 분리 * processor 없이 처리 * 필요없는 job 삭제
- Loading branch information
Showing
7 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
batch/src/main/kotlin/com/wafflestudio/snuttev/sync/SnuttRatingSyncJobConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.wafflestudio.snuttev.sync | ||
|
||
import com.wafflestudio.snuttev.core.domain.lecture.model.SnuttLectureIdMap | ||
import com.wafflestudio.snuttev.core.domain.lecture.repository.LectureRepository | ||
import jakarta.persistence.EntityManagerFactory | ||
import org.springframework.batch.core.Job | ||
import org.springframework.batch.core.Step | ||
import org.springframework.batch.core.job.builder.JobBuilder | ||
import org.springframework.batch.core.repository.JobRepository | ||
import org.springframework.batch.core.step.builder.StepBuilder | ||
import org.springframework.batch.item.ItemWriter | ||
import org.springframework.batch.item.database.JpaPagingItemReader | ||
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.data.mongodb.core.BulkOperations | ||
import org.springframework.data.mongodb.core.MongoTemplate | ||
import org.springframework.data.mongodb.core.query.Criteria | ||
import org.springframework.data.mongodb.core.query.Query | ||
import org.springframework.data.mongodb.core.query.Update | ||
import org.springframework.orm.jpa.JpaTransactionManager | ||
|
||
@Configuration | ||
@Profile(value = ["!test"]) | ||
class SnuttRatingSyncJobConfig( | ||
private val entityManagerFactory: EntityManagerFactory, | ||
private val mongoTemplate: MongoTemplate, | ||
private val lectureRepository: LectureRepository, | ||
) { | ||
companion object { | ||
const val RATING_SYNC_JOB_NAME = "RATING_SYNC_JOB" | ||
private const val CUSTOM_READER_JOB_STEP = RATING_SYNC_JOB_NAME + "_STEP" | ||
private const val CHUNK_SIZE = 1000000 | ||
} | ||
|
||
@Bean | ||
fun ratingSyncJob(jobRepository: JobRepository): Job { | ||
return JobBuilder(RATING_SYNC_JOB_NAME, jobRepository) | ||
.start(customReaderStep(jobRepository)) | ||
.build() | ||
} | ||
|
||
private fun customReaderStep(jobRepository: JobRepository): Step { | ||
return StepBuilder(CUSTOM_READER_JOB_STEP, jobRepository) | ||
.chunk<SnuttLectureIdMap, SnuttLectureIdMap>( | ||
CHUNK_SIZE, | ||
JpaTransactionManager().apply { | ||
this.entityManagerFactory = this@SnuttRatingSyncJobConfig.entityManagerFactory | ||
}, | ||
) | ||
.reader(reader()) | ||
.writer(writer()) | ||
.build() | ||
} | ||
|
||
private fun reader(): JpaPagingItemReader<SnuttLectureIdMap> = | ||
JpaPagingItemReaderBuilder<SnuttLectureIdMap>() | ||
.name("snuttLectureIdMapReader") | ||
.entityManagerFactory(entityManagerFactory) | ||
.queryString("SELECT s FROM SnuttLectureIdMap s JOIN FETCH s.semesterLecture") | ||
.pageSize(CHUNK_SIZE) | ||
.build() | ||
|
||
private fun writer(): ItemWriter<SnuttLectureIdMap> { | ||
return ItemWriter { items -> | ||
val lectureIdtoLectureRatingMap = | ||
lectureRepository.findAllRatingsByLectureIds( | ||
items.mapNotNull { it.semesterLecture.lecture.id }, | ||
) | ||
.associateBy { it.id } | ||
val bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, "lectures") | ||
items.forEach { | ||
val evInfo = lectureIdtoLectureRatingMap[it.semesterLecture.lecture.id] | ||
bulkOps.updateOne( | ||
Query(Criteria.where("_id").`is`(it.snuttId)), | ||
Update().set("evInfo.evId", evInfo?.id) | ||
.set("evInfo.avgRating", evInfo?.avgRating) | ||
.set("evInfo.count", evInfo?.count), | ||
) | ||
} | ||
bulkOps.execute() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
core/src/main/kotlin/com/wafflestudio/snuttev/core/domain/mongo/MongoService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.wafflestudio.snuttev.core.domain.mongo | ||
|
||
import com.wafflestudio.snuttev.core.domain.lecture.model.LectureRatingDao | ||
import org.springframework.data.mongodb.core.ReactiveMongoTemplate | ||
import org.springframework.data.mongodb.core.query.Criteria | ||
import org.springframework.data.mongodb.core.query.Query | ||
import org.springframework.data.mongodb.core.query.Update | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class MongoService( | ||
private val mongoTemplate: ReactiveMongoTemplate, | ||
) { | ||
fun updateEvInfoToSnuttIds(snuttIds: List<String>, evInfo: LectureRatingDao?) = | ||
mongoTemplate.updateMulti( | ||
Query(Criteria.where("_id").`in`(snuttIds)), | ||
Update().set("evInfo.evId", evInfo?.id) | ||
.set("evInfo.avgRating", evInfo?.avgRating) | ||
.set("evInfo.count", evInfo?.count), | ||
"lectures", | ||
).subscribe() | ||
} |