Example #1
0
def dbm():
    if project.verbose:
        print('DBM')

    # name is redundant if title is given, allow skipping use of name for creating reports
    if 'body' in project.task['report'] and 'name' not in project.task[
            'report']:
        project.task['report']['name'] = project.task['report']['body'][
            'metadata']['title']

    # check if report is to be deleted
    if project.task.get('delete', False):
        if project.verbose:
            print('DBM DELETE')
        report_delete(project.task['auth'],
                      project.task['report'].get('report_id', None),
                      project.task['report'].get('name', None))

    # check if report is to be created
    if 'body' in project.task['report']:
        if project.verbose:
            print('DBM BUILD',
                  project.task['report']['body']['metadata']['title'])

        # ceck if filters given ( returns new body )
        if 'filters' in project.task['report']:
            project.task['report']['body'] = report_filter(
                project.task['auth'], project.task['report']['body'],
                project.task['report']['filters'])

        # create the report
        report = report_build(project.task['auth'],
                              project.task['report']['body'])

    # moving a report
    if 'out' in project.task:

        filename, report = report_file(
            project.task['auth'],
            project.task['report'].get('report_id', None),
            project.task['report'].get('name', None),
            project.task['report'].get('timeout', 10))

        # if a report exists
        if report:
            if project.verbose:
                print('DBM FILE', filename)

            # clean up the report
            rows = report_to_rows(report)
            rows = report_clean(rows)

            # write rows using standard out block in json ( allows customization across all scripts )
            if rows:
                put_rows(project.task['auth'], project.task['out'], rows)
Example #2
0
def dbm():
    if project.verbose: print('DBM')

    # legacy translations ( changed report title to name )
    if 'title' in project.task['report']:
        project.task['report']['name'] = project.task['report']['title']

    # check if report is to be deleted
    if project.task.get('delete', False):
        if project.verbose: print('DBM DELETE', end='')
        report_delete(project.task['auth'],
                      project.task['report'].get('report_id', None),
                      project.task['report'].get('name', None))

    # check if report is to be created ( LEGACY, DO NOT USE, SEE body format below )
    # REASON: this call tried to pass all parts of the json as parameters, this does not scale
    #         the new body call simply passes the report json in, leaving flexibility in the JSON recipe
    if 'type' in project.task['report']:

        if project.verbose: print('DBM CREATE', end='')

        partners = get_rows(
            project.task['auth'], project.task['report']
            ['partners']) if 'partners' in project.task['report'] else []
        advertisers = get_rows(
            project.task['auth'], project.task['report']['advertisers']
        ) if 'advertisers' in project.task['report'] else []

        report_create(
            project.task['auth'], project.task['report']['name'],
            project.task['report']['type'], partners, advertisers,
            project.task['report'].get('filters'),
            project.task['report'].get('dimensions'),
            project.task['report'].get('metrics'),
            project.task['report'].get('data_range'),
            project.task['report'].get('timezone', 'America/Los Angeles'),
            project.id, project.task['report'].get('dataset_id', None))

    # check if report is to be created
    if 'body' in project.task['report']:
        if project.verbose:
            print('DBM BUILD',
                  project.task['report']['body']['metadata']['title'])

        # filters can be passed using special get_rows handler, allows reading values from sheets etc...
        if 'filters' in project.task['report']:
            for f, d in project.task['report']['filters'].items():
                for v in get_rows(project.task['auth'], d):
                    project.task['report']['body']['params'].setdefault(
                        'filters', []).append({
                            "type": f,
                            "value": v
                        })

        # create the report
        report = report_build(project.task['auth'],
                              project.task['report']['body'])

    # moving a report
    if 'out' in project.task:

        filename, report = report_file(
            project.task['auth'],
            project.task['report'].get('report_id', None),
            project.task['report'].get('name', None),
            project.task['report'].get('timeout', 10), DBM_CHUNKSIZE)

        # if a report exists
        if report:
            if project.verbose: print('DBM FILE', filename)

            # clean up the report
            rows = report_to_rows(report)
            rows = report_clean(rows,
                                datastudio=project.task.get(
                                    'datastudio', False),
                                nulls=True)

            # write rows using standard out block in json ( allows customization across all scripts )
            if rows: put_rows(project.task['auth'], project.task['out'], rows)