<commit_before><?php
/* Libre.fm -- a free network service for sharing your music listening habits

   Copyright (C) 2009 Libre.fm Project

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

require_once($install_path . '/database.php');
require_once($install_path . '/data/Server.php');

class Statistic {
	/*
	 * returns an array counting appareances of a given field and his corresponding bargraph size
	 * @param string $table table name to be queried
	 * @param string $field field name to count
	 * @param integer $limit limit of the query
	 * @param string $constraint username or artistname depending on field
	 * @param integer $maxwidth bargraph max width ( to express visually the number of plays )
	 * inaccurate @param integer $sizes quantity of possible sizes
	 * inaccurate @param float $max_font_size maximum font size (px, em, %, etc)
	 * @return array playstats
	 */
	static function generatePlayStats($table, $field, $limit = 40, $constraint = null, $maxwidth = 100 ) {
		global $adodb;
		if (!is_string($field))          return false;
		if (!is_string($table))          return false;
		if (!is_integer($limit))         return false;
		$query = 'SELECT ' . $field . ', count(*) AS count FROM ' . $table;
		$query .= (!is_null($constraint)) ? ' WHERE ' : null;
		if ($field == 'track') {
			$query .= (!is_null($constraint)) ? ' artist = ' . $adodb->qstr($constraint) : null;
		} else {
			$query .= (!is_null($constraint)) ? ' userid = ' . ($constraint) : null;
		}
		$query .= ' GROUP BY ' . $field . ' ORDER BY count DESC LIMIT ' . $limit;
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		try {
			$res = $adodb->GetAll($query);
		}
		catch (exception $e) {
			echo('ERROR' . $e->getMessage());
		}
		if (!$res) {
			return false;
		} else {
			$max = $res[0]['count'];

			foreach($res as &$i){
				$i['pageurl'] = Server::getArtistURL($i['artist']);
				$i['size'] = $i['count'] / $max * $maxwidth;
			}

			return $res;
		}
	}

	static function generatePlayByDays($table, $limit = 100, $constraint = null, $maxwidth = 100 ) {
		global $adodb;
		global $connect_string;

		if (!is_string($table))          return false;
		if (!is_integer($limit))         return false;

		/*
		 * todo: completly remove this dirty db type check.
		 */
		$query = 'SELECT COUNT(*) as count, DATE(TO_TIMESTAMP(time)) as date FROM ' . $table;
		if( strpos($connect_string , 'mysql' ) === 0 ) $query = 'SELECT COUNT(*) as count,DATE(FROM_UNIXTIME(time)) as date FROM ' .  $table;

		$query .= (!is_null($constraint)) ? ' WHERE ' : null;
		$query .= (!is_null($constraint)) ? ' username = ' . ($constraint) : null;
		$query .= ' GROUP BY date ORDER BY date DESC LIMIT ' . $limit;
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		try {
			$res = $adodb->GetAll($query);
		}
		catch (exception $e) {
			echo('ERROR' . $e->getMessage());
		}
		if (!$res) {
			return false;
		} else {
			$max = 0;

			foreach($res as &$i){
				if( $i['count'] > $max ) $max =  $i['count'];
			}

			foreach($res as &$i){
				$i['size'] = $i['count'] / $max * $maxwidth;
			}

			return $res;
		}
	}
}
?>
<commit_msg>yeesh<commit_after><?php
/* Libre.fm -- a free network service for sharing your music listening habits

   Copyright (C) 2009 Libre.fm Project

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

require_once($install_path . '/database.php');
require_once($install_path . '/data/Server.php');

class Statistic {
	/*
	 * returns an array counting appareances of a given field and his corresponding bargraph size
	 * @param string $table table name to be queried
	 * @param string $field field name to count
	 * @param integer $limit limit of the query
	 * @param string $constraint username or artistname depending on field
	 * @param integer $maxwidth bargraph max width ( to express visually the number of plays )
	 * inaccurate @param integer $sizes quantity of possible sizes
	 * inaccurate @param float $max_font_size maximum font size (px, em, %, etc)
	 * @return array playstats
	 */
	static function generatePlayStats($table, $field, $limit = 40, $constraint = null, $maxwidth = 100 ) {
		global $adodb;
		if (!is_string($field))          return false;
		if (!is_string($table))          return false;
		if (!is_integer($limit))         return false;
		$query = 'SELECT ' . $field . ', count(*) AS count FROM ' . $table;
		$query .= (!is_null($constraint)) ? ' WHERE ' : null;
		if ($field == 'track') {
			$query .= (!is_null($constraint)) ? ' artist = ' . $adodb->qstr($constraint) : null;
		} else {
			$query .= (!is_null($constraint)) ? ' userid = ' . ($constraint) : null;
		}
		$query .= ' GROUP BY ' . $field . ' ORDER BY count DESC LIMIT ' . $limit;
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		try {
			$res = $adodb->GetAll($query);
		}
		catch (exception $e) {
			echo('ERROR' . $e->getMessage());
		}
		if (!$res) {
			return false;
		} else {
			$max = $res[0]['count'];

			foreach($res as &$i){
				$i['pageurl'] = Server::getArtistURL($i['artist']);
				$i['size'] = $i['count'] / $max * $maxwidth;
			}

			return $res;
		}
	}

	static function generatePlayByDays($table, $limit = 100, $constraint = null, $maxwidth = 100 ) {
		global $adodb;
		global $connect_string;

		if (!is_string($table))          return false;
		if (!is_integer($limit))         return false;

		/*
		 * todo: completly remove this dirty db type check.
		 */
		$query = 'SELECT COUNT(*) as count, DATE(TO_TIMESTAMP(time)) as date FROM ' . $table;
		if( strpos($connect_string , 'mysql' ) === 0 ) $query = 'SELECT COUNT(*) as count,DATE(FROM_UNIXTIME(time)) as date FROM ' .  $table;

		$query .= (!is_null($constraint)) ? ' WHERE ' : null;
		$query .= (!is_null($constraint)) ? ' userid = ' . ($constraint) : null;
		$query .= ' GROUP BY date ORDER BY date DESC LIMIT ' . $limit;
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		try {
			$res = $adodb->GetAll($query);
		}
		catch (exception $e) {
			echo('ERROR' . $e->getMessage());
		}
		if (!$res) {
			return false;
		} else {
			$max = 0;

			foreach($res as &$i){
				if( $i['count'] > $max ) $max =  $i['count'];
			}

			foreach($res as &$i){
				$i['size'] = $i['count'] / $max * $maxwidth;
			}

			return $res;
		}
	}
}
?>

{'fraction_non_alphanumeric': 0.14559697460831983, 'fraction_numerical': 0.005132360886007563, 'mean_word_length': 3.2977365060940222, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2981589', 'n_tokens_mistral': 2618, 'n_tokens_neox': 2321, 'n_words': 790}