Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.itsaky.androidide.ui

import android.content.Context
import androidx.annotation.UiThread
import androidx.collection.IntObjectMap
import androidx.collection.MutableIntIntMap
Expand Down Expand Up @@ -107,7 +108,7 @@ class MemoryUsageChartRenderer(
setDrawCircleHole(false)
setDrawValues(false)
isHighlightEnabled = false
label = labelFor(proc.pname, entries.lastOrNull()?.y ?: 0f)
label = labelFor(chart.context, proc.pname, entries.lastOrNull()?.y ?: 0f)
}
}

Expand Down Expand Up @@ -182,7 +183,7 @@ class MemoryUsageChartRenderer(
dataset.entries[index].y = proc.usageHistory.megabytesAt(index)
}

dataset.label = labelFor(proc.pname, dataset.entries.lastOrNull()?.y ?: 0f)
dataset.label = labelFor(chart.context, proc.pname, dataset.entries.lastOrNull()?.y ?: 0f)
dataset.notifyDataSetChanged()
dataChanged = true
}
Expand Down Expand Up @@ -218,9 +219,10 @@ class MemoryUsageChartRenderer(
}

private fun labelFor(
context: Context,
pname: String,
megabytes: Float,
): String = "%s - %.2fMB".format(pname, megabytes)
): String = context.getString(R.string.metrics_legend_entry, pname, "%.2fMB".format(megabytes))
}

internal const val BYTES_PER_MEGABYTE = 1024.0 * 1024.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ abstract class MetricsChartRenderer(
// chooser -- so one gesture both undocked the strip and cleared every buffer.
onSecondPointerDown = { axisTapListener?.abandonGesture() }

xAxis.valueFormatter = ElapsedTimeFormatter(sampleIntervalMillis)
xAxis.valueFormatter =
ElapsedTimeFormatter(sampleIntervalMillis, context.getString(R.string.metrics_axis_now))
// One label per 15 samples keeps the window readable without crowding.
xAxis.granularity = X_LABEL_GRANULARITY_SAMPLES
xAxis.isGranularityEnabled = true
Expand Down Expand Up @@ -656,14 +657,15 @@ abstract class MetricsChartRenderer(
*/
private class ElapsedTimeFormatter(
private val sampleIntervalMillis: () -> Long,
private val nowLabel: String,
) : IAxisValueFormatter {
override fun getFormattedValue(
value: Float,
axis: AxisBase?,
): String {
val newestIndex = (axis?.mAxisMaximum ?: value)
val secondsAgo = ((newestIndex - value) * sampleIntervalMillis() / 1000f).roundToLong()
return if (secondsAgo <= 0L) "now" else "-%ds".format(secondsAgo)
return if (secondsAgo <= 0L) nowLabel else "-%ds".format(secondsAgo)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.itsaky.androidide.ui

import android.content.Context
import android.graphics.Color
import androidx.annotation.UiThread
import com.github.mikephil.charting.components.AxisBase
Expand All @@ -29,7 +30,6 @@ import com.itsaky.androidide.idetooltips.TooltipTag
import com.itsaky.androidide.utils.MetricsAnnotationStore
import com.itsaky.androidide.utils.NetworkUsageWatcher
import com.itsaky.androidide.utils.NetworkUsageWatcher.NetworkUsage
import java.util.Locale
import kotlin.math.ceil
import kotlin.math.log10
import kotlin.math.max
Expand Down Expand Up @@ -77,8 +77,18 @@ class NetworkUsageChartRenderer(

val datasets =
arrayOf(
dataset(usage.received, chart.context.getString(R.string.metrics_network_received), RECEIVED_COLOR),
dataset(usage.transmitted, chart.context.getString(R.string.metrics_network_transmitted), TRANSMITTED_COLOR),
dataset(
chart.context,
usage.received,
chart.context.getString(R.string.metrics_network_received),
RECEIVED_COLOR,
),
dataset(
chart.context,
usage.transmitted,
chart.context.getString(R.string.metrics_network_transmitted),
TRANSMITTED_COLOR,
),
)

setData(chart, datasets) { applyAxisRange(it, usage) }
Expand Down Expand Up @@ -108,13 +118,19 @@ class NetworkUsageChartRenderer(
return
}

update(received, usage.received, chart.context.getString(R.string.metrics_network_received))
update(transmitted, usage.transmitted, chart.context.getString(R.string.metrics_network_transmitted))
update(chart.context, received, usage.received, chart.context.getString(R.string.metrics_network_received))
update(
chart.context,
transmitted,
usage.transmitted,
chart.context.getString(R.string.metrics_network_transmitted),
)

redraw(chart) { applyAxisRange(it, usage) }
}

private fun dataset(
context: Context,
samples: LongArray,
label: String,
lineColor: Int,
Expand All @@ -134,18 +150,19 @@ class NetworkUsageChartRenderer(
setDrawCircleHole(false)
setDrawValues(false)
isHighlightEnabled = false
this.label = labelFor(label, samples.lastOrNull() ?: 0L)
this.label = labelFor(context, label, samples.lastOrNull() ?: 0L)
}

private fun update(
context: Context,
dataset: LineDataSet,
samples: LongArray,
label: String,
) {
for (index in samples.indices) {
dataset.entries[index].y = samples[index].toLogBytes()
}
dataset.label = labelFor(label, samples.lastOrNull() ?: 0L)
dataset.label = labelFor(context, label, samples.lastOrNull() ?: 0L)
dataset.notifyDataSetChanged()
}

Expand All @@ -158,9 +175,15 @@ class NetworkUsageChartRenderer(
* throughput fivefold, with the axis agreeing.
*/
private fun labelFor(
context: Context,
label: String,
bytes: Long,
): String = "%s - %s/s".format(label, formatBytes(bytesPerSecond(bytes), decimals = 1))
): String =
context.getString(
R.string.metrics_legend_entry,
label,
"%s/s".format(formatBytes(bytesPerSecond(bytes), decimals = 1)),
)

/** A per-interval byte count as a per-second rate. */
private fun bytesPerSecond(bytes: Long): Double = bytes.toDouble() * MILLIS_PER_SECOND / sampleInterval().coerceAtLeast(1L)
Expand Down Expand Up @@ -265,9 +288,9 @@ private fun formatBytes(
): String {
val clamped = bytes.coerceAtLeast(0.0)
return when {
clamped < 1_000 -> "%d B".format(Locale.US, clamped.roundToLong())
clamped < 1_000_000 -> "%.${decimals}f kB".format(Locale.US, clamped / 1_000)
clamped < 1_000_000_000 -> "%.${decimals}f MB".format(Locale.US, clamped / 1_000_000)
else -> "%.${decimals}f GB".format(Locale.US, clamped / 1_000_000_000)
clamped < 1_000 -> "%d B".format(clamped.roundToLong())
clamped < 1_000_000 -> "%.${decimals}f kB".format(clamped / 1_000)
clamped < 1_000_000_000 -> "%.${decimals}f MB".format(clamped / 1_000_000)
else -> "%.${decimals}f GB".format(clamped / 1_000_000_000)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.itsaky.androidide.ui

import android.content.Context
import android.graphics.Color
import androidx.annotation.UiThread
import androidx.core.graphics.ColorUtils
Expand Down Expand Up @@ -82,13 +83,15 @@ class PowerUsageChartRenderer(
val datasets =
arrayOf(
series(
context = context,
values = usage.temperatureMilliCelsius,
label = context.getString(R.string.metrics_power_temperature),
lineColor = TEMPERATURE_COLOR,
axis = YAxis.AxisDependency.LEFT,
transform = ::milliCelsiusToCelsius,
),
series(
context = context,
values = usage.powerMicroWatts,
label = context.getString(R.string.metrics_power_draw),
lineColor = POWER_COLOR,
Expand Down Expand Up @@ -126,13 +129,15 @@ class PowerUsageChartRenderer(

val context = chart.context
update(
context = context,
dataset = temperature,
values = usage.temperatureMilliCelsius,
label = context.getString(R.string.metrics_power_temperature),
axis = YAxis.AxisDependency.LEFT,
transform = ::milliCelsiusToCelsius,
)
update(
context = context,
dataset = power,
values = usage.powerMicroWatts,
label = context.getString(R.string.metrics_power_draw),
Expand All @@ -146,6 +151,7 @@ class PowerUsageChartRenderer(

/** Rewrites one series' values in place and refreshes its legend entry. */
private fun update(
context: Context,
dataset: LineDataSet,
values: LongArray,
label: String,
Expand All @@ -155,7 +161,7 @@ class PowerUsageChartRenderer(
for (index in values.indices) {
dataset.entries[index].y = transform(values[index])
}
dataset.label = labelFor(label, values.lastOrNull(), axis)
dataset.label = labelFor(context, label, values.lastOrNull(), axis)
dataset.notifyDataSetChanged()
}

Expand Down Expand Up @@ -259,6 +265,7 @@ class PowerUsageChartRenderer(
}

private fun series(
context: Context,
values: LongArray,
label: String,
lineColor: Int,
Expand All @@ -276,24 +283,23 @@ class PowerUsageChartRenderer(
setDrawCircleHole(false)
setDrawValues(false)
isHighlightEnabled = false
this.label = labelFor(label, values.lastOrNull(), axis)
this.label = labelFor(context, label, values.lastOrNull(), axis)
}

private fun labelFor(
context: Context,
label: String,
latest: Long?,
axis: YAxis.AxisDependency,
): String {
val value = latest ?: PowerUsageWatcher.UNAVAILABLE
if (value == PowerUsageWatcher.UNAVAILABLE) {
return "%s - n/a".format(label)
}

return if (axis == YAxis.AxisDependency.LEFT) {
"%s - %.1fC".format(label, milliCelsiusToCelsius(value))
} else {
"%s - %s".format(label, formatPower(value))
}
val reading =
when {
value == PowerUsageWatcher.UNAVAILABLE -> context.getString(R.string.metrics_value_unavailable)
axis == YAxis.AxisDependency.LEFT -> "%.1f\u00b0".format(milliCelsiusToCelsius(value))
else -> formatPower(value)
}
return context.getString(R.string.metrics_legend_entry, label, reading)
}

/**
Expand All @@ -318,7 +324,7 @@ class PowerUsageChartRenderer(

// Integer labels need integer grid lines, exactly as the watt axis below does. Now that
// the range is tight -- 29 to 33 rather than 0 to 36 -- the axis would otherwise place
// lines half a degree apart and "%dC" would print 29C, 30C, 30C, 31C, 31C.
// lines half a degree apart and the integer format would print 29, 30, 30, 31, 31.
chart.axisLeft.granularity = 1f
chart.axisLeft.isGranularityEnabled = true

Expand All @@ -327,7 +333,7 @@ class PowerUsageChartRenderer(
override fun getFormattedValue(
value: Float,
axis: AxisBase?,
): String = "%dC".format(value.roundToLong())
): String = "%d\u00b0".format(value.roundToLong())
}

// Watts, not milliwatts: a build peaks in single digit watts, so mW labels spent three
Expand Down
Loading
Loading