Last active
December 5, 2024 08:35
-
-
Save razvanphp/0f0f83eb3318b054b0dc7b81b5fa16c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
from datetime import datetime, timedelta, timezone | |
import argparse | |
# This script can be useful when filling your freelancer timesheets, | |
# it generates a heatmap image with the messages/hour/day. | |
# | |
# Input file should be a valid json generated with: | |
# go run ./cmd/slackdump search messages "from:<@U014RHC5U4A> before:2024-04-01 after:2024-02-29" | |
# | |
# See: https://github.com/rusq/slackdump | |
# Parse command line arguments | |
parser = argparse.ArgumentParser(description="Generate a heatmap of Slack messages.") | |
parser.add_argument('filename', type=str, help='The JSON file containing Slack messages') | |
args = parser.parse_args() | |
# Set the timezone directly in the script | |
user_tz = timezone(timedelta(hours=1)) # CET (Central European Time), you can change this as needed | |
# Load JSON data from the file | |
with open(args.filename, 'r') as file: | |
data_list = json.load(file) | |
# Extract messages from all items in the list | |
messages = [] | |
for item in data_list: | |
if 'sm' in item: | |
messages.extend([msg for msg in item['sm'] if msg.get('type') in {'message', 'group', 'im'}]) | |
# Convert timestamps to timezone-aware datetime | |
for message in messages: | |
timestamp = float(message["ts"].split('.')[0]) | |
message["datetime"] = datetime.fromtimestamp(timestamp, tz=timezone.utc).astimezone(user_tz) | |
# Create a DataFrame | |
df = pd.DataFrame(messages) | |
# Extract date and hour from datetime | |
df['date'] = df['datetime'].dt.date | |
df['hour'] = df['datetime'].dt.hour | |
# Pivot table to create heat map data | |
heatmap_data = df.pivot_table(index='date', columns='hour', aggfunc='size', fill_value=0) | |
# Plot the heatmap | |
plt.figure(figsize=(16, 10)) | |
sns.heatmap(heatmap_data, cmap="YlGnBu", linewidths=.5) | |
plt.title("Heatmap of Messages per Day and Hour") | |
plt.xlabel("Hour of Day") | |
plt.ylabel("Date") | |
plt.xticks(rotation=90) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment