Analytics Incomplete Image Fix

This commit is contained in:
shabinder 2021-05-20 15:53:08 +05:30
parent 821f07a5d6
commit 0269838669
10 changed files with 55 additions and 11 deletions

View File

@ -18,6 +18,7 @@ application {
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:${Versions.kotlinVersion}")
implementation("io.ktor:ktor-client-core:1.5.4")
implementation("io.ktor:ktor-client-apache:1.5.4")
implementation("io.ktor:ktor-client-serialization:1.5.4")

View File

@ -3,6 +3,7 @@
package analytics_html_img
import io.ktor.client.HttpClient
import io.ktor.client.features.HttpTimeout
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.json.serializer.KotlinxSerializer
import kotlinx.serialization.json.Json
@ -14,6 +15,7 @@ internal object Common {
const val USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0"
}
internal val client = HttpClient {
install(HttpTimeout)
install(JsonFeature) {
serializer = KotlinxSerializer(
Json {

View File

@ -1,6 +1,10 @@
package analytics_html_img
import io.ktor.client.features.timeout
import io.ktor.client.request.head
import io.ktor.client.statement.HttpResponse
import kotlinx.coroutines.runBlocking
import utils.RETRY_LIMIT_EXHAUSTED
import utils.debug
internal fun updateAnalyticsImage() {
@ -16,17 +20,9 @@ internal fun updateAnalyticsImage() {
fileName = "README.md"
)
// debug("OLD FILE CONTENT",oldGithubFile)
/*
* Use Any Random useless query param ,
* As HCTI Demo, `caches value for a specific Link`
* */
val randomID = (1..100000).random()
val imageURL = HCTIService.getImageURLFromURL(
url = "https://kind-grasshopper-73.telebit.io/matomo/index.php?module=Widgetize&action=iframe&containerId=VisitOverviewWithGraph&disableLink=0&widget=1&moduleToWidgetize=CoreHome&actionToWidgetize=renderWidgetContainer&idSite=1&period=week&date=today&disableLink=1&widget=$randomID",
delayInMilliSeconds = 5000
)
debug("Updated IMAGE", imageURL)
val imageURL = getAnalyticsImage().also {
debug("Updated IMAGE", it)
}
val replacementText = """
${Common.START_SECTION(secrets.tagName)}
@ -56,3 +52,37 @@ internal fun updateAnalyticsImage() {
debug("File Updation Response", updationResponse.toString())
}
}
internal suspend fun getAnalyticsImage(): String {
var contentLength: Long
var analyticsImage: String
var retryCount = 5
do {
/*
* Get a new Image from Analytics,
* - Use Any Random useless query param ,
* As HCTI Demo, `caches value for a specific Link`
* */
val randomID = (1..100000).random()
analyticsImage = HCTIService.getImageURLFromURL(
url = "https://kind-grasshopper-73.telebit.io/matomo/index.php?module=Widgetize&action=iframe&containerId=VisitOverviewWithGraph&disableLink=0&widget=1&moduleToWidgetize=CoreHome&actionToWidgetize=renderWidgetContainer&idSite=1&period=week&date=today&disableLink=1&widget=$randomID",
delayInMilliSeconds = 5000
)
// Sometimes we get incomplete image, hence verify `content-length`
val req = client.head<HttpResponse>(analyticsImage) {
timeout {
socketTimeoutMillis = 100_000
}
}
contentLength = req.headers["Content-Length"]?.toLong() ?: 0
debug(contentLength.toString())
if(retryCount-- == 0){
// FAIL Gracefully
throw(RETRY_LIMIT_EXHAUSTED())
}
}while (contentLength<1_20_000)
return analyticsImage
}

View File

@ -6,7 +6,12 @@ data class ENV_KEY_MISSING(
val keyName: String,
override val message: String? = "$keyName was not found, please check your ENV variables"
) : Exception(message)
data class HCTI_URL_RESPONSE_ERROR(
val response: String,
override val message: String? = "Server Error, We Recieved this Resp: $response"
) : Exception(message)
data class RETRY_LIMIT_EXHAUSTED(
override val message: String? = "RETRY LIMIT EXHAUSTED!"
) : Exception(message)

View File

@ -0,0 +1,6 @@
package utils
import kotlinx.coroutines.runBlocking
// Test Class- at development Time
fun main() = runBlocking {}