from sfm.routes.work_items import crud

# from sfm.database import create_db_and_tables
from sfm.routes.projects import crud as proj_crud
from sfm.routes.commits import crud as commit_crud
from sfm.dependencies import get_db
from sfm.models import WorkItemCreate, ProjectCreate, CommitCreate
from sqlmodel import SQLModel, Session
from fastapi import Depends
from sfm.database import engine
from datetime import datetime, timedelta
from sfm.config import get_settings
from opencensus.ext.azure.log_exporter import AzureLogHandler
import string
import random
from sfm.logger import create_logger


app_settings = get_settings()


logger = create_logger(__name__)


def random_sha(seed):  # pragma: no cover
    N = 20
    random.seed(a=seed)
    res = "".join(random.choices(string.ascii_uppercase + string.digits, k=N))
    return res


def populate_db(
    db: Session = Depends(get_db),
):  # pragma: no cover
    """
    ## Populate Local Database to house Mock Data

    Calling this endpoint creates a database at SFM/src/backend/sfm/issues.db. This database is set in .env and created in database.py. Sample work items, projects, and commits are generated to be used in metrics testing. A date offset can be manually set on the mock data to allow the dates of the data to be shifted nearer the current date for a more realistic mock data set.

    """
    time_shift = timedelta(days=32)

    # fIRST PROJECT: Create project to file deployments under:
    project_dict = {
        "name": "Project for Deployments Testing",
        "lead_name": "Gabe",
        "description": "Project to hold deployment frequency data for local testing",
        "location": "Indianapolis",
        "repo_url": "a_sample_url",
        "on_prem": False,
    }

    proj_data = ProjectCreate(**project_dict)
    [project, project_auth_token] = proj_crud.create_project(
        db, proj_data, admin_key="admin_key"
    )

    # Creates the deployment items:
    dates = [
        datetime(2021, 5, 31),  # Week 1
        datetime(2021, 6, 7),  # Week 2
        datetime(2021, 6, 12),
        datetime(2021, 6, 14),  # Week 3
        datetime(2021, 6, 16),
        datetime(2021, 6, 18),
        datetime(2021, 6, 21),  # Week 4
        datetime(2021, 6, 22),
        datetime(2021, 6, 25),
        datetime(2021, 6, 26),
        datetime(2021, 6, 28),  # Week 5
        datetime(2021, 6, 29),
        datetime(2021, 6, 30),
        datetime(2021, 7, 5),  # Week 6
        datetime(2021, 7, 8),
        datetime(2021, 7, 12),  # Week 7
        datetime(2021, 7, 14),
        datetime(2021, 7, 16),
        datetime(2021, 7, 19),  # Week 8
        datetime(2021, 7, 21),
        datetime(2021, 7, 26),  # Week 9
        datetime(2021, 7, 27),
        datetime(2021, 7, 29),
        datetime(2021, 7, 30),
        datetime(2021, 8, 2),  # Week 10
        datetime(2021, 8, 9),  # Week 11
        datetime(2021, 8, 11),
        datetime(2021, 8, 11),  # purposeful duplicate day
        datetime(2021, 8, 12),
        datetime(2021, 8, 13),
        datetime(2021, 8, 16),  # Week 12
        datetime(2021, 8, 18),
        datetime(2021, 8, 19),
    ]

    for date in dates:
        deployment_dict = {
            "category": "Deployment",
            "end_time": date
            + time_shift,  # ADDED TIME DELTA SHIFT TO BRING CLOSER TO CURRENT DATE
            "project_id": project.id,
        }

        work_item_data = WorkItemCreate(**deployment_dict)
        crud.create_work_item(db, work_item_data, project_auth_token)

    # SECOND PROJECT
    project_dict2 = {
        "name": "2nd Project for testing",
        "lead_name": "Gabe",
        "description": "Second project",
        "location": "Indianapolis",
        "repo_url": "a_different_sample_url",
        "on_prem": False,
    }

    proj_data2 = ProjectCreate(**project_dict2)
    [project2, project_auth_token2] = proj_crud.create_project(
        db, proj_data2, admin_key="admin_key"
    )

    # Creates the deployment items:
    dates2 = [
        datetime(2021, 5, 31),  # Week 1
        datetime(2021, 6, 7),  # Week 2
        datetime(2021, 8, 4),  # Week 10
        datetime(2021, 8, 10),  # Week 11
        datetime(2021, 8, 11),
        datetime(2021, 8, 16),  # Week 12
    ]

    for date in dates2:
        deployment_dict = {
            "category": "Deployment",
            "end_time": date
            + time_shift,  # ADDED TIME DELTA SHIFT TO BRING CLOSER TO CURRENT DATE
            "project_id": project2.id,
        }

        work_item_data = WorkItemCreate(**deployment_dict)
        crud.create_work_item(db, work_item_data, project_auth_token2)

    # Create Pull Request Items
    pull_dates = [
        datetime(2021, 8, 11),
        datetime(2021, 8, 19),
    ]

    pull_req_work_items = []
    project_ids = [project.id, project2.id]
    # print(project_ids)
    project_auth_tokens = [project_auth_token, project_auth_token2]
    # print(project_auth_tokens)
    proj_iter = 0
    for date in pull_dates:
        pull_dict = {
            "category": "Pull Request",
            "end_time": date
            + time_shift,  # ADDED TIME DELTA SHIFT TO BRING CLOSER TO CURRENT DATE
            "project_id": project_ids[proj_iter],
        }
        work_item_data = WorkItemCreate(**pull_dict)
        work_item_id = crud.create_work_item(
            db, work_item_data, project_auth_tokens[proj_iter]
        )
        pull_req_work_items.append(work_item_id)
        proj_iter += 1

    commit_dates = [
        datetime(2021, 8, 8),
        datetime(2021, 8, 9),
        datetime(2021, 8, 10),
        datetime(2021, 8, 11),
    ]

    i = 0
    proj_iter = 0
    for item_id in pull_req_work_items:
        for date in commit_dates:
            commit_dict = {
                "sha": random_sha(i),
                "date": date + time_shift,
                "author": "<NAME>",
                "work_item_id": item_id,
            }

            commit_data = CommitCreate(**commit_dict)
            commit_crud.create_commit(db, commit_data, project_auth_tokens[proj_iter])
            i += 1
        proj_iter += 1

    all_projects = proj_crud.get_all(db)

    if len(all_projects) != 2:
        logger.debug(
            "Incorrect number of project present. Clear database and rerun.",
        )
        return "Incorrect number of projects present. Clear database and rerun."

    else:
        pass

    all_work_items = crud.get_all(db)

    if len(all_work_items) != (len(dates) + len(dates2) + len(pull_dates)):
        logger.debug(
            "Incorrect number of work items present. Clear database and rerun.",
        )
        return "Incorrect number of work items present. Clear database and rerun."
    else:
        pass

    return (
        "Successfully created local database. Project Auth tokens are:",
        project_auth_token,
        project_auth_token2,
    )


# TODO: Turn off this endpoint before deploying to production
def clear_db(db=Depends(get_db)):  # pragma: no cover

    """
    ## Clear Local Database

    Calling this endpoint drops all entries from all tables present in the local issues.db database.

    """
    logger.warning("Database cleared")
    SQLModel.metadata.drop_all(engine)
    SQLModel.metadata.create_all(engine)
    # create_db_and_tables()
    return "Database cleared"

sfm.routes.work_items.crud.create_work_item random.seed sqlmodel.SQLModel.metadata.create_all datetime.datetime sfm.routes.commits.crud.create_commit sfm.config.get_settings sqlmodel.SQLModel.metadata.drop_all sfm.routes.work_items.crud.get_all fastapi.Depends sfm.logger.create_logger random.choices sfm.models.CommitCreate sfm.models.ProjectCreate sfm.models.WorkItemCreate datetime.timedelta sfm.routes.projects.crud.get_all sfm.routes.projects.crud.create_project
[((612, 626), 'sfm.config.get_settings', 'get_settings', ([], {}), '()\n', (624, 626), False, 'from sfm.config import get_settings\n'), ((638, 661), 'sfm.logger.create_logger', 'create_logger', (['__name__'], {}), '(__name__)\n', (651, 661), False, 'from sfm.logger import create_logger\n'), ((721, 740), 'random.seed', 'random.seed', ([], {'a': 'seed'}), '(a=seed)\n', (732, 740), False, 'import random\n'), ((872, 887), 'fastapi.Depends', 'Depends', (['get_db'], {}), '(get_db)\n', (879, 887), False, 'from fastapi import Depends\n'), ((1375, 1393), 'datetime.timedelta', 'timedelta', ([], {'days': '(32)'}), '(days=32)\n', (1384, 1393), False, 'from datetime import datetime, timedelta\n'), ((1766, 1795), 'sfm.models.ProjectCreate', 'ProjectCreate', ([], {}), '(**project_dict)\n', (1779, 1795), False, 'from sfm.models import WorkItemCreate, ProjectCreate, CommitCreate\n'), ((1832, 1894), 'sfm.routes.projects.crud.create_project', 'proj_crud.create_project', (['db', 'proj_data'], {'admin_key': '"""admin_key"""'}), "(db, proj_data, admin_key='admin_key')\n", (1856, 1894), True, 'from sfm.routes.projects import crud as proj_crud\n'), ((3805, 3835), 'sfm.models.ProjectCreate', 'ProjectCreate', ([], {}), '(**project_dict2)\n', (3818, 3835), False, 'from sfm.models import WorkItemCreate, ProjectCreate, CommitCreate\n'), ((3874, 3937), 'sfm.routes.projects.crud.create_project', 'proj_crud.create_project', (['db', 'proj_data2'], {'admin_key': '"""admin_key"""'}), "(db, proj_data2, admin_key='admin_key')\n", (3898, 3937), True, 'from sfm.routes.projects import crud as proj_crud\n'), ((6133, 6154), 'sfm.routes.projects.crud.get_all', 'proj_crud.get_all', (['db'], {}), '(db)\n', (6150, 6154), True, 'from sfm.routes.projects import crud as proj_crud\n'), ((6424, 6440), 'sfm.routes.work_items.crud.get_all', 'crud.get_all', (['db'], {}), '(db)\n', (6436, 6440), False, 'from sfm.routes.work_items import crud\n'), ((6967, 6982), 'fastapi.Depends', 'Depends', (['get_db'], {}), '(get_db)\n', (6974, 6982), False, 'from fastapi import Depends\n'), ((7196, 7230), 'sqlmodel.SQLModel.metadata.drop_all', 'SQLModel.metadata.drop_all', (['engine'], {}), '(engine)\n', (7222, 7230), False, 'from sqlmodel import SQLModel, Session\n'), ((7235, 7271), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (7263, 7271), False, 'from sqlmodel import SQLModel, Session\n'), ((759, 818), 'random.choices', 'random.choices', (['(string.ascii_uppercase + string.digits)'], {'k': 'N'}), '(string.ascii_uppercase + string.digits, k=N)\n', (773, 818), False, 'import random\n'), ((1968, 1989), 'datetime.datetime', 'datetime', (['(2021)', '(5)', '(31)'], {}), '(2021, 5, 31)\n', (1976, 1989), False, 'from datetime import datetime, timedelta\n'), ((2009, 2029), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(7)'], {}), '(2021, 6, 7)\n', (2017, 2029), False, 'from datetime import datetime, timedelta\n'), ((2049, 2070), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(12)'], {}), '(2021, 6, 12)\n', (2057, 2070), False, 'from datetime import datetime, timedelta\n'), ((2080, 2101), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(14)'], {}), '(2021, 6, 14)\n', (2088, 2101), False, 'from datetime import datetime, timedelta\n'), ((2121, 2142), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(16)'], {}), '(2021, 6, 16)\n', (2129, 2142), False, 'from datetime import datetime, timedelta\n'), ((2152, 2173), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(18)'], {}), '(2021, 6, 18)\n', (2160, 2173), False, 'from datetime import datetime, timedelta\n'), ((2183, 2204), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(21)'], {}), '(2021, 6, 21)\n', (2191, 2204), False, 'from datetime import datetime, timedelta\n'), ((2224, 2245), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(22)'], {}), '(2021, 6, 22)\n', (2232, 2245), False, 'from datetime import datetime, timedelta\n'), ((2255, 2276), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(25)'], {}), '(2021, 6, 25)\n', (2263, 2276), False, 'from datetime import datetime, timedelta\n'), ((2286, 2307), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(26)'], {}), '(2021, 6, 26)\n', (2294, 2307), False, 'from datetime import datetime, timedelta\n'), ((2317, 2338), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(28)'], {}), '(2021, 6, 28)\n', (2325, 2338), False, 'from datetime import datetime, timedelta\n'), ((2358, 2379), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(29)'], {}), '(2021, 6, 29)\n', (2366, 2379), False, 'from datetime import datetime, timedelta\n'), ((2389, 2410), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(30)'], {}), '(2021, 6, 30)\n', (2397, 2410), False, 'from datetime import datetime, timedelta\n'), ((2420, 2440), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(5)'], {}), '(2021, 7, 5)\n', (2428, 2440), False, 'from datetime import datetime, timedelta\n'), ((2460, 2480), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(8)'], {}), '(2021, 7, 8)\n', (2468, 2480), False, 'from datetime import datetime, timedelta\n'), ((2490, 2511), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(12)'], {}), '(2021, 7, 12)\n', (2498, 2511), False, 'from datetime import datetime, timedelta\n'), ((2531, 2552), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(14)'], {}), '(2021, 7, 14)\n', (2539, 2552), False, 'from datetime import datetime, timedelta\n'), ((2562, 2583), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(16)'], {}), '(2021, 7, 16)\n', (2570, 2583), False, 'from datetime import datetime, timedelta\n'), ((2593, 2614), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(19)'], {}), '(2021, 7, 19)\n', (2601, 2614), False, 'from datetime import datetime, timedelta\n'), ((2634, 2655), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(21)'], {}), '(2021, 7, 21)\n', (2642, 2655), False, 'from datetime import datetime, timedelta\n'), ((2665, 2686), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(26)'], {}), '(2021, 7, 26)\n', (2673, 2686), False, 'from datetime import datetime, timedelta\n'), ((2706, 2727), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(27)'], {}), '(2021, 7, 27)\n', (2714, 2727), False, 'from datetime import datetime, timedelta\n'), ((2737, 2758), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(29)'], {}), '(2021, 7, 29)\n', (2745, 2758), False, 'from datetime import datetime, timedelta\n'), ((2768, 2789), 'datetime.datetime', 'datetime', (['(2021)', '(7)', '(30)'], {}), '(2021, 7, 30)\n', (2776, 2789), False, 'from datetime import datetime, timedelta\n'), ((2799, 2819), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(2)'], {}), '(2021, 8, 2)\n', (2807, 2819), False, 'from datetime import datetime, timedelta\n'), ((2840, 2860), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(9)'], {}), '(2021, 8, 9)\n', (2848, 2860), False, 'from datetime import datetime, timedelta\n'), ((2881, 2902), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(11)'], {}), '(2021, 8, 11)\n', (2889, 2902), False, 'from datetime import datetime, timedelta\n'), ((2912, 2933), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(11)'], {}), '(2021, 8, 11)\n', (2920, 2933), False, 'from datetime import datetime, timedelta\n'), ((2971, 2992), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(12)'], {}), '(2021, 8, 12)\n', (2979, 2992), False, 'from datetime import datetime, timedelta\n'), ((3002, 3023), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(13)'], {}), '(2021, 8, 13)\n', (3010, 3023), False, 'from datetime import datetime, timedelta\n'), ((3033, 3054), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(16)'], {}), '(2021, 8, 16)\n', (3041, 3054), False, 'from datetime import datetime, timedelta\n'), ((3075, 3096), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(18)'], {}), '(2021, 8, 18)\n', (3083, 3096), False, 'from datetime import datetime, timedelta\n'), ((3106, 3127), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(19)'], {}), '(2021, 8, 19)\n', (3114, 3127), False, 'from datetime import datetime, timedelta\n'), ((3412, 3445), 'sfm.models.WorkItemCreate', 'WorkItemCreate', ([], {}), '(**deployment_dict)\n', (3426, 3445), False, 'from sfm.models import WorkItemCreate, ProjectCreate, CommitCreate\n'), ((3454, 3515), 'sfm.routes.work_items.crud.create_work_item', 'crud.create_work_item', (['db', 'work_item_data', 'project_auth_token'], {}), '(db, work_item_data, project_auth_token)\n', (3475, 3515), False, 'from sfm.routes.work_items import crud\n'), ((4012, 4033), 'datetime.datetime', 'datetime', (['(2021)', '(5)', '(31)'], {}), '(2021, 5, 31)\n', (4020, 4033), False, 'from datetime import datetime, timedelta\n'), ((4053, 4073), 'datetime.datetime', 'datetime', (['(2021)', '(6)', '(7)'], {}), '(2021, 6, 7)\n', (4061, 4073), False, 'from datetime import datetime, timedelta\n'), ((4093, 4113), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(4)'], {}), '(2021, 8, 4)\n', (4101, 4113), False, 'from datetime import datetime, timedelta\n'), ((4134, 4155), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(10)'], {}), '(2021, 8, 10)\n', (4142, 4155), False, 'from datetime import datetime, timedelta\n'), ((4176, 4197), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(11)'], {}), '(2021, 8, 11)\n', (4184, 4197), False, 'from datetime import datetime, timedelta\n'), ((4207, 4228), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(16)'], {}), '(2021, 8, 16)\n', (4215, 4228), False, 'from datetime import datetime, timedelta\n'), ((4526, 4559), 'sfm.models.WorkItemCreate', 'WorkItemCreate', ([], {}), '(**deployment_dict)\n', (4540, 4559), False, 'from sfm.models import WorkItemCreate, ProjectCreate, CommitCreate\n'), ((4568, 4630), 'sfm.routes.work_items.crud.create_work_item', 'crud.create_work_item', (['db', 'work_item_data', 'project_auth_token2'], {}), '(db, work_item_data, project_auth_token2)\n', (4589, 4630), False, 'from sfm.routes.work_items import crud\n'), ((4691, 4712), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(11)'], {}), '(2021, 8, 11)\n', (4699, 4712), False, 'from datetime import datetime, timedelta\n'), ((4722, 4743), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(19)'], {}), '(2021, 8, 19)\n', (4730, 4743), False, 'from datetime import datetime, timedelta\n'), ((5257, 5284), 'sfm.models.WorkItemCreate', 'WorkItemCreate', ([], {}), '(**pull_dict)\n', (5271, 5284), False, 'from sfm.models import WorkItemCreate, ProjectCreate, CommitCreate\n'), ((5308, 5381), 'sfm.routes.work_items.crud.create_work_item', 'crud.create_work_item', (['db', 'work_item_data', 'project_auth_tokens[proj_iter]'], {}), '(db, work_item_data, project_auth_tokens[proj_iter])\n', (5329, 5381), False, 'from sfm.routes.work_items import crud\n'), ((5506, 5526), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(8)'], {}), '(2021, 8, 8)\n', (5514, 5526), False, 'from datetime import datetime, timedelta\n'), ((5536, 5556), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(9)'], {}), '(2021, 8, 9)\n', (5544, 5556), False, 'from datetime import datetime, timedelta\n'), ((5566, 5587), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(10)'], {}), '(2021, 8, 10)\n', (5574, 5587), False, 'from datetime import datetime, timedelta\n'), ((5597, 5618), 'datetime.datetime', 'datetime', (['(2021)', '(8)', '(11)'], {}), '(2021, 8, 11)\n', (5605, 5618), False, 'from datetime import datetime, timedelta\n'), ((5956, 5983), 'sfm.models.CommitCreate', 'CommitCreate', ([], {}), '(**commit_dict)\n', (5968, 5983), False, 'from sfm.models import WorkItemCreate, ProjectCreate, CommitCreate\n'), ((5996, 6070), 'sfm.routes.commits.crud.create_commit', 'commit_crud.create_commit', (['db', 'commit_data', 'project_auth_tokens[proj_iter]'], {}), '(db, commit_data, project_auth_tokens[proj_iter])\n', (6021, 6070), True, 'from sfm.routes.commits import crud as commit_crud\n')]