Skip to content

Commit

Permalink
Move score from lnk_contest_execution to execution (#1156)
Browse files Browse the repository at this point in the history
* Move `score` from `lnk_contest_execution` to `execution`
* Forbid rerun of `TestingType.CONTEST_MODE` executions on backend
* Get best_score directly from lnk_contest_project (this column is not being set yet, will be added in subsequent PRs)

Second part of #1115
  • Loading branch information
petertrr authored Sep 5, 2022
1 parent 1e86316 commit d76428d
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 45 deletions.
6 changes: 6 additions & 0 deletions db/test-data/execution-insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<column name="expected_checks" value="14"/>
<column name="unexpected_checks" value="0"/>
<column name="test_suite_source_name" value="unknown"/>
<column name="score" value="100.0"/>
</insert>
<insert dbms="mysql" tableName="execution">
<column name="id" value="2" />
Expand Down Expand Up @@ -61,6 +62,7 @@
<column name="expected_checks" value="0"/>
<column name="unexpected_checks" value="0"/>
<column name="test_suite_source_name" value="unknown"/>
<column name="score" value="75.0"/>
</insert>
<insert dbms="mysql" tableName="execution">
<column name="id" value="3" />
Expand Down Expand Up @@ -89,6 +91,7 @@
<column name="expected_checks" value="2"/>
<column name="unexpected_checks" value="0"/>
<column name="test_suite_source_name" value="unknown"/>
<column name="score" value="24.4"/>
</insert>
<insert dbms="mysql" tableName="execution">
<column name="id" value="4" />
Expand Down Expand Up @@ -117,6 +120,7 @@
<column name="expected_checks" value="2"/>
<column name="unexpected_checks" value="0"/>
<column name="test_suite_source_name" value="unknown"/>
<column name="score" value="11.2"/>
</insert>
<insert dbms="mysql" tableName="execution">
<column name="id" value="5" />
Expand Down Expand Up @@ -145,6 +149,7 @@
<column name="expected_checks" value="14"/>
<column name="unexpected_checks" value="3"/>
<column name="test_suite_source_name" value="unknown"/>
<column name="score" value="0.0"/>
</insert>

<insert dbms="mysql" tableName="execution">
Expand Down Expand Up @@ -174,6 +179,7 @@
<column name="expected_checks" value="14"/>
<column name="unexpected_checks" value="3"/>
<column name="test_suite_source_name" value="unknown"/>
<column name="score" value="11.11"/>
</insert>
</changeSet>

Expand Down
1 change: 0 additions & 1 deletion db/test-data/lnk-contest-execution-insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<column header="id" name="id" type="NUMERIC" />
<column header="contest_id" name="contest_id" type="NUMERIC" />
<column header="execution_id" name="execution_id" type="NUMERIC" />
<column header="score" name="score" type="double" />
</loadData>
</changeSet>

Expand Down
18 changes: 9 additions & 9 deletions db/test-data/sqlRequests/lnk-contest-execution.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
id;contest_id;execution_id;score
1;1;1;100.0
2;1;2;75.0
3;1;3;24.4
4;1;4;11.2
5;2;1;0.0
6;2;2;11.11
7;2;3;22.22
8;2;4;88.88
id;contest_id;execution_id
1;1;1
2;1;2
3;1;3
4;1;4
5;2;1
6;2;2
7;2;3
8;2;4
10 changes: 10 additions & 0 deletions db/v-2/tables/lnk-contest-execution.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@
</rollback>
</changeSet>

<changeSet id="move-score-from-lnk_contest_execution-to-execution" author="petertrr" context="dev or prod">
<addColumn tableName="execution">
<column name="score" type="double" defaultValue="null"/>
</addColumn>
<sql>
update lnk_contest_execution lce inner join execution e on e.id = lce.execution_id set e.score = lce.score;
</sql>
<dropColumn tableName="lnk_contest_execution" columnName="score"/>
</changeSet>

</databaseChangeLog>
4 changes: 4 additions & 0 deletions db/v-2/tables/lnk-contest-project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@
</addColumn>
</changeSet>

<changeSet id="lnk_contest_project_make_best_score_float" author="petertrr">
<modifyDataType tableName="lnk_contest_project" columnName="best_score" newDataType="double"/>
</changeSet>

</databaseChangeLog>
8 changes: 8 additions & 0 deletions save-backend/backend-api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -7426,6 +7426,10 @@
"testSuiteSourceName": {
"type": "string"
},
"score": {
"type": "number",
"format": "double"
},
"id": {
"type": "integer",
"format": "int64"
Expand Down Expand Up @@ -7604,6 +7608,10 @@
},
"testSuiteSourceName": {
"type": "string"
},
"score": {
"type": "number",
"format": "double"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class LnkContestProjectController(
).getScores()

private fun Flux<LnkContestProject>.getScores() = map {
it to lnkContestExecutionService.getBestScoreOfProjectInContestWithName(it.project, it.contest.name)
it to lnkContestProjectService.getBestScoreOfProjectInContestWithName(it.project, it.contest.name)
}
.map { (lnkContestProject, score) ->
lnkContestProject.toContestResult(score)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class RunExecutionController(
authentication: Authentication,
): Mono<StringResponse> = blockingToMono { executionService.findExecution(executionId) }
.switchIfEmptyToNotFound { "Not found execution id = $executionId" }
.filter { it.type != TestingType.CONTEST_MODE }
.switchIfEmptyToResponseException(HttpStatus.CONFLICT) {
"Rerun is not supported for executions that were performed under a contest"
}
.validateAccess(authentication) { execution ->
ProjectCoordinates(
execution.project.organization.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface LnkContestExecutionRepository : BaseEntityRepository<LnkContestExecuti
* @param pageable
* @return record with a page of N best scores of a contests of the project
*/
fun findByExecutionProjectAndContestNameOrderByScoreDesc(project: Project, contestName: String, pageable: Pageable): Page<LnkContestExecution>
fun findByExecutionProjectAndContestNameOrderByExecutionScoreDesc(project: Project, contestName: String, pageable: Pageable): Page<LnkContestExecution>

/**
* Get N executions of a [Project] in [Contest]
Expand Down Expand Up @@ -46,4 +46,11 @@ interface LnkContestExecutionRepository : BaseEntityRepository<LnkContestExecuti
*/
@Suppress("IDENTIFIER_LENGTH")
fun findByContestAndExecutionProjectIdInOrderByExecutionStartTimeDesc(contest: Contest, projectIds: List<Long>): List<LnkContestExecution>

/**
* @param project
* @param contestName
* @return [LnkContestExecution] associated with [project] and [Contest] with name [contestName]
*/
fun findByExecutionProjectAndContestName(project: Project, contestName: String): LnkContestExecution?
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ interface LnkContestProjectRepository : BaseEntityRepository<LnkContestProject>
* @return list of [LnkContestProject] linked to contest with name [contestName]
*/
fun findByContestName(contestName: String): List<LnkContestProject>

/**
* @param project
* @param contestName
* @return a [LnkContestProject], if any, associated with [project] and [Contest] named [contestName]
*/
fun findByProjectAndContestName(project: Project, contestName: String): LnkContestProject?
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class ExecutionService(
execCmd = execCmd,
batchSizeForAnalyzer = batchSizeForAnalyzer,
testSuiteSourceName = testSuiteSourceName,
score = null,
)
val savedExecution = saveExecution(execution)
log.info("Created a new execution id=${savedExecution.id} for project id=${project.id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.saveourtool.save.backend.service
import com.saveourtool.save.backend.repository.LnkContestExecutionRepository
import com.saveourtool.save.entities.*
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service

/**
Expand All @@ -13,17 +12,6 @@ import org.springframework.stereotype.Service
class LnkContestExecutionService(
private val lnkContestExecutionRepository: LnkContestExecutionRepository,
) {
/**
* @param project
* @param contestName
* @return best score of a [Project] in contest with name [contestName]
*/
fun getBestScoreOfProjectInContestWithName(project: Project, contestName: String) =
lnkContestExecutionRepository.findByExecutionProjectAndContestNameOrderByScoreDesc(project, contestName, Pageable.ofSize(1))
.content
.singleOrNull()
?.score

/**
* @param contest
* @param project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class LnkContestProjectService(
lnkContestProjectRepository.findByContestNameAndProjectIdIn(contestName, it)
}

/**
* @param project
* @param contestName
* @return best score of [project] under [Contest] with name [contestName]
*/
fun getBestScoreOfProjectInContestWithName(project: Project, contestName: String) =
lnkContestProjectRepository.findByProjectAndContestName(project, contestName)?.bestScore

/**
* @param project a [Project]
* @param contest a [Contest]
Expand All @@ -54,7 +62,7 @@ class LnkContestProjectService(
fun saveLnkContestProject(project: Project, contest: Contest): Boolean = if (lnkContestProjectRepository.findByContestAndProject(contest, project).isPresent) {
false
} else {
lnkContestProjectRepository.save(LnkContestProject(project, contest, null, 0))
lnkContestProjectRepository.save(LnkContestProject(project, contest, null, 0.0))
true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlinx.serialization.Serializable
* @property expectedChecks
* @property unexpectedChecks
* @property testSuiteSourceName
* @property score see [Execution.score]
*/
@Serializable
@Suppress("LongParameterList")
Expand All @@ -39,6 +40,7 @@ data class ExecutionDto(
val expectedChecks: Long,
val unexpectedChecks: Long,
val testSuiteSourceName: String?,
val score: Double?,
) {
companion object {
val empty = ExecutionDto(
Expand All @@ -58,6 +60,7 @@ data class ExecutionDto(
expectedChecks = 0,
unexpectedChecks = 0,
testSuiteSourceName = "",
score = null,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import javax.persistence.ManyToOne
* @property execCmd
* @property batchSizeForAnalyzer
* @property testSuiteSourceName
* @property score a rating of this execution. Specific meaning may vary depending on [type]
*/
@Suppress("LongParameterList")
@Entity
Expand Down Expand Up @@ -95,28 +96,31 @@ class Execution(

var testSuiteSourceName: String?,

var score: Double?,

) : BaseEntity() {
/**
* @return Execution dto
*/
@Suppress("UnsafeCallOnNullableType")
fun toDto() = ExecutionDto(
id!!,
status,
type,
version,
startTime.toEpochSecond(ZoneOffset.UTC),
endTime?.toEpochSecond(ZoneOffset.UTC),
allTests,
runningTests,
passedTests,
failedTests,
skippedTests,
unmatchedChecks,
matchedChecks,
expectedChecks,
unexpectedChecks,
testSuiteSourceName,
id = id!!,
status = status,
type = type,
version = version,
startTime = startTime.toEpochSecond(ZoneOffset.UTC),
endTime = endTime?.toEpochSecond(ZoneOffset.UTC),
allTests = allTests,
runningTests = runningTests,
passedTests = passedTests,
failedTests = failedTests,
skippedTests = skippedTests,
unmatchedChecks = unmatchedChecks,
matchedChecks = matchedChecks,
expectedChecks = expectedChecks,
unexpectedChecks = unexpectedChecks,
testSuiteSourceName = testSuiteSourceName,
score = score,
)

/**
Expand Down Expand Up @@ -173,6 +177,7 @@ class Execution(
execCmd = null,
batchSizeForAnalyzer = null,
testSuiteSourceName = "",
score = null,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import javax.persistence.OneToOne
*
* @property contest
* @property execution
* @property score the result of an execution
*/
@Entity
class LnkContestExecution(
Expand All @@ -22,7 +21,6 @@ class LnkContestExecution(
@JoinColumn(name = "contest_id")
var contest: Contest,

var score: Double
) : BaseEntity() {
/**
* @return [ContestResult] from [LnkContestExecution]
Expand All @@ -31,7 +29,7 @@ class LnkContestExecution(
execution.project.name,
execution.project.organization.name,
contest.name,
score,
execution.score,
execution.startTime,
execution.status,
execution.sdk,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LnkContestProject(
@JoinColumn(name = "best_execution_id")
var bestExecution: Execution?,

var bestScore: Int,
var bestScore: Double?,

) : BaseEntity() {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ExecutionStatisticsValuesTest {
expectedChecks = 25,
unexpectedChecks = 5,
testSuiteSourceName = "",
score = null,
)
val executionStatisticsValues = ExecutionStatisticsValues(executionDto)
assertEquals("danger", executionStatisticsValues.style)
Expand Down

0 comments on commit d76428d

Please sign in to comment.