Visualization of iBatyr Monitor data in reports via PowerBI

Preparing the iBatyr Monitor server

By default, remote connection to the database on the iBatyr Monitor server is disabled. To enable it:

  1. Create a user who will work with PowerBI:

  • start the PostgreSQL console:

    sudo -u postgres psql -d ibatyr
    
  • In the console, create a new user and give him rights to work with tables:

    CREATE USER powerbi WITH PASSWORD 'password';
    GRANT CONNECT ON DATABASE ibatyr TO powerbi;
    GRANT USAGE ON SCHEMA public TO powerbi;
    GRANT SELECT ON ALL TABLES IN SCHEMA public TO powerbi;
    
  1. In the database configuration file /etc/postgresql/15/main/pg_hba.conf add the line:

host ibatyr powerbi 00.00.00.000/32 md5

The IP address and subnet mask indicate that access is allowed only from the specified IP address. Replace 00.00.00.000/32 with your real IP.

If you are using PostgreSQL 11, the file is located in /etc/postgresql/11/main/pg_hba.conf.

  1. In the file /etc/postgresql/15/main/postgresql.conf specify the directive:

listen_addresses = 'localhost, 00.00.00.00'

Replace 00.00.00.000 with the IP interface that will be listening.

  1. Restart PostgreSQL:

sudo systemctl stop postgresql && systemctl start postgresql

Ready! PostgreSQL listens on all available network interfaces and waits for connections from PowerBI.

Setting up a computer with PowerBI installed

To get data into PowerBI:

  1. Go to Get Data → Other → PostgreSQL Database.

  2. In the window that opens, specify the IP address of the iBatyr Monitor server and the name of the database.

  3. In the window that opens, select the table from which you want to load data and click Load.

  4. After receiving the table, open the Advanced Editor and enter the SQL query in it:

let

  Source = Value.NativeQuery(PostgreSQL.Database("192.168.0.56", "ibatyr", [CreateNavigationProperties=false]), "

SELECT agent_time.year AS year,
     agent_time.month AS month,
     agent_time.day AS day,
     agent_agent.id AS id,
     agent_agent.guid AS guid,
     agent_agent.computer_name AS computer_name,
     analytics_filter.category AS category,
     analytics_filter.name_ru AS NAME,
     agent_web.site AS site,
     agent_application.app_name AS app_name,
     agent_time.time_zone AS time_zone,
     agent_time.year AS year,
     agent_time.month AS month,
     agent_time.day AS day,
     agent_time.hour AS hour,
     agent_time.quarter AS quarter,
     analytics_filter.type AS type,
     Min(analytics_session_extended.local_time) AS local_time_min,
     Max(analytics_session_extended.local_time) AS local_time_max,
     Max(analytics_session_extended.time_finish) AS time_finish_max,
     Min(analytics_session_extended.come) AS come_min,
     Max(analytics_session_extended.leave) AS leave_max,
     Min(analytics_session_extended.accounting_start) AS accounting_start_min,
     Max(analytics_session_extended.accounting_end) AS accounting_end_max,
     Min(analytics_session_extended.start_work_time) AS start_work_time_min,
     Max(analytics_session_extended.end_work_time) AS end_work_time_max,
     Min(analytics_session_extended.start_break_time) AS start_break_time_min,
     Max(analytics_session_extended.end_break_time) AS end_break_time_max,
     Max(analytics_session_extended.work_time) AS work_time_max,
     Every(analytics_session_extended.selected) AS selected_every,
     Array_agg(analytics_session_extended.source_table) AS source_table_array_agg,
     Array_agg(analytics_session_extended.source_id) AS source_id_array_agg,
     Sum(COALESCE(analytics_session_extended.activity_time, 0)) AS activity_time_sum,
     Sum(COALESCE(analytics_session_extended.idle_time, 0))     AS idle_time_sum,
     Count(*) AS record_count FROM analytics_session_extended
     LEFT JOIN agent_agent ON analytics_session_extended.agent_id = agent_agent.id
     LEFT JOIN agent_application ON analytics_session_extended.application_id =
               agent_application.id LEFT JOIN agent_time ON analytics_session_extended.time_id = agent_time.id
     LEFT JOIN agent_web ON analytics_session_extended.web_data_id = agent_web.id
     LEFT JOIN analytics_filter_events ON analytics_filter_events.event_id = analytics_session_extended.id
     LEFT JOIN analytics_filter ON analytics_filter.id = analytics_filter_events.filter_id
WHERE  ( analytics_session_extended.local_time <='2023-06-14 23:59:59.999000+07:00'
       AND analytics_session_extended.local_time >='2023-06-14 00:00:00+07:00' )
GROUP  BY agent_time.year,
        agent_time.month,
        agent_time.day,
        agent_agent.id,
        agent_agent.guid,
        agent_agent.computer_name,
        analytics_filter.category,
        analytics_filter.name_ru,
        agent_web.site,
        agent_application.app_name,
        agent_time.time_zone,
        agent_time.year,
        month,
        day,
        hour,
        quarter,
        analytics_filter.type
ORDER  BY agent_time.year,
        agent_time.month,
        agent_time.day,
        agent_agent.computer_name,
        start_work_time_min ASC

" , null, [EnableFolding=false])

in
  Source
  1. In the warning window that opens, click Change resolution.

  2. After that, in the Custom Database Query window, click Run.

Ready! Data from iBatyr Monitor is loaded into the PowerBI editor. You can start setting up your visualization!

In case of errors

Verify that the created database user has table view access. To do this, log into the PostgreSQL console on behalf of the created user:

sudo -u postgres psql -d ibatyr -U powerbi

and check the user’s access to the database tables:

\dp

If the command output is empty, exit the PostgreSQL console:

\q

and log into it again, but as an administrator:

sudo -u postgres psql -d ibatyr

After this, run the commands:

ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO powerbi;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO powerbi;

This should be enough to resolve problems with reading data from the database.