Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add project dashboard page #915 - migrate on startup
- Loading branch information
Showing
12 changed files
with
225 additions
and
38 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
17 changes: 5 additions & 12 deletions
17
backend/app/src/main/kotlin/io/tolgee/MigrationJobsCommandLineRunner.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
102 changes: 102 additions & 0 deletions
102
.../app/src/test/kotlin/io/tolgee/jobs/migration/translationStats/TranslationStatsJobTest.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,102 @@ | ||
package io.tolgee.jobs.migration.translationStats | ||
|
||
import io.tolgee.AbstractSpringTest | ||
import io.tolgee.development.testDataBuilder.data.TranslationsTestData | ||
import io.tolgee.repository.TranslationRepository | ||
import io.tolgee.testing.assertions.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.batch.core.BatchStatus | ||
import org.springframework.batch.core.Job | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.context.transaction.TestTransaction | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@SpringBootTest | ||
class TranslationStatsJobTest : AbstractSpringTest() { | ||
|
||
@Autowired | ||
@Qualifier("translationStatsJob") | ||
lateinit var translationStatsJob: Job | ||
|
||
@Autowired | ||
lateinit var translationsStatsUpdateJobRunner: TranslationsStatsUpdateJobRunner | ||
|
||
@Autowired | ||
lateinit var translationRepository: TranslationRepository | ||
|
||
@Test | ||
@Transactional | ||
fun `it adds the stats`() { | ||
prepareData(10) | ||
|
||
val instance = translationsStatsUpdateJobRunner.run() | ||
|
||
assertStatsAdded() | ||
assertThat(instance?.status).isEqualTo(BatchStatus.COMPLETED) | ||
} | ||
|
||
@Test | ||
@Transactional | ||
fun `it does not run multiple times for same `() { | ||
prepareData() | ||
|
||
// first - it really runs | ||
val instance = translationsStatsUpdateJobRunner.run() | ||
// nothing to migrate, no run | ||
val instance2 = translationsStatsUpdateJobRunner.run() | ||
|
||
assertThat(instance).isNotNull | ||
assertThat(instance2).isNull() | ||
} | ||
|
||
@Test | ||
@Transactional | ||
fun `it runs again when new translation without stats is created`() { | ||
val testData = prepareData() | ||
|
||
val instance = translationsStatsUpdateJobRunner.run() | ||
|
||
TestTransaction.start() | ||
val newTranslationId = translationService.setForKey(testData.aKey, mapOf("en" to "Hellooooo!"))["en"]!!.id | ||
entityManager | ||
.createNativeQuery( | ||
"update translation set word_count = null, character_count = null where id = $newTranslationId" | ||
).executeUpdate() | ||
TestTransaction.flagForCommit() | ||
TestTransaction.end() | ||
|
||
val instance2 = translationsStatsUpdateJobRunner.run() | ||
assertThat(instance2?.id).isNotEqualTo(instance?.id) | ||
} | ||
|
||
private fun assertStatsAdded() { | ||
val translations = translationRepository.findAll().toMutableList() | ||
translations.sortBy { it.id } | ||
|
||
assertThat(translations) | ||
.allMatch { it.wordCount != null } | ||
.allMatch { it.characterCount != null } | ||
} | ||
|
||
private fun prepareData(keysToCreateCount: Long = 10): TranslationsTestData { | ||
val testData = TranslationsTestData() | ||
testData.generateLotOfData(keysToCreateCount) | ||
testDataService.saveTestData(testData.root) | ||
|
||
commitTransaction() | ||
|
||
entityManager | ||
.createNativeQuery("update translation set word_count = null, character_count = null") | ||
.executeUpdate() | ||
|
||
commitTransaction() | ||
|
||
val translations = translationRepository.findAll() | ||
assertThat(translations).allMatch { it.wordCount == null }.allMatch { it.characterCount == null } | ||
|
||
TestTransaction.end() | ||
return testData | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...rc/main/kotlin/io/tolgee/jobs/migration/translationStats/StatsMigrationTranslationView.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,7 @@ | ||
package io.tolgee.jobs.migration.translationStats | ||
|
||
interface StatsMigrationTranslationView { | ||
val id: Long | ||
val text: String? | ||
val languageTag: String | ||
} |
12 changes: 6 additions & 6 deletions
12
...nd/data/src/main/kotlin/io/tolgee/jobs/migration/translationStats/TranslationProcessor.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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package io.tolgee.jobs.migration.translationStats | ||
|
||
import io.tolgee.model.translation.Translation | ||
import io.tolgee.util.WordCounter | ||
import org.springframework.batch.item.ItemProcessor | ||
|
||
class TranslationProcessor : ItemProcessor<Translation, Translation> { | ||
override fun process(item: Translation): Translation { | ||
item.wordCount = item.text?.let { WordCounter.countWords(it, item.language.tag) } | ||
item.characterCount = item.text?.length ?: 0 | ||
return item | ||
class TranslationProcessor : ItemProcessor<StatsMigrationTranslationView, TranslationStats> { | ||
override fun process(item: StatsMigrationTranslationView): TranslationStats { | ||
return TranslationStats( | ||
id = item.id, wordCount = item.text?.let { WordCounter.countWords(it, item.languageTag) } ?: 0, | ||
characterCount = item.text?.length ?: 0 | ||
) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/data/src/main/kotlin/io/tolgee/jobs/migration/translationStats/TranslationStats.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,7 @@ | ||
package io.tolgee.jobs.migration.translationStats | ||
|
||
class TranslationStats( | ||
val id: Long, | ||
val wordCount: Int, | ||
val characterCount: Int | ||
) |
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
46 changes: 46 additions & 0 deletions
46
...main/kotlin/io/tolgee/jobs/migration/translationStats/TranslationsStatsUpdateJobRunner.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,46 @@ | ||
package io.tolgee.jobs.migration.translationStats | ||
|
||
import io.tolgee.configuration.tolgee.TolgeeProperties | ||
import io.tolgee.repository.TranslationRepository | ||
import org.apache.commons.codec.digest.DigestUtils | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.batch.core.Job | ||
import org.springframework.batch.core.JobExecution | ||
import org.springframework.batch.core.JobParameter | ||
import org.springframework.batch.core.JobParameters | ||
import org.springframework.batch.core.launch.JobLauncher | ||
import org.springframework.batch.core.repository.JobRepository | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class TranslationsStatsUpdateJobRunner( | ||
val tolgeeProperties: TolgeeProperties, | ||
@Qualifier(TranslationStatsJobConfiguration.JOB_NAME) | ||
val translationStatsJob: Job, | ||
val jobLauncher: JobLauncher, | ||
val translationRepository: TranslationRepository, | ||
val jobRepository: JobRepository | ||
) { | ||
|
||
val log = LoggerFactory.getLogger(this::class.java) | ||
|
||
fun run(): JobExecution? { | ||
val params = getJobParams() | ||
|
||
if (params != null) { | ||
return jobRepository.getLastJobExecution(TranslationStatsJobConfiguration.JOB_NAME, params) | ||
?: return jobLauncher.run(translationStatsJob, params) | ||
} | ||
return null | ||
} | ||
|
||
private fun getJobParams(): JobParameters? { | ||
val ids = translationRepository.findAllIdsForStatsUpdate() | ||
if (ids.isEmpty()) { | ||
return null | ||
} | ||
val hash = DigestUtils.sha256Hex(ids.flatMap { it.toBigInteger().toByteArray().toList() }.toByteArray()) | ||
return JobParameters(mapOf("idsHash" to JobParameter(hash))) | ||
} | ||
} |
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