Skip to content

Commit

Permalink
Add initial project code
Browse files Browse the repository at this point in the history
  • Loading branch information
mzjp2 committed Feb 7, 2021
0 parents commit ab189e8
Show file tree
Hide file tree
Showing 15 changed files with 1,647 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Lint and test

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install poetry && poetry install
- name: Run linters
run: poetry run make lint
- name: Run tests
run: poetry run make test
53 changes: 53 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class


# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
.coverage
.coverage.*
.cache
.pytest_cache/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# VSCode
.vscode/

# Mac OSX
.DS_Store
32 changes: 32 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- id: check-merge-conflict
- id: debug-statements
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: local
hooks:
- id: isort
name: Sort imports
types: [file, python]
language: system
entry: isort
- id: pylint-src
name: PyLint on kedro_local_notify/*
types: [file, python]
language: system
files: ^kedro_local_notify
entry: pylint
- id: pylint-test
name: PyLint on tests/*
types: [file, python]
language: system
files: ^tests
entry: pylint --disable=missing-docstring,unused-variable
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/Users/zain_patel/code/kedro/.env/ked-notify/bin/python"
}
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
lint:
pre-commit run --hook-stage manual --all-files

test:
pytest

clean:
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -exec rm -r {} \+ -o -type d -name '.pytest_cache' -exec rm -r {} \+ -o -type d -name dist -exec rm -r {} \+
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
![kedro-local-notify-logo](static/logo.png)

# kedro-local-notify

![github-action](https://github.com/mzjp2/kedro-local-notify/workflows/Lint%20and%20test/badge.svg)
![code-style](https://img.shields.io/badge/code%20style-black-000000.svg)
![license](https://img.shields.io/badge/License-MIT-green.svg)

## How do I get started?

```console
$ pip install --upgrade kedro-local-notify
```

### Then what?

Nothing! Kedro will automagically pick up the hook and ping you a notification after the pipeline finished running succesfully or fails.

## What is `kedro-local-notify`?

Ever kicked off a long-running pipeline and come back to check in an hour later, only to find that it failed 2 minutes in?

Or come back to see your pipeline finished running an hour ago and you have nothing to justify your reddit browsing anymore?

`kedro-local-notify` will ping you a notification indicating that your pipeline ran sucessfully or failed.

![kedro-local-notify-demo](static/demo.png)

## Won't this spam me?

By default, notifications will only trigger if the pipeline has been running for more than 1 minute. You can change this thresholf for notifying you by setting the `KEDRO_LOCAL_NOTIFY_THRESHOLD` environment variable to be the number of seconds of pipeline run time before a notification is trigerred. The default is:

```console
$ export KEDRO_LOCAL_NOTIFY=60
```

note that this environment variable needs to be set in the same shell that you're trigerring the Kedro pipeline run in.

## Caveats

You probably shouldn't add this to your requirements. It's a silly little tool meant to be used for some quality of life improvements on your local machine.

This is currently limited to Mac OS X 10.10 or higher. Windows support is in the works!
3 changes: 3 additions & 0 deletions kedro_local_notify/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""kedro-local-notify"""

__version__ = "0.1.0"
67 changes: 67 additions & 0 deletions kedro_local_notify/hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""The hook definitions for kedro-local-notify"""
import os
import time
from pathlib import Path
from typing import Any, Dict

import pync
from kedro.framework.hooks import hook_impl
from kedro.pipeline import Pipeline

APP_ICON = Path(__file__).parent / "static" / "kedro.png"
NOTIFICATION_THRESHOLD = int(os.getenv("KEDRO_LOCAL_NOTIFY_THRESHOLD", "60"))


class KedroLocalNotifyHooks:
"""The hook class for kedro local notify.
Implements the pipeline error and run hooks.
"""

def __init__(self):
self._start_time = None

@hook_impl
def before_pipeline_run(self):
"""Starts the pipeline run timer"""
self._start_time = time.time()

@hook_impl
def on_pipeline_error(self, error, run_params: Dict[str, Any]):
"""Triggers the pipeline error notifcation"""
self._kedro_notify(
run_params=run_params,
subtitle="❌ Failed",
message=f"failed with error {error}",
)

@hook_impl
def after_pipeline_run(self, run_params: Dict[str, Any], pipeline: Pipeline):
"""Triggers the pipeline successful run notification"""
num_nodes = len(pipeline.nodes)
self._kedro_notify(
run_params=run_params,
subtitle="✅ Finished",
message=f"finished sucesfully with {num_nodes} nodes executed",
)

def _kedro_notify(self, subtitle: str, run_params: Dict[str, Any], message: str):
pipeline_run_time = time.time() - self._start_time
if pipeline_run_time < NOTIFICATION_THRESHOLD:
return

pipeline_name = run_params.get("pipeline_name")
if pipeline_name:
message = f"Your pipeline {pipeline_name}: {message}"
else:
message = f"Your pipeline: {message}"

pync.notify(
title="Kedro",
subtitle=f"{subtitle} in {pipeline_run_time:.2f}s",
message=message,
appIcon=str(APP_ICON),
)


hooks = KedroLocalNotifyHooks()
Binary file added kedro_local_notify/static/kedro.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ab189e8

Please sign in to comment.