<commit_before><?php
namespace AppBundle\Command;

use AppBundle\Command\Base\SqsCommand;
use Survos\Client\Resource\ObserveResource;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class GoogleStaypointsImportCommand extends SqsCommand
{
    protected function configure()
    {
        parent::configure();

        $this
            ->setName('remote:import-staypoints')
            ->setDescription('Google Timeline "My Places" Import')
        ;
    }

    private $queue = [];

    protected function processMessage(array $data, array $message) : bool
    {
        $data = $this->validateMessage($data);
        $payload = (array)$data['payload'];
        if ($this->input->getOption('verbose')) {
            dump($data, $payload);
        }
        $this->survosClient = $this->getClient($data['apiUrl'], $data['accessToken']);
        $queueType = $this->input->getArgument('queue-type');
        try {
            $localPath = $queueType === 'sqs' ? $this->downloadFile($data['parameters']['imageUrl']) : $this->resolveLocalPath($data['parameters']['imageUrl']);
            $answersResolver = new OptionsResolver();
            $answersResolver->setDefaults($payload);
            $answers = $answersResolver->resolve($this->processFile($localPath));
        } catch (\Throwable $e) {
            $errorMessage = 'Uploaded file is invalid';
            $this->sendError($data['channelCode'], $errorMessage, $data['taskId'], $data['assignmentId']);
            $this->output->writeln('unable to process file: '. $e->getMessage());
            return false; // true;
        }
        if ($this->input->getOption('verbose')) {
            dump($answers);
        }

        if ($answers) {
            $data['command'] = 'partial';
            $this->sendData(array_filter($data, function ($key) { return in_array($key, ['command', 'taskId','assignmentId','channelCode']);}, ARRAY_FILTER_USE_KEY),
                $answers);
        }

        die('stopped');

        return true; // use --delete-bad to leave the message in the queue.
    }

    private function resolveLocalPath($url): string
    {
        $platformRoot = realpath($this->getContainer()->getParameter('kernel.project_dir') . '/../platform/web');
        if (!file_exists($platformRoot)) {
            throw new \Exception("Can't find the platform root. It's supposed to be here: {$platformRoot}");
        }
        if (!preg_match('/(\/uploads.+$)/', $url, $matches)) {
            throw new \Exception("Can't parse file location from url: {$url}");
        }
        $path = $platformRoot . $matches[1];
        if (!file_exists($path)) {
            throw new \Exception("Can't resolve local path ({$path}) from url: {$url}");
        }
        return $path;
    }

    private function downloadFile($url)
    {
        $path = sys_get_temp_dir() . '/' . md5($url). '.zip';
        if (!file_exists($path)) {
            $newfname = $path;<commit_msg>cleanup<commit_after><?php
namespace AppBundle\Command;

use AppBundle\Command\Base\SqsCommand;
use Survos\Client\Resource\ObserveResource;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class GoogleStaypointsImportCommand extends SqsCommand
{
    protected function configure()
    {
        parent::configure();

        $this
            ->setName('remote:import-staypoints')
            ->setDescription('Google Timeline "My Places" Import')
        ;
    }

    private $queue = [];

    protected function processMessage(array $data, array $message) : bool
    {
        $data = $this->validateMessage($data);
        $payload = (array)$data['payload'];
        if ($this->input->getOption('verbose')) {
            dump($data, $payload);
        }
        $this->survosClient = $this->getClient($data['apiUrl'], $data['accessToken']);
        $queueType = $this->input->getArgument('queue-type');
        try {
            $localPath = $queueType === 'sqs' ? $this->downloadFile($data['parameters']['imageUrl']) : $this->resolveLocalPath($data['parameters']['imageUrl']);
            $answersResolver = new OptionsResolver();
            $answersResolver->setDefaults($payload);
            $answers = $answersResolver->resolve($this->processFile($localPath));
        } catch (\Throwable $e) {
            $errorMessage = 'Uploaded file is invalid';
            $this->sendError($data['channelCode'], $errorMessage, $data['taskId'], $data['assignmentId']);
            $this->output->writeln('unable to process file: '. $e->getMessage());
            return false;
        }
        if ($this->input->getOption('verbose')) {
            dump($answers);
        }

        if ($answers) {
            $data['command'] = 'partial';
            $this->sendData(array_filter($data, function ($key) { return in_array($key, ['command', 'taskId','assignmentId','channelCode']);}, ARRAY_FILTER_USE_KEY),
                $answers);
        }

        return true; // use --delete-bad to leave the message in the queue.
    }

    private function resolveLocalPath($url): string
    {
        $platformRoot = realpath($this->getContainer()->getParameter('kernel.project_dir') . '/../platform/web');
        if (!file_exists($platformRoot)) {
            throw new \Exception("Can't find the platform root. It's supposed to be here: {$platformRoot}");
        }
        if (!preg_match('/(\/uploads.+$)/', $url, $matches)) {
            throw new \Exception("Can't parse file location from url: {$url}");
        }
        $path = $platformRoot . $matches[1];
        if (!file_exists($path)) {
            throw new \Exception("Can't resolve local path ({$path}) from url: {$url}");
        }
        return $path;
    }

    private function downloadFile($url)
    {
        $path = sys_get_temp_dir() . '/' . md5($url). '.zip';
        if (!file_exists($path)) {
            $newfname = $path;
{'fraction_non_alphanumeric': 0.16248348745046234, 'fraction_numerical': 0.0006605019815059445, 'mean_word_length': 2.7091243110838947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6610568', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1655, 'n_words': 343}