by Lazy Sloth
Send a daily report of some metrics from BigQuery to Slack
import logging
import time
from typing import Dict, Any
from abilities import run_query_on_bigquery
import os
import slack_sdk
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from datetime import datetime, timedelta
from tabulate import tabulate
import requests
import json
import pytz
from apscheduler.schedulers.background import BackgroundScheduler
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)
#TODO replace the query and title
TITLE = "Example title"
QUERY = """
your query goes here
"""
Frequently Asked Questions
What business problem does this daily report app solve?
This daily report app addresses the need for regular, automated data insights. By sending a daily report of metrics from BigQuery to Slack, it ensures that teams have consistent access to important data without manual intervention. This can be particularly useful for tracking key performance indicators (KPIs), monitoring business trends, or keeping stakeholders informed about critical metrics on a daily basis.
How can this template be customized for different business needs?
The daily report app is highly customizable to suit various business needs. You can modify the SQL query in the QUERY
variable to fetch different metrics from BigQuery that are relevant to your business. Additionally, you can adjust the TITLE
to reflect the specific metrics being reported. The scheduling can also be modified to send reports at different times or frequencies by changing the parameters in the scheduler.add_job()
function.
What are some potential applications of this daily report app across different industries?
This daily report app can be applied across various industries: - E-commerce: Daily sales figures, conversion rates, or inventory levels - Finance: Daily market performance, risk metrics, or transaction volumes - Healthcare: Patient admission rates, bed occupancy, or treatment outcomes - Marketing: Campaign performance, engagement rates, or lead generation metrics - Manufacturing: Production output, quality control metrics, or supply chain efficiency
How can I modify the app to send reports to multiple Slack channels?
To send reports to multiple Slack channels, you can modify the send_metrics()
function to accept a list of channels. Here's an example of how you could implement this:
```python def send_metrics(channels): # ... (existing code to fetch and format data)
for channel in channels:
send_data_to_slack(f"Metrics for {formatted_date}:", channel)
send_data_to_slack(TITLE, channel)
send_data_to_slack(table, channel)
# In the main function: channels = ["#channel1", "#channel2", "#channel3"] scheduler.add_job(send_metrics, 'cron', hour=15, args=[channels]) ```
This modification allows the daily report app to send the same report to multiple Slack channels simultaneously.
Can the app be extended to include data visualizations along with the tabular data?
Yes, the daily report app can be extended to include data visualizations. You can use libraries like matplotlib or seaborn to create charts or graphs based on the data from BigQuery. Here's a basic example of how you might add a bar chart to the report:
```python import matplotlib.pyplot as plt import io
def create_bar_chart(data): plt.figure(figsize=(10, 6)) plt.bar(data['column1'], data['column2']) plt.title('Your Chart Title') plt.xlabel('X-axis Label') plt.ylabel('Y-axis Label')
img_buffer = io.BytesIO()
plt.savefig(img_buffer, format='png')
img_buffer.seek(0)
return img_buffer
def send_metrics(): # ... (existing code)
# Create and send chart
chart = create_bar_chart(data)
client.files_upload(
channels=SLACK_CHANNEL,
file=chart,
filename="daily_chart.png",
title="Daily Metrics Chart"
)
```
This addition to the daily report app would create a bar chart from the BigQuery data and send it to the Slack channel along with the tabular data, providing a more comprehensive and visually appealing report.
Created: | Last Updated:
Introduction to the Daily BigQuery Report to Slack Template
Welcome to the Daily BigQuery Report to Slack Template! This template is designed to help you automate the process of fetching data from BigQuery, formatting it into an easy-to-read table, and sending this report to a Slack channel of your choice. This is particularly useful for teams who need to keep track of key metrics and want to receive daily updates without manual intervention.
Getting Started with the Template
To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will set up the template in your Lazy Builder interface, ready for customization and use.
Initial Setup: Adding Environment Secrets
Before you can use this template, you'll need to set up a couple of environment secrets that the code requires to function properly:
- SLACK_API_TOKEN: Your Slack API token which allows the app to post messages to your Slack channel.
- SLACK_CHANNEL: The ID of the Slack channel where you want the reports to be sent.
To add these environment secrets, navigate to the Environment Secrets tab within the Lazy Builder and enter the key-value pairs for the SLACK_API_TOKEN and SLACK_CHANNEL.
Test: Pressing the Test Button
Once you have configured the environment secrets, it's time to test the app to ensure everything is working as expected. Press the "Test" button in the Lazy Builder. This will deploy the app and launch the Lazy CLI.
Entering Input
If the template code requires any user input through the CLI, you will be prompted to provide it after pressing the Test button. In this template, there is no need for additional user input as the query and Slack channel are already defined in the code and environment secrets.
Using the App
There is no frontend experience for this app as its primary function is to send data to Slack. Once the app is running, it will automatically perform its task without further interaction.
Integrating the App
After testing the app and confirming that the reports are being sent to your Slack channel, you may want to customize the QUERY and TITLE in the code to match the specific data you wish to report on. Replace the placeholder text in the QUERY constant with your actual BigQuery SQL query and update the TITLE constant with a descriptive title for your report.
Remember, you can always modify the scheduler settings in the code if you need the report to be sent at a different time or with a different frequency. The scheduler is currently set to trigger the send_metrics function every day at 10 am UK time.
If you need to refer to any documentation or require assistance, please use the links provided in the code comments or reach out to the Lazy customer support for further help.
That's it! You're now ready to receive daily BigQuery reports in your Slack channel, all automated through your Lazy app.
Template Benefits
-
Automated Daily Reporting: This template automates the process of generating and distributing daily reports, saving time and ensuring consistent delivery of important metrics to stakeholders.
-
Data-Driven Decision Making: By providing daily updates on key metrics from BigQuery, this solution enables teams to make informed, data-driven decisions quickly and efficiently.
-
Improved Team Communication: Posting reports directly to Slack enhances team communication and keeps everyone aligned on important business metrics without the need for manual updates or meetings.
-
Customizable Insights: The template allows for easy customization of the SQL query, enabling businesses to focus on the most relevant metrics for their specific needs and goals.
-
Scalable Reporting Solution: As a programmatic solution, this template can be easily scaled to handle multiple reports, different data sources, or additional Slack channels, making it adaptable to growing business needs.