<commit_before>package com.elastisys.scale.cloudpool.kubernetes.client.impl;

import java.util.function.Function;

import org.joda.time.DateTime;

import com.elastisys.scale.cloudpool.api.types.Machine;
import com.elastisys.scale.cloudpool.api.types.MachineState;
import com.elastisys.scale.commons.util.time.UtcTime;
import com.google.gson.JsonObject;

/**
 * A {@link Function} that takes a pod status JSON document (one
 * <code>item</code> of the output from
 * <code>kubectl get pods --selector="app=nginx" --output=json</code>) and
 * converts it to a {@link Machine} instance.
 *
 */
public class PodToMachine implements Function<JsonObject, Machine> {

	/**
	 * Takes a pod status JSON document (one <code>item</code> of the output
	 * from <code>kubectl get pods --selector="app=nginx" --output=json</code>)
	 * and converts it to a {@link Machine} instance.
	 */
	@Override
	public Machine apply(JsonObject pod) {
		JsonObject status = pod.get("status").getAsJsonObject();
		JsonObject metadata = pod.get("metadata").getAsJsonObject();

		String id = metadata.get("name").getAsString();
		MachineState machineState = new PodStateToMachineState()
				.apply(status.get("phase").getAsString());
		String cloudProvider = "Kubernetes";
		String region = "N/A";
		String machineSize = "N/A";
		DateTime requestTime = UtcTime
				.parse(metadata.get("creationTimestamp").getAsString());
		DateTime launchTime = UtcTime
				.parse(status.get("startTime").getAsString());
		String publicIp = status.get("hostIP").getAsString();
		String privateIp = status.get("podIP").getAsString();

		return Machine.builder().id(id).machineState(machineState)
				.cloudProvider(cloudProvider).region(region)
				.machineSize(machineSize).launchTime(launchTime)
				.requestTime(requestTime).publicIp(publicIp)
				.privateIp(privateIp).metadata(pod).build();
	}

}<commit_msg>kubernetes pool: PodToMachine: some pod fields may be missing depending on pod phase<commit_after>package com.elastisys.scale.cloudpool.kubernetes.client.impl;

import java.util.function.Function;

import org.joda.time.DateTime;

import com.elastisys.scale.cloudpool.api.types.Machine;
import com.elastisys.scale.cloudpool.api.types.MachineState;
import com.elastisys.scale.commons.util.time.UtcTime;
import com.google.gson.JsonObject;

/**
 * A {@link Function} that takes a pod status JSON document (one
 * <code>item</code> of the output from
 * <code>kubectl get pods --selector="app=nginx" --output=json</code>) and
 * converts it to a {@link Machine} instance.
 *
 */
public class PodToMachine implements Function<JsonObject, Machine> {

	/**
	 * Takes a pod status JSON document (one <code>item</code> of the output
	 * from <code>kubectl get pods --selector="app=nginx" --output=json</code>)
	 * and converts it to a {@link Machine} instance.
	 */
	@Override
	public Machine apply(JsonObject pod) {
		JsonObject status = pod.get("status").getAsJsonObject();
		JsonObject metadata = pod.get("metadata").getAsJsonObject();

		String id = metadata.get("name").getAsString();
		MachineState machineState = new PodStateToMachineState()
				.apply(status.get("phase").getAsString());
		String cloudProvider = "Kubernetes";
		String region = "N/A";
		String machineSize = "N/A";
		DateTime requestTime = UtcTime
				.parse(metadata.get("creationTimestamp").getAsString());

		// if pod isn't running, no startTime will be available
		String launchTimeValue = getWithDefault(status, "startTime", null);
		DateTime launchTime = launchTimeValue != null
				? UtcTime.parse(launchTimeValue) : null;
		// if pod isn't running, no IPs will be available
		String publicIp = getWithDefault(status, "hostIP", null);
		String privateIp = getWithDefault(status, "podIP", null);

		return Machine.builder().id(id).machineState(machineState)
				.cloudProvider(cloudProvider).region(region)
				.machineSize(machineSize).launchTime(launchTime)
				.requestTime(requestTime).publicIp(publicIp)
				.privateIp(privateIp).metadata(pod).build();
	}

	/**
	 * Returns a given property field from a JSON object or returns a default
	 * value in case the property is missing.
	 *
	 * @param object
	 * @param property
	 * @param defaultValue
	 * @return
	 */
	private String getWithDefault(JsonObject object, String property,
			String defaultValue) {
		if (object.has(property)) {
			return object.get(property).getAsString();
		}
		return defaultValue;
	}

}
{'fraction_non_alphanumeric': 0.1316386745347254, 'fraction_numerical': 0.0, 'mean_word_length': 4.76078431372549, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '6652676', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1318, 'n_words': 326}