Fix wrong month numeration
[idea/community.git] / plugins / stats-collector / src / com / intellij / performance / IntervalCounter.kt
1 /*
2  * Copyright 2000-2017 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.intellij.performance
18
19 data class IntervalData(val intervalStart: Double, val intervalEnd: Double, val count: Int)
20
21 class IntervalCounter(
22         private val minPower: Int,
23         private val maxPower: Int,
24         private val exponent: Double,
25         val data: Array<Int> = Array(maxPower - minPower, { 0 })
26 ) {
27
28     fun register(value: Long) {
29         val log = roundedLog(value)
30         val bucket = Math.min(maxPower - 1, Math.max(minPower, log.toInt())) - minPower
31         data[bucket] += 1
32     }
33
34     fun intervals(): List<IntervalData> {
35         return data.indices.map { interval(it) }
36     }
37
38     private fun interval(index: Int): IntervalData {
39         val start = Math.pow(exponent, minPower + index.toDouble())
40         val end = Math.pow(exponent, minPower + (index + 1).toDouble())
41
42         val count = data[index]
43
44         return IntervalData(start, end, count)
45     }
46
47     private fun roundedLog(time: Long): Double {
48         val dTime = time.toDouble()
49         return (Math.log(dTime) / Math.log(exponent))
50     }
51
52 }