Skip to content

Commit

Permalink
Add -Segments support to CubeJS
Browse files Browse the repository at this point in the history
  • Loading branch information
TehMuffinMoo committed Jul 15, 2024
1 parent 7510156 commit 64e6066
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 1 deletion.
87 changes: 87 additions & 0 deletions Modules/ibPS/Functions/BloxOne/BloxOneDDI/Get-B1Asset.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
function Get-B1Asset {
<#
.SYNOPSIS
Queries a list of Assets discovered by BloxOne
.DESCRIPTION
This function is used to query a list of Assets discovered by BloxOne
.PARAMETER Summary
Provides a summary of discovered asset counts by category
.EXAMPLE
.FUNCTIONALITY
BloxOneDDI
.FUNCTIONALITY
Discovery
#>
param(
[ValidateSet('ByCategory','ByVendor','ByRegion','ByAccount')]
[String]$Type
)

Switch($Type) {
"ByCategory" {
## List Asset Count, Category & Context
Invoke-B1CubeJS -Cube CloudDiscoveryResources -Measures assets -Dimensions doc_asset_category,doc_asset_context -Segments countable -Grouped
break
}
"ByVendor" {
# List Cloud Asset Count by Vendor
$Filters = @(
@{
"member" = "CloudDiscoveryResources.doc_asset_vendor"
"operator" = "set"
}
@{
"member" = "CloudDiscoveryResources.doc_asset_context"
"operator" = "equals"
"values" = @("cloud")
}
)
Invoke-B1CubeJS -Cube CloudDiscoveryResources -Measures assets -Dimensions doc_asset_vendor -Segments countable -Filters $Filters -Grouped
break
}
"ByRegion" {
# List Cloud Asset Count by Region & Vendor
$Filters = @(
@{
"member" = "CloudDiscoveryResources.doc_asset_context"
"operator" = "equals"
"values" = @("cloud")
}
)
Invoke-B1CubeJS -Cube CloudDiscoveryResources -Measures assets -Dimensions doc_asset_region,doc_asset_vendor -Segments countable -Filters $Filters -Grouped
break
}
"ByAccount" {
# List Cloud Asset Count by Azure Subscription, GCP Project and AWS Account ID.
$Filters = @(
@{
"member" = "CloudDiscoveryResources.doc_asset_context"
"operator" = "equals"
"values" = @("cloud")
}
)
Invoke-B1CubeJS -Cube CloudDiscoveryResources -Measures assets -Dimensions doc_asset_aws_account_id,doc_asset_gcp_project_id,doc_asset_azure_subscription_id,doc_asset_vendor -Filters $Filters -Segments countable -Grouped
break
}
}





# $Filters = @(
# @{
# "member" = "asset_daily_counts.asset_context"
# "operator" = "equals"
# "values" = @("cloud","onprem")
# }
# )
# Invoke-B1CubeJS -Cube asset_daily_counts -Measures asset_count -Dimensions asset_context -TimeDimension asset_date -Granularity day -Start (Get-Date).AddDays(-30) -Filters $Filters -Grouped

}
8 changes: 8 additions & 0 deletions Modules/ibPS/Functions/BloxOne/Private/ArgCompleter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ $B1CubeJSDimensions = {
}
Register-ArgumentCompleter -CommandName Invoke-B1CubeJS -ParameterName Dimensions -ScriptBlock $B1CubeJSDimensions

$B1CubeJSSegments = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
(Get-B1CubeJSSegments -Cube $fakeBoundParameters['Cube']).Name | %{
$_.replace("$($fakeBoundParameters['Cube']).","")
} | Where-Object {$_ -like "$($wordToComplete)*" -and $_ -notin @($fakeBoundParameters['Segments'])}
}
Register-ArgumentCompleter -CommandName Invoke-B1CubeJS -ParameterName Segments -ScriptBlock $B1CubeJSSegments

$B1CubeJSOrderBy = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
Switch($commandName) {
Expand Down
9 changes: 9 additions & 0 deletions Modules/ibPS/Functions/BloxOne/Private/CubeJS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ function Get-B1CubeJSMeasures {
return $CubeResult.measures
}

function Get-B1CubeJSSegments {
param(
[Parameter(Mandatory=$true)]
[String]$Cube
)
$CubeResult = Get-B1CubeJSCubes -Cube $Cube
return $CubeResult.segments
}

function Get-B1CubeJSDimensions {
param(
[Parameter(Mandatory=$true)]
Expand Down
13 changes: 12 additions & 1 deletion Modules/ibPS/Functions/BloxOne/Wrapper/Invoke-B1CubeJS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function Invoke-B1CubeJS {
.PARAMETER Cube
.PARAMETER Measures
.PARAMETER Dimensions
.PARAMETER Segments
.PARAMETER AllDimensions
.PARAMETER Filters
.PARAMETER Limit
Expand Down Expand Up @@ -44,6 +45,7 @@ function Invoke-B1CubeJS {
[String]$Cube,
[String[]]$Measures,
[String[]]$Dimensions,
[String[]]$Segments,
[Switch]$AllDimensions,
[System.Object]$Filters,
[Int]$Limit,
Expand Down Expand Up @@ -88,6 +90,7 @@ function Invoke-B1CubeJS {
$splat = @{
"measures" = @()
"dimensions" = @()
"segments" = @()
"ungrouped" = $(if ($Grouped) { $False } else { $True })
}

Expand Down Expand Up @@ -127,8 +130,16 @@ function Invoke-B1CubeJS {
}
}

if ($Segments) {
$splat.segments = @($(
$Segments | %{
"$($Cube).$($_)"
}
))
}

if ($Filters) {
$splat.Filters = @($Filters)
$splat.filters = @($Filters)
}

if ($TimeDimension) {
Expand Down
59 changes: 59 additions & 0 deletions Modules/ibPS/Functions/Misc/Misc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ function DevelopmentFunctions {
"Get-B1Context"
"Set-B1Context"
"Initialize-B1Config"
"Write-Chart"
"Write-Graph"
)
}

Expand Down Expand Up @@ -962,4 +964,61 @@ function New-ibPSTelemetry {
$QueryString = ConvertTo-QueryString $Query
$OutNull = Invoke-WebRequest -Method POST -Uri "https://google-analytics.com/g/collect$($QueryString)" -UseBasicParsing | Out-Null
}
}

Function Write-Graph($YAxisLabel, $Row, $RowColor) {
Write-Host -Object $([char]9474) -NoNewline
Write-Host -Object "$($YAxisLabel.tostring().PadLeft($LengthOfMaxYAxisLabel+2) + [Char]9508)" -NoNewline
Write-Host -Object $Row -ForegroundColor $RowColor -NoNewline
Write-Host -Object " $([char]9474)"
}

function Write-Chart {
param(
[Int[]]$YAxis,
[ValidateScript({
if($_.Count -gt 30){
Throw "Maximum date plots supported is 30"
}
else{
$true
}
})]
[DateTime[]]$XAxis
)
$DateStrings = $XAxis | %{ $_.ToString('dd/MM') }
$UnderChar = [Char]9472

$C = 0
$Map = @($XAxis | %{
[PSCustomObject]@{ "Date" = $_
"_Count" = $YAxis[$C]
"Instance" = $C
}
$C++
})


Write-Host "$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$(1..$($Map.Count) | %{ Write-Host -NoNewline "$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)" })"
$YAxisUnique = $Map._Count | Select-Object -Unique | Sort-Object -Desc
foreach ($YAU in $YAxisUnique) {
$Row = ""
## Get list of X-Axis which have the same count
$XAxisObjs = $Map | Where-Object {$_._Count -eq $YAU}
$(0..$($Map.Count-1) | %{
## Check if the instance exists in list to print
$Instance = $_
if ($XAxisObjs | Where-Object {$_.Instance -eq $Instance}) {
$Row += "$([char]9608)$([char]9608)$([char]9608)$([char]9608)$([char]9608)$([char]9608)$([char]9608)$([char]9608)$([char]9608)$([char]9608)"
} else {
$Row += " "
}
})
Write-Graph -YAxisLabel $("$($YAU)".PadLeft(8)) -Row $Row -RowColor Green -LabelColor 'Blue'
}

Write-Host "$([char]9474)$(''.PadLeft(8))$([char]9474)" -NoNewline
$(1..$($Map.Count) | %{ Write-Host -NoNewline "$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)$([char]9516)$($UnderChar)$($UnderChar)$($UnderChar)$($UnderChar)" })
Write-Host ""
Write-Graph -YAxisLabel ' ' -Row " $($DateStrings -join " $([char]9474) ")" -RowColor Black -LabelColor 'Blue'
}
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ The list of items below are those which are not yet implemented within ibPS. Onc
- [ ] Improve coverage of Pester Tests
- [ ] Add TSIG_KEY support to `Set-B1DNSACL` & `New-B1DNSACLItem`
- [ ] Add IPAM Hosts, Tags & Metadata Network Scope support for `New-B1SecurityPolicy` & `Set-B1SecurityPolicy`
- [ ] Add help info on new CubeJS Wrapper
- [ ] Add help info on new `Get-B1Asset` function

## Bug Fixes

0 comments on commit 64e6066

Please sign in to comment.