modules/Downloader/RedashMultipart/RedashMultiPartDownloader.psm1
using module '..\..\Enums.psm1' using module '..\..\Helper\ObjectHelper.psm1' using module '..\..\SessionConfig.psm1' using module '..\DownloaderBase.psm1' using module '.\RedashQueryApi.psm1' class RedashMultiPartDownloader: Downloader{ #region Private Properties hidden $DATE_FORMAT = 'yyyy-MM-dd HH:mm:ss' # Urls to query audit log files hidden [string] $urlInit = '{0}://{1}/api/queries/{2}/results' hidden [string] $urlJob = '' hidden [string] $urlJobTemplate = '{0}://{1}/api/jobs/{2}' hidden [string] $urlResult = '' hidden [string] $urlResultTemplate = '{0}://{1}/api/query_results/{2}.{3}' hidden [string] $protocol hidden [string] $url hidden [string] $token hidden [string] $order hidden [string] $ORDER_ASC = 'asc' hidden [string] $ORDER_DESC = 'desc' hidden [string] $limitRange hidden [string] $RANGE_OLDEST = 'oldest' hidden [string] $RANGE_NEWEST = 'newest' hidden [int] $limitRows hidden [hashtable] $queries hidden [string] $timezone #endregion RedashMultiPartDownloader([SessionConfig] $config): base($config){ $this.timezone = $config.timezone $this.protocol = $config.downloader.protocol $this.url = $config.downloader.url $this.token = $config.downloader.token $this.fileExtension = $config.downloader.fileExtension $this.order = $config.downloader.order $this.limitRange = $config.downloader.limit.range $this.limitRows = $config.downloader.limit.rows $this.queries = @{} foreach ($source in $config.downloader.sources.PSObject.Properties){ if ($source.Value){ $this.queries.Add([SOURCE]($source.Name), $source.Value) } } } #region Private Methods hidden [hashtable] getRequestBody([string] $matchId, [datetime] $from, [datetime] $to, [string] $messageId ){ $requestBody = @{} $requestBody.Add('parameters', @{}) $requestBody.parameters.Add('matchId', $matchId) $requestBody.parameters.Add('date_created', @{}) $requestBody.parameters.date_created.Add('start', $from.ToString($this.DATE_FORMAT)) $requestBody.parameters.date_created.Add('end', $to.ToString($this.DATE_FORMAT)) $requestBody.parameters.Add('messageId', $messageId) $requestBody.Add('maxAge', 1800) return $requestBody } #endregion [hashtable] Download([SOURCE]$source, [bool] $runAsJob){ $retValue = @{} $headers = @{ 'Authorization' = ('Key {0}' -f $this.token) 'cache-control' = 'no-cache' } $matchId=0 switch ($this.config.queryType){ ([QUERY_TYPE]::matchId) { $matchId = $this.config.matchId } ([QUERY_TYPE]::datetimeInterval) { # NOP } ([QUERY_TYPE]::combined) { $matchId = $this.config.matchId } } #adjust times based on environment timezone $from = $this.config.afterDateTime.ToUniversalTime().AddHours($this.timezone) $to = $this.config.beforeDateTime.ToUniversalTime().AddHours($this.timezone) $body = $this.getRequestBody($matchId, $from, $to, 0) $this.fileName = $this.config.getFileName($source, $this.fileExtension) if ($runAsJob) { $env:WhereAmI = $PSScriptRoot $job = Start-Job -Name $source -ScriptBlock { param($domain, $queryId, $filterParams, $fileExtension, $headers, $outFile, $oldest, $descending, $limitRows) (Get-RedashContent -domain $domain -queryId $queryId -filterParams $filterParams -fileExtension $fileExtension ` -headers $headers -outFile $outFile -oldest $oldest -descending $descending -limitRows $limitRows) } -ArgumentList (('{0}://{1}' -f @($this.protocol, $this.url)), $this.queries[$source], $body, $this.fileExtension, ` $headers, $this.fileName, ($this.limitRange -eq $this.RANGE_OLDEST), ` ($this.order -eq $this.ORDER_DESC), $this.limitRows) ` -InitializationScript { Import-Module "$env:WhereAmI\RedashQueryApi.psd1" } $retValue.Add($source, $job.Id) Remove-Item env:\WhereAmI } else { Get-RedashContent -domain ('{0}://{1}' -f @($this.protocol, $this.url)) -queryId $this.queries[$source] -filterParams $body ` -fileExtension $this.fileExtension -headers $headers -outFile $this.fileName ` -oldest ($this.limitRange -eq $this.RANGE_OLDEST) -descending ($this.order -eq $this.ORDER_DESC) -limitRows $this.limitRows } return $retValue } } |