<commit_before><?php
/**
 *
 * This file is part of the CampaignChain package.
 *
 * (c) CampaignChain, Inc. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace CampaignChain\Location\TwitterBundle\Job;

use CampaignChain\Channel\TwitterBundle\REST\TwitterClient;
use CampaignChain\CoreBundle\Entity\Location;
use CampaignChain\CoreBundle\Entity\SchedulerReportLocation;
use CampaignChain\CoreBundle\Job\JobReportInterface;
use Doctrine\Common\Persistence\ManagerRegistry;

class ReportTwitterUserMetrics implements JobReportInterface
{
    const BUNDLE_NAME = 'campaignchain/location-twitter';
    const METRIC_FOLLOWERS = 'Followers';

    protected $em;
    protected $container;

    protected $message;

    public function __construct(ManagerRegistry $managerRegistry, $container)
    {
        $this->em = $managerRegistry->getManager();
        $this->container = $container;
    }

    public function schedule($location, $facts = null)
    {
        $scheduler = new SchedulerReportLocation();
        $scheduler->setLocation($location);
        $scheduler->setInterval('1 hour');
        $this->em->persist($scheduler);

        $facts[self::METRIC_FOLLOWERS] = 0;

        $factService = $this->container->get('campaignchain.core.fact');
        $factService->addLocationFacts(self::BUNDLE_NAME, $location, $facts);

    }

    public function execute($locationId)
    {
        $client = $this->container->get('campaignchain.channel.twitter.rest.client');
        $location = $this->em->getRepository('CampaignChainCoreBundle:Location')->find($locationId);
        /** @var TwitterClient $connection */
        $connection = $client->connectByLocation($location);

        if ($connection) {

            $response = $connection->getUser($location->getIdentifier());
            $followers = $response['followers_count'];
        } else {
            return self::STATUS_ERROR;
        }

        // Add report data.
        $facts[self::METRIC_FOLLOWERS] = $followers;

        $factService = $this->container->get('campaignchain.core.fact');
        $factService->addLocationFacts(self::BUNDLE_NAME, $location, $facts);

        $this->message = 'Added to report: followers = '.$followers . '.';

        return self::STATUS_OK;
    }

    public function getMessage(){
        return $this->message;
    }
}
<commit_msg>CampaignChain/campaignchain#427 Use flush() in schedule() of jobs to persist scheduler data<commit_after><?php
/**
 *
 * This file is part of the CampaignChain package.
 *
 * (c) CampaignChain, Inc. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace CampaignChain\Location\TwitterBundle\Job;

use CampaignChain\Channel\TwitterBundle\REST\TwitterClient;
use CampaignChain\CoreBundle\Entity\Location;
use CampaignChain\CoreBundle\Entity\SchedulerReportLocation;
use CampaignChain\CoreBundle\Job\JobReportInterface;
use Doctrine\Common\Persistence\ManagerRegistry;

class ReportTwitterUserMetrics implements JobReportInterface
{
    const BUNDLE_NAME = 'campaignchain/location-twitter';
    const METRIC_FOLLOWERS = 'Followers';

    protected $em;
    protected $container;

    protected $message;

    public function __construct(ManagerRegistry $managerRegistry, $container)
    {
        $this->em = $managerRegistry->getManager();
        $this->container = $container;
    }

    public function schedule($location, $facts = null)
    {
        $scheduler = new SchedulerReportLocation();
        $scheduler->setLocation($location);
        $scheduler->setInterval('1 hour');
        $this->em->persist($scheduler);
        $this->em->flush();

        $facts[self::METRIC_FOLLOWERS] = 0;

        $factService = $this->container->get('campaignchain.core.fact');
        $factService->addLocationFacts(self::BUNDLE_NAME, $location, $facts);

    }

    public function execute($locationId)
    {
        $client = $this->container->get('campaignchain.channel.twitter.rest.client');
        $location = $this->em->getRepository('CampaignChainCoreBundle:Location')->find($locationId);
        /** @var TwitterClient $connection */
        $connection = $client->connectByLocation($location);

        if ($connection) {

            $response = $connection->getUser($location->getIdentifier());
            $followers = $response['followers_count'];
        } else {
            return self::STATUS_ERROR;
        }

        // Add report data.
        $facts[self::METRIC_FOLLOWERS] = $followers;

        $factService = $this->container->get('campaignchain.core.fact');
        $factService->addLocationFacts(self::BUNDLE_NAME, $location, $facts);

        $this->message = 'Added to report: followers = '.$followers . '.';

        return self::STATUS_OK;
    }

    public function getMessage(){
        return $this->message;
    }
}

{'fraction_non_alphanumeric': 0.12865731462925853, 'fraction_numerical': 0.0014028056112224449, 'mean_word_length': 3.420726306465899, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, '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': '5914820', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1357, 'n_words': 275}