Skip to content

Commit

Permalink
refactor:remove dataloader
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Apr 8, 2024
1 parent 5ad5d90 commit 62d498f
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class PostUrlAction(
getUrl(e.dataContext)?.let {
val request = GithubApi.postUrl(text, it, getData(e.dataContext))
val context = e.getRequiredData(ActionKeys.ACTION_DATA_CONTEXT)
val future = context.dataLoader.createDataProvider(request).request
val future = context.createDataProvider(request).request
future.thenApply {
afterPostUrl()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.dsoftware.ghmanager.api.model.Job
import com.dsoftware.ghmanager.api.model.JobStep
import com.dsoftware.ghmanager.i18n.MessagesBundle.message
import com.intellij.openapi.diagnostic.logger
import com.intellij.util.io.HttpSecurityUtil
import kotlinx.datetime.Instant
import org.jetbrains.annotations.VisibleForTesting
import org.jetbrains.plugins.github.api.GithubApiRequest
Expand Down Expand Up @@ -106,7 +107,7 @@ class GetJobLogRequest(private val job: Job) : GithubApiRequest.Get<String>(job.
if (res.length + (stepLogs[stepNumber]?.length ?: 0) < 990_000) {
res.append(stepLogs[stepNumber])
} else {
res.append(message("log.step.truncated", job.htmlUrl+"?#step:$stepNumber"))
res.append(message("log.step.truncated", job.htmlUrl + "?#step:$stepNumber"))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/dsoftware/ghmanager/api/GithubApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data class WorkflowRunFilter(

object GithubApi : GithubApiRequests.Entity("/repos") {
private val LOG = logger<GithubApi>()
fun getJobLog(job: Job) = GetJobLogRequest(job).withOperationName("Get Job log ${job.id}")
fun getLogForSingleJob(job: Job) = GetJobLogRequest(job).withOperationName("Get Job log ${job.id}")

fun postUrl(name: String, url: String, data: Any = Object()) =
GithubApiRequest.Post.Json(url, data, Object::class.java, null).withOperationName(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.dsoftware.ghmanager.data

import com.dsoftware.ghmanager.api.model.WorkflowRunJobs
import com.dsoftware.ghmanager.data.providers.DataProvider
import com.dsoftware.ghmanager.data.providers.WorkflowRunJobsDataProvider
import com.dsoftware.ghmanager.data.providers.JobsDataProvider
import com.intellij.collaboration.ui.SingleValueModel
import com.intellij.openapi.Disposable
import com.intellij.openapi.util.Disposer
Expand All @@ -12,7 +12,7 @@ import org.jetbrains.plugins.github.pullrequest.ui.GHLoadingModel

class JobsLoadingModelListener(
workflowRunDisposable: Disposable,
dataProviderModel: SingleValueModel<WorkflowRunJobsDataProvider?>,
dataProviderModel: SingleValueModel<JobsDataProvider?>,
runSelectionHolder: WorkflowRunListSelectionHolder,
) : GHLoadingModel.StateChangeListener {
val jobsModel = SingleValueModel<WorkflowRunJobs?>(null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.dsoftware.ghmanager.data

import com.dsoftware.ghmanager.data.providers.SingleRunDataLoader
import com.dsoftware.ghmanager.ui.settings.GhActionsSettingsService
import com.intellij.collaboration.async.CompletableFutureUtil.submitIOTask
import com.intellij.collaboration.async.CompletableFutureUtil.successOnEdt
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.CheckedDisposable
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.wm.ToolWindow
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
import com.intellij.util.concurrency.annotations.RequiresEdt
import org.jetbrains.plugins.github.api.GHRepositoryPath
import org.jetbrains.plugins.github.api.GithubApiRequestExecutor
Expand All @@ -23,7 +18,6 @@ import org.jetbrains.plugins.github.exceptions.GithubMissingTokenException
import org.jetbrains.plugins.github.util.GHCompatibilityUtil
import org.jetbrains.plugins.github.util.GHGitRepositoryMapping
import org.jetbrains.plugins.github.util.LazyCancellableBackgroundProcessValue
import java.io.IOException
import java.util.concurrent.CompletableFuture

data class RepositoryCoordinates(val serverPath: GithubServerPath, val repositoryPath: GHRepositoryPath)
Expand Down Expand Up @@ -56,6 +50,9 @@ class WorkflowDataContextService(private val project: Project) {
} else {
settingsService.state.apiToken
}

val requestExecutor =
GithubApiRequestExecutor.Factory.getInstance().create { token }
if (checkedDisposable.isDisposed) {
throw ProcessCanceledException(
RuntimeException("Skipped creating data context for ${repositoryMapping.remote.url} because it was disposed")
Expand All @@ -66,7 +63,7 @@ class WorkflowDataContextService(private val project: Project) {
toolWindow,
account,
repositoryMapping,
token,
requestExecutor,
)
} catch (e: Exception) {
if (e !is ProcessCanceledException)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package com.dsoftware.ghmanager.data

import com.dsoftware.ghmanager.api.WorkflowRunFilter
import com.dsoftware.ghmanager.api.model.Job
import com.dsoftware.ghmanager.api.model.WorkflowRun
import com.dsoftware.ghmanager.data.providers.DataProvider
import com.dsoftware.ghmanager.data.providers.JobsDataProvider
import com.dsoftware.ghmanager.data.providers.LogDataProvider
import com.dsoftware.ghmanager.data.providers.SingleRunDataLoader
import com.dsoftware.ghmanager.data.providers.WorkflowRunJobsDataProvider
import com.dsoftware.ghmanager.ui.ToolbarUtil
import com.dsoftware.ghmanager.ui.panels.wfruns.LoadingErrorHandler
import com.google.common.cache.CacheBuilder
import com.intellij.collaboration.ui.SingleValueModel
import com.intellij.openapi.Disposable
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.util.CheckedDisposable
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.wm.ToolWindow
import com.intellij.util.EventDispatcher
import org.jetbrains.plugins.github.api.GithubApiRequest
import org.jetbrains.plugins.github.api.GithubApiRequestExecutor
import org.jetbrains.plugins.github.api.data.GHUser
import org.jetbrains.plugins.github.authentication.accounts.GithubAccount
import org.jetbrains.plugins.github.util.GHGitRepositoryMapping
import org.jetbrains.plugins.github.util.GithubUrlUtil
import java.util.EventListener
import java.util.concurrent.ScheduledFuture


Expand All @@ -25,28 +31,33 @@ class WorkflowRunSelectionContext internal constructor(
val toolWindow: ToolWindow,
val account: GithubAccount,
val repositoryMapping: GHGitRepositoryMapping,
token: String,
val requestExecutor: GithubApiRequestExecutor,
val runSelectionHolder: WorkflowRunListSelectionHolder = WorkflowRunListSelectionHolder(),
val jobSelectionHolder: JobListSelectionHolder = JobListSelectionHolder(),
) : Disposable.Parent {
private val task: ScheduledFuture<*>
val requestExecutor = GithubApiRequestExecutor.Factory.getInstance().create(token = token)

val currentBranchName: String?
get() = repositoryMapping.gitRepository.currentBranchName
val runsListLoader: WorkflowRunListLoader

private val invalidationEventDispatcher = EventDispatcher.create(DataInvalidatedListener::class.java)

private val cache = CacheBuilder.newBuilder()
.removalListener<String, DataProvider<*>> {}
.maximumSize(200)
.build<String, DataProvider<*>>()

var selectedRunDisposable = Disposer.newDisposable("Selected run disposable")
val jobDataProviderLoadModel: SingleValueModel<WorkflowRunJobsDataProvider?> = SingleValueModel(null)
val jobsDataProvider: WorkflowRunJobsDataProvider?
get() = runSelectionHolder.selection?.let { dataLoader.getJobsDataProvider(it) }
val jobDataProviderLoadModel: SingleValueModel<JobsDataProvider?> = SingleValueModel(null)
val jobsDataProvider: JobsDataProvider?
get() = runSelectionHolder.selection?.let { getJobsDataProvider(it) }

var selectedJobDisposable = Disposer.newDisposable("Selected job disposable")
val logDataProviderLoadModel: SingleValueModel<LogDataProvider?> = SingleValueModel(null)

val dataLoader = SingleRunDataLoader(requestExecutor)
val logDataProvider: LogDataProvider?
get() = jobSelectionHolder.selection?.let { dataLoader.getJobLogDataProvider(it) }
get() = jobSelectionHolder.selection?.let { getLogDataProvider(it) }

val currentBranchName: String?
get() = repositoryMapping.gitRepository.currentBranchName

init {
Disposer.register(parentDisposable, this)
Expand Down Expand Up @@ -86,12 +97,22 @@ class WorkflowRunSelectionContext internal constructor(
selectedJobDisposable.dispose()
selectedJobDisposable = Disposer.newDisposable("Selected job disposable")
}
dataLoader.addInvalidationListener(this) {// When wf-runs are invalidated, invalidate jobs and logs
LOG.debug("invalidation listener")
jobDataProviderLoadModel.value = null
jobDataProviderLoadModel.value = null
selectedRunDisposable.dispose()
}

}


private fun getLogDataProvider(job: Job): LogDataProvider {
return cache.get("${job.url}/logs") { LogDataProvider(requestExecutor, job) } as LogDataProvider
}

private fun getJobsDataProvider(workflowRun: WorkflowRun): JobsDataProvider {
return cache.get(workflowRun.jobsUrl) {
JobsDataProvider(requestExecutor, workflowRun.jobsUrl)
} as JobsDataProvider
}

fun <T> createDataProvider(request: GithubApiRequest<T>): DataProvider<T> {
return DataProvider(requestExecutor, request, null)
}

fun getCurrentAccountGHUser(): GHUser {
Expand All @@ -118,7 +139,9 @@ class WorkflowRunSelectionContext internal constructor(
fun resetAllData() {
runsListLoader.reset()
runsListLoader.loadMore(true)
dataLoader.invalidateAllData()
jobDataProviderLoadModel.value = null
jobDataProviderLoadModel.value = null
selectedRunDisposable.dispose()
}

companion object {
Expand All @@ -139,4 +162,16 @@ class WorkflowRunSelectionContext internal constructor(
runsListLoader.setFilter(filter)
resetAllData()
}


fun addInvalidationListener(disposable: Disposable, listener: () -> Unit) =
invalidationEventDispatcher.addListener(object : DataInvalidatedListener {
override fun dataLoadersInvalidated() {
listener()
}
}, disposable)

private interface DataInvalidatedListener : EventListener {
fun dataLoadersInvalidated()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import java.util.concurrent.CompletableFuture
import kotlin.properties.ReadOnlyProperty

open class DataProvider<T>(
progressManager: ProgressManager,
private val requestExecutor: GithubApiRequestExecutor,
private val githubApiRequest: GithubApiRequest<T>,
private val errorValue: T?,
) {
private val runChangesEventDispatcher = EventDispatcher.create(DataProviderChangeListener::class.java)

private val processValue: LazyCancellableBackgroundProcessValue<T> =
LazyCancellableBackgroundProcessValue.create(progressManager) {
LazyCancellableBackgroundProcessValue.create(ProgressManager.getInstance()) {
try {
LOG.info("Executing ${githubApiRequest.url}")
val request = githubApiRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package com.dsoftware.ghmanager.data.providers

import com.dsoftware.ghmanager.api.GithubApi
import com.dsoftware.ghmanager.api.model.WorkflowRunJobs
import com.intellij.openapi.progress.ProgressManager
import org.jetbrains.plugins.github.api.GithubApiRequestExecutor

class WorkflowRunJobsDataProvider(
progressManager: ProgressManager,
class JobsDataProvider(
requestExecutor: GithubApiRequestExecutor,
jobsUrl: String
) : DataProvider<WorkflowRunJobs>(
progressManager,
requestExecutor,
GithubApi.getWorkflowRunJobs(jobsUrl),
WorkflowRunJobs(0, emptyList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package com.dsoftware.ghmanager.data.providers

import com.dsoftware.ghmanager.api.GithubApi
import com.dsoftware.ghmanager.api.model.Job
import com.intellij.openapi.progress.ProgressManager
import org.jetbrains.plugins.github.api.GithubApiRequestExecutor

class LogDataProvider(
progressManager: ProgressManager,
requestExecutor: GithubApiRequestExecutor,
job: Job
) : DataProvider<String>(
progressManager,
requestExecutor,
GithubApi.getJobLog(job),
GithubApi.getLogForSingleJob(job),
null
)

This file was deleted.

0 comments on commit 62d498f

Please sign in to comment.