# ruiromano/pbimonitor architecture

> Architecture diagram of the GitHub repository ruiromano/pbimonitor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ruiromano/pbimonitor
- Repository: https://github.com/ruiromano/pbimonitor

## Overview

##### Project type and purpose
- This is a monitoring/ETL solution for Power BI tenants. It uses PowerShell scripts orchestrated by Azure Functions (timer triggers) to collect data from Power BI Admin APIs, Microsoft Graph API, and Fabric/Tenant APIs, storing the raw output in Azure Blob Storage. The data is then consumed by Power BI Desktop templates for analysis.

##### Main components
a. Azure Active Directory
- Service Principal (App Registration)
- Security Group (holds Service Principal)
b. Azure Function App
- Five timer‐triggered functions:
- AuditsTimer
- CatalogTimer
- DatasetRefreshTimer
- GraphTimer
- TenantSettingsTimer
- Shared Scripts folder and Modules (utils.psm1, Scripts/*.ps1)
- Configuration via Application Settings (ServicePrincipalId, Secret, TenantId, Storage settings, etc.)
- Optional Key Vault references for secrets
- App Insights for logging/monitoring
c. Azure Storage Account (Blob Storage)
- Container “pbimonitor”
- Raw JSON blobs organized by function and date
d. Power BI Desktop Templates (.pbit)
- Templates for Blob Storage and local file scenarios
- Incremental refresh parameters
e. Local PowerShell alternative
- Fetch-Run.ps1 entrypoint
- Config.json

##### Interactions and data flow
1. Timer triggers fire in Azure Function App
2. Function code loads PS scripts from its Scripts folder or zip deployment
3. Scripts authenticate using Service Principal against AAD
4. Scripts call:
- Power BI Admin APIs (Activity, Scan, RefreshHistory)
- Microsoft Graph API (Users, Licenses)
- Fabric Tenant Settings API
5. Results written as JSON to Blob Storage under “raw” path
6. State file (state.json) in function AppData tracks last run timestamps
7. Power BI Desktop template connects to Blob Storage, applies transformations, builds reports

##### Architectural patterns & principles
- Serverless timer-based ETL (Azure Functions)
- Infrastructure-as-code compatible (zip deploy, PS modules)
- State management via file on function disk (state.json)
- Secure credentials via App Settings or Key Vault references
- Separation of concerns: one timer per data domain
- Idempotent blob outputs enable incremental processing

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Azure Subscription Boundary
    subgraph "Azure Subscription" 
        direction TB

        %% Identity
        subgraph "Identity" 
            direction TB
            SP["Service Principal"]:::identity
            SG["Security Group"]:::identity
        end

        %% Compute
        subgraph "Function App" 
            direction TB
            FA["Azure Function App"]:::compute

            subgraph "Timer Functions" 
                direction TB
                AT["AuditsTimer"]:::compute
                CT["CatalogTimer"]:::compute
                DRT["DatasetRefreshTimer"]:::compute
                GT["GraphTimer"]:::compute
                TST["TenantSettingsTimer"]:::compute
            end

            subgraph "Scripts & Modules"
                direction TB
                SF["Scripts Folder"]:::scripts
                UM["utils.psm1"]:::scripts
                SF -->|imports| UM
            end

            AS["App Settings\n(ServicePrincipalId, Secret, TenantId,\nStorageConnStr,...)"]:::compute
            ST["state.json"]:::compute
            FA -->|contains| AT
            FA -->|contains| CT
            FA -->|contains| DRT
            FA -->|contains| GT
            FA -->|contains| TST
            FA -->|contains| SF
            FA -->|contains| UM
            FA -->|contains| AS
            FA -->|contains| ST
        end

        %% Key Vault
        KV["Key Vault (optional)"]:::identity

        %% Storage
        SA["Storage Account\n(Blob Storage)"]:::storage
        CB["Container: pbimonitor"]:::storage

        SA -->|contains| CB

        %% Monitoring
        AI["App Insights"]:::monitoring

        %% Connections inside Azure
        AT -->|timer trigger| FA
        CT -->|timer trigger| FA
        DRT -->|timer trigger| FA
        GT -->|timer trigger| FA
        TST -->|timer trigger| FA

        FA -->|GET token| SP
        FA -->|GET secrets| KV
        FA -->|write JSON| CB
        FA -->|log telemetry| AI
    end

    %% External Services
    subgraph "External Services"
        direction TB
        PBIAPI["Power BI Admin API"]:::external
        GraphAPI["Microsoft Graph API"]:::external
        FabricAPI["Fabric Tenant Settings API"]:::external
    end

    FA -->|call Admin APIs| PBIAPI
    FA -->|call Graph API| GraphAPI
    FA -->|call Fabric API| FabricAPI

    %% Power BI Desktop Client
    PBI["Power BI Desktop"]:::client
    PBI -->|read blobs| CB

    %% Local PowerShell Alternative
    subgraph "Local PowerShell Alternative" 
        direction TB
        CF["Config.json"]:::scripts
        FR["Fetch-Run.ps1"]:::scripts
        CF --> FR
        FR -->|imports scripts| SF
        FR -->|writes JSON| CB
    end

    %% Styles
    classDef identity fill:#FFECB3,stroke:#FFB300,color:#000;
    classDef compute fill:#C8E6C9,stroke:#388E3C,color:#000;
    classDef storage fill:#BBDEFB,stroke:#1976D2,color:#000;
    classDef monitoring fill:#E1BEE7,stroke:#8E24AA,color:#000;
    classDef external fill:#FFCDD2,stroke:#C62828,color:#000;
    classDef client fill:#FFE0B2,stroke:#EF6C00,color:#000;
    classDef scripts fill:#D7CCC8,stroke:#5D4037,color:#000;

    %% Click Events - Azure Function Code
    click FA "https://github.com/ruiromano/pbimonitor/tree/main/AzureFunction/"
    click SF "https://github.com/ruiromano/pbimonitor/tree/main/AzureFunction/Scripts/"
    click UM "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/utils.psm1"
    click AT "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/AuditsTimer/run.ps1"
    click AT "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/AuditsTimer/function.json"
    click CT "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/CatalogTimer/run.ps1"
    click CT "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/CatalogTimer/function.json"
    click DRT "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/DatasetRefreshTimer/run.ps1"
    click DRT "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/DatasetRefreshTimer/function.json"
    click GT "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/GraphTimer/run.ps1"
    click GT "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/GraphTimer/function.json"
    click TST "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/TenantSettingsTimer/run.ps1"
    click TST "https://github.com/ruiromano/pbimonitor/blob/main/AzureFunction/TenantSettingsTimer/function.json"
    click CF "https://github.com/ruiromano/pbimonitor/blob/main/Config.json"
    click FR "https://github.com/ruiromano/pbimonitor/blob/main/Fetch - Run.ps1"
```
