๐Ÿ“ฆ manideepk90 / react-native-hyperswitch-sdk

๐Ÿ“„ HyperLogManager.kt ยท 105 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105package io.hyperswitch.logs

import io.hyperswitch.networking.HyperNetworking
import org.json.JSONArray

object HyperLogManager {

    private val logsBatch = mutableListOf<HSLog>()
    private var publishableKey: String? = null
    private var loggingEndPoint: String? = null
    private const val DEFAULT_DELAY_IN_MILLIS = 2000L
    private var delayInMillis: Long = DEFAULT_DELAY_IN_MILLIS
    private var hyperOtaVersion: String = ""
    private val debouncer = Debouncer(DEFAULT_DELAY_IN_MILLIS)

    fun initialise(
        publishableKey: String,
        loggingEndPoint: String,
        delay: Long = DEFAULT_DELAY_IN_MILLIS
    ) {
        delayInMillis = delay
        this.publishableKey = publishableKey
        this.loggingEndPoint = loggingEndPoint
    }

    fun addLog(log: HSLog) {
        logsBatch.add(log)
        debouncedPushLogs()
    }

    fun sendLogsFromFile(fileManager: LogFileManager) {
        if (publishableKey.isNullOrBlank()) return
        try {
            val logArray = fileManager.getAllLogs()
            if (logArray.length() > 0) {
                enrichLogs(logArray)
                loggingEndPoint?.let { endpoint ->
                    try {
                        HyperNetworking.makePostRequest(
                            endpoint,
                            logArray.toString(),
                            callback = { result ->
                                result.onSuccess {
                                    fileManager.clearFile()
                                }
                            })
                    } catch (_: Exception) {
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun setOtaVersion(version: String) {
        hyperOtaVersion = version
        debouncedPushLogs()
    }

    private fun debouncedPushLogs() {
        debouncer.debounce { sendLogsOverNetwork() }
    }

    private fun enrichLogs(logsArray: JSONArray) {
        for (i in 0 until logsArray.length()) {
            logsArray.getJSONObject(i).apply {
                put("merchant_id", publishableKey)
                put("code_push_version", hyperOtaVersion)
                put("client_core_version", hyperOtaVersion)
            }
        }
    }

    fun getAllLogsAsString(): String = logsBatch.joinToString(prefix = "[", postfix = "]") { it.toJson() }

    private fun sendLogsOverNetwork() {
        if (logsBatch.isEmpty()){
            return
        }
        if (!publishableKey.isNullOrBlank() && !loggingEndPoint.isNullOrBlank()) {
            logsBatch.map { log ->
                log.apply {
                    merchantId = publishableKey!!
                    codePushVersion = hyperOtaVersion
                    clientCoreVersion = hyperOtaVersion
                }
            }
            val logsToSend = getAllLogsAsString()
            logsBatch.clear()
            loggingEndPoint?.let { endpoint ->
                try {
                    HyperNetworking.makePostRequest(
                        endpoint, logsToSend,
                        callback = {}
                    )
                } catch (_: Exception) {
                }
            }
        } else {
            debouncedPushLogs()
        }
    }
}