<commit_before><?php

namespace Raspberry\Sensors;

use Matze\Core\Traits\RedisCacheTrait;
use Matze\Core\Traits\RedisTrait;

/**
 * @Service(public=false)
 */
class SensorValuesGateway {

	const SENSOR_VALUES_PREFIX = 'sensor_values:';

	use RedisTrait;

	/**
	 * @param integer $sensor_id
	 * @param double $value
	 */
	public function addValue($sensor_id, $value) {
		$predis = $this->getPredis();

		$key = self::SENSOR_VALUES_PREFIX . $sensor_id;
		$predis->ZADD($key, time(), time().'-'.$value);

		$predis->HSET(SensorGateway::SENSOR_PREFIX . $sensor_id, 'last_value', $value);
	}

	/**
	 * @param integer $sensor_id
	 * @param integer $from
	 * @return array[]
	 */
	public function getSensorValues($sensor_id, $from) {
		if ($from) {
			$from = time() - $from;
		}

		$key = self::SENSOR_VALUES_PREFIX . $sensor_id;
		$redis_result = $this->getPredis()->ZRANGEBYSCORE($key, $from, time(), 'WITHSCORES');
		$result = [];

		foreach ($redis_result as $result) {
			$result[$result[1]] = explode('-', $result[0])[1];
		}

		return $result;
	}

	/**
	 * @param integer $days
	 * @param integer $deleted_percent
	 */
	public function deleteOldValues($days, $deleted_percent) {
		return;
		//TODO
		$query = '
			DELETE FROM sensor_values
			WHERE (crc32(MD5(id)) % 100 < ?)
			AND timestamp < (DATE_SUB(NOW(), INTERVAL ? DAY));
		';
	}

} <commit_msg>#4 ServerCron: add possibility to for measure<commit_after><?php

namespace Raspberry\Sensors;

use Matze\Core\Traits\RedisCacheTrait;
use Matze\Core\Traits\RedisTrait;

/**
 * @Service(public=false)
 */
class SensorValuesGateway {

	const SENSOR_VALUES_PREFIX = 'sensor_values:';

	use RedisTrait;

	/**
	 * @param integer $sensor_id
	 * @param double $value
	 */
	public function addValue($sensor_id, $value) {
		$predis = $this->getPredis();

		$key = self::SENSOR_VALUES_PREFIX . $sensor_id;
		$predis->ZADD($key, time(), time().'-'.$value);

		$predis->HSET(SensorGateway::SENSOR_PREFIX . $sensor_id, 'last_value', $value);
	}

	/**
	 * @param integer $sensor_id
	 * @param integer $from
	 * @return array[]
	 */
	public function getSensorValues($sensor_id, $from) {
		if ($from) {
			$from = time() - $from;
		}

		$key = self::SENSOR_VALUES_PREFIX . $sensor_id;
		$redis_result = $this->getPredis()->ZRANGEBYSCORE($key, $from, time(), 'WITHSCORES');
		$result = [];

		foreach ($redis_result as $part) {
			$result[$part[1]] = explode('-', $part[0])[1];
		}

		return $result;
	}

	/**
	 * @param integer $days
	 * @param integer $deleted_percent
	 */
	public function deleteOldValues($days, $deleted_percent) {
		return;
		//TODO
		$query = '
			DELETE FROM sensor_values
			WHERE (crc32(MD5(id)) % 100 < ?)
			AND timestamp < (DATE_SUB(NOW(), INTERVAL ? DAY));
		';
	}

} 
{'fraction_non_alphanumeric': 0.17543859649122806, 'fraction_numerical': 0.006944444444444444, 'mean_word_length': 4.022018348623853, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '3304758', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1019, 'n_words': 198}