forked from airbytehq/airbyte-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cron description endpoint (#14463)
Co-authored-by: Vladimir <[email protected]>
- Loading branch information
Showing
27 changed files
with
502 additions
and
596 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
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
44 changes: 44 additions & 0 deletions
44
airbyte-server/src/main/kotlin/io/airbyte/server/handlers/WebBackendCronExpressionHandler.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,44 @@ | ||
package io.airbyte.server.handlers | ||
|
||
import com.cronutils.descriptor.CronDescriptor | ||
import com.cronutils.model.CronType | ||
import com.cronutils.model.definition.CronDefinitionBuilder | ||
import com.cronutils.model.time.ExecutionTime | ||
import com.cronutils.parser.CronParser | ||
import io.airbyte.api.model.generated.WebBackendCronExpressionDescription | ||
import io.airbyte.api.model.generated.WebBackendDescribeCronExpressionRequestBody | ||
import io.airbyte.api.problems.model.generated.ProblemCronExpressionData | ||
import io.airbyte.api.problems.throwable.generated.CronValidationInvalidExpressionProblem | ||
import jakarta.inject.Singleton | ||
import java.time.ZonedDateTime | ||
import java.util.Locale | ||
|
||
@Singleton | ||
class WebBackendCronExpressionHandler { | ||
fun describeCronExpression(body: WebBackendDescribeCronExpressionRequestBody): WebBackendCronExpressionDescription? { | ||
val cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ) | ||
|
||
try { | ||
val cron = CronParser(cronDefinition).parse(body.cronExpression) | ||
cron.validate() | ||
|
||
val description = CronDescriptor.instance(Locale.ENGLISH).describe(cron) | ||
|
||
val executionTime = ExecutionTime.forCron(cron) | ||
val nextExecutions = mutableListOf<Long>() | ||
var nextExecution = ZonedDateTime.now() | ||
|
||
for (i in 1..3) { | ||
nextExecution = executionTime.nextExecution(nextExecution).orElse(null) ?: break | ||
nextExecutions.add(nextExecution.toEpochSecond()) | ||
} | ||
|
||
return WebBackendCronExpressionDescription() | ||
.cronExpression(body.cronExpression) | ||
.description(description) | ||
.nextExecutions(nextExecutions) | ||
} catch (e: IllegalArgumentException) { | ||
throw CronValidationInvalidExpressionProblem(ProblemCronExpressionData().cronExpression(body.cronExpression).validationErrorMessage(e.message)) | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...-server/src/test/kotlin/io/airbyte/server/handlers/WebBackendCronExpressionHandlerTest.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,63 @@ | ||
package io.airbyte.server.handlers | ||
|
||
import io.airbyte.api.model.generated.WebBackendDescribeCronExpressionRequestBody | ||
import io.airbyte.api.problems.throwable.generated.CronValidationInvalidExpressionProblem | ||
import junit.framework.TestCase.assertEquals | ||
import junit.framework.TestCase.assertNotNull | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
const val CRON_EVERY_HOUR = "0 0 * * * ?" | ||
const val CRON_EVERY_MINUTE = "0 * * * * ?" | ||
const val Y2K = "0 0 0 1 1 ? 2000" | ||
|
||
class WebBackendCronExpressionHandlerTest { | ||
private var webBackendCronExpressionHandler: WebBackendCronExpressionHandler = WebBackendCronExpressionHandler() | ||
|
||
@Test | ||
fun testDescribeEveryHourCronExpression() { | ||
val body = WebBackendDescribeCronExpressionRequestBody().cronExpression(CRON_EVERY_HOUR) | ||
val result = webBackendCronExpressionHandler.describeCronExpression(body) | ||
|
||
assertNotNull(result) | ||
if (result != null) { | ||
assertEquals(CRON_EVERY_HOUR, result.cronExpression) | ||
assertEquals(3, result.nextExecutions.size) | ||
assertEquals(result.nextExecutions[1] - result.nextExecutions[0], 3600) | ||
} | ||
} | ||
|
||
@Test | ||
fun testDescribeEveryMinuteCronExpression() { | ||
val body = WebBackendDescribeCronExpressionRequestBody().cronExpression(CRON_EVERY_MINUTE) | ||
val result = webBackendCronExpressionHandler.describeCronExpression(body) | ||
|
||
assertNotNull(result) | ||
if (result != null) { | ||
assertEquals(CRON_EVERY_MINUTE, result.cronExpression) | ||
assertEquals(3, result.nextExecutions.size) | ||
assertEquals(result.nextExecutions[1] - result.nextExecutions[0], 60) | ||
} | ||
} | ||
|
||
@Test | ||
fun testDescribeY2KCronExpression() { | ||
val body = WebBackendDescribeCronExpressionRequestBody().cronExpression(Y2K) | ||
val result = webBackendCronExpressionHandler.describeCronExpression(body) | ||
|
||
assertNotNull(result) | ||
if (result != null) { | ||
assertEquals(Y2K, result.cronExpression) | ||
// Y2K already passed, so there are no future executions | ||
assertEquals(0, result.nextExecutions.size) | ||
} | ||
} | ||
|
||
@Test | ||
fun testThrowsInvalidCronExpression() { | ||
val body = WebBackendDescribeCronExpressionRequestBody().cronExpression("invalid") | ||
assertThrows<CronValidationInvalidExpressionProblem> { | ||
webBackendCronExpressionHandler.describeCronExpression(body) | ||
} | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
...omponents/connection/ConnectionForm/ScheduleFormField/CronScheduleFormControl.module.scss
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.