Skip to content

Instantly share code, notes, and snippets.

@jayozer
Last active March 30, 2023 17:25
Show Gist options
  • Save jayozer/96d42c9b12eecfd2b3d95eb70198ee04 to your computer and use it in GitHub Desktop.
Save jayozer/96d42c9b12eecfd2b3d95eb70198ee04 to your computer and use it in GitHub Desktop.
import looker_sdk
import slack
from datetime import datetime
#load the connection parameters
sdk = looker_sdk.init31("/root/Looker_SDK/Looker.ini")
queries = sdk.all_running_queries() #load all running queries
threshold = 60 * 10 # Threshold for Query termination - currently set for 10mins
results=[]
exceptions=[]
sources_to_exclude = ['PDT Regenerator', 'alerts', 'regenerator'] # job wont terminate queries from these sources
for query in queries:
query_created_at = datetime.strptime(query.created_at.split('.')[0].replace('T',' '),'%Y-%m-%d %H:%M:%S')
source = query.source
running_time = (datetime.utcnow() - query_created_at).total_seconds()
if running_time > threshold and source not in sources_to_exclude:
results.append(query.query.id)
try:
sdk.kill_query(query.query_task_id)
except:
exceptions.append(query.query_task_id)
token='my_slack_token' #Slack token for lookerhealth app
channel='#looker_health' #Name of the Slack channel
# https://your_instance.looker.com/dashboards/489
# Remove square brackets from list: First convert using str() then use list slicing with [1:-1]
# And I realize I have spaces in my list so now - Remove spaces between with replace() simultanously
res = str(results).replace(" ", "")[1:-1]
dash = 'https://your_instance.looker.com/dashboards/489?QueryID={}'.format(res) #using .format to add the queryids to the dashboard link
def post_message_to_slack(channel, text):
if len(results) != 0: #Do not send if text is empty
client = slack.WebClient(token=token)
client.chat_postMessage(channel=channel, text=text)
# Exceptions includes a list of query task ids that failed to run.
# This posts the dash with the 'res' variable as the query filters.
post_message_to_slack(channel, dash)
if exceptions:
message = "Exceptions: {}".format(exceptions)
post_message_to_slack(channel, message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment