IFT-555 Validate task id as simple integer

There is no need in the separate validation rule for it.
Also, this validation rule has side effects: it executed the LessonContext of the lesson to collect the number of tasks. But some for some lessons, it modified the state of the lesson. For example, the code in `OnboardingTourLessonBase.checkUiSettings` modifies the variables of the onboarding lesson, so it caused the problem in the mentioned ticket.

GitOrigin-RevId: d39d464eb0635dcf58842816cfa46fe0e1c4ab82
This commit is contained in:
Konstantin Hudyakov
2024-02-20 11:41:50 +02:00
committed by intellij-monorepo-bot
parent 53969dd6db
commit 8c3a5be33e
3 changed files with 5 additions and 56 deletions

View File

@@ -55,8 +55,6 @@
implementation="training.statistic.SupportedLanguageRuleValidator"/>
<statistics.validation.customValidationRule
implementation="training.statistic.ActionIdRuleValidator"/>
<statistics.validation.customValidationRule
implementation="training.statistic.TaskIdRuleValidator"/>
<statistics.validation.customValidationRule
implementation="training.statistic.KeymapSchemeRuleValidator"/>
<statistics.counterUsagesCollector implementationClass="training.statistic.StatisticBase"/>

View File

@@ -95,7 +95,7 @@ object StatisticBase : CounterUsagesCollector() {
private val LOG = logger<StatisticBase>()
private val sessionLessonTimestamp: ConcurrentHashMap<String, Long> = ConcurrentHashMap()
private var prevRestoreLessonProgress: LessonProgress = LessonProgress("", 0)
private val GROUP: EventLogGroup = EventLogGroup("ideFeaturesTrainer", 20)
private val GROUP: EventLogGroup = EventLogGroup("ideFeaturesTrainer", 21)
var isLearnProjectCloseLogged = false
@@ -104,7 +104,7 @@ object StatisticBase : CounterUsagesCollector() {
private val languageField = EventFields.StringValidatedByCustomRule(LANGUAGE, SupportedLanguageRuleValidator::class.java)
private val completedCountField = EventFields.Int(COMPLETED_COUNT)
private val courseSizeField = EventFields.Int(COURSE_SIZE)
private val taskIdField = EventFields.StringValidatedByCustomRule(TASK_ID, TaskIdRuleValidator::class.java)
private val taskIdField = EventFields.Int(TASK_ID)
private val actionIdField = EventFields.StringValidatedByCustomRule(ACTION_ID, ActionIdRuleValidator::class.java)
private val keymapSchemeField = EventFields.StringValidatedByCustomRule(KEYMAP_SCHEME, KeymapSchemeRuleValidator::class.java)
private val versionField = EventFields.Version
@@ -204,7 +204,7 @@ object StatisticBase : CounterUsagesCollector() {
val lessonId = lessonManager.currentLesson!!.id
val taskId = lessonManager.currentLessonExecutor!!.currentTaskIndex
lessonStoppedEvent.log(lessonIdField with lessonId,
taskIdField with taskId.toString(),
taskIdField with taskId,
languageField with courseLanguage(),
reasonField with reason
)
@@ -222,7 +222,7 @@ object StatisticBase : CounterUsagesCollector() {
shortcutClickedEvent.log(inputEventField with createInputEvent(actionId),
keymapSchemeField with keymap.name,
lessonIdField with lesson.id,
taskIdField with lessonManager.currentLessonExecutor?.currentTaskIndex.toString(),
taskIdField with lessonManager.currentLessonExecutor?.currentTaskIndex!!,
actionIdField with actionId,
versionField with getPluginVersion(lesson))
}
@@ -233,7 +233,7 @@ object StatisticBase : CounterUsagesCollector() {
if (curLessonProgress != prevRestoreLessonProgress) {
prevRestoreLessonProgress = curLessonProgress
restorePerformedEvent.log(lessonIdField with lesson.id,
taskIdField with taskId.toString(),
taskIdField with taskId,
versionField with getPluginVersion(lesson))
}
}

View File

@@ -1,49 +0,0 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package training.statistic
import com.intellij.internal.statistic.eventLog.validator.ValidationResultType
import com.intellij.internal.statistic.eventLog.validator.rules.EventContext
import com.intellij.internal.statistic.eventLog.validator.rules.impl.CustomValidationRule
import training.dsl.LessonContext
import training.dsl.TaskContext
import training.learn.CourseManager
import training.learn.course.KLesson
import training.statistic.FeatureUsageStatisticConsts.LESSON_ID
import training.statistic.FeatureUsageStatisticConsts.TASK_ID
class TaskIdRuleValidator : CustomValidationRule() {
override fun getRuleId(): String = TASK_ID
override fun doValidate(data: String, context: EventContext): ValidationResultType {
val taskId = data.toIntOrNull()
val lessonId = context.eventData[LESSON_ID]
if (taskId != null && lessonId is String) {
val lesson = CourseManager.instance.findLessonById(lessonId)
if (lesson is KLesson) {
val taskCount = lesson.getTaskCount()
if (taskId in 0 until taskCount) {
return ValidationResultType.ACCEPTED
}
}
}
return ValidationResultType.REJECTED
}
private fun KLesson.getTaskCount(): Int {
val context = ExtractTaskCountContext(this)
fullLessonContent(context)
return context.taskCount
}
}
private class ExtractTaskCountContext(override val lesson: KLesson) : LessonContext() {
var taskCount = 0
override fun task(taskContent: TaskContext.() -> Unit) {
taskCount++
}
override fun waitBeforeContinue(delayMillis: Int) {
taskCount++
}
}