<commit_before><?php

class MysqlTimeSeries
{

    private $mysqli;

    /**
     * Constructor.
     *
     * @param api $mysqli Instance of mysqli
     *
     * @api
    */
    public function __construct($mysqli)
    {
        $this->mysqli = $mysqli;
    }

    /**
     * Creates a histogram type mysql table.
     *
     * @param integer $feedid The feedid of the histogram table to be created
    */
    public function create($feedid,$options)
    {
        $feedname = "feed_".trim($feedid)."";

        $result = $this->mysqli->query(
        "CREATE TABLE $feedname (
    time INT UNSIGNED, data float,
        INDEX ( `time` )) ENGINE=MYISAM");

        return true;
    }

    public function post($feedid,$time,$value)
    {
        $feedname = "feed_".trim($feedid)."";
        $this->mysqli->query("INSERT INTO $feedname (`time`,`data`) VALUES ('$time','$value')");
    }

    public function update($feedid,$time,$value)
    {
        $feedname = "feed_".trim($feedid)."";
        // a. update or insert data value in feed table
        $result = $this->mysqli->query("SELECT * FROM $feedname WHERE time = '$time'");

        if (!$result) return $value;
        $row = $result->fetch_array();

        if ($row) $this->mysqli->query("UPDATE $feedname SET data = '$value' WHERE time = '$time'");
        if (!$row) {$value = 0; $this->mysqli->query("INSERT INTO $feedname (`time`,`data`) VALUES ('$time','$value')");}

        return $value;
    }

    public function get_data($feedid,$start,$end,$outinterval)
    {
        //echo $feedid;
        $outinterval = intval($outinterval);
        $feedid = intval($feedid);
        $start = floatval($start/1000);
        $end = floatval($end/1000);
                
        if ($outinterval<1) $outinterval = 1;
        $dp = ceil(($end - $start) / $outinterval);
        $end = $start + ($dp * $outinterval);
        if ($dp<1) return false;

        // Check if datatype is daily so that select over range is used rather than 
        // skip select approach
        $result = $this->mysqli->query("SELECT datatype FROM feeds WHERE `id` = '$feedid'");
        $row = $result->fetch_array();
        $datatype = $row['datatype'];
        if ($datatype==2) $dp = 0;

        $feedname = "feed_".trim($feedid)."";

        $data = array();
        $range = $end - $start;
        if ($range > 180000 && $dp > 0) // 50 hours
        {
            $td = $range / $dp;
            $stmt = $this->mysqli->prepare("SELECT time, data FROM $feedname WHERE time BETWEEN ? AND ? LIMIT 1");
            $t = $start; $tb = 0;
            $stmt->bind_param("ii", $t, $tb);
            $stmt->bind_result($dataTime, $dataValue);
            for ($i=0; $i<$dp; $i++)
            {
                $tb = $start + intval(($i+1)*$td);
                $stmt->execute();
                if ($stmt->fetch()) {
                    if ($dataValue!=NULL) { // Remove this to show white space gaps in graph
                        $time = $dataTime * 1000;
                        $data[] = array($time, $dataValue);
                    }
                }
                $t = $tb;
            }
        } else {
            if ($range > 5000 && $dp > 0)
            {
                $td = intval($range / $dp);
                $sql = "SELECT FLOOR(time/$td) AS time, AVG(data) AS data".
                    " FROM $feedname WHERE time BETWEEN $start AND $end".
                    " GROUP BY 1";
            } else {
                $td = 1;
                $sql = "SELECT time, data FROM $feedname".
                    " WHERE time BETWEEN $start AND $end ORDER BY time DESC";
            }

            $result = $this->mysqli->query($sql);
            if($result) {
                while($row = $result->fetch_array()) {
                    $dataValue = $row['data'];
                    if ($dataValue!=NULL) { // Remove this to show white space gaps in graph
                        $time = $row['time'] * 1000 * $td;
                        $data[] = array($time , $dataValue);<commit_msg>output data values as float type<commit_after><?php

class MysqlTimeSeries
{

    private $mysqli;

    /**
     * Constructor.
     *
     * @param api $mysqli Instance of mysqli
     *
     * @api
    */
    public function __construct($mysqli)
    {
        $this->mysqli = $mysqli;
    }

    /**
     * Creates a histogram type mysql table.
     *
     * @param integer $feedid The feedid of the histogram table to be created
    */
    public function create($feedid,$options)
    {
        $feedname = "feed_".trim($feedid)."";

        $result = $this->mysqli->query(
        "CREATE TABLE $feedname (
    time INT UNSIGNED, data float,
        INDEX ( `time` )) ENGINE=MYISAM");

        return true;
    }

    public function post($feedid,$time,$value)
    {
        $feedname = "feed_".trim($feedid)."";
        $this->mysqli->query("INSERT INTO $feedname (`time`,`data`) VALUES ('$time','$value')");
    }

    public function update($feedid,$time,$value)
    {
        $feedname = "feed_".trim($feedid)."";
        // a. update or insert data value in feed table
        $result = $this->mysqli->query("SELECT * FROM $feedname WHERE time = '$time'");

        if (!$result) return $value;
        $row = $result->fetch_array();

        if ($row) $this->mysqli->query("UPDATE $feedname SET data = '$value' WHERE time = '$time'");
        if (!$row) {$value = 0; $this->mysqli->query("INSERT INTO $feedname (`time`,`data`) VALUES ('$time','$value')");}

        return $value;
    }

    public function get_data($feedid,$start,$end,$outinterval)
    {
        //echo $feedid;
        $outinterval = intval($outinterval);
        $feedid = intval($feedid);
        $start = floatval($start/1000);
        $end = floatval($end/1000);
                
        if ($outinterval<1) $outinterval = 1;
        $dp = ceil(($end - $start) / $outinterval);
        $end = $start + ($dp * $outinterval);
        if ($dp<1) return false;

        // Check if datatype is daily so that select over range is used rather than 
        // skip select approach
        $result = $this->mysqli->query("SELECT datatype FROM feeds WHERE `id` = '$feedid'");
        $row = $result->fetch_array();
        $datatype = $row['datatype'];
        if ($datatype==2) $dp = 0;

        $feedname = "feed_".trim($feedid)."";

        $data = array();
        $range = $end - $start;
        if ($range > 180000 && $dp > 0) // 50 hours
        {
            $td = $range / $dp;
            $stmt = $this->mysqli->prepare("SELECT time, data FROM $feedname WHERE time BETWEEN ? AND ? LIMIT 1");
            $t = $start; $tb = 0;
            $stmt->bind_param("ii", $t, $tb);
            $stmt->bind_result($dataTime, $dataValue);
            for ($i=0; $i<$dp; $i++)
            {
                $tb = $start + intval(($i+1)*$td);
                $stmt->execute();
                if ($stmt->fetch()) {
                    if ($dataValue!=NULL) { // Remove this to show white space gaps in graph
                        $time = $dataTime * 1000;
                        $data[] = array($time, (float)$dataValue);
                    }
                }
                $t = $tb;
            }
        } else {
            if ($range > 5000 && $dp > 0)
            {
                $td = intval($range / $dp);
                $sql = "SELECT FLOOR(time/$td) AS time, AVG(data) AS data".
                    " FROM $feedname WHERE time BETWEEN $start AND $end".
                    " GROUP BY 1";
            } else {
                $td = 1;
                $sql = "SELECT time, data FROM $feedname".
                    " WHERE time BETWEEN $start AND $end ORDER BY time DESC";
            }

            $result = $this->mysqli->query($sql);
            if($result) {
                while($row = $result->fetch_array()) {
                    $dataValue = $row['data'];
                    if ($dataValue!=NULL) { // Remove this to show white space gaps in graph
                        $time = $row['time'] * 1000 * $td;
                        $data[] = array($time , (float)$dataValue);
{'fraction_non_alphanumeric': 0.1592505854800937, 'fraction_numerical': 0.010353753235547885, 'mean_word_length': 1.6849768365320978, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 59, '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': '4809196', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2381, 'n_words': 638}