From 1433f42dfce3495acd7230ee63d0ca2b66b370e0 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Mon, 8 Mar 2021 16:09:16 -0800 Subject: [PATCH] working on a backend visualization system to proof my idea --- Makefile | 6 +++ app/cli.py | 119 ++++++++++++++++++++++++++++++++++++++++++++ app/factory.py | 4 +- app/models.py | 5 +- docker-compose.yaml | 25 ++++++++++ requirements.txt | 1 + 6 files changed, 155 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index addfb64..5455452 100644 --- a/Makefile +++ b/Makefile @@ -13,3 +13,9 @@ dev: prod: ./bin/prod_app + +logs: + docker-compose logs -f + +init: + ./bin/cmd init diff --git a/app/cli.py b/app/cli.py index 9cae1e2..dd9eeec 100644 --- a/app/cli.py +++ b/app/cli.py @@ -10,3 +10,122 @@ cli_bp = Blueprint('cli', 'cli', cli_group=None) def init(): import app.models db.create_all() + +@cli_bp.cli.command('delete_events') +def delete_events(): + from app.models import WorkEvent + w = WorkEvent.query.all() + for i in w: + print('Deleting', i.id) + db.session.delete(i) + db.session.commit() + +@cli_bp.cli.command('populate_dev_data') +def populate_dev_data(): + import arrow + import namegenerator + from app.models import WorkEvent + from random import randrange, choice + from datetime import datetime, timedelta + options = { + 'types': ['intro_call', 'saod_call', 'scoping_call', 'proposal_work', 'proposal_call'], + 'sas': ['gary', 'dave', 'mark', 'jd'], + 'channels': [namegenerator.gen() for i in range(100)] + } + for cx in options['channels']: + sa = choice(options['sas']) + channel = cx + sc_months = randrange(1, 6) + sc_weeks = sc_months * 4 + if sc_months > 2: + scoping_calls = randrange(2, 4) + else: + scoping_calls = randrange(1, 3) + saods = 1 + proposal_work = randrange(1, 3) + proposal_call = 1 + internal_work = randrange(1, 8) + start_hour = randrange(6, 18) + start_min = randrange(1, 60) + start_month = randrange(1, 8) + start_day = randrange(1, 28) + start_date = datetime(2021, start_month, start_day, start_hour, start_min) + + saod = WorkEvent( + create_date=datetime(2021, start_month, start_day, start_hour + 1, start_min), + slack_channel=channel, + work_type='saod_call', + start_date=start_date, + end_date=datetime(2021, start_month, start_day, start_hour + 1, start_min), + user=sa, + ) + db.session.add(saod) + db.session.commit() + + for i in range(scoping_calls): + new_start = start_date + timedelta(days=7*i) + sc = WorkEvent( + create_date=new_start + timedelta(hours=1), + slack_channel=channel, + work_type='scoping_call', + start_date=new_start, + end_date=new_start + timedelta(hours=1), + user=sa, + ) + db.session.add(sc) + db.session.commit() + + for i in range(proposal_work): + pw_start = start_date + timedelta(days=((7 * scoping_calls) + (3 * i))) + pw = WorkEvent( + create_date=pw_start + timedelta(hours=1), + slack_channel=channel, + work_type='proposal_work', + start_date=pw_start, + end_date=pw_start + timedelta(hours=1), + user=sa, + ) + db.session.add(pw) + db.session.commit() + + pc_date = start_date + timedelta(days=(7 * scoping_calls) + (3 * proposal_work) + 5) + pc = WorkEvent( + create_date=pc_date + timedelta(hours=1), + slack_channel=channel, + work_type='proposal_call', + start_date=pc_date, + end_date=pc_date + timedelta(hours=1), + user=sa, + ) + db.session.add(pc) + db.session.commit() + + # for sa in options['sas']: + # for month in range(1, 12): + # for day in range(1, 28): + # for i in range(0, randrange(0, 5)): + # if i == 5: + # break + # # random number of events per day + # hour = randrange(6, 18) + # min = randrange(1, 60) + # start_date = datetime(2021, month, day, hour, min) + # end_date = datetime(2021, month, day, hour + 1, min) + # channel = choice(options['channels']) + # if start_date.strftime("%A") in ['Saturday', 'Sunday']: + # break + # if channel == 'sa-team': + # work_type = 'internal' + # else: + # work_type = choice(options['types']) + # w = WorkEvent( + # create_date=end_date, + # slack_channel=channel, + # work_type=work_type, + # start_date=start_date, + # end_date=end_date, + # user=sa, + # ) + # db.session.add(w) + # db.session.commit() + # print(f'Added work event {w.id} for {sa} ({work_type}) on {start_date}') diff --git a/app/factory.py b/app/factory.py index 0909085..aaac3cc 100644 --- a/app/factory.py +++ b/app/factory.py @@ -10,8 +10,8 @@ def setup_db(app: Flask): uri = 'postgresql+psycopg2://{user}:{pw}@{host}:{port}/{db}'.format( user=getenv('DB_USER'), pw=getenv('DB_PASS'), - host=getenv('DB_HOST'), - port=getenv('DB_PORT'), + host=getenv('DB_HOST', 'localhost'), + port=getenv('DB_PORT', 5432), db=getenv('DB_NAME') ) app.config['SQLALCHEMY_DATABASE_URI'] = uri diff --git a/app/models.py b/app/models.py index 20c4559..15731e0 100644 --- a/app/models.py +++ b/app/models.py @@ -1,17 +1,16 @@ from datetime import datetime from sqlalchemy import func from app.factory import db -from app import config class WorkEvent(db.Model): __tablename__ = 'work_events' - id = db.Column(db.String(80), primary_key=True) + id = db.Column(db.Integer, primary_key=True) create_date = db.Column(db.DateTime, server_default=func.now()) + user = db.Column(db.String(50)) slack_channel = db.Column(db.String(50)) work_type = db.Column(db.String(50)) - account_idx = db.Column(db.Integer) start_date = db.Column(db.DateTime) end_date = db.Column(db.DateTime) diff --git a/docker-compose.yaml b/docker-compose.yaml index 46621b3..385408a 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -11,3 +11,28 @@ services: POSTGRES_DB: ${DB_NAME:-salesbot} volumes: - ${DATA_DIR:-./data/postgresql}:/var/lib/postgresql/data + metabase-app: + image: metabase/metabase:latest + ports: + - 127.0.0.1:3000:3000 + volumes: + - ${MB_DATA_DIR:-./data/metabase-app}:/metabase-data + environment: + MB_DB_TYPE: postgres + MB_DB_DBNAME: ${MB_DB_NAME:-metabase} + MB_DB_PORT: 5432 + MB_DB_USER: ${MB_DB_USER:-metabase} + MB_DB_PASS: ${MB_DB_PASS:-metabase} + MB_DB_HOST: metabase-db + depends_on: + - metabase-db + links: + - metabase-db + metabase-db: + image: postgres:9.6.15-alpine + environment: + POSTGRES_PASSWORD: ${MB_DB_PASS:-metabase} + POSTGRES_USER: ${MB_DB_USER:-metabase} + POSTGRES_DB: ${MB_DB_NAME:-metabase} + volumes: + - ${MB_DATA_DIR:-./data/metabase-db}:/var/lib/postgresql/data diff --git a/requirements.txt b/requirements.txt index 05f8a93..5ea5dd1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ python-slugify google-api-python-client google-auth-httplib2 google-auth-oauthlib +namegenerator