This commit is contained in:
kehaoyuan
2020-08-28 18:44:21 +08:00
parent 727d78d0e5
commit a4ca2628dc
2 changed files with 72 additions and 1 deletions

View File

@ -4,7 +4,9 @@ import com.gh.common.exposure.ExposureManager
import com.gh.common.filter.RegionSettingHelper
import com.gh.common.loghub.LoghubUtils
import com.gh.common.util.doOnMainProcessOnly
import com.gh.common.util.tryCatchInRelease
import com.gh.common.videolog.VideoRecordUtils
import com.gh.download.DownloadDataHelper
import com.gh.gamecenter.entity.TimeEntity
import com.gh.gamecenter.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
@ -12,13 +14,16 @@ import com.halo.assistant.HaloApp
import kotlin.concurrent.fixedRateTimer
object FixedRateJobHelper {
private const val CHECKER_PERIOD: Long = 60 * 1000L
private const val CHECKER_PERIOD: Long = 15 * 1000L
private const val TIME_PERIOD: Long = 600 * 1000L
private const val LOGHUB_PERIOD: Long = 120 * 1000L
private const val EXPOSURE_PERIOD: Long = 300 * 1000L
private const val REGION_SETTING_PERIOD: Long = 300 * 1000L
private const val VIDEO_RECORD_PERIOD: Long = 60 * 1000L
private const val DOWNLOAD_HEARTBEAT_PERIOD: Long = 60 * 1000L
private const val DOWNLOAD_HEARTBEAT_SHEET_PERIOD: Long = 15 * 1000L
private var mExecuteCount: Int = 0
var timeDeltaBetweenServerAndClient: Long = 0
@ -44,6 +49,14 @@ object FixedRateJobHelper {
ExposureManager.commitSavedExposureEvents(true)
}
// 分片检测下载进度
if ((mExecuteCount * CHECKER_PERIOD) % DOWNLOAD_HEARTBEAT_SHEET_PERIOD == 0L) {
tryCatchInRelease {
val upload = (mExecuteCount * CHECKER_PERIOD) % DOWNLOAD_HEARTBEAT_PERIOD == 0L
DownloadDataHelper.uploadDownloadHeartbeat(upload)
}
}
// 提交普通 loghub 数据
if ((mExecuteCount * CHECKER_PERIOD) % LOGHUB_PERIOD == 0L) {
LoghubUtils.commitSavedLoghubEvents()

View File

@ -28,6 +28,9 @@ object DownloadDataHelper {
private val mDownloadSpeedMap = HashMap<String, MutableList<Long>>()
private val mDownloadHeartbeatList = mutableListOf<JSONObject>()
private val mDownloadHeartbeatSheet = HashMap<String, JSONObject>()
@JvmStatic
fun getDownloadStatusAlias(downloadEntity: DownloadEntity, downloadStatus: DownloadStatus? = null): String {
val status = downloadStatus ?: downloadEntity.status
@ -221,6 +224,61 @@ object DownloadDataHelper {
LoghubUtils.log(jsonObject, "download_debug", false)
}
/**
* 分片检测下载进度,每隔15秒内记录一次,60秒上传一次
*
* 请见:https://gitlab.ghzs.com/stats/stats-issues/-/issues/188#note_66919
*/
fun uploadDownloadHeartbeat(upload: Boolean) {
val allDownloadEntity = DownloadManager.getInstance(HaloApp.getInstance().application).allDownloadEntity
for (downloadEntity in allDownloadEntity) {
// 刚开始下载时 size 有可能为空,但是过滤的话就少了一条数据
if (downloadEntity.status == DownloadStatus.downloading && downloadEntity.size > 0) {
var sheet = mDownloadHeartbeatSheet[downloadEntity.url]
if (sheet == null) {
sheet = JSONObject()
sheet.put("game_id", downloadEntity.gameId)
sheet.put("platform", downloadEntity.platform)
sheet.put("package", downloadEntity.packageName)
sheet.put("filename", downloadEntity.path.substring(downloadEntity.path.lastIndexOf("/") + 1))
sheet.put("total_size", downloadEntity.size / 1024 / 1024)
sheet.put("current_progress_size", downloadEntity.progress / 1024)
mDownloadHeartbeatSheet[downloadEntity.url] = sheet
} else {
val progressSize = sheet.getLong("current_progress_size")
sheet.put("progress_size", downloadEntity.progress / 1024 - progressSize)
sheet.put("current_progress_size", downloadEntity.progress / 1024)
mDownloadHeartbeatList.add(JSONObject(sheet.toString()))
}
} else {
mDownloadHeartbeatSheet.remove(downloadEntity.url)
}
}
if (upload && mDownloadHeartbeatList.isNotEmpty()) {
val jsonObject = JSONObject()
try {
jsonObject.put("event", "progress")
jsonObject.put("meta", getMetaJson())
jsonObject.put("timestamp", System.currentTimeMillis() / 1000)
val payloads = JSONArray()
for (heartbeatData in mDownloadHeartbeatList) {
heartbeatData.remove("current_progress_size")
payloads.put(heartbeatData)
}
jsonObject.put("payloads", payloads)
} catch (e: Exception) {
e.printStackTrace()
}
if (BuildConfig.DEBUG) {
Utils.log("LogUtils->$jsonObject")
}
mDownloadHeartbeatList.clear()
LoghubUtils.log(jsonObject, "download_debug", false)
}
}
private fun getMetaJson(): JSONObject {
val context = HaloApp.getInstance().application
val meta = getMeta()