<commit_before><?php

namespace Minds\Core\Helpdesk\Question;

use Cassandra\Set;
use Cassandra\Type;
use Cassandra\Uuid;
use Cassandra\Varint;
use Minds\Common\Repository\Response;
use Minds\Core;
use Minds\Core\Data\Cassandra;
use Minds\Core\Data\Cassandra\Prepared\Custom;
use Minds\Core\Data\ElasticSearch;
use Minds\Core\Di\Di;
use Minds\Core\Helpdesk\Category;

class Repository
{
    /** @var Core\Data\Cassandra\Client */
    protected $cassandraClient;

    /** @var Core\Data\ElasticSearch\Client */
    protected $esClient;

    /** @var Category\Repository */
    protected $repository;

    public function __construct(
        Cassandra\Client $cassandraClient = null,
        ElasticSearch\Client $esClient = null,
        Category\Repository $categoryRepository = null
    )
    {
        $this->cassandraClient = $cassandraClient ?: Di::_()->get('Database\Cassandra\Cql');
        $this->esClient = $esClient ?: Di::_()->get('Database\ElasticSearch');
        $this->repository = $categoryRepository ?: Di::_()->get('Helpdesk\Category\Repository');
    }

    /**
     * Get questions
     * @param array $opts
     * @return Response
     */
    public function getList(array $opts = [])
    {
        $opts = array_merge([
            'limit' => 10,
            'offset' => '',
            'category_uuid' => null,
        ], $opts);

        $query = "SELECT * FROM helpdesk_faq_by_category_uuid ";
        $where = [];
        $values = [];

        if ($opts['category_uuid']) {
            $where[] = "category_uuid = ?";
            $values[] = new Uuid($opts['category_uuid']);
        }

        if (count($where) > 0) {
            $query .= 'WHERE ' . implode(' AND ', $where);
        }

        $prepared = (new Custom())
            ->query($query, $values);

        $prepared->setOpts([
            'page_size' => (int) $opts['limit'],
            'paging_state_token' => base64_decode($opts['offset']),
        ]);

        $response = new Response([]);

        try {
            $data = $this->cassandraClient->request($prepared);

            foreach ($data as $row) {
                $question = new Question();

                $question->setUuid($row['uuid']->uuid())
                    ->setQuestion($row['question'])
                    ->setAnswer($row['answer'])
                    ->setCategoryUuid($row['category_uuid'] ? $row['category_uuid']->uuid() : null)
                    ->setScore($row['score'])
                    ->setPosition($row['position'] ?? 10);

                $response[] = $question;
            }
            $response->setPagingToken($data->pagingStateToken());
        } catch (\Exception $e) {
            error_log($e);
        }

        return $response;
    }

    /**
     * Return a single question
     * @param $uuid
     * @return Question
     */
    public function get($uuid)
    {
        if (!isset($uuid)) {
            throw new \Exception('uuid must be provided');
        }

        $query = "SELECT * FROM helpdesk_faq WHERE uuid = ?";

        $prepared = (new Custom())
            ->query($query, [new Uuid($uuid)]);

        $question = null;
        try {
            $result = $this->cassandraClient->request($prepared);
            if ($result->count() > 0) {
                $row = $result->current();

                $question = (new Question())
                    ->setUuid($row['uuid']->uuid())
                    ->setQuestion($row['question'])
                    ->setAnswer($row['answer'])
                    ->setCategoryUuid($row['category_uuid'] ? $row['category_uuid']->uuid() : null)
                    ->setThumbsUp($this->getThumbsFromSet($row['votes_up']))
                    ->setThumbsDown($this->getThumbsFromSet($row['votes_down']));
            }
        } catch (\Exception $e) {
            error_log($e);
        }

        return $question;
    }
<commit_msg>(fix): positioning not returning for singleton<commit_after><?php

namespace Minds\Core\Helpdesk\Question;

use Cassandra\Set;
use Cassandra\Type;
use Cassandra\Uuid;
use Cassandra\Varint;
use Minds\Common\Repository\Response;
use Minds\Core;
use Minds\Core\Data\Cassandra;
use Minds\Core\Data\Cassandra\Prepared\Custom;
use Minds\Core\Data\ElasticSearch;
use Minds\Core\Di\Di;
use Minds\Core\Helpdesk\Category;

class Repository
{
    /** @var Core\Data\Cassandra\Client */
    protected $cassandraClient;

    /** @var Core\Data\ElasticSearch\Client */
    protected $esClient;

    /** @var Category\Repository */
    protected $repository;

    public function __construct(
        Cassandra\Client $cassandraClient = null,
        ElasticSearch\Client $esClient = null,
        Category\Repository $categoryRepository = null
    )
    {
        $this->cassandraClient = $cassandraClient ?: Di::_()->get('Database\Cassandra\Cql');
        $this->esClient = $esClient ?: Di::_()->get('Database\ElasticSearch');
        $this->repository = $categoryRepository ?: Di::_()->get('Helpdesk\Category\Repository');
    }

    /**
     * Get questions
     * @param array $opts
     * @return Response
     */
    public function getList(array $opts = [])
    {
        $opts = array_merge([
            'limit' => 10,
            'offset' => '',
            'category_uuid' => null,
        ], $opts);

        $query = "SELECT * FROM helpdesk_faq_by_category_uuid ";
        $where = [];
        $values = [];

        if ($opts['category_uuid']) {
            $where[] = "category_uuid = ?";
            $values[] = new Uuid($opts['category_uuid']);
        }

        if (count($where) > 0) {
            $query .= 'WHERE ' . implode(' AND ', $where);
        }

        $prepared = (new Custom())
            ->query($query, $values);

        $prepared->setOpts([
            'page_size' => (int) $opts['limit'],
            'paging_state_token' => base64_decode($opts['offset']),
        ]);

        $response = new Response([]);

        try {
            $data = $this->cassandraClient->request($prepared);

            foreach ($data as $row) {
                $question = new Question();

                $question->setUuid($row['uuid']->uuid())
                    ->setQuestion($row['question'])
                    ->setAnswer($row['answer'])
                    ->setCategoryUuid($row['category_uuid'] ? $row['category_uuid']->uuid() : null)
                    ->setScore($row['score'])
                    ->setPosition($row['position'] ?: 10);

                $response[] = $question;
            }
            $response->setPagingToken($data->pagingStateToken());
        } catch (\Exception $e) {
            error_log($e);
        }

        return $response;
    }

    /**
     * Return a single question
     * @param $uuid
     * @return Question
     */
    public function get($uuid)
    {
        if (!isset($uuid)) {
            throw new \Exception('uuid must be provided');
        }

        $query = "SELECT * FROM helpdesk_faq WHERE uuid = ?";

        $prepared = (new Custom())
            ->query($query, [new Uuid($uuid)]);

        $question = null;
        try {
            $result = $this->cassandraClient->request($prepared);
            if ($result->count() > 0) {
                $row = $result->current();

                $question = (new Question())
                    ->setUuid($row['uuid']->uuid())
                    ->setQuestion($row['question'])
                    ->setAnswer($row['answer'])
                    ->setCategoryUuid($row['category_uuid'] ? $row['category_uuid']->uuid() : null)
                    ->setThumbsUp($this->getThumbsFromSet($row['votes_up']))
                    ->setThumbsDown($this->getThumbsFromSet($row['votes_down']))
                    ->setPosition($row['position'] ?: 10);
            }
        } catch (\Exception $e) {
            error_log($e);
        }

        return $question;
    }

{'fraction_non_alphanumeric': 0.1618188746348279, 'fraction_numerical': 0.002286294932046234, 'mean_word_length': 1.9424514200298955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 88, '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': '1542203', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2222, 'n_words': 407}