Skip to content

Commit

Permalink
send current version to versions api
Browse files Browse the repository at this point in the history
  • Loading branch information
shalom938 committed Dec 17, 2024
1 parent dfb33c0 commit 95d47a2
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ object InternalFileSettings {
return getProperty("UpdateService.monitor.delay.seconds")?.toInt() ?: defaultValue
}

fun getUIVersioningServiceMonitorDelaySeconds(defaultValue: Int): Int {
return getProperty("UIVersioningService.monitor.delay.seconds")?.toInt() ?: defaultValue
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.digma.intellij.plugin.updates

import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
Expand All @@ -23,6 +24,7 @@ import org.digma.intellij.plugin.scheduling.disposingPeriodicTask
import org.digma.intellij.plugin.scheduling.oneShotTask
import org.digma.intellij.plugin.settings.InternalFileSettings
import org.digma.intellij.plugin.ui.panels.DigmaResettablePanel
import org.digma.intellij.plugin.updates.ui.NewUIVersionAvailableEvent
import org.digma.intellij.plugin.updates.ui.UIVersioningService
import java.util.concurrent.TimeUnit
import kotlin.time.Duration
Expand Down Expand Up @@ -101,7 +103,7 @@ class UpdatesService(private val project: Project) : Disposable {



project.messageBus.connect().subscribe(
project.messageBus.connect(this).subscribe(
AggressiveUpdateStateChangedEvent.UPDATE_STATE_CHANGED_TOPIC, object : AggressiveUpdateStateChangedEvent {
override fun stateChanged(updateState: PublicUpdateState) {
oneShotTask("UpdatesService.aggressiveUpdateStateChanged") {
Expand All @@ -123,6 +125,15 @@ class UpdatesService(private val project: Project) : Disposable {
}
})

//make sure to update the state and panel as soon as possible after a new ui version is available
ApplicationManager.getApplication().messageBus.connect(this).subscribe(
NewUIVersionAvailableEvent.NEW_UI_VERSION_AVAILABLE_EVENT_TOPIC,object: NewUIVersionAvailableEvent{
override fun newUIVersionAvailable() {
checkForNewerVersions()
}
}
)

}

override fun dispose() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.digma.intellij.plugin.updates.ui

import com.intellij.util.messages.Topic

interface NewUIVersionAvailableEvent {

companion object {
@JvmField
@Topic.AppLevel
val NEW_UI_VERSION_AVAILABLE_EVENT_TOPIC: Topic<NewUIVersionAvailableEvent> =
Topic.create("NEW_UI_VERSION_AVAILABLE_EVENT_TOPIC", NewUIVersionAvailableEvent::class.java)
}


fun newUIVersionAvailable()

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.digma.intellij.plugin.updates.ui

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
Expand All @@ -19,14 +20,17 @@ import org.digma.intellij.plugin.posthog.ActivityMonitor
import org.digma.intellij.plugin.reload.ReloadService
import org.digma.intellij.plugin.reload.ReloadSource
import org.digma.intellij.plugin.scheduling.disposingPeriodicTask
import org.digma.intellij.plugin.settings.InternalFileSettings
import java.io.File
import java.net.HttpURLConnection
import java.net.URI
import java.net.URL
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import kotlin.io.path.deleteIfExists
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

@Service(Service.Level.APP)
class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
Expand Down Expand Up @@ -67,7 +71,9 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
}
}


fun getDefaultDelayBetweenUpdatesSeconds(): Duration {
return InternalFileSettings.getUIVersioningServiceMonitorDelaySeconds(300).seconds
}
}


Expand All @@ -81,7 +87,9 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
}

fun isNewUIBundleAvailable(): Boolean {
return getLatestDownloadedVersion() != null
return getLatestDownloadedVersion()?.let {
buildUiBundleLocalFile(it).exists()
} ?: false
}

fun getCurrentUiBundlePath(): String {
Expand All @@ -90,7 +98,7 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {


fun getUiVersionForVersionRequest(): String {
return getLatestDownloadedVersion() ?: getCurrentUiVersion()
return getCurrentUiVersion()
}

fun getCurrentUiVersion(): String {
Expand Down Expand Up @@ -220,9 +228,12 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {


private fun startMonitoring() {
disposingPeriodicTask("UIVersioningService.periodic", 1.minutes.inWholeMilliseconds, 5.minutes.inWholeMilliseconds, true) {

val delayMillis = getDefaultDelayBetweenUpdatesSeconds().inWholeMilliseconds
disposingPeriodicTask("UIVersioningService.periodic", 1.minutes.inWholeMilliseconds, delayMillis, true) {
findActiveProject()?.let { project ->
try {

val versionsResponse = AnalyticsService.getInstance(project).getVersions(buildVersionRequest())

Log.log(logger::trace, "got version response {}", versionsResponse)
Expand All @@ -232,7 +243,7 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
val currentUiVersion = getCurrentUiVersion()
if (!requiredUiVersion.isNullOrBlank() && ComparableVersion(requiredUiVersion).newerThan(ComparableVersion(currentUiVersion))) {
Log.log(logger::info, "got ui force update to {}", requiredUiVersion)
//if there is already a latest downloaded version delete it before downloading the new one
//if there is already a latest downloaded version, delete it before downloading the new one
// and reset the property
getLatestDownloadedVersion()?.let {
deleteUiBundle(it)
Expand All @@ -247,14 +258,33 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
val currentUiVersion = getCurrentUiVersion()
if (!requiredUiVersion.isNullOrBlank() && ComparableVersion(requiredUiVersion).newerThan(ComparableVersion(currentUiVersion))) {
Log.log(logger::info, "got ui update to {}", requiredUiVersion)
//if there is already a latest downloaded version delete it before downloading the new one
// and reset the property
getLatestDownloadedVersion()?.let {
deleteUiBundle(it)
setLatestDownloadedVersion(null)
}
if (downloadUiBundle(requiredUiVersion)) {
setLatestDownloadedVersion(requiredUiVersion)

//if LatestDownloadedVersion equals requiredUiVersion then it was already downloaded in the previous round,
// so no need to download it again.
if (getLatestDownloadedVersion() != requiredUiVersion) {
Log.log(
logger::info,
"requiredUiVersion {} is different from latest downloaded {}, downloading.. ",
requiredUiVersion,
getLatestDownloadedVersion()
)
//if there is already a latest downloaded version, delete it before downloading the new one
// and reset the property
getLatestDownloadedVersion()?.let {
deleteUiBundle(it)
setLatestDownloadedVersion(null)
}
if (downloadUiBundle(requiredUiVersion)) {
setLatestDownloadedVersion(requiredUiVersion)
fireNewUIVersionAvailable()
}
} else {
Log.log(
logger::info,
"requiredUiVersion {} is the same as latest downloaded {}, no need to download again ",
requiredUiVersion,
getLatestDownloadedVersion()
)
}
}
}
Expand All @@ -267,6 +297,10 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
}
}

private fun fireNewUIVersionAvailable() {
ApplicationManager.getApplication().messageBus.syncPublisher(NewUIVersionAvailableEvent.NEW_UI_VERSION_AVAILABLE_EVENT_TOPIC).newUIVersionAvailable()
}


fun updateToLatestDownloaded() {
val latestDownloadedUiVersion = getLatestDownloadedVersion()
Expand All @@ -277,8 +311,16 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
updateToDownloadedVersion(latestDownloadedUiVersion)
setLatestDownloadedVersion(null)
} else {
//something is wrong, we have the property latestDownloadedVersion but there is no file, maybe it was deleted.
//something is wrong, we have the property latestDownloadedVersion, but there is no file, maybe it was deleted.
//reset latestDownloadedVersion
ErrorReporter.getInstance().reportError(
"UIVersioningService.updateToLatestDownloaded",
"updateToLatestDownloaded called but ui bundle file does not exist", mapOf(
"current ui version" to getCurrentUiVersion(),
"latest downloaded version" to getLatestDownloadedVersion().toString(),
"bundled version" to bundledUiVersion,
)
)
Log.log(
logger::warn,
"latest downloaded version property exists but file does not exist, not updating"
Expand Down
3 changes: 2 additions & 1 deletion ide-common/src/main/resources/digma-settings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ AggressiveUpdateService.notification.delay.minutes=1440



UpdateService.monitor.delay.seconds=300
UpdateService.monitor.delay.seconds=300
UIVersioningService.monitor.delay.seconds=300
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ class UpdateVersionPanel(

updateTextProperty.set(buildText(updateState))
if (updateState.shouldUpdateAny()) {
isVisible = true
ActivityMonitor.getInstance(project).registerCustomEvent(
"update button shown",
mapOf(
"shouldUpdateBackend" to updateState.shouldUpdateBackend,
"shouldUpdatePlugin" to updateState.shouldUpdatePlugin,
"backendDeploymentType" to updateState.backendDeploymentType
if (!isVisible) {
isVisible = true
ActivityMonitor.getInstance(project).registerCustomEvent(
"update button shown",
mapOf(
"shouldUpdateBackend" to updateState.shouldUpdateBackend,
"shouldUpdatePlugin" to updateState.shouldUpdatePlugin,
"backendDeploymentType" to updateState.backendDeploymentType
)
)
)
}
}else{
isVisible = false
}
Expand Down

0 comments on commit 95d47a2

Please sign in to comment.