Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #956 from chaos-genius/develop
Browse files Browse the repository at this point in the history
release: v0.7.0
  • Loading branch information
Samyak2 authored May 5, 2022
2 parents 31e7dfc + 33fdf1c commit ee1f68b
Show file tree
Hide file tree
Showing 63 changed files with 2,663 additions and 1,032 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ FLASK_DEBUG=0
FLASK_RUN_PORT=5000
SECRET_KEY="t8GIEp8hWmR8y6VLqd6qQCMXzjRaKsx8nRruWNtFuec="
SEND_FILE_MAX_AGE_DEFAULT=31556926
CORS_ENABLED=False

### Database Configuration
DB_HOST=chaosgenius-db
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ ipython_config.py
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat-schedule*
celerybeat.pid

# SageMath parsed files
Expand Down Expand Up @@ -345,3 +345,7 @@ docs/yarn-error.log*
########### Third Party Integrations ##########
.integrations.json
.connectors

########### Gitpod #########################

dump.rdb
10 changes: 10 additions & 0 deletions .gitpod.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM gitpod/workspace-postgres

# Install Redis.
RUN sudo apt-get update && \
sudo apt-get install -y redis-server && \
sudo rm -rf /var/lib/apt/lists/*

ENV PYTHONUSERBASE=/workspace/.pip-modules
ENV PATH=$PYTHONUSERBASE/bin:$PATH
ENV PIP_USER=yes
6 changes: 6 additions & 0 deletions .gitpod.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CHAOSGENIUS_WEBAPP_URL=CHAOSGENIUS_WEBAPP_URL_HERE

DATABASE_URL_CG_DB=postgresql+psycopg2://gitpod@localhost/postgres

CELERY_RESULT_BACKEND=redis://localhost:6379/1
CELERY_BROKER_URL=redis://localhost:6379/1
77 changes: 77 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
image:
file: .gitpod.dockerfile

# List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
tasks:
# modify .bashrc here.
# `PGHOSTADDR` is set to the Postgres server running on Gitpod. pyscopg2 picks up this
# variable and connects to that DB instead of the one we specify through data sources
# for some reason. So we unset this.
# See `DATABASE_URL_CG_DB` in `.gitpod.env` for the credentials to the Postgres server
# running inside Gitpod.
- before: printf 'unset PGHOSTADDR\n' >> $HOME/.bashrc && exit

# the backend server
- name: API Server
init: |
pip install wheel
pip install -r requirements/dev.txt
# notify that backend requirements have finished installing
gp sync-done backend-reqs
command: |
cp .gitpod.env .env.local
# get the URL for port 5000 exposed through Gitpod and use it as the WEBAPP_URL
# TODO: links to "View KPI", etc. won't work since they are on port 3000
sed -i "s~CHAOSGENIUS_WEBAPP_URL_HERE~`gp url 5000`~g" ".env.local"
# start postgres server
pg_start
# apply migrations
flask db upgrade
# notify that backend has been setup completely
gp sync-done backend-setup
bash dev_server.sh
- name: Webapp
init: |
cd frontend
npm install
command: |
cd frontend
# BASE_URL is set to port 5000 exposed through gitpod
REACT_APP_BASE_URL=`gp url 5000` npm start
- name: Redis
command: redis-server

- name: Workers and Scheduler
# TODO: is the await needed here?
init: gp sync-await backend-reqs
command: |
# wait all of backend setup (incl. migrations, env vars) to be completed
gp sync-await backend-setup
bash dev_workers.sh
ports:
# webapp
- port: 3000
onOpen: open-browser
visibility: "public"
# backend server
- port: 5000
visibility: "public"

vscode:
extensions:
- "ms-python.python"
- "samuelcolvin.jinjahtml"

github:
prebuilds:
# add a check to pull requests (defaults to true)
addCheck: false
# add a "Review in Gitpod" button as a comment to pull requests (defaults to false)
addComment: true
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ RUN apt-get update \

COPY requirements /requirements

RUN pip install -r /requirements/prod.txt --no-cache-dir
ARG DEV

RUN pip install -r /requirements/prod.txt ${DEV:+-r /requirements/dev.txt} --no-cache-dir

COPY . .
9 changes: 8 additions & 1 deletion chaos_genius/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ def create_app(config_object="chaos_genius.settings"):
register_shellcontext(app)
register_commands(app)
configure_logger(app)
CORS(app) # TODO: Remove the CORS in v1 release
configure_cors(app)
return app


def configure_cors(app):
"""Configure cross origin request sharing."""
if app.config["CORS_ENABLED"]:
CORS(app)
return None


def register_extensions(app):
"""Register Flask extensions."""
bcrypt.init_app(app)
Expand Down
100 changes: 78 additions & 22 deletions chaos_genius/controllers/alert_controller.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,96 @@
# -*- coding: utf-8 -*-
"""alert controlller."""
from typing import Any, Dict, List, Literal, Optional, Sequence, Tuple, overload

from flask_sqlalchemy import Pagination

from chaos_genius.databases.models.alert_model import Alert

ALERT_CHANNELS = {
"email": "E-mail",
"slack": "Slack",
}
"""A mapping of channel types and a readable name for them."""

def get_alert_list(frequency: str = None, as_obj: bool = False):
"""get the list of the alerts

Args:
frequency (str, optional): schedular frequency for which
alert need to tested. Defaults to None.
as_obj (bool, optional): send the list as Alert object.
Defaults to False.
@overload
def get_alert_list(
frequency: Optional[str] = None,
as_obj: Literal[False] = False,
page_num_size: Optional[None] = None,
extra_filters: Optional[Sequence] = None,
) -> List[Dict[str, Any]]:
...


@overload
def get_alert_list(
frequency: Optional[str] = None,
as_obj: Literal[True] = True,
page_num_size: Optional[None] = None,
extra_filters: Optional[Sequence] = None,
) -> List[Alert]:
...


# TODO: find a way to specify the type inside Pagination i.e, type of Pagination.items
@overload
def get_alert_list(
frequency: Optional[str] = None,
as_obj: bool = False,
page_num_size: Tuple[int, int] = (0, 0),
extra_filters: Optional[Sequence] = None,
) -> Pagination:
...


Returns:
list: List of the alerts
def get_alert_list(
frequency: Optional[str] = None,
as_obj: bool = False,
page_num_size: Optional[Tuple[int, int]] = None,
extra_filters: Optional[Sequence] = None,
):
"""Retrieve all alerts.
Args:
frequency: alert frequency to filter on. If not provided, all
alerts are retrieved.
as_obj: returns a list of Alert objects if True, otherwise a
list of Dicts representing the alerts is returned.
page_num_size: if provided, returns a Pagination object instead of the whole
list. This is a tuple of (page, per_page). The items inside the Pagination
object are either Alert objects or Dicts based on as_obj.
extra_filters: SQLalchemy filters added to the query if provided.
"""
filters = [Alert.alert_status == True]
filters = [Alert.alert_status == True] # noqa: E712
if frequency:
filters.extend([Alert.alert_frequency == frequency])
data = Alert.query.filter(*filters).order_by(Alert.id.desc()).all()
if as_obj:
results = data
if extra_filters is not None:
filters.extend([filter for filter in extra_filters if filter is not None])

query = Alert.query.filter(*filters).order_by(Alert.created_at.desc())

if page_num_size is not None:
page, per_page = page_num_size
alerts_paginated: Pagination = query.paginate(page=page, per_page=per_page)
if not as_obj:
alerts_paginated.items = [alert.as_dict for alert in alerts_paginated.items]
return alerts_paginated
else:
results = [point.as_dict for point in data]
return results
alerts: List[Alert] = query.all()

if not as_obj:
alerts_dict: List[Dict[str, Any]] = [alert.as_dict for alert in alerts]
return alerts_dict

def get_alert_info(id: int):
"""alert info for any given alert id
return alerts

Args:
id (int): alert id
"""
alert = Alert.get_by_id(id)

def get_alert_info(alert_id: int) -> Dict[str, Any]:
"""Retrieve Alert object by ID."""
alert = Alert.get_by_id(alert_id)

if not alert:
raise Exception("Alert ID doesn't exist")
raise Exception(f"Alert with ID {alert_id} does not exist.")
else:
return alert.as_dict
Loading

0 comments on commit ee1f68b

Please sign in to comment.