<commit_before><?php
/**
 * Webiny (http://www.webiny.com/)
 *
 * @link      http://www.webiny.com/ for the canonical source repository
 * @copyright Copyright (c) 2009-2015 Webiny LTD. (http://www.webiny.com)
 * @license   MIT
 */

namespace Webiny\AnalyticsDb\Query;

use Webiny\AnalyticsDb\AnalyticsDb;

/**
 * Class QueryBuilder
 * @package Webiny\AnalyticsDb
 */
class Dimensions extends AbstractQuery
{
    /**
     * Filter the dimension records.
     *
     * @param string      $dimensionName  Dimension name.
     * @param string|null $dimensionValue Dimension value.
     *
     * @return $this
     */
    public function setDimension($dimensionName, $dimensionValue = null)
    {
        $this->pipeline['$match']['name'] = $dimensionName;

        if (!empty($dimensionValue)) {
            $this->pipeline['$match']['value'] = $dimensionValue;
        }

        return $this;
    }

    /**
     * Groups the records by dimension name.
     *
     * @return $this
     */
    public function groupByDimensionName()
    {
        $this->pipeline['$group'] = ['_id' => '$name', 'totalCount' => ['$sum' => $this->getSumField()]];

        return $this;
    }

    /**
     * Groups records by dimension value.
     *
     * @return $this
     */
    public function groupByDimensionValue()
    {
        $this->pipeline['$group'] = [
            '_id'        => ['name' => '$name', 'value' => '$value'],
            'totalCount' => ['$sum' => $this->getSumField()]
        ];

        return $this;
    }

    /**
     * Groups the records by entity name.
     *
     * @overwrite
     * @return $this
     */
    public function groupByEntityName()
    {
        $this->pipeline['$group'] = ['_id' => '$entity', 'totalCount' => ['$sum' => $this->getSumField()]];

        return $this;
    }

    /**
     * This is the callback method that is triggered after the constructor call.
     *
     * @return void
     */
    protected function populate()
    {
        $this->collection = AnalyticsDb::ADB_DIMS;

        // $match
        $this->pipeline['$match']['entity'] = $this->entity;
        $this->pipeline['$match']['ts'] = ['$gte' => $this->dateRange[0], '$lte' => $this->dateRange[1]];

        // $project
        $this->pipeline['$project'] = [
            'ts'     => 1,
            'entity' => 1,
            'month'  => 1,
            'year'   => 1,
            'name'   => 1,
            'value'  => 1,
            'total'  => 1
        ];
<commit_msg>Minor bug fix on Dimension::groupByTimestamp<commit_after><?php
/**
 * Webiny (http://www.webiny.com/)
 *
 * @link      http://www.webiny.com/ for the canonical source repository
 * @copyright Copyright (c) 2009-2015 Webiny LTD. (http://www.webiny.com)
 * @license   MIT
 */

namespace Webiny\AnalyticsDb\Query;

use Webiny\AnalyticsDb\AnalyticsDb;

/**
 * Class QueryBuilder
 * @package Webiny\AnalyticsDb
 */
class Dimensions extends AbstractQuery
{
    /**
     * Filter the dimension records.
     *
     * @param string      $dimensionName  Dimension name.
     * @param string|null $dimensionValue Dimension value.
     *
     * @return $this
     */
    public function setDimension($dimensionName, $dimensionValue = null)
    {
        $this->pipeline['$match']['name'] = $dimensionName;

        if (!empty($dimensionValue)) {
            $this->pipeline['$match']['value'] = $dimensionValue;
        }

        return $this;
    }

    /**
     * Groups the records by dimension name.
     *
     * @return $this
     */
    public function groupByDimensionName()
    {
        $this->pipeline['$group'] = ['_id' => '$name', 'totalCount' => ['$sum' => $this->getSumField()]];

        return $this;
    }

    /**
     * Groups records by dimension value.
     *
     * @return $this
     */
    public function groupByDimensionValue()
    {
        $this->pipeline['$group'] = [
            '_id'        => ['name' => '$name', 'value' => '$value'],
            'totalCount' => ['$sum' => $this->getSumField()]
        ];

        return $this;
    }

    /**
     * Groups the records by entity name.
     *
     * @overwrite
     * @return $this
     */
    public function groupByEntityName()
    {
        $this->pipeline['$group'] = ['_id' => '$entity', 'totalCount' => ['$sum' => $this->getSumField()]];

        return $this;
    }

    /**
     * Groups the records by timestamp.
     *
     * @overwrite
     * @return $this
     */
    public function groupByTimestamp()
    {
        $this->pipeline['$group'] = ['_id' => '$ts', 'totalCount' => ['$sum' => $this->getSumField()]];

        return $this;
    }

    /**
     * This is the callback method that is triggered after the constructor call.
     *
     * @return void
     */
    protected function populate()
    {
        $this->collection = AnalyticsDb::ADB_DIMS;

        // $match
        $this->pipeline['$match']['entity'] = $this->entity;
        $this->pipeline['$match']['ts'] = ['$gte' => $this->dateRange[0], '$lte' => $this->dateRange[1]];

        // $project
        $this->pipeline['$project'] = [
            'ts'     => 1,
            'entity' => 1,
            'month'  => 1,
            'year'   => 1,
            'name'   => 1,
            'value'  => 1,
            'total'  => 1
        ];

{'fraction_non_alphanumeric': 0.17355842185128983, 'fraction_numerical': 0.006449165402124431, 'mean_word_length': 2.009703196347032, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '1550657', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1631, 'n_words': 349}