public/LogFiles/Import-AuditLogs.ps1

using namespace System.Management.Automation
using module '..\..\modules\Enums.psm1'
using module '..\..\modules\Helper\DateTimeHelper.psm1'
using module '..\..\modules\Session.psd1'
using module '..\..\modules\Downloader\DownloaderFactory.psm1'

function Import-AuditLogs {
    [Alias('impal')]
    param(
        [Parameter(Mandatory=$true)]
        # Specifies the operator from where this match should be downloaded.
        [OPERATOR] $operator,
        [Parameter(Mandatory=$true)]
        # Specifies the environment from where the match logs will be downloaded.
        [ENVIRONMENT] $environment,
        # Specifies the matchId from the Control Center
        [string] $matchId,
        # Defines the source log files to be downloaded. By default it will downlada
        # all configured logs for this Operator and Environment.
        [SOURCE[]] $source,
        # Defines the source from where the matchID should be located.
        # Typically used when there is external (provider) ID of the match.
        [ValidateSet([ValidExternalKeys])]
        [string] $externalKey,
        # Referent message time taken as baseline from where the messages will be start analyzing.
        [string] $referentTime,
        # Timeframe in which the messages will be analyzed after the referent time.
        [string] $timeSpan,
        # run this cmdlet as background job
        [switch] $AsJob,
        # created new session and preserve previous
        [switch] $newSession
    )

    #region Init

    $convert = $false
    $saveConverted = $false

    if ($newSession -or -not [Session]::getCurrent()){
        $currentSession = [Session]::NewSession($operator, $environment, $convert, $saveConverted)
        Write-Host ('New session [{0}] created' -f $currentSession.sessionId)
    }
    else {
        if (-not [Session]::getCurrent().canImport()) { Write-Error 'Import terminated, jobs in progress.'; return }

        [Session]::getCurrent().Reset($operator, $environment, $convert, $saveConverted)
        $currentSession = [Session]::getCurrent()
    }

    if ($externalKey){
        Write-Host "Identifying match: $matchId from '$externalKey' ... " -NoNewLine
        $matchId = [Session]::getCurrent().getMatchIdByExternal($externalKey, $matchId)

        if ($matchId){
            Write-Host 'Identified as id {0}' -f $_defMatchId
        }
        else{
            Write-Host ''
            Write-Error "Call to '$_uri' wasn't successfull"
            return
        }
    }

    $currentSession.setAnalyzedScope($source, $matchId, $referentTime, $timeSpan)

    if ($currentSession.config.queryType -eq [QUERY_TYPE]::datetimeInterval){
        Write-Error "Search without matchId is not supported with the current downloaders."
        return
    }

    #endregion

    if ($currentSession.config.ApproveDownload()) {
        $currentSession.RegisterDownloader($AsJob)
        if (-not $AsJob) { $currentSession.RegisterLogFiles($AsJob, $true) }
    }
    else {
        $currentSession.RegisterLogFiles($AsJob, $false)
        Write-Host ''
        Write-Host ('Total processing time: {0}' -f [DateTimeHelper]::FormatMilliseconds($currentSession.auditLogFile.getProcessingTime((Get-Date)).TotalMilliseconds))
    }

    if (-not $AsJob -or -not [Session]::getCurrent()) {
        [Session]::currentSessionId = $currentSession.sessionId
    }
}