Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Last active October 31, 2023 10:37
Show Gist options
  • Save dharmatech/c7abe356f3aeb7d74691a52055d1fd4d to your computer and use it in GitHub Desktop.
Save dharmatech/c7abe356f3aeb7d74691a52055d1fd4d to your computer and use it in GitHub Desktop.
$base = 'https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/dts'
$rest_url = '/deposits_withdrawals_operating_cash?filter=record_date:gte:{0},transaction_catg:eq:{1}&fields=record_date,transaction_today_amt&page[number]=1&page[size]=900'
# ----------------------------------------------------------------------
$date = '2020-10-09'
$account_type = 'Treasury General Account Total Deposits'
$rest_url = "/deposits_withdrawals_operating_cash?filter=record_date:gte:$date,account_type:eq:$account_type&fields=record_date,transaction_today_amt&page[number]=1&page[size]=900"
$result_deposits = Invoke-RestMethod -Method Get -Uri ($base + $rest_url)
$account_type = 'Treasury General Account Total Withdrawals'
$rest_url = "/deposits_withdrawals_operating_cash?filter=record_date:gte:$date,account_type:eq:$account_type&fields=record_date,transaction_today_amt&page[number]=1&page[size]=900"
$result_withdrawals = Invoke-RestMethod -Method Get -Uri ($base + $rest_url)
# ----------------------------------------------------------------------
function Get-WeekNumber([datetime]$DateTime = (Get-Date))
{
$cultureInfo = [System.Globalization.CultureInfo]::CurrentCulture
$cultureInfo.Calendar.GetWeekOfYear($DateTime,$cultureInfo.DateTimeFormat.CalendarWeekRule,$cultureInfo.DateTimeFormat.FirstDayOfWeek)
}
function year-week ([datetime]$dt)
{
'{0} {1:00}' -f $dt.Year, (Get-WeekNumber $dt)
}
# ----------------------------------------------------------------------
$groups = $result_deposits.data | Select-Object record_date, transaction_today_amt, @{ L = 'year-week'; E = { year-week $_.record_date } } | Group-Object year-week
$deposits_weekly = foreach ($group in $groups)
{
[PSCustomObject]@{
week_date = $group.Group[0].record_date
sum = $group.Group | Measure-Object -Property transaction_today_amt -Sum | % Sum
}
}
# ----------------------------------------------------------------------
$groups = $result_withdrawals.data | Select-Object record_date, transaction_today_amt, @{ L = 'year-week'; E = { year-week $_.record_date } } | Group-Object year-week
$withdrawals_weekly = foreach ($group in $groups)
{
[PSCustomObject]@{
week_date = $group.Group[0].record_date
sum = $group.Group | Measure-Object -Property transaction_today_amt -Sum | % Sum
}
}
# ----------------------------------------------------------------------
$json = @{
chart = @{
type = 'bar'
data = @{
labels = $deposits_weekly | % week_date
datasets = @(
@{ label = 'Deposits'; data = $deposits_weekly | % sum }
@{ label = 'Withdrawals'; data = $withdrawals_weekly | % sum }
)
}
options = @{
title = @{ display = $true; text = 'TGA : Weekly' }
scales = @{
xAxes = @( @{ stacked = $true } )
yAxes = @( @{ stacked = $true } )
}
}
}
} | ConvertTo-Json -Depth 100
$result = Invoke-RestMethod -Method Post -Uri 'https://quickchart.io/chart/create' -Body $json -ContentType 'application/json'
# Start-Process $result.url
$id = ([System.Uri] $result.url).Segments[-1]
if ($display_chart_url)
{
Write-Host
Write-Host ('https://quickchart.io/chart-maker/view/{0}' -f $id) -ForegroundColor Yellow
}
else
{
Start-Process ('https://quickchart.io/chart-maker/view/{0}' -f $id)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment