<commit_before>﻿using Common.Logging;
using LiteGuard;
using System;
using System.Linq;
using System.Threading.Tasks;
using ZendeskApi_v2.Models.Tickets;

namespace ZendeskTicketExporter.Core
{
    public class Exporter
    {
        private readonly ILog _log;
        private readonly IDatabase _database;
        private readonly IMarkerStorage _markerStorage;
        private readonly ITicketRetriever _ticketRetriever;
        private readonly IMergedTicketExporter _mergeExporter;
        private readonly ICsvFileWriter _csvFileWriter;

        public Exporter(
            ILog log,
            IDatabase database,
            IMarkerStorage markerStorage,
            ITicketRetriever ticketRetriever,
            IMergedTicketExporter mergeExporter,
            ICsvFileWriter csvFileWriter)
        {
            _log = log;
            _database = database;
            _markerStorage = markerStorage;
            _ticketRetriever = ticketRetriever;
            _mergeExporter = mergeExporter;
            _csvFileWriter = csvFileWriter;
        }

        public static Exporter GetDefaultInstance(string siteName, string username, string apiToken)
        {
            Guard.AgainstNullArgument("siteName", siteName);
            Guard.AgainstNullArgument("username", username);
            Guard.AgainstNullArgument("apiToken", apiToken);

            var log = LogManager.GetCurrentClassLogger();
            var dbFile = "ZendeskTicketExporter.sqlite";
            var database = new Database(dbFile);

            return new Exporter(
                log,
                database,
                new SQLiteMarkerStorage(database),
                new TicketRetriever(siteName, username, apiToken, log),
                new SQLiteMergedTicketExporter(database),
                new CsvFileWriter());
        }

        public async Task RefreshLocalCopyFromServer(bool newDatabase = false)
        {
            var marker = await _markerStorage.GetCurrentMarker();

            VerifyValidConfiguration(newDatabase, marker);

            while (true)
            {
                _log.InfoFormat("Begin copying tickets using marker {0}", marker.GetValueOrDefault());

                var batch = await _ticketRetriever.GetBatch(marker);
                if (batch.Results.Any())<commit_msg>Fix unintended db sharing for different sitenames<commit_after>﻿using Common.Logging;
using LiteGuard;
using System;
using System.Linq;
using System.Threading.Tasks;
using ZendeskApi_v2.Models.Tickets;

namespace ZendeskTicketExporter.Core
{
    public class Exporter
    {
        private readonly ILog _log;
        private readonly IDatabase _database;
        private readonly IMarkerStorage _markerStorage;
        private readonly ITicketRetriever _ticketRetriever;
        private readonly IMergedTicketExporter _mergeExporter;
        private readonly ICsvFileWriter _csvFileWriter;

        public Exporter(
            ILog log,
            IDatabase database,
            IMarkerStorage markerStorage,
            ITicketRetriever ticketRetriever,
            IMergedTicketExporter mergeExporter,
            ICsvFileWriter csvFileWriter)
        {
            _log = log;
            _database = database;
            _markerStorage = markerStorage;
            _ticketRetriever = ticketRetriever;
            _mergeExporter = mergeExporter;
            _csvFileWriter = csvFileWriter;
        }

        public static Exporter GetDefaultInstance(string siteName, string username, string apiToken)
        {
            Guard.AgainstNullArgument("siteName", siteName);
            Guard.AgainstNullArgument("username", username);
            Guard.AgainstNullArgument("apiToken", apiToken);

            var log = LogManager.GetCurrentClassLogger();
            var dbFile = siteName + ".sqlite";
            var database = new Database(dbFile);

            return new Exporter(
                log,
                database,
                new SQLiteMarkerStorage(database),
                new TicketRetriever(siteName, username, apiToken, log),
                new SQLiteMergedTicketExporter(database),
                new CsvFileWriter());
        }

        public async Task RefreshLocalCopyFromServer(bool newDatabase = false)
        {
            var marker = await _markerStorage.GetCurrentMarker();

            VerifyValidConfiguration(newDatabase, marker);

            while (true)
            {
                _log.InfoFormat("Begin copying tickets using marker {0}", marker.GetValueOrDefault());

                var batch = await _ticketRetriever.GetBatch(marker);
                if (batch.Results.Any())
{'fraction_non_alphanumeric': 0.06260757314974183, 'fraction_numerical': 0.0008605851979345956, 'mean_word_length': 2.2039972432804964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1072934', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1129, 'n_words': 276}