• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Unleash / unleash-android / 10077204014

24 Jul 2024 01:04PM UTC coverage: 78.922% (+2.4%) from 76.546%
10077204014

Pull #66

github

web-flow
Merge 6f0bba963 into 812f882d3
Pull Request #66: chore: several test cases in 3 commits

197 of 280 branches covered (70.36%)

Branch coverage included in aggregate %.

6 of 6 new or added lines in 2 files covered. (100.0%)

6 existing lines in 1 file now uncovered.

608 of 740 relevant lines covered (82.16%)

5.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.72
/unleashandroidsdk/src/main/java/io/getunleash/android/metrics/MetricsSender.kt
1
package io.getunleash.android.metrics
2

3
import android.util.Log
4
import io.getunleash.android.UnleashConfig
5
import io.getunleash.android.data.Parser
6
import io.getunleash.android.data.Variant
7
import io.getunleash.android.http.Throttler
8
import okhttp3.Call
9
import okhttp3.Callback
10
import okhttp3.Headers.Companion.toHeaders
11
import okhttp3.HttpUrl.Companion.toHttpUrl
12
import okhttp3.MediaType.Companion.toMediaType
13
import okhttp3.OkHttpClient
14
import okhttp3.Request
15
import okhttp3.RequestBody.Companion.toRequestBody
16
import okhttp3.Response
17
import java.io.IOException
18
import java.util.Date
19
import java.util.concurrent.TimeUnit
20

21
class MetricsSender(
11✔
22
    private val config: UnleashConfig,
3✔
23
    private val httpClient: OkHttpClient,
3✔
24
    private val applicationHeaders: Map<String, String> = config.getApplicationHeaders(config.metricsStrategy)
8✔
25
): MetricsHandler {
26
    companion object {
27
        private const val TAG: String = "MetricsSender"
28
    }
29
    private val metricsUrl = config.proxyUrl.toHttpUrl().newBuilder().addPathSegment("client")
8✔
30
        .addPathSegment("metrics").build()
5✔
31
    private var bucket: CountBucket = CountBucket(start = Date())
13✔
32
    private val throttler =
33
        Throttler(
5✔
34
            TimeUnit.MILLISECONDS.toSeconds(config.metricsStrategy.interval),
6✔
35
            longestAcceptableIntervalSeconds = 300,
1✔
36
            metricsUrl.toString()
3✔
37
        )
38

39
    override suspend fun sendMetrics() {
40
        if (bucket.isEmpty()) {
4✔
41
            Log.d(TAG, "No metrics to report")
4✔
42
            return
2✔
43
        }
44
        if (throttler.performAction()) {
4!
45
            val toReport = swapAndFreeze()
3✔
46
            val payload = MetricsPayload(
4✔
47
                appName = config.appName,
3✔
48
                instanceId = config.instanceId,
3✔
49
                bucket = toReport
1✔
50
            )
51
            val request = Request.Builder()
4✔
52
                .headers(applicationHeaders.toHeaders())
5✔
53
                .url(metricsUrl).post(
4✔
54
                    Parser.jackson.writeValueAsString(payload)
7✔
55
                        .toRequestBody("application/json".toMediaType())
5✔
56
                ).build()
1✔
57
            httpClient.newCall(request).enqueue(object : Callback {
15✔
58
                override fun onFailure(call: Call, e: IOException) {
59
                    Log.i(TAG, "Failed to report metrics for interval", e)
×
60
                }
×
61

62
                override fun onResponse(call: Call, response: Response) {
63
                    Log.d(
×
64
                        TAG,
×
65
                        "Received status code ${response.code} from ${request.method} $metricsUrl"
×
66
                    )
67
                    throttler.handle(response.code)
×
68
                    response.body.use { // Need to consume body to ensure we don't keep connection open
×
69
                    }
70
                }
×
71
            })
72
        } else {
73
            throttler.skipped()
×
74
        }
75
    }
2✔
76

77
    private fun swapAndFreeze(): Bucket {
78
        val bucketRef = bucket
3✔
79
        bucket = CountBucket(start = Date())
13✔
80
        return bucketRef.copy().toBucket(bucket.start)
13✔
81
    }
82

83
    override fun count(featureName: String, enabled: Boolean): Boolean {
84
        return bucket.count(featureName, enabled)
6✔
85
    }
86

87
    override fun countVariant(featureName: String, variant: Variant): Variant {
88
        return bucket.countVariant(featureName, variant)
6✔
89
    }
90
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc