<commit_before>package com.github.kerubistan.kerub.utils.junix.procfs

import com.github.kerubistan.kerub.model.HostCapabilities
import com.github.kerubistan.kerub.model.OperatingSystem
import com.github.kerubistan.kerub.utils.junix.common.OsCommand
import com.github.kerubistan.kerub.utils.substringBetween
import com.github.kerubistan.kerub.utils.toSize
import org.apache.sshd.client.session.ClientSession
import java.math.BigInteger

object CpuInfo : OsCommand {

	override fun available(hostCapabilities: HostCapabilities?): Boolean =
			hostCapabilities?.os == OperatingSystem.Linux

	internal fun value(properties: String, property: String) =
			properties.substringBetween(property, "\n").substringAfter(":").trim()

	fun listPpc(session: ClientSession): List<CpuInfoRecord> =
			session.createSftpClient().use {
				sftp ->
				sftp.read("/proc/cpuinfo").reader(Charsets.US_ASCII).use {
					reader ->
					val text = reader.readText()
					val sections = text.split("\n\n").filter { it.isNotBlank() }
					val archInfo = sections.last()
					val model = value(archInfo, "model")
					sections.minus(archInfo).map {
						CpuInfoRecord(
								vendorId = model,
								cacheSize = BigInteger.ZERO,
								cpuFamily = 0,
								mhz = value(it, "clock").substringBefore("MHz").toFloat(),
								modelId = 0,
								nr = value(it, "processor").toInt(),
								modelName = value(it, "cpu"),
								flags = listOf()
						)
					}
				}
			}

	fun listArm(session: ClientSession): List<ArmCpuInfoRecord> =
			session.createSftpClient().use {
				sftp ->
				sftp.read("/proc/cpuinfo").reader(Charsets.US_ASCII).use {
					reader ->
					val text = reader.readText()
					text.split("\n\n").filter { it.isNotBlank() }.map {
						ArmCpuInfoRecord(
								flags = value(it, "Features").split(" ").toList(),
								nr = value(it, "processor").toInt()
						)
					}
				}
			}

	fun list(session: ClientSession): List<CpuInfoRecord> =
			session.createSftpClient().use {
				sftp ->
				sftp.read("/proc/cpuinfo").reader(Charsets.US_ASCII).use {
					reader ->
					val text = reader.readText()
					text.split("\n\n").filter { it.isNotBlank() }.map {
						CpuInfoRecord(
								vendorId = value(it, "vendor_id"),
								cacheSize = value(it, "cache size").toSize(),
								cpuFamily = value(it, "cpu family").toInt(),
								mhz = value(it, "cpu MHz").toFloat(),
								modelId = value(it, "model").toInt(),
								nr = value(it, "processor").toInt(),
								modelName = value(it, "model name"),
								flags = value(it, "flags").split(" ").toList()
						)
					}
				}
			}
}<commit_msg>tiny cleanup on CpuInfo (sonar warning + simplification)<commit_after>package com.github.kerubistan.kerub.utils.junix.procfs

import com.github.kerubistan.kerub.model.HostCapabilities
import com.github.kerubistan.kerub.model.OperatingSystem
import com.github.kerubistan.kerub.utils.junix.common.OsCommand
import com.github.kerubistan.kerub.utils.substringBetween
import com.github.kerubistan.kerub.utils.toSize
import org.apache.sshd.client.session.ClientSession
import java.math.BigInteger

object CpuInfo : OsCommand {

	private const val path = "/proc/cpuinfo"

	override fun available(hostCapabilities: HostCapabilities?): Boolean =
			hostCapabilities?.os == OperatingSystem.Linux

	internal fun value(properties: String, property: String) =
			properties.substringBetween(property, "\n").substringAfter(":").trim()

	fun listPpc(session: ClientSession): List<CpuInfoRecord> =
			readCpuInfo(session).let { text ->
				val sections = text.split("\n\n").filter { it.isNotBlank() }
				val archInfo = sections.last()
				val model = value(archInfo, "model")
				sections.minus(archInfo).map {
					CpuInfoRecord(
							vendorId = model,
							cacheSize = BigInteger.ZERO,
							cpuFamily = 0,
							mhz = value(it, "clock").substringBefore("MHz").toFloat(),
							modelId = 0,
							nr = value(it, "processor").toInt(),
							modelName = value(it, "cpu"),
							flags = listOf()
					)
				}
			}

	private fun readCpuInfo(session: ClientSession) = session.createSftpClient().use { sftp ->
		sftp.read(path).reader(Charsets.US_ASCII).use { reader ->
			reader.readText()
		}
	}

	fun listArm(session: ClientSession): List<ArmCpuInfoRecord> =
			readCpuInfo(session).split("\n\n").filter { it.isNotBlank() }.map {
				ArmCpuInfoRecord(
						flags = value(it, "Features").split(" ").toList(),
						nr = value(it, "processor").toInt()
				)
			}

	fun list(session: ClientSession): List<CpuInfoRecord> =
			readCpuInfo(session).split("\n\n").filter { it.isNotBlank() }.map {
				CpuInfoRecord(
						vendorId = value(it, "vendor_id"),
						cacheSize = value(it, "cache size").toSize(),
						cpuFamily = value(it, "cpu family").toInt(),
						mhz = value(it, "cpu MHz").toFloat(),
						modelId = value(it, "model").toInt(),
						nr = value(it, "processor").toInt(),
						modelName = value(it, "model name"),
						flags = value(it, "flags").split(" ").toList()
				)
			}

}
{'fraction_non_alphanumeric': 0.15169101460876525, 'fraction_numerical': 0.0008004802881729037, 'mean_word_length': 3.349869451697128, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3404899', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1610, 'n_words': 287}