<commit_before><?php

namespace Keboola\SklikExtractorBundle;

use Keboola\Csv\CsvFile;
use Keboola\ExtractorBundle\Common\Logger;
use Keboola\ExtractorBundle\Extractor\Extractors\JsonExtractor as Extractor;
use Keboola\SklikExtractorBundle\Sklik\ApiException;
use Keboola\SklikExtractorBundle\Sklik\EventLogger;
use Keboola\StorageApi\Client;
use Keboola\StorageApi\Event;
use Syrup\ComponentBundle\Exception\UserException;

class SklikExtractor extends Extractor
{
	/**
	 * @var Client
	 */
	protected $storageApi;
	/**
	 * @var Sklik\EventLogger
	 */
	protected $eventLogger;
	protected $name = "sklik";
	protected $files;
	protected $tables = array(
		'accounts' => array(
			'columns' => array('userId', 'username', 'access', 'relationName', 'relationStatus', 'relationType',
				'walletCredit', 'walletCreditWithVat', 'walletVerified', 'accountLimit', 'dayBudgetSum')
		),
		'campaigns' => array(
			'columns' => array('accountId', 'id', 'name', 'deleted', 'status', 'dayBudget', 'exhaustedDayBudget',
				'adSelection', 'createDate', 'totalBudget', 'exhaustedTotalBudget', 'totalClicks', 'exhaustedTotalClicks')
		),
		'stats' => array(
			'columns' => array('accountId', 'campaignId', 'date', 'target', 'impressions', 'clicks', 'ctr', 'cpc',
				'price', 'avgPosition', 'conversions', 'conversionRatio', 'conversionAvgPrice', 'conversionValue',
				'conversionAvgValue', 'conversionValueRatio', 'transactions', 'transactionAvgPrice', 'transactionAvgValue',
				'transactionAvgCount')
		)
	);

	public function __construct(EventLogger $eventLogger)
	{
		$this->eventLogger = $eventLogger;
	}

	protected function prepareFiles()
	{
		$this->incrementalUpload = false;
		foreach ($this->tables as $k => $v) {
			$f = new CsvFile($this->temp->createTmpFile());
			$f->writeRow($v['columns']);
			$this->files[$k] = $f;
		}
	}

	protected function saveToFile($table, $data)
	{
		if (!isset($this->tables[$table])) {
			throw new \Exception('Table ' . $table . ' not configured for the Extractor');
		}
		/** @var CsvFile $f */
		$f = $this->files[$table];

		$dataToSave = array();<commit_msg>fix: remove data tables in the beginning of exctraction<commit_after><?php

namespace Keboola\SklikExtractorBundle;

use Keboola\Csv\CsvFile;
use Keboola\ExtractorBundle\Common\Logger;
use Keboola\ExtractorBundle\Extractor\Extractors\JsonExtractor as Extractor;
use Keboola\SklikExtractorBundle\Sklik\ApiException;
use Keboola\SklikExtractorBundle\Sklik\EventLogger;
use Keboola\StorageApi\Client;
use Keboola\StorageApi\Event;
use Syrup\ComponentBundle\Exception\UserException;

class SklikExtractor extends Extractor
{
	/**
	 * @var Client
	 */
	protected $storageApi;
	/**
	 * @var Sklik\EventLogger
	 */
	protected $eventLogger;
	protected $name = "sklik";
	protected $files;
	protected $tables = array(
		'accounts' => array(
			'columns' => array('userId', 'username', 'access', 'relationName', 'relationStatus', 'relationType',
				'walletCredit', 'walletCreditWithVat', 'walletVerified', 'accountLimit', 'dayBudgetSum')
		),
		'campaigns' => array(
			'columns' => array('accountId', 'id', 'name', 'deleted', 'status', 'dayBudget', 'exhaustedDayBudget',
				'adSelection', 'createDate', 'totalBudget', 'exhaustedTotalBudget', 'totalClicks', 'exhaustedTotalClicks')
		),
		'stats' => array(
			'columns' => array('accountId', 'campaignId', 'date', 'target', 'impressions', 'clicks', 'ctr', 'cpc',
				'price', 'avgPosition', 'conversions', 'conversionRatio', 'conversionAvgPrice', 'conversionValue',
				'conversionAvgValue', 'conversionValueRatio', 'transactions', 'transactionAvgPrice', 'transactionAvgValue',
				'transactionAvgCount')
		)
	);

	public function __construct(EventLogger $eventLogger)
	{
		$this->eventLogger = $eventLogger;
	}

	protected function prepareFiles()
	{
		$bucketId = sprintf("in.c-%s-%s", $this->getFullName(), $this->configName);
		if ($this->storageApi->bucketExists($bucketId)) foreach ($this->storageApi->listTables($bucketId) as $t) {
			$this->storageApi->dropTable($t['id']);
		}

		$this->incrementalUpload = true;
		foreach ($this->tables as $k => $v) {
			$f = new CsvFile($this->temp->createTmpFile());
			$f->writeRow($v['columns']);
			$this->files[$k] = $f;
		}
	}

	protected function saveToFile($table, $data)
	{
		if (!isset($this->tables[$table])) {
			throw new \Exception('Table ' . $table . ' not configured for the Extractor');
		}
		/** @var CsvFile $f */
		$f = $this->files[$table];

		$dataToSave = array();
{'fraction_non_alphanumeric': 0.1698746642793196, 'fraction_numerical': 0.0, 'mean_word_length': 5.495639534883721, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '5129371', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1506, 'n_words': 294}