Tuesday 9 June 2020

Creating Notion Table/Database 


This is 2 of 3 part series discussing automation of generating on call summary of PagerDuty incidents into a Notion page. In this blog we will discuss about creating a Notion page with a database that will show the alert summary. Part 1 on this series can be found here.

PagerDuty is designed to alert clients to disruptions and outages through machine learning and automation. Notion is a collaboration platform with markdown support that integrates kanban boards, tasks, wikis, and databases.

When I write this blog, there is still no official API support provided by Notion. More details here

But don’t worry, there is an unofficial python API - https://pypi.org/project/notion/. This provides all necessary APIs we need. One caveat is that since there is no official API support yet, there is no standard way of obtaining API token. However, if you are logged into Notion, you can find this (token_v2) by inspecting the browser.

Once you obtain the token, we are ready to create a new Notion page.

client = NotionClient(token_v2=“XXXXXXXXXXXXXXXXX")
page = client.get_block("https://www.notion.so/yourorg/OnCallSummary-wfhwjfwcnccwlrhfow2486r9wn")
page2 = page.children.add_new(notion.block.PageBlock, icon="\U0001F9EF")

This will create a new page. Let’s now set the page title to today’s date.

page2.title = str(datetime.today().strftime('%Y-%m-%d'))

It’s time to create a table block inside the new page.

new_table = page2.children.add_new(notion.block.CollectionViewBlock)
new_table.collection = client.get_collection(
client.create_record("collection", parent=new_table, schema=get_collection_schema())
)
new_table.views.add_new(view_type="table")

where get_collection_schema() corresponds to: 

def get_collection_schema():
return {
"count": {"name": "Count", "type": "number"},
"action": {"name": "Remedies taken", "type": "text"},
"title": {"name": "Alert Name", "type": "title"},
"mttr": {"name": "MTTR", "type": "text"},
"notes": {"name": "Other Notes", "type": "text"},
"runbook": {"name": "Runbook", "type": "url"},
"=d{|": {
"name": "Service",
"type": "select",
"options": [
{
"color": "green",
"id": "695667ab-c776-43d1-3420-27f5611fcaty",
"value": "Service 1",
},
{
"color": "yellow",
"id": “452c7016-ef57-445a-90a6-64afadfb042d",
"value": "Service 2",
},
],
},
}

We have a blank table created. Next step is to populate data. (incidentSummary dict is generated as part of pulling incidents from PagerDuty)

    total_alerts = 0
    for alert in incidentSummary:
        row = new_table.collection.add_row()
        row.alert = str(alert)
        row.count = (incidentSummary.get(alert).get('count'))
        total_alerts = total_alerts + incidentSummary.get(alert).get('count')
        mttr_min = round((incidentSummary.get(alert).get('time')/incidentSummary.get(alert).get('count'))/60)
        if mttr_min > 59:
            mttr_hrs = mttr_min/60
            if mttr_hrs >= 24:
                row.mttr = str(round(mttr_hrs/24)) + " days"
            else:
                row.mttr = str(mttr_hrs) + " hrs"
        else:
            row.mttr = str(mttr_min) + " min"
        row.service = str(incidentSummary.get(alert).get('service'))
    new_table.title = "Total Alerts - " + str(total_alerts) 


and Voila! 

Complete application code can be found in my GitHub. In the next part we will discuss about scheduling this application as a K8 CronJob.

No comments:

Post a Comment