Commit e1732296 authored by Vitaliy.Bibaev's avatar Vitaliy.Bibaev
Browse files

[stats-collector] Add daily limit for data reporting 15 mb

parent b1baf37c
Showing with 117 additions and 4 deletions
+117 -4
......@@ -40,7 +40,7 @@ class SimpleRequestService: RequestService() {
it.setRequestProperty(HttpHeaders.CONTENT_ENCODING, "gzip")
}.connect { request ->
request.write(zippedArray)
return@connect request.connection.asResponseData()
return@connect request.connection.asResponseData(zippedArray.size)
}
}
catch (e: HttpRequests.HttpStatusException) {
......@@ -71,9 +71,9 @@ class SimpleRequestService: RequestService() {
}
}
private fun URLConnection.asResponseData(): ResponseData? {
private fun URLConnection.asResponseData(sentDataSize: Int?): ResponseData? {
if (this is HttpURLConnection) {
return ResponseData(this.responseCode, StringUtil.notNullize(this.responseMessage, ""))
return ResponseData(this.responseCode, StringUtil.notNullize(this.responseMessage, ""), sentDataSize)
}
LOG.error("Could not get code and message from http response")
......
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.stats.sender
import com.intellij.ide.util.PropertiesComponent
import java.text.SimpleDateFormat
import java.util.*
class DailyLimitSendingWatcher(private val dailyLimit: Int, private val info: SentDataInfo) {
fun isLimitReached(): Boolean = info.sentToday(System.currentTimeMillis()) >= dailyLimit
fun dataSent(size: Int) {
info.dataSent(System.currentTimeMillis(), size)
}
abstract class SentDataInfo {
companion object {
private val FORMAT = SimpleDateFormat("yyyyMMdd")
}
fun sentToday(timestamp: Long): Int {
if (!isSameDay(timestamp, latestSendingTimestamp)) {
return 0
}
return sentCount
}
fun dataSent(timestamp: Long, bytesCount: Int) {
sentCount = sentToday(timestamp) + bytesCount
latestSendingTimestamp = timestamp
}
private fun isSameDay(ts1: Long, ts2: Long): Boolean {
fun Long.asString(): String = FORMAT.format(Date(this))
return ts1.asString() == ts2.asString()
}
protected abstract var sentCount: Int
protected abstract var latestSendingTimestamp: Long
class DumbInfo : SentDataInfo() {
override var sentCount: Int = 0
override var latestSendingTimestamp: Long = 0
}
}
}
\ No newline at end of file
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.stats.sender
import com.intellij.ide.util.PropertiesComponent
class PersistentSentDataInfo(private val properties: PropertiesComponent) : DailyLimitSendingWatcher.SentDataInfo() {
companion object {
private const val SENT_DATA_SIZE_TODAY_PROPERTY = "stats.collector.today.sent.data.size"
private const val DATA_SENT_TIMESTAMP_PROPERTY = "stats.collector.latest.sending.timestamp"
}
override var sentCount: Int
get() = properties.getInt(SENT_DATA_SIZE_TODAY_PROPERTY, 0)
set(value) = properties.setValue(SENT_DATA_SIZE_TODAY_PROPERTY, value, 0)
override var latestSendingTimestamp: Long
get() = properties.getOrInitLong(DATA_SENT_TIMESTAMP_PROPERTY, 0L)
set(value) = properties.setValue(DATA_SENT_TIMESTAMP_PROPERTY, value.toString())
}
\ No newline at end of file
......@@ -16,6 +16,7 @@
package com.intellij.stats.sender
import com.intellij.ide.util.PropertiesComponent
import com.intellij.stats.network.service.RequestService
import com.intellij.stats.network.assertNotEDT
import com.intellij.stats.storage.FilePathProvider
......@@ -25,12 +26,18 @@ class StatisticSenderImpl(
private val requestService: RequestService,
private val filePathProvider: FilePathProvider
): StatisticSender {
companion object {
const val DAILY_LIMIT = 15 * 1024 * 1024 // 15 mb
}
private val limitWatcher = DailyLimitSendingWatcher(DAILY_LIMIT, PersistentSentDataInfo(PropertiesComponent.getInstance()))
override fun sendStatsData(url: String) {
assertNotEDT()
if (limitWatcher.isLimitReached()) return
val filesToSend = filePathProvider.getDataFiles()
filesToSend.forEach {
if (it.length() > 0) {
if (it.length() > 0 && !limitWatcher.isLimitReached()) {
val isSentSuccessfully = sendContent(url, it)
if (isSentSuccessfully) {
it.delete()
......@@ -45,6 +52,9 @@ class StatisticSenderImpl(
private fun sendContent(url: String, file: File): Boolean {
val data = requestService.postZipped(url, file)
if (data != null && data.code >= 200 && data.code < 300) {
if (data.sentDataSize != null) {
limitWatcher.dataSent(data.sentDataSize)
}
return true
}
return false
......
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package sender
import com.intellij.stats.sender.DailyLimitSendingWatcher
import com.intellij.testFramework.UsefulTestCase
import junit.framework.TestCase
class DailySendingLimitTest : UsefulTestCase() {
fun testLimitInfo() {
val info = DailyLimitSendingWatcher.SentDataInfo.DumbInfo()
TestCase.assertEquals(0, info.sentToday(10))
info.dataSent(10, 100)
TestCase.assertEquals(100, info.sentToday(10))
info.dataSent(10, 1)
TestCase.assertEquals(101, info.sentToday(10))
val nextDay: Long = 10 + 24 * 60 * 60 * 1000
TestCase.assertEquals(0, info.sentToday(nextDay))
info.dataSent(nextDay, 1)
TestCase.assertEquals(1, info.sentToday(nextDay))
}
fun testSendingWatcher() {
val watcher = DailyLimitSendingWatcher(2500, DailyLimitSendingWatcher.SentDataInfo.DumbInfo())
TestCase.assertFalse(watcher.isLimitReached())
watcher.dataSent(2300)
TestCase.assertFalse(watcher.isLimitReached())
watcher.dataSent(500)
if (!watcher.isLimitReached()) {
println("is it really 12 o'clock?!")
watcher.dataSent(2300)
watcher.dataSent(500)
}
TestCase.assertTrue(watcher.isLimitReached())
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment