From 9a473f307bbe6e0f5edc4957556ae71723bb6759 Mon Sep 17 00:00:00 2001 From: mikrise2 Date: Wed, 18 Oct 2023 01:02:54 +0200 Subject: [PATCH] [ML4SE-169] Removed Web* states. Object for requests --- .../tasktracker/requests/FileRequests.kt | 64 +++++++ .../ui/main/panel/MainPluginPanelFactory.kt | 176 ++---------------- .../ui/main/panel/template/SurveyTemplate.kt | 4 +- .../resources/model/emotion-ferplus-18.onnx | 0 ijPlugin/src/main/resources/model/log.txt | 1 - 5 files changed, 77 insertions(+), 168 deletions(-) create mode 100644 ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/requests/FileRequests.kt delete mode 100644 ijPlugin/src/main/resources/model/emotion-ferplus-18.onnx delete mode 100644 ijPlugin/src/main/resources/model/log.txt diff --git a/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/requests/FileRequests.kt b/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/requests/FileRequests.kt new file mode 100644 index 00000000..c0f821d3 --- /dev/null +++ b/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/requests/FileRequests.kt @@ -0,0 +1,64 @@ +package org.jetbrains.research.tasktracker.requests + +import com.intellij.openapi.diagnostic.Logger +import io.ktor.client.* +import io.ktor.client.engine.cio.* +import io.ktor.client.request.forms.* +import io.ktor.http.* +import kotlinx.coroutines.runBlocking +import org.jetbrains.research.tasktracker.tracking.activity.ActivityTracker +import org.jetbrains.research.tasktracker.tracking.fileEditor.FileEditorTracker +import org.jetbrains.research.tasktracker.tracking.toolWindow.ToolWindowTracker +import org.jetbrains.research.tasktracker.tracking.webcam.WebCamTracker +import org.jetbrains.research.tasktracker.ui.main.panel.storage.MainPanelStorage +import java.io.File + +object FileRequests { + + private val client = HttpClient(CIO) + private val logger: Logger = Logger.getInstance(FileRequests::class.java) + + fun sendToolWindowFiles(toolWindowTracker: ToolWindowTracker) = toolWindowTracker.getLogFiles().all { + sendFile(it, "toolWindow") + } + + fun sendActivityFiles(activityTracker: ActivityTracker) = activityTracker.getLogFiles().all { + sendFile(it, "activity") + } + + fun sendFileEditorFiles(fileEditorTracker: FileEditorTracker) = fileEditorTracker.getLogFiles().all { + sendFile(it, "fileEditor") + } + + fun sendWebcamFiles(webCamTracker: WebCamTracker) = webCamTracker.getLogFiles().all { + sendFile(it, "webCam") + } + + fun sendSurvey(surveyFiles: List) = surveyFiles.all { sendFile(it, "survey") } + + @Suppress("TooGenericExceptionCaught") + private fun sendFile(file: File, subdir: String) = runBlocking { + try { + client.submitFormWithBinaryData( + url = "$DOMAIN/upload-document/${MainPanelStorage.currentResearchId}?subdir=$subdir", + formData = formData { + append( + "file", + file.readBytes(), + Headers.build { + append( + HttpHeaders.ContentDisposition, "filename=\"${file.name}\"" + ) + } + ) + } + ) + true + } catch (e: Exception) { + logger.warn("Server interaction error! File to send: ${file.path}", e) + false + } + } + + const val DOMAIN = "http://3.249.245.244:8888" +} diff --git a/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/MainPluginPanelFactory.kt b/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/MainPluginPanelFactory.kt index 4e0daa75..81b41209 100644 --- a/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/MainPluginPanelFactory.kt +++ b/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/MainPluginPanelFactory.kt @@ -13,19 +13,17 @@ import com.intellij.openapi.wm.ToolWindowFactory import com.intellij.ui.components.JBPanel import com.intellij.ui.jcef.JBCefApp import com.intellij.util.ui.JBUI -import io.ktor.client.* -import io.ktor.client.call.* -import io.ktor.client.engine.cio.* -import io.ktor.client.request.* -import io.ktor.client.request.forms.* -import io.ktor.http.* import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import org.jetbrains.research.tasktracker.TaskTrackerPlugin import org.jetbrains.research.tasktracker.config.content.task.base.Task import org.jetbrains.research.tasktracker.config.content.task.base.TaskWithFiles import org.jetbrains.research.tasktracker.modelInference.model.EmoModel +import org.jetbrains.research.tasktracker.requests.FileRequests.sendActivityFiles +import org.jetbrains.research.tasktracker.requests.FileRequests.sendFileEditorFiles +import org.jetbrains.research.tasktracker.requests.FileRequests.sendSurvey +import org.jetbrains.research.tasktracker.requests.FileRequests.sendToolWindowFiles +import org.jetbrains.research.tasktracker.requests.FileRequests.sendWebcamFiles import org.jetbrains.research.tasktracker.tracking.BaseTracker import org.jetbrains.research.tasktracker.tracking.TaskFileHandler import org.jetbrains.research.tasktracker.tracking.activity.ActivityTracker @@ -41,7 +39,6 @@ import org.jetbrains.research.tasktracker.util.survey.SurveyParser import java.awt.BorderLayout import java.awt.FlowLayout import java.awt.event.ActionListener -import java.io.File import javax.swing.JButton /** @@ -63,9 +60,7 @@ class MainPluginPanelFactory : ToolWindowFactory { private lateinit var mainWindow: MainPluginWindow private lateinit var project: Project - private val client = HttpClient(CIO) override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) { - MainPanelStorage.userId = getUserId() this.project = project mainWindow = project.getService(MainWindowService::class.java).mainWindow mainWindow.jComponent.size = JBUI.size(toolWindow.component.width, toolWindow.component.height) @@ -83,49 +78,24 @@ class MainPluginPanelFactory : ToolWindowFactory { override fun isApplicable(project: Project) = super.isApplicable(project) && JBCefApp.isSupported() - private fun webWelcomePage() { - nextButton.text = UIBundle.message("ui.button.next") - backButton.isVisible = false - mainWindow.loadHtmlTemplate(MainPageTemplate.loadCurrentTemplate()) - nextButton.addListener { - webCamPage() - } - } - - private fun webSolvePage() { - nextButton.text = UIBundle.message("ui.button.next") - backButton.isVisible = true -// mainWindow.loadHtmlTemplate(SolveWebPageTemplate.loadCurrentTemplate()) // TODO replace to the solvePage - - startTracking() - - backButton.addListener { - webCamPage() - } - nextButton.addListener { - survey() - } - } - private fun startTracking() { if (trackers.isNotEmpty()) { // Otherwise we can lose data return } - trackers.clear() - - // TODO: make better shared loggers TaskTrackerPlugin.mainConfig.emotionConfig?.let { GlobalPluginStorage.emoPredictor = EmoModel(it) } ?: error("emotion config must exist by this moment") - val webCamTracker = WebCamTracker(project, GlobalPluginStorage.emoPredictor!!) + trackers.addAll( listOf( ActivityTracker(project), ToolWindowTracker(project), - webCamTracker, FileEditorTracker(project) ) ) + GlobalPluginStorage.emoPredictor?.let { + trackers.add(WebCamTracker(project, it)) + } trackers.forEach { it.startTracking() } } @@ -145,6 +115,7 @@ class MainPluginPanelFactory : ToolWindowFactory { } } + @Suppress("UnusedPrivateMember") private fun collectAllDevicesWithProgressBarAndShowNextPage(project: Project) { ProgressManager.getInstance().run(object : Modal( project, UIBundle.message("ui.progress.webcam.title"), false @@ -160,30 +131,6 @@ class MainPluginPanelFactory : ToolWindowFactory { }) } - /** - * Switches the panel to select a webcam. - */ - private fun webCamPage() { - MainPanelStorage.currentResearchId = getResearchId() - with(MainPanelStorage) { - registerResearch(userId, currentResearchId) - } - collectAllDevicesWithProgressBarAndShowNextPage(project) - nextButton.text = UIBundle.message("ui.button.select") - backButton.isVisible = true - backButton.addListener { - webWelcomePage() - } - nextButton.addListener { - mainWindow.getElementValue("cameras").onSuccess { deviceNumber -> - GlobalPluginStorage.currentDeviceNumber = deviceNumber?.toInt() - webSolvePage() - }.onError { - error(it.localizedMessage) - } - } - } - // TODO refactor it for many configs /** * Switches the panel to the task selection window. @@ -208,6 +155,7 @@ class MainPluginPanelFactory : ToolWindowFactory { * Loads configs by selected task and language */ private fun processTask(name: String) { + startTracking() // TODO: change to task by id val task = MainPanelStorage.taskIdTask.values.find { it.name == name } ?: error("Can't find task with name '$name'") @@ -271,21 +219,13 @@ class MainPluginPanelFactory : ToolWindowFactory { } } trackers.clear() - resetAllIds() - webFinalPage() } } backButton.addListener { } } - private fun resetAllIds() { - MainPanelStorage.currentResearchId = null - MainPanelStorage.userId = null - } - private fun serverErrorPage() { - resetAllIds() trackers.clear() loadBasePage( ServerErrorPage(), "ui.button.welcome", false @@ -293,15 +233,6 @@ class MainPluginPanelFactory : ToolWindowFactory { nextButton.isVisible = false } - private fun webFinalPage() { - loadBasePage( - FinalPageTemplate.loadCurrentTemplate(), "ui.button.welcome", false - ) - nextButton.addListener { - welcomePage() - } - } - /** * Switches the panel to the task solving window. * It contains task name, description and I/O data. @@ -355,89 +286,4 @@ class MainPluginPanelFactory : ToolWindowFactory { } addActionListener(listener) } - - private fun getUserId() = getId("get-user-id") - private fun getResearchId() = getId("get-research-id") - - @Suppress("TooGenericExceptionCaught") - private fun getId(request: String): Int? = runBlocking { - try { - client.get("$DOMAIN/$request").body() - } catch (e: Exception) { - logger.warn("Server interaction error! Request: $request", e) - null - } - } - - private fun sendToolWindowFiles(toolWindowTracker: ToolWindowTracker) = toolWindowTracker.getLogFiles().all { - sendFile(it, "toolWindow") - } - - private fun sendActivityFiles(activityTracker: ActivityTracker) = activityTracker.getLogFiles().all { - sendFile(it, "activity") - } - - private fun sendFileEditorFiles(fileEditorTracker: FileEditorTracker) = fileEditorTracker.getLogFiles().all { - sendFile(it, "fileEditor") - } - - private fun sendWebcamFiles(webCamTracker: WebCamTracker) = webCamTracker.getLogFiles().all { - sendFile(it, "webCam") - } - - private fun sendSurvey(surveyFiles: List) = surveyFiles.all { sendFile(it, "survey") } - - @Suppress("TooGenericExceptionCaught") - private fun sendFile(file: File, subdir: String) = runBlocking { - try { - client.submitFormWithBinaryData( - url = "$DOMAIN/upload-document/${MainPanelStorage.currentResearchId}?subdir=$subdir", - formData = formData { - append( - "file", - file.readBytes(), - Headers.build { - append( - HttpHeaders.ContentDisposition, "filename=\"${file.name}\"" - ) - } - ) - } - ) - true - } catch (e: Exception) { - logger.warn("Server interaction error! File to send: ${file.path}", e) - false - } - } - - @Suppress("TooGenericExceptionCaught") - private fun registerResearch(userId: Int?, researchId: Int?, name: String = "hackathon2023") { // TODO name - if (userId == null || researchId == null) { - logger.warn( - "Found problems with the server connection! " + - "Continue doing this research offline. userId=$userId, researchId=$researchId" - ) - return - } - runBlocking { - val url = "$DOMAIN/create-research" - try { - client.submitForm( - url = url, - formParameters = parameters { - append("id", researchId.toString()) - append("user_id", userId.toString()) - append("name", name) - } - ) - } catch (e: Exception) { - logger.warn("Server interaction error! Url: $url", e) - } - } - } - - companion object { - const val DOMAIN = "http://3.249.245.244:8888" - } } diff --git a/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/template/SurveyTemplate.kt b/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/template/SurveyTemplate.kt index 59f1969e..b21a65fa 100644 --- a/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/template/SurveyTemplate.kt +++ b/ijPlugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/template/SurveyTemplate.kt @@ -2,7 +2,7 @@ package org.jetbrains.research.tasktracker.ui.main.panel.template import org.jetbrains.research.tasktracker.TaskTrackerPlugin import org.jetbrains.research.tasktracker.config.survey.SurveyConfig -import org.jetbrains.research.tasktracker.ui.main.panel.MainPluginPanelFactory +import org.jetbrains.research.tasktracker.requests.FileRequests import org.jetbrains.research.tasktracker.ui.main.panel.storage.MainPanelStorage class SurveyTemplate(val config: SurveyConfig) : HtmlBaseFileTemplate() { @@ -15,7 +15,7 @@ class SurveyTemplate(val config: SurveyConfig) : HtmlBaseFileTemplate() { override val arguments: Array get() = arrayOf(request(), config.toHtml()) - private fun request() = "${MainPluginPanelFactory.DOMAIN}/confirm-survey?id=${MainPanelStorage.currentResearchId}" + private fun request() = "${FileRequests.DOMAIN}/confirm-survey?id=${MainPanelStorage.currentResearchId}" companion object { fun loadCurrentTemplate(): SurveyTemplate { diff --git a/ijPlugin/src/main/resources/model/emotion-ferplus-18.onnx b/ijPlugin/src/main/resources/model/emotion-ferplus-18.onnx deleted file mode 100644 index e69de29b..00000000 diff --git a/ijPlugin/src/main/resources/model/log.txt b/ijPlugin/src/main/resources/model/log.txt deleted file mode 100644 index 8b137891..00000000 --- a/ijPlugin/src/main/resources/model/log.txt +++ /dev/null @@ -1 +0,0 @@ -