Example #1
0
 def load_reports(topic: str, message, repo: ReportsRepository):
     if topic == 'NewReport':
         return [repo.get(message['report'])]
     elif topic == 'DataUpdate':
         return [
             report for table in message['tables']
             for report in repo.get_by_tables(table)
         ]
Example #2
0
def test_get(dynamo_mock):
    with ReportsRepository() as repo:
        get_result = repo.get("testReport")
    assert get_result['name'] == 'testReport'
    assert get_result['dataProtection'] == 3
    assert get_result[
        'queryString'] == "COUNT(*) from dev_level_3_database.cv_partner_employees"
Example #3
0
def test_update_cache_time_non_existing(dynamo_mock):
    with patch("common.repositories.reports.datetime") as mock_datetime:
        mock_datetime.now.return_value = datetime(2020, 1, 1)
        mock_datetime.side_effect = lambda *args, **kwargs: datetime(
            *args, **kwargs)
        with ReportsRepository() as repo:
            with pytest.raises(KeyError):
                repo.update_cache_time("testReport_non_existing")
Example #4
0
def test_update_cache_time(dynamo_mock):
    with patch("common.repositories.reports.datetime") as mock_datetime:
        mock_datetime.now.return_value = datetime(2020, 1, 1)
        mock_datetime.side_effect = lambda *args, **kwargs: datetime(
            *args, **kwargs)
        with ReportsRepository() as repo:
            repo.update_cache_time("testReport")
        new_item_result = dynamo_mock.get_item(Key={"name": "testReport"})
        assert new_item_result['Item']['lastCacheUpdate'] == datetime(
            2020, 1, 1).isoformat()
Example #5
0
    def get(self, name):
        filters = filter_parser.parse_args().get('filter', [])

        with ReportsRepository() as repo:
            report = repo.get(name)

        query = cache_table_service.query_cache_table(report['dataProtection'],
                                                      report['name'])

        return Response(query(filters), content_type='application/json')
Example #6
0
def handler(event, context):
    records = event['Records']

    def load_message(record):
        rec = record.get('Sns')
        return (rec.get('Subject', None), rec.get('Message', None))

    messages = [(topic, json.loads(msg))
                for topic, msg in [load_message(record) for record in records]
                if topic]

    def load_reports(topic: str, message, repo: ReportsRepository):
        if topic == 'NewReport':
            return [repo.get(message['report'])]
        elif topic == 'DataUpdate':
            return [
                report for table in message['tables']
                for report in repo.get_by_tables(table)
            ]

    with ReportsRepository() as repo:
        report_sets = [
            load_reports(topic, message, repo) for topic, message in messages
        ]
    reports = [report for reports in report_sets for report in reports]
    for report in reports:
        updateCache = cache_table_service.cache_table(report['dataProtection'],
                                                      report['name'])
        updateCache(
            execute_query(report['queryString'],
                          protection_level=report['dataProtection'],
                          preprocess_sql=False))

        with ReportsRepository() as repo:
            repo.update_cache_time(report['name'])

    return {}
Example #7
0
def test_create(dynamo_mock):
    additional_report = {
        "dataProtection": 3,
        "name": "new_report",
        "queryString":
        "COUNT(*) from dev_level_3_database.cv_partner_employees",
        "tables": ["test_table5", "test_table6"],
        "lastUsed": "fail",
        "lastCacheUpdate": "fail"
    }
    with ReportsRepository() as repo:
        repo.create(additional_report)
    new_item_result = dynamo_mock.get_item(Key={"name": "new_report"})
    assert new_item_result['Item']['name'] == "new_report"
    assert new_item_result['Item']['created'] is not None
Example #8
0
def new_report(new_report: Dict[str, str]):
    sql, used_tables, _ = process_sql(new_report['queryString'])

    tables = [table for (database, table) in used_tables]
    data_protection_level = reduce(
        lambda x, y: x if x >= y else y,
        [
            int(next(iter(re.findall(r'\d', database))))
            for (database, table) in used_tables
        ], 3
    )

    with ReportsRepository() as repo:
        repo.create({
            'name': new_report['name'],
            'queryString': sql,
            'tables': tables,
            'dataProtection': data_protection_level
        })
        pub_new_report(new_report['name'])
        return repo.get(new_report['name'])
Example #9
0
def delete_report(name: str):
    with ReportsRepository() as repo:
        report = repo.get(name)
        repo.delete(report['name'])
        pub_delete_report(report['name'], int(report['dataProtection']))
    return f"Deleted ${name}"
Example #10
0
def test_delete(dynamo_mock):
    with ReportsRepository() as repo:
        repo.delete("testReport")
    all_reports = dynamo_mock.scan()
    assert len(all_reports['Items']) == 1
    assert all_reports['Items'][0]['name'] == "anotherReport"
Example #11
0
def test_all(dynamo_mock):
    with ReportsRepository() as repo:
        all_result = repo.all()
    assert len(all_result) == 2
    assert all_result[0]["name"] == "testReport"
    assert all_result[1]["name"] == "anotherReport"
Example #12
0
def test_get_by_many_tables(dynamo_mock):
    with ReportsRepository() as repo:
        get_by_tables_result = repo.get_by_tables(
            ["test_table1", "test_table2"])
    assert len(get_by_tables_result) == 1
    assert get_by_tables_result[0]["name"] == "testReport"
Example #13
0
 def get(self):
     with ReportsRepository() as repo:
         return repo.all()