<commit_before><?php

namespace Rogue\Console\Commands;

use Carbon\Carbon;
use League\Csv\Reader;
use Rogue\Models\Campaign;
use Rogue\Services\Registrar;
use Illuminate\Console\Command;

class ImportAshesCampaigns extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rogue:ashescampaignsimport {path}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Imports legacy campaigns from Ashes based on the data in the given (link to a) CSV file.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(Registrar $registrar)
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // Make a local copy of the CSV
        $path = $this->argument('path');
        info('rogue:legacycampaignimport: Loading in csv from ' . $path);

        $temp = tempnam(sys_get_temp_dir(), 'command_csv');
        file_put_contents($temp, fopen($this->argument('path'), 'r'));

        // Load the legacy campaigns from the CSV
        $legacy_campaigns_csv = Reader::createFromPath($temp, 'r');
        $legacy_campaigns_csv->setHeaderOffset(0);
        $legacy_campaigns = iterator_to_array($legacy_campaigns_csv->getRecords());

        // Import each legacy campaign
        info('rogue:legacycampaignimport: Importing legacy campaigns...');

        foreach ($legacy_campaigns as $iterator => $legacy_campaign) {
            // Normalize all "NULL" values to null
            foreach ($legacy_campaign as $key => $value) {
                if ($value === "NULL") {
                    $legacy_campaign[$key] = null;
                }
            }

            // See if the campaign exists
            $existing_campaign = Campaign::where('id', $legacy_campaign['campaign_id'])->orWhere('id', $legacy_campaign['run_id'])->first();

            // Create the campaign if there isn't one already
            if (! $existing_campaign) {
                // If there is no campaign_id, set the campaign_run_id to the id.
                if (is_null($legacy_campaign['campaign_id'])) {
                    $campaign = $this->createCampaign($legacy_campaign['run_id'], $legacy_campaign);
                    // If the campaign_id does not equal the next record's campaign_id or the previous record's campaign id, this is either the latest run or there is only one campaign run, so keep the original campaign_id as the id in Rogue.
                } else if ($legacy_campaigns_csv->fetchOne($iterator) && $legacy_campaign['campaign_id'] != $legacy_campaigns_csv->fetchOne($iterator)['campaign_id'] && $legacy_campaign['campaign_id'] != $legacy_campaigns_csv->fetchOne($iterator - 1)) {
                    $campaign = $this->createCampaign($legacy_campaign['campaign_id'], $legacy_campaign);
                    // If this is the last row in the CSV, it must either be the only run or last run, so use the campaign_id as the id in Rogue.
                } else if (!$legacy_campaigns_csv->fetchOne($iterator)) {
                    $campaign = $this->createCampaign($legacy_campaign['campaign_id'], $legacy_campaign);
                    // Else use the campaign_run_id as the id in Rogue.
                } else {
                    $campaign = $this->createCampaign($legacy_campaign['run_id'], $legacy_campaign);
                }

                info('rogue:legacycampaignimport: Created campaign ' . $campaign->id);
            }<commit_msg>spacing<commit_after><?php

namespace Rogue\Console\Commands;

use Carbon\Carbon;
use League\Csv\Reader;
use Rogue\Models\Campaign;
use Rogue\Services\Registrar;
use Illuminate\Console\Command;

class ImportAshesCampaigns extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rogue:ashescampaignsimport {path}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Imports legacy campaigns from Ashes based on the data in the given (link to a) CSV file.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(Registrar $registrar)
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // Make a local copy of the CSV
        $path = $this->argument('path');
        info('rogue:legacycampaignimport: Loading in csv from ' . $path);

        $temp = tempnam(sys_get_temp_dir(), 'command_csv');
        file_put_contents($temp, fopen($this->argument('path'), 'r'));

        // Load the legacy campaigns from the CSV
        $legacy_campaigns_csv = Reader::createFromPath($temp, 'r');
        $legacy_campaigns_csv->setHeaderOffset(0);
        $legacy_campaigns = iterator_to_array($legacy_campaigns_csv->getRecords());

        // Import each legacy campaign
        info('rogue:legacycampaignimport: Importing legacy campaigns...');

        foreach ($legacy_campaigns as $iterator => $legacy_campaign) {
            // Normalize all "NULL" values to null
            foreach ($legacy_campaign as $key => $value) {
                if ($value === "NULL") {
                    $legacy_campaign[$key] = null;
                }
            }

            // See if the campaign exists
            $existing_campaign = Campaign::where('id', $legacy_campaign['campaign_id'])->orWhere('id', $legacy_campaign['run_id'])->first();

            // Create the campaign if there isn't one already
            if (! $existing_campaign) {
                // If there is no campaign_id, set the campaign_run_id to the id.
                if (is_null($legacy_campaign['campaign_id'])) {
                    $campaign = $this->createCampaign($legacy_campaign['run_id'], $legacy_campaign);
                    // If the campaign_id does not equal the next record's campaign_id or the previous record's campaign id, this is either the latest run or there is only one campaign run, so keep the original campaign_id as the id in Rogue.
                } else if ($legacy_campaigns_csv->fetchOne($iterator) &&
                           $legacy_campaign['campaign_id'] != $legacy_campaigns_csv->fetchOne($iterator)['campaign_id'] &&
                           $legacy_campaign['campaign_id'] != $legacy_campaigns_csv->fetchOne($iterator - 1)) {

                           $campaign = $this->createCampaign($legacy_campaign['campaign_id'], $legacy_campaign);
                    // If this is the last row in the CSV, it must either be the only run or last run, so use the campaign_id as the id in Rogue.
                } else if (!$legacy_campaigns_csv->fetchOne($iterator)) {
                    $campaign = $this->createCampaign($legacy_campaign['campaign_id'], $legacy_campaign);
                    // Else use the campaign_run_id as the id in Rogue.
                } else {
                    $campaign = $this->createCampaign($legacy_campaign['run_id'], $legacy_campaign);
                }

                info('rogue:legacycampaignimport: Created campaign ' . $campaign->id);
            }
{'fraction_non_alphanumeric': 0.11401360544217687, 'fraction_numerical': 0.0005442176870748299, 'mean_word_length': 2.370472260430995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '7548799', 'n_tokens_mistral': 2147, 'n_tokens_neox': 1990, 'n_words': 552}