# Create monitoring views

## 1. Monitor user events

### 1.1 Setup a connection to KAWA's database from the KAWA GUI

From the datasource section, create a new data source: (+ Data Source) > (Connect to an external system) > (+ Create new connection)

Pick the postgres connector:

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-940a980329d980f031bd5dadf92ea53eadf2c3d2%2Fexploitation2.png?alt=media" alt=""></div>

Then, configure the connection to the KAWA store (The configuration is present in KAWA server configuration, in the *POSTGRES JDBC URL*). Click on Test Connection and then Create.

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-2b581f9fb69ac81af126847ec73f658486d39444%2Fexploitation1.png?alt=media" alt=""></div>

*The above screenshot contains some example parameters*.

### 1.2 Create a new datasource to load application events

Making sure that you selected your new connection (here KAWA), click on the Custom SQL query option.

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-2c5efaeac354e4ba9b6c9a5cee6bc001d75e91ed%2Fexploitation3.png?alt=media" alt=""></div>

Then, copy paste the following query in the text area (feel free to adapt it to your needs).

```sql
-- Query to load events and associated user information
SELECT
    E.event_id,
    upper(E.event_name) as event_name,
    upper(E.event_group) as event_group,
    upper(E.event_context_id) as event_context_id,
    E.event_date,
    E.event_correlated_entity_id,
    E.event_principal_id,
    E.event_correlated_entity_type,
    -- E.event_payload::text as payload, (OPTIONAL for more detailed information)
    P.display_name,
    P.payload->>'email' as email,
    P.payload->>'uniqueId' as uniqueId,
    P.payload->>'role' as role


FROM kawa.application_event as E
    LEFT JOIN kawa.entity_principal as P 
    ON E.event_principal_id = P.entity_id::text

```

CLick on (Preview data) > (Next) > (Save).

Input a name for your datasource: **Application Events** then click on (Save and Run).

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-ccd4fa1327cc4e090317af9317e9b0bc32c0b027%2Fexploitation4.png?alt=media" alt=""></div>

Once your data is ready, feel free to schedule it:

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-db336d412efddc4d5cf7152ca89cc03968118d15%2Fexploitation5.png?alt=media" alt=""></div>

*On this example, we schedule a full refresh of this data every 10 minutes. Feel free to adjust the frequency to your requirements.*

### 1.3 Create some charts and views to visualize this data

Below are a few examples of views that you can do to visualize the data:

*Chart to show number of interactions per user and command name:*

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-4d66c0673e023287d46defff6c01ef2d6ee1776f%2Fexploitation6.png?alt=media" alt=""></div>

*Chart to show when users interact with the platform:*

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-554c7fbc305a41bc334975231f5ac71de5a6f31e%2Fexploitation7.png?alt=media" alt=""></div>

*Chart that shows which show the evolution of events per type*:

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-e1fc247e60e3dbc28e748a17542f3debcb59687d%2Fexploitation8.png?alt=media" alt=""></div>

## 2. Monitor script execution

In order to monitor script execution, please follow the steps described in 1.1 to connect to KAWA's database.

### 2.1 Create a new datasource to load the script monitoring table

Making sure that you selected your new connection (here KAWA), click on the Custom SQL query option.

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-2c5efaeac354e4ba9b6c9a5cee6bc001d75e91ed%2Fexploitation3.png?alt=media" alt=""></div>

Then, copy paste the following query in the text area (feel free to adapt it to your needs).

```sql
SELECT
    JOB.job_id,
    JOB.status,
    JOB.date_created,
    JOB.date_updated,
    ROUND(EXTRACT(EPOCH FROM (JOB.date_updated - JOB.date_created)))::int AS duration_seconds,
    SCRIPT.display_name,
    SCRIPT.workspace_id
FROM kawa.python_jobs AS JOB
    LEFT JOIN kawa.entity_script as SCRIPT ON JOB.script_id = SCRIPT.entity_id
```

CLick on (Preview data) > (Next) > (Save).

Input a name for your datasource: Application Events then click on (Save and Run).

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-eeffc333f5e66782a43b79d49da1f479542f684c%2Fexploitation0.png?alt=media" alt=""></div>

Like for the events, feel free to schedule the ETL to run automatically.

### 2.2 Create some charts and views to visualize this data

*Boxplot chart to show script execution time*:

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-fec43ffba28e2161c5b05eb5c3b454cd0e0a6d2b%2Fexploitation9.png?alt=media" alt=""></div>

*Boxplot chart to show script status over time*:

<div data-with-frame="true"><img src="https://2701963830-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6wXylCyyATfFd8yjjWTn%2Fuploads%2Fgit-blob-a30da3bb3c5616d94f5760ce37d5bbbf811e5bf0%2Fexploitation10.png?alt=media" alt=""></div>
