-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from JetBrains-Research/store-survey
Store activity tracker key and survey info
- Loading branch information
Showing
11 changed files
with
252 additions
and
86 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
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/org/jetbrains/research/ml/codetracker/models/StoredInfo.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,9 @@ | ||
package org.jetbrains.research.ml.codetracker.models | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class StoredInfo( | ||
var loggedUIData: Map<String, String> = mapOf(), | ||
var activityTrackerKey: String? = null | ||
) |
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
74 changes: 74 additions & 0 deletions
74
src/main/kotlin/org/jetbrains/research/ml/codetracker/tracking/StoredInfoHandler.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,74 @@ | ||
package org.jetbrains.research.ml.codetracker.tracking | ||
|
||
import com.intellij.openapi.diagnostic.Logger | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonConfiguration | ||
import org.jetbrains.research.ml.codetracker.Plugin.codeTrackerFolderPath | ||
import org.jetbrains.research.ml.codetracker.models.Keyed | ||
import org.jetbrains.research.ml.codetracker.models.StoredInfo | ||
import java.io.File | ||
import java.io.PrintWriter | ||
|
||
|
||
object StoredInfoHandler{ | ||
|
||
val logger: Logger = Logger.getInstance(javaClass) | ||
|
||
fun getIntStoredField(field: UiLoggedDataHeader, defaultValue: Int): Int { | ||
return run{ | ||
val storedField = StoredInfoWrapper.info.loggedUIData[field.header]?.toIntOrNull() | ||
logger.info("Stored field $storedField for the ${field.header} value has been received successfully") | ||
storedField | ||
} ?: run { | ||
logger.info("Default value $defaultValue for the ${field.header} value has been received successfully") | ||
defaultValue | ||
} | ||
} | ||
|
||
fun <T : Keyed> getIndexByStoredKey(field: UiLoggedDataHeader, list: List<T>, defaultValue: Int): Int { | ||
StoredInfoWrapper.info.loggedUIData[field.header]?.let { storedKey -> | ||
val storedKeyIndex = list.indexOfFirst { it.key == storedKey } | ||
logger.info("Stored index $storedKeyIndex for the ${field.header} value has been received successfully") | ||
return storedKeyIndex | ||
} | ||
logger.info("Default value $defaultValue for the ${field.header} value has been received successfully") | ||
return defaultValue | ||
} | ||
} | ||
|
||
/* | ||
This class provides storing survey info and activity tracker key | ||
*/ | ||
object StoredInfoWrapper { | ||
|
||
private const val storedInfoFileName = "storedInfo.txt" | ||
private val storedInfoFilePath = "${codeTrackerFolderPath}/$storedInfoFileName" | ||
private val json by lazy { | ||
Json(JsonConfiguration.Stable) | ||
} | ||
private val serializer = StoredInfo.serializer() | ||
|
||
var info: StoredInfo = readStoredInfo() | ||
|
||
private fun readStoredInfo(): StoredInfo { | ||
val file = File(storedInfoFilePath) | ||
if (!file.exists()) { | ||
return StoredInfo() | ||
} | ||
return json.parse(serializer, file.readText()) | ||
} | ||
|
||
fun updateStoredInfo(surveyInfo: Map<String, String>? = null, | ||
activityTrackerKey: String? = null) { | ||
surveyInfo?.let{ info.loggedUIData = it } | ||
activityTrackerKey?.let{ info.activityTrackerKey = it } | ||
writeStoredInfo() | ||
} | ||
|
||
private fun writeStoredInfo() { | ||
val file = File(storedInfoFilePath) | ||
val writer = PrintWriter(file) | ||
writer.print(json.stringify(serializer, info)) | ||
writer.close() | ||
} | ||
} |
Oops, something went wrong.