Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Prepare for CT -> Refactoring & Possible performance improvements #2057

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import io.tolgee.hateoas.translations.comments.TranslationCommentModelAssembler
import io.tolgee.hateoas.translations.comments.TranslationWithCommentModel
import io.tolgee.model.enums.Scope
import io.tolgee.model.enums.TranslationCommentState
import io.tolgee.model.enums.TranslationState
import io.tolgee.model.translation.Translation
import io.tolgee.model.translation.TranslationComment
import io.tolgee.security.ProjectHolder
Expand Down Expand Up @@ -174,27 +173,12 @@ class TranslationCommentController(
@RequestBody @Valid
dto: TranslationCommentWithLangKeyDto,
): ResponseEntity<TranslationWithCommentModel> {
val translation = translationService.getOrCreate(dto.keyId, dto.languageId)
if (translation.key.project.id != projectHolder.project.id) {
throw BadRequestException(io.tolgee.constants.Message.KEY_NOT_FROM_PROJECT)
}

if (translation.language.project.id != projectHolder.project.id) {
throw BadRequestException(io.tolgee.constants.Message.LANGUAGE_NOT_FROM_PROJECT)
}

// Translation was just created
if (translation.id == 0L) {
translation.state = TranslationState.UNTRANSLATED
}

translationService.save(translation)

val comment = translationCommentService.create(dto, translation, authenticationFacade.authenticatedUserEntity)
val comment =
translationCommentService.create(dto, projectHolder.project.id, authenticationFacade.authenticatedUserEntity)
return ResponseEntity(
TranslationWithCommentModel(
comment = translationCommentModelAssembler.toModel(comment),
translation = translationModelAssembler.toModel(translation),
translation = translationModelAssembler.toModel(comment.translation),
),
HttpStatus.CREATED,
)
Expand All @@ -211,9 +195,13 @@ class TranslationCommentController(
@RequestBody @Valid
dto: TranslationCommentDto,
): ResponseEntity<TranslationCommentModel> {
val translation = translationService.find(translationId) ?: throw NotFoundException()
translation.checkFromProject()
val comment = translationCommentService.create(dto, translation, authenticationFacade.authenticatedUserEntity)
val comment =
translationCommentService.create(
dto,
translationId,
projectHolder.project.id,
authenticationFacade.authenticatedUserEntity,
)
return ResponseEntity(translationCommentModelAssembler.toModel(comment), HttpStatus.CREATED)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import io.tolgee.service.key.KeyService
import io.tolgee.service.key.ScreenshotService
import io.tolgee.service.queryBuilders.CursorUtil
import io.tolgee.service.security.SecurityService
import io.tolgee.service.translation.AllTranslationsService
import io.tolgee.service.translation.TranslationService
import jakarta.validation.Valid
import org.springdoc.core.annotations.ParameterObject
Expand Down Expand Up @@ -104,6 +105,7 @@ class TranslationsController(
private val activityHolder: ActivityHolder,
private val activityService: ActivityService,
private val projectTranslationLastModifiedManager: ProjectTranslationLastModifiedManager,
private val allTranslationsService: AllTranslationsService,
) : IController {
@GetMapping(value = ["/{languages}"])
@Operation(
Expand Down Expand Up @@ -160,7 +162,7 @@ When null, resulting file will be a flat key-value object.
.filterViewPermissionByTag(projectId = projectHolder.project.id, languageTags = languages)

val response =
translationService.getTranslations(
allTranslationsService.getAllTranslations(
languageTags = permittedTags,
namespace = ns,
projectId = projectHolder.project.id,
Expand All @@ -187,7 +189,8 @@ When null, resulting file will be a flat key-value object.
val key = keyService.get(projectHolder.project.id, dto.key, dto.namespace)
securityService.checkLanguageTranslatePermissionsByTag(dto.translations.keys, projectHolder.project.id)

val modifiedTranslations = translationService.setForKey(key, dto.translations)
val modifiedTranslations =
translationService.set(key, dto.translations, projectHolder.project.id)

val translations =
dto.languagesToReturn
Expand All @@ -210,14 +213,14 @@ When null, resulting file will be a flat key-value object.
dto: SetTranslationsWithKeyDto,
): SetTranslationsResponseModel {
val key =
keyService.find(projectHolder.projectEntity.id, dto.key, dto.namespace)?.also {
keyService.find(projectHolder.project.id, dto.key, dto.namespace)?.also {
activityHolder.activity = ActivityType.SET_TRANSLATIONS
} ?: let {
checkKeyEditScope()
activityHolder.activity = ActivityType.CREATE_KEY
keyService.create(projectHolder.projectEntity, dto.key, dto.namespace)
}
val translations = translationService.setForKey(key, dto.translations)
val translations = translationService.set(key, dto.translations, projectHolder.project.id)
return getSetTranslationsResponse(key, translations)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ class KeyComplexEditHelper(
}.toMap()

val translations =
translationService.setForKey(
translationService.set(
key,
oldTranslations = oldTranslations,
translations = modifiedTranslations,
projectId = projectHolder.project.id,
)

translations.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.tolgee.security.authentication.AllowApiAccess
import io.tolgee.security.authentication.AuthenticationFacade
import io.tolgee.security.authorization.RequiresProjectPermissions
import io.tolgee.service.security.PermissionService
import io.tolgee.service.translation.AllTranslationsService
import io.tolgee.service.translation.TranslationService
import io.tolgee.util.StreamingResponseBodyProvider
import org.apache.tomcat.util.http.fileupload.IOUtils
Expand Down Expand Up @@ -40,6 +41,7 @@ class ExportController(
private val projectHolder: ProjectHolder,
private val authenticationFacade: AuthenticationFacade,
private val streamingResponseBodyProvider: StreamingResponseBodyProvider,
private val allTranslationsService: AllTranslationsService,
) : IController {
@GetMapping(value = ["/jsonZip"], produces = ["application/zip"])
@Operation(summary = "Exports data as ZIP of jsons", deprecated = true)
Expand All @@ -65,7 +67,7 @@ class ExportController(
streamingResponseBodyProvider.createStreamingResponseBody { out: OutputStream ->
val zipOutputStream = ZipOutputStream(out)
val translations =
translationService.getTranslations(
allTranslationsService.getAllTranslations(
allLanguages.map { it.tag }.toSet(),
null,
projectHolder.project.id,
Expand Down
2 changes: 1 addition & 1 deletion backend/app/src/main/resources/application-dbschema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ spring:
drop-first: true
change-log: classpath:db/changelog/schema.xml
datasource:
url: jdbc:postgresql://localhost:55432/postgres
url: jdbc:postgresql://localhost:55438/postgres
username: postgres
password: postgres
tolgee:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ class TranslationsControllerCachingTest : ProjectAuthControllerTest("/v2/project

val newNow = Date(Date().time + 50000)
setForcedDate(newNow)
translationService.setTranslation(testData.aKey, testData.englishLanguage, "This was changed!")
translationService.set(
testData.aKey,
testData.englishLanguage,
"This was changed!",
testData.project.id,
)

val newLastModified = performWithIsModifiedSince(lastModified).andIsOk.lastModified()
assertEqualsDate(newLastModified, newNow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class TranslationStatsJobTest : AbstractSpringTest() {
val instance = translationsStatsUpdateJobRunner.run()

executeInNewTransaction {
val newTranslationId = translationService.setForKey(testData.aKey, mapOf("en" to "Hellooooo!"))["en"]!!.id
val newTranslationId =
translationService.set(testData.aKey, mapOf("en" to "Hellooooo!"), testData.project.id)["en"]!!.id
entityManager
.createNativeQuery(
"update translation set word_count = null, character_count = null where id = $newTranslationId",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.tolgee.service

import io.tolgee.AbstractSpringTest
import io.tolgee.dtos.request.key.CreateKeyDto
import io.tolgee.testing.assertions.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.transaction.annotation.Transactional
import java.util.*

@SpringBootTest
@Transactional
class AllTranslationsServiceTest : AbstractSpringTest() {
@Transactional
@Test
fun getTranslations() {
val id = dbPopulator.populate("App").project.id
val data =
allTranslationsService.getAllTranslations(
languageTags = HashSet(Arrays.asList("en", "de")),
namespace = null,
projectId = id,
structureDelimiter = '.',
)
assertThat(data["en"]).isInstanceOf(MutableMap::class.java)
}

@Transactional
@Test
fun `returns correct map when collision`() {
val project = dbPopulator.populate("App").project
keyService.create(project, CreateKeyDto("folder.folder", null, mapOf("en" to "Ha")))
keyService.create(project, CreateKeyDto("folder.folder.translation", null, mapOf("en" to "Ha")))

val viewData =
allTranslationsService.getAllTranslations(
languageTags = HashSet(Arrays.asList("en", "de")),
namespace = null,
projectId = project.id,
structureDelimiter = '.',
)
@Suppress("UNCHECKED_CAST")
assertThat(viewData["en"] as Map<String, *>).containsKey("folder.folder.translation")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.tolgee.service

import io.tolgee.AbstractSpringTest
import io.tolgee.development.testDataBuilder.data.TranslationsTestData
import io.tolgee.dtos.request.key.CreateKeyDto
import io.tolgee.testing.assertions.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
Expand All @@ -12,38 +11,6 @@ import java.util.*
@SpringBootTest
@Transactional
class TranslationServiceTest : AbstractSpringTest() {
@Transactional
@Test
fun getTranslations() {
val id = dbPopulator.populate("App").project.id
val data =
translationService.getTranslations(
languageTags = HashSet(Arrays.asList("en", "de")),
namespace = null,
projectId = id,
structureDelimiter = '.',
)
assertThat(data["en"]).isInstanceOf(MutableMap::class.java)
}

@Transactional
@Test
fun `returns correct map when collision`() {
val project = dbPopulator.populate("App").project
keyService.create(project, CreateKeyDto("folder.folder", null, mapOf("en" to "Ha")))
keyService.create(project, CreateKeyDto("folder.folder.translation", null, mapOf("en" to "Ha")))

val viewData =
translationService.getTranslations(
languageTags = HashSet(Arrays.asList("en", "de")),
namespace = null,
projectId = project.id,
structureDelimiter = '.',
)
@Suppress("UNCHECKED_CAST")
assertThat(viewData["en"] as Map<String, *>).containsKey("folder.folder.translation")
}

@Test
fun `adds stats on translation save and update`() {
val testData =
Expand Down Expand Up @@ -73,7 +40,7 @@ class TranslationServiceTest : AbstractSpringTest() {
fun `clears auto translation when set empty`() {
val testData = TranslationsTestData()
testDataService.saveTestData(testData.root)
translationService.setForKey(testData.aKey, mapOf("de" to ""))
translationService.set(testData.aKey, mapOf("de" to ""), testData.project.id)
val translation = translationService.get(testData.aKeyGermanTranslation.id)
assertThat(translation.auto).isFalse
assertThat(translation.mtProvider).isNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ open class ActivityHolder(val applicationContext: ApplicationContext) {
activityRevision.describingRelations.associateBy { it.entityId to it.entityClass }.toMutableMap()
}

open val modifiedEntityInstances = mutableListOf<Any>()

fun getDescribingRelationFromCache(
entityId: Long,
entityClass: String,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.tolgee.component.demoProject

import io.tolgee.activity.ActivityHolder
import io.tolgee.activity.data.ActivityType
import io.tolgee.dtos.KeyAndLanguage
import io.tolgee.dtos.RelatedKeyDto
import io.tolgee.dtos.request.KeyInScreenshotPositionDto
import io.tolgee.dtos.request.ScreenshotInfoDto
Expand All @@ -11,7 +12,6 @@ import io.tolgee.model.Project
import io.tolgee.model.Screenshot
import io.tolgee.model.enums.TranslationState
import io.tolgee.model.key.Key
import io.tolgee.model.translation.Translation
import io.tolgee.service.LanguageService
import io.tolgee.service.bigMeta.BigMetaService
import io.tolgee.service.key.KeyService
Expand Down Expand Up @@ -54,11 +54,17 @@ class DemoProjectCreator(
}

private val translations by lazy {
DemoProjectData.translations.flatMap { (languageTag, translations) ->
translations.map { (key, text) ->
setTranslation(key, languageTag, text)
}
}.associateBy { it.language.tag to it.key.name }
val toSave =
DemoProjectData.translations.flatMap { (languageTag, translations) ->
translations.mapNotNull { (key, text) ->
KeyAndLanguage(keys[key] ?: return@mapNotNull null, languages[languageTag] ?: return@mapNotNull null) to text
}
}.toMap()

translationService.set(
toSave,
project.id,
).entries.associate { (it.key.language.tag to it.key.key.name) to it.value }
}

private fun addBigMeta() {
Expand All @@ -79,17 +85,6 @@ class DemoProjectCreator(
}
}

private fun setTranslation(
keyName: String,
languageTag: String,
translation: String,
): Translation {
val language = languages[languageTag]!!
return translationService.setTranslation(getOrCreateKey(keyName), language, translation).also {
it.state = TranslationState.REVIEWED
}
}

private fun addScreenshots() {
val screenshot = saveScreenshot()

Expand Down
Loading
Loading