-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebhook.py
135 lines (97 loc) · 3.43 KB
/
Webhook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Databricks notebook source
# MAGIC %md
# MAGIC ## Implementing a Webhook
# MAGIC
# MAGIC We're going to implement a couple of notification webhooks using HTTP endpoints and Slack.
# MAGIC
# MAGIC ### Helper Function
# MAGIC
# MAGIC The first thing we're going to do is create a helper function to call MLflow endpoints.
# COMMAND ----------
import mlflow
from mlflow.utils.rest_utils import http_request
import json
def client():
return mlflow.tracking.client.MlflowClient()
host_creds = client()._tracking_client.store.get_host_creds()
host = host_creds.host
token = host_creds.token
def mlflow_call_endpoint(endpoint, method, body='{}'):
if method == 'GET':
response = http_request(
host_creds=host_creds, endpoint="/api/2.0/mlflow/{}".format(endpoint), method=method, params=json.loads(body)
)
else:
response = http_request(
host_creds=host_creds, endpoint="/api/2.0/mlflow/{}".format(endpoint), method=method, json=json.loads(body)
)
return response.json()
# COMMAND ----------
# MAGIC %run ./utilities/WebhookURL
# COMMAND ----------
# MAGIC %md
# MAGIC ### Transition Request Notification
# MAGIC
# MAGIC First, we set up a webhook to notify us whenever a **Model Registry transition request is created**.
# COMMAND ----------
import json
model_name = "BaselineJiji"
trigger_slack = json.dumps({
"model_name": model_name,
"events": ["TRANSITION_REQUEST_CREATED"],
"description": "This notification triggers when a model is requested to be transitioned to a new stage.",
"status": "ACTIVE",
"http_url_spec": {
"url": slack_webhook
}
})
mlflow_call_endpoint("registry-webhooks/create", method = "POST", body = trigger_slack)
# COMMAND ----------
dbutils.widgets.help("combobox")
# COMMAND ----------
# MAGIC %md
# MAGIC #### Transition Notification
# MAGIC
# MAGIC Rather than triggering on a request, this notification will trigger a Slack message when a model is successfully transitioned to a new stage.
# COMMAND ----------
import json
trigger_slack = json.dumps({
"model_name": model_name,
"events": ["MODEL_VERSION_TRANSITIONED_STAGE"],
"description": "This notification triggers when a model is transitioned to a new stage.",
"http_url_spec": {
"url": slack_webhook
}
})
mlflow_call_endpoint("registry-webhooks/create", method = "POST", body = trigger_slack)
# COMMAND ----------
list_model_webhooks = json.dumps({"model_name": model_name})
model_webhooks = mlflow_call_endpoint("registry-webhooks/list", method = "GET", body = list_model_webhooks)
model_webhooks
# COMMAND ----------
# MAGIC %md
# MAGIC You can also **delete webhooks**.
# MAGIC
# MAGIC You can use the below cell to delete webhooks by ID.
# COMMAND ----------
#mlflow_call_endpoint(
# "registry-webhooks/delete",
# method="DELETE",
# body=json.dumps({'id': model_webhooks["webhooks"][0]["id"]})
#)
# COMMAND ----------
# MAGIC %md
# MAGIC Or you can use the below cell to delete all webhooks for a specific model.
# COMMAND ----------
#for webhook in model_webhooks["webhooks"]:
# mlflow_call_endpoint(
# "registry-webhooks/delete",
# method="DELETE",
# body=json.dumps({'id': webhook["id"]})
#)
# COMMAND ----------
# MAGIC %md
# MAGIC And finally, verify that they're all deleted.
# COMMAND ----------
#updated_model_webhooks = mlflow_call_endpoint("registry-webhooks/list", method = "GET", body = list_model_webhooks)
#updated_model_webhooks