Exemple #1
0
def delete_set(header, main_url, organization_id, dataset_id, log):
    # Delete all buildings
    # print ('API Function: delete_inventory\n'),
    # partmsg = 'delete_buildings'
    # result = requests.delete(
    #     main_url + '/app/delete_organization_inventory/',
    #     headers=header,
    #     params={'organization_id': organization_id}
    # )
    # check_status(result, partmsg, log)

    # Delete dataset
    print('API Function: delete_dataset\n'),
    partmsg = 'delete_dataset'
    result = requests.delete(
        main_url + '/api/v2/datasets/{}/'.format(dataset_id),
        headers=header,
        params={'organization_id': organization_id},
    )
    check_status(result, partmsg, log)
Exemple #2
0
def delete_set(header, main_url, organization_id, dataset_id, project_slug, log):

    # Delete all buildings
    print ('API Function: delete_buildings\n'),
    partmsg = 'delete_buildings'
    payload = {'organization_id': organization_id,
               'search_payload': {'select_all_checkbox': True}}

    result = requests.delete(main_url + '/app/delete_buildings/',
                             headers=header,
                             data=json.dumps(payload))
    check_status(result, partmsg, log)

    # Delete dataset
    print ('API Function: delete_dataset\n'),
    partmsg = 'delete_dataset'
    payload = {'dataset_id': dataset_id,
               'organization_id': organization_id}

    result = requests.delete(main_url + '/app/delete_dataset/',
                             headers=header,
                             data=json.dumps(payload))
    check_status(result, partmsg, log)

    # Delete project
    print ('API Function: delete_project\n'),
    partmsg = 'delete_project'
    payload = {'organization_id': organization_id,
               'project_slug': project_slug}

    result = requests.delete(main_url + '/app/projects/delete_project/',
                             headers=header,
                             data=json.dumps(payload))
    check_status(result, partmsg, log)
Exemple #3
0
def labels(header, main_url, organization_id, cycle_id, log):

    # Create label
    print('API Function: create_label\n')
    partmsg = 'create_label'
    params = {
        'organization_id': organization_id
    }
    payload = {
        'name': 'TestLabel',
        'color': 'red'
    }
    result = requests.post(main_url + '/api/v3/labels/',
                           headers=header,
                           params=params,
                           json=payload)
    check_status(result, partmsg, log)
    label_id = result.json()['id']

    # Get IDs for all properties
    params = {
        'organization_id': organization_id,
        'cycle': cycle_id,
        'page': 1,
        'per_page': 999999999
    }
    result = requests.post(main_url + '/api/v3/properties/filter/',
                           headers=header,
                           params=params)
    inventory_ids = [prop['property_view_id'] for prop in result.json()['results']]

    # Apply label to properties
    print('API Function: apply_label\n')
    partmsg = 'apply_label'
    params = {
        'organization_id': organization_id
    }
    payload = {
        'add_label_ids': [label_id],
        'inventory_ids': inventory_ids
    }
    result = requests.put(main_url + '/api/v3/labels_property/',
                          headers=header,
                          params=params,
                          json=payload)
    check_status(result, partmsg, log)

    # Delete label
    print('API Function: delete_label\n')
    partmsg = 'delete_label'
    params = {
        'organization_id': organization_id
    }
    result = requests.delete(main_url + '/api/v3/labels/%s/' % label_id,
                             headers=header,
                             params=params)
    check_status(result, partmsg, log)
def delete_set(header, main_url, organization_id, dataset_id, project_slug,
               log):

    #Delete all buildings
    print('API Function: delete_buildings\n'),
    partmsg = 'delete_buildings'
    payload = {
        'organization_id': organization_id,
        'search_payload': {
            'select_all_checkbox': True
        }
    }
    try:
        result = requests.delete(main_url + '/app/delete_buildings/',
                                 headers=header,
                                 data=json.dumps(payload))
        check_status(result, partmsg, log)
    except:
        print("\n WARNING: Can't delete BUILDING RECORDS, delete manually!")
        raw_input("Press Enter to continue...")

    #Delete dataset
    print('API Function: delete_dataset\n'),
    partmsg = 'delete_dataset'
    payload = {'dataset_id': dataset_id, 'organization_id': organization_id}
    try:
        result = requests.delete(main_url + '/app/delete_dataset/',
                                 headers=header,
                                 data=json.dumps(payload))
        check_status(result, partmsg, log)
    except:
        print("\n WARNING: Can't delete BUILDING SET, delete manually!")
        raw_input("Press Enter to continue...")

    #Delete project
    print('API Function: delete_project\n'),
    partmsg = 'delete_project'
    payload = {
        'organization_id': organization_id,
        'project_slug': project_slug
    }
    try:
        result = requests.delete(main_url + '/app/projects/delete_project/',
                                 headers=header,
                                 data=json.dumps(payload))
        check_status(result, partmsg, log)
    except:
        print("\n WARNING: Can't delete PROJECT, delete manually!")
        raw_input("Press Enter to continue...")

    return
Exemple #5
0
def cycles(header, main_url, organization_id, log):
    print('API Function: get_cycles\n')
    partmsg = 'get_cycles'
    result = requests.get(main_url + '/api/v2/cycles/',
                          headers=header,
                          params={'organization_id': organization_id})
    check_status(result, partmsg, log, piid_flag='cycles')

    cycles = result.json()['cycles']
    print "current cycles are {}".format(cycles)
    for cyc in cycles:
        if cyc['name'] == 'TestCycle':
            cycle_id = cyc['id']
            break
    else:
        # Create cycle (only if it does not exist, until there is a function to delete cycles)
        print('API Function: create_cycle\n')
        partmsg = 'create_cycle'
        payload = {
            'start': "2015-01-01T08:00",
            'end': "2016-01-01T08:00",
            'name': "TestCycle"
        }
        result = requests.post(main_url + '/api/v2/cycles/',
                               headers=header,
                               params={'organization_id': organization_id},
                               json=payload)
        check_status(result, partmsg, log)

        cycle_id = result.json()['cycles']['id']

    # Update cycle
    print('\nAPI Function: update_cycle')
    partmsg = 'update_cycle'
    payload = {
        'start': "2015-01-01T08:00",
        'end': "2016-01-01T08:00",
        'name': "TestCycle",
        'id': cycle_id
    }
    result = requests.put(main_url + '/api/v2/cycles/{}/'.format(cycle_id),
                          headers=header,
                          params={'organization_id': organization_id},
                          json=payload)
    check_status(result, partmsg, log)

    # TODO: Test deleting a cycle
    return cycle_id
Exemple #6
0
def delete_set(header, main_url, organization_id, dataset_id, project_slug, log):

    #Delete all buildings
    print ('API Function: delete_buildings\n'),
    partmsg = 'delete_buildings'
    payload = {'organization_id': organization_id,
               'search_payload': {'select_all_checkbox': True}}
    try:
        result = requests.delete(main_url+'/app/delete_buildings/',
                                    headers = header,
                                    data=json.dumps(payload))
        check_status(result, partmsg, log)
    except:
        print("\n WARNING: Can't delete BUILDING RECORDS, delete manually!")
        raw_input("Press Enter to continue...")


    #Delete dataset
    print ('API Function: delete_dataset\n'),
    partmsg = 'delete_dataset'
    payload = {'dataset_id': dataset_id,
               'organization_id': organization_id}
    try:
        result = requests.delete(main_url+'/app/delete_dataset/',
                                 headers = header,
                                 data=json.dumps(payload))
        check_status(result, partmsg, log)
    except:
        print("\n WARNING: Can't delete BUILDING SET, delete manually!")
        raw_input("Press Enter to continue...")

    #Delete project
    print ('API Function: delete_project\n'),
    partmsg = 'delete_project'
    payload = {'organization_id': organization_id,
               'project_slug': project_slug}
    try:
        result = requests.delete(main_url+'/app/projects/delete_project/',
                                 headers = header,
                                 data=json.dumps(payload))
        check_status(result, partmsg, log)
    except:
        print("\n WARNING: Can't delete PROJECT, delete manually!")
        raw_input("Press Enter to continue...")

    return
Exemple #7
0
def account(header, main_url, username, log):
    # Retrieve the user id key for later retrievals
    print('API Function: current_user_id\n')
    result = requests.get(main_url + '/api/v2/users/current_user_id/',
                          headers=header)
    user_pk = json.loads(result.content)['pk']

    # Retrieve the user profile
    print('API Function: get_user_profile\n')
    partmsg = 'get_user_profile'
    result = requests.get(main_url + '/api/v2/users/%s/' % user_pk,
                          headers=header)
    check_status(result, partmsg, log)

    # Retrieve the organizations
    print('API Function: get_organizations\n'),
    partmsg = 'get_organizations'
    result = requests.get(main_url + '/api/v2/organizations/', headers=header)
    check_status(result, partmsg, log, piid_flag='organizations')

    # Get the organization id to be used.
    # NOTE: Loop through the organizations and get the org_id
    # where the organization owner is 'username' else get the first organization.
    orgs_result = result.json()

    for org in orgs_result['organizations']:
        try:
            if org['owners'][0]['email'] == username:
                organization_id = org['org_id']
                break
        except IndexError:
            pass
    else:
        organization_id = orgs_result['organizations'][0]['org_id']

    # Get the organization details
    partmsg = 'get_organization (2)'
    mod_url = main_url + '/api/v2/organizations/%s' % str(organization_id)
    result = requests.get(mod_url, headers=header)
    check_status(result, partmsg, log)

    # Change user profile
    # NOTE: Make sure these credentials are ok.
    print('API Function: update_user\n'),
    partmsg = 'update_user'
    user_payload = {
        'first_name': 'Sherlock',
        'last_name': 'Holmes',
        'email': username
    }
    result = requests.put(main_url + '/api/v2/users/%s/' % user_pk,
                          headers=header,
                          data=user_payload)
    check_status(result, partmsg, log)

    # Get organization users
    print('API Function: get_organizations_users\n'),
    partmsg = 'get_organizations_users'
    result = requests.get(main_url +
                          '/api/v2/organizations/%s/users/' % organization_id,
                          headers=header)
    check_status(result, partmsg, log, piid_flag='users')

    # Get organizations settings
    print('API Function: get_query_treshold\n'),
    partmsg = 'get_query_threshold'
    result = requests.get(
        main_url +
        '/api/v2/organizations/%s/query_threshold/' % organization_id,
        headers=header)
    check_status(result, partmsg, log)

    # Get shared fields
    print('API Function: get_shared_fields\n'),
    partmsg = 'get_shared_fields'
    result = requests.get(
        main_url + '/api/v2/organizations/%s/shared_fields/' % organization_id,
        headers=header)
    check_status(result, partmsg, log)

    return organization_id
Exemple #8
0
def data_quality(header, main_url, organization_id, log):

    # get the data quality rules for the organization
    print('API Function: get_data_quality_rules\n')
    partmsg = 'get_data_quality_rules'
    result = requests.get(
        main_url + f'/api/v3/data_quality_checks/{organization_id}/rules/',
        headers=header
    )
    check_status(result, partmsg, log)

    # create a new rule
    print('API Function: create_data_quality_rule\n')
    partmsg = 'create_data_quality_rule'
    payload = {
        'field': 'city',
        'enabled': True,
        'data_type': 1,
        'condition': 'not_null',
        'rule_type': 1,
        'required': False,
        'not_null': False,
        'min': None,
        'max': None,
        'text_match': None,
        'severity': 1,
        'units': '',
        'status_label': None,
        'table_name': "PropertyState"
    }
    result = requests.post(
        main_url + f'/api/v3/data_quality_checks/{organization_id}/rules/',
        headers=header,
        json=payload
    )
    new_rule_id = result.json().get('id')
    check_status(result, partmsg, log)

    # delete the new rule
    print('API Function: delete_data_quality_rule\n')
    partmsg = 'delete_data_quality_rule'
    result = requests.delete(
        main_url + f'/api/v3/data_quality_checks/{organization_id}/rules/{new_rule_id}/',
        headers=header
    )
    check_status(result, partmsg, log)

    # get some property view ids
    result = requests.get(
        main_url + '/api/v3/property_views/',
        headers=header
    )
    prop_view_ids = [prop['id'] for prop in result.json()['property_views']]

    # create a new data quality check process
    print('API Function: create_data_quality_check\n')
    partmsg = 'create_data_quality_check'
    payload = {
        'property_view_ids': prop_view_ids,
        'taxlot_view_ids': []
    }
    result = requests.post(
        main_url + f'/api/v3/data_quality_checks/{organization_id}/start/',
        headers=header,
        json=payload
    )
    check_status(result, partmsg, log)
    data_quality_id = result.json()['progress']['unique_id']

    # perform the data quality check
    print('API Function: perform_data_quality_check\n')
    partmsg = 'perform_data_quality_check'
    params = {
        'organization_id': organization_id,
        'run_id': data_quality_id
    }
    result = requests.get(
        main_url + '/api/v3/data_quality_checks/results/',
        headers=header,
        params=params
    )
    check_status(result, partmsg, log)
Exemple #9
0
def search_and_project(header, main_url, organization_id, log):
    # Search CanonicalBuildings
    print('API Function: search_buildings\n'),
    partmsg = 'search_buildings'
    search_payload = {
        'filter_params': {
            u'address_line_1': u'94734 SE Honeylocust Street'
        }
    }

    result = requests.get(main_url + '/api/v1/search_buildings/',
                          headers=header,
                          data=json.dumps(search_payload))
    check_status(result, partmsg, log)

    # Project
    print('\n-------Project-------\n')

    # Create a Project for 'Condo' in 'use_description'
    print('API Function: create_project\n'),
    partmsg = 'create_project'
    time1 = dt.datetime.now()
    newproject_payload = {
        'name': 'New Project_' + str(time1.day) + str(time1.second),
        'compliance_type': 'describe compliance type',
        'description': 'project description'
    }
    result = requests.post(main_url + '/api/v2/projects/',
                           headers=header,
                           params=json.dumps(
                               {'organization_id': organization_id}),
                           data=json.dumps(newproject_payload))
    write_out_django_debug(partmsg, result)
    check_status(result, partmsg, log)

    # Get project slug
    project_slug = result.json()['project_slug']

    # Get the projects for the organization
    print('API Function: get_project\n'),
    partmsg = 'get_project'

    result = requests.get(main_url + '/api/v2/projects/',
                          headers=header,
                          params=json.dumps({
                              'project_slug': project_slug,
                              'organization_id': organization_id
                          }))
    check_status(result, partmsg, log)

    # Populate project by search buildings result
    print('API Function: add_buildings_to_project\n'),
    partmsg = 'add_buildings_to_project'
    projectbldg_payload = {
        'project': {
            'status': 'active',
            'project_slug': project_slug,
            'slug': project_slug,
            'select_all_checkbox': True,
            'selected_buildings': [],
            'filter_params': {
                'use_description': 'CONDO'
            }
        },
        'organization_id': organization_id
    }

    result = requests.post(main_url +
                           '/app/projects/add_buildings_to_project/',
                           headers=header,
                           data=json.dumps(projectbldg_payload))
    time.sleep(10)
    check_status(result, partmsg, log)

    # Get the percent/progress of buildings added to project
    progress = requests.post(
        main_url +
        '/app/projects/get_adding_buildings_to_project_status_percentage/',
        headers=header,
        data=json.dumps({
            'project_loading_cache_key':
            result.json()['project_loading_cache_key']
        }))
    log.debug(pprint.pformat(progress.json()))
Exemple #10
0
def upload_match_sort(header, main_url, organization_id, dataset_id, cycle_id,
                      filepath, filetype, mappingfilepath, log):
    # Upload the covered-buildings-sample file
    print('API Function: upload_file\n'),
    partmsg = 'upload_file'
    result = upload_file(header, filepath, main_url, dataset_id, filetype)
    check_status(result, partmsg, log)

    # Get import ID
    import_id = result.json()['import_file_id']

    # Save the data to BuildingSnapshots
    print('API Function: save_raw_data\n'),
    partmsg = 'save_raw_data'
    payload = {'organization_id': organization_id, 'cycle_id': cycle_id}
    result = requests.post(
        main_url + '/api/v2/import_files/{}/save_raw_data/'.format(import_id),
        headers=header,
        json=payload)
    check_progress(main_url, header, result.json()['progress_key'])
    check_status(result, partmsg, log)

    # Get the mapping suggestions
    print('API Function: get_column_mapping_suggestions\n'),
    partmsg = 'get_column_mapping_suggestions'
    result = requests.get(
        main_url +
        '/api/v2/import_files/{}/mapping_suggestions/'.format(import_id),
        params={"organization_id": organization_id},
        headers=header)
    check_status(result, partmsg, log, piid_flag='mappings')

    # Save the column mappings
    print('API Function: save_column_mappings\n'),
    partmsg = 'save_column_mappings'
    payload = {'mappings': read_map_file(mappingfilepath)}
    result = requests.post(
        main_url +
        '/api/v2/import_files/{}/save_column_mappings/'.format(import_id),
        params={"organization_id": organization_id},
        headers=header,
        json=payload)
    check_status(result, partmsg, log)

    # Perform mapping
    print('API Function: perform_mapping\n'),
    partmsg = 'save_column_mappings'
    result = requests.post(
        main_url +
        '/api/v2/import_files/{}/perform_mapping/'.format(import_id),
        params={"organization_id": organization_id},
        headers=header)
    print result.json()
    check_progress(main_url, header, result.json()['progress_key'])
    check_status(result, partmsg, log)

    # Get Data Quality Message
    print('API Function: data_quality\n'),
    partmsg = 'data_quality'

    result = requests.get(
        main_url +
        '/api/v2/import_files/{}/data_quality_results/'.format(import_id),
        headers=header,
        params={"organization_id": organization_id})
    check_status(result, partmsg, log, piid_flag='data_quality')

    # Match uploaded buildings with buildings already in the organization.
    print('API Function: start_system_matching\n'),
    partmsg = 'start_system_matching'
    payload = {'file_id': import_id, 'organization_id': organization_id}

    result = requests.post(
        main_url +
        '/api/v2/import_files/{}/start_system_matching/'.format(import_id),
        headers=header,
        params={"organization_id": organization_id},
        json=payload)
    result = check_progress(main_url, header, result.json()['progress_key'])
    check_status(result, partmsg, log)

    # Check number of matched and unmatched BuildingSnapshots
    print('API Function: matching_results\n'),
    partmsg = 'matching_results'

    result = requests.get(
        main_url +
        '/api/v2/import_files/{}/matching_results/'.format(import_id),
        headers=header,
        params={})
    check_status(result, partmsg, log)
def upload_match_sort(header, main_url, organization_id, dataset_id, filepath,
                      filetype, mappingfilepath, log):

    # Upload the covered-buildings-sample file
    print('API Function: upload_file\n'),
    partmsg = 'upload_file'
    try:
        result = upload_file(header, filepath, main_url, dataset_id, filetype)
        check_status(result, partmsg, log)
    except:
        log.error("Following API could not be tested")
        return

    # Get import ID
    import_id = result.json()['import_file_id']

    # Save the data to BuildingSnapshots
    print('API Function: save_raw_data\n'),
    partmsg = 'save_raw_data'
    payload = {'file_id': import_id, 'organization_id': organization_id}
    try:
        result = requests.post(main_url + '/app/save_raw_data/',
                               headers=header,
                               data=json.dumps(payload))
        progress = check_progress(main_url, header,
                                  result.json()['progress_key'])
        check_status(result, partmsg, log)
    except:
        log.error("Following API could not be tested")
        return

    # Get the mapping suggestions
    print('API Function: get_column_mapping_suggestions\n'),
    partmsg = 'get_column_mapping_suggestions'
    payload = {'import_file_id': import_id, 'org_id': organization_id}
    try:
        result = requests.get(main_url +
                              '/app/get_column_mapping_suggestions/',
                              headers=header,
                              data=json.dumps(payload))
        check_status(result, partmsg, log, PIIDflag='mappings')
    except:
        pass

    # Save the column mappings
    print('API Function: save_column_mappings\n'),
    partmsg = 'save_column_mappings'
    payload = {'import_file_id': import_id, 'organization_id': organization_id}
    payload['mappings'] = read_map_file(mappingfilepath)
    try:
        result = requests.get(main_url + '/app/save_column_mappings/',
                              headers=header,
                              data=json.dumps(payload))
        check_status(result, partmsg, log)
    except:
        log.error("Following API could not be tested")
        return

    # Map the buildings with new column mappings.
    print('API Function: remap_buildings\n'),
    partmsg = 'remap_buildings'
    payload = {'file_id': import_id, 'organization_id': organization_id}
    try:
        result = requests.get(main_url + '/app/remap_buildings/',
                              headers=header,
                              data=json.dumps(payload))

        progress = check_progress(main_url, header,
                                  result.json()['progress_key'])
        check_status(result, partmsg, log)
    except:
        log.error("Following API could not be tested")
        return

    # Get Data Cleansing Message
    print('API Function: cleansing\n'),
    partmsg = 'cleansing'
    try:
        result = requests.get(main_url + '/cleansing/results/',
                              headers=header,
                              params={'import_file_id': import_id})
        check_status(result, partmsg, log, PIIDflag='cleansing')
    except:
        pass

    # Match uploaded buildings with buildings already in the organization.
    print('API Function: start_system_matching\n'),
    partmsg = 'start_system_matching'
    payload = {'file_id': import_id, 'organization_id': organization_id}
    try:
        result = requests.post(main_url + '/app/start_system_matching/',
                               headers=header,
                               data=json.dumps(payload))

        progress = check_progress(main_url, header,
                                  result.json()['progress_key'])
        check_status(result, partmsg, log)
    except:
        pass

    # Check number of matched and unmatched BuildingSnapshots
    print('API Function: get_PM_filter_by_counts\n'),
    partmsg = 'get_PM_filter_by_counts'
    try:
        result = requests.get(main_url + '/app/get_PM_filter_by_counts/',
                              headers=header,
                              params={'import_file_id': import_id})
        check_status(result, partmsg, log, PIIDflag='PM_filter')
    except:
        pass

    return
Exemple #12
0
def export_data(header, main_url, organization_id, cycle_id, log):

    # Get IDs for some properties
    num_props = 25
    params = {
        'organization_id': organization_id,
        'cycle': cycle_id,
        'page': 1,
        'per_page': 999999999
    }
    result = requests.post(main_url + '/api/v2/properties/filter/',
                           headers=header,
                           params=params)
    prop_ids = [prop['id'] for prop in result.json()['results']]
    prop_ids = prop_ids[:num_props]

    print('API Function: export_properties\n')
    partmsg = 'export_properties'
    params = {
        'organization_id': organization_id,
        'cycle_id': cycle_id,
        'inventory_type': 'properties'
    }
    payload = {
        'ids': prop_ids,
        'filename': 'test_seed_host_api---properties-export.csv',
        'profile_id': None,
        'export_type': 'csv',
    }
    result = requests.post(main_url + '/api/v2.1/tax_lot_properties/export/',
                           headers=header,
                           params=params,
                           json=payload)
    check_status(result, partmsg, log, piid_flag='export')

    # Get IDs for some taxlots
    num_lots = 25
    params = {
        'organization_id': organization_id,
        'cycle': cycle_id,
        'page': 1,
        'per_page': 999999999
    }
    result = requests.post(main_url + '/api/v2/taxlots/filter/',
                           headers=header,
                           params=params)
    lot_ids = [lot['id'] for lot in result.json()['results']]
    lot_ids = lot_ids[:num_lots]

    print('API Function: export_taxlots\n')
    partmsg = 'export_taxlots'
    params = {
        'organization_id': organization_id,
        'cycle_id': cycle_id,
        'inventory_type': 'taxlots'
    }
    payload = {
        'ids': lot_ids,
        'filename': 'test_seed_host_api---taxlots-export.csv',
        'profile_id': None,
        'export_type': 'csv',
    }
    result = requests.post(main_url + '/api/v2.1/tax_lot_properties/export/',
                           headers=header,
                           params=params,
                           json=payload)
    check_status(result, partmsg, log, piid_flag='export')
Exemple #13
0
def upload_match_sort(header, main_url, organization_id, dataset_id, filepath, filetype, mappingfilepath, log):

    # Upload the covered-buildings-sample file
    print ('API Function: upload_file\n'),
    partmsg = 'upload_file'
    try:
        result = upload_file(header, filepath, main_url, dataset_id, filetype)
        check_status(result, partmsg, log)
    except:
        log.error("Following API could not be tested")
        return

    # Get import ID
    import_id = result.json()['import_file_id']

    # Save the data to BuildingSnapshots
    print ('API Function: save_raw_data\n'),
    partmsg = 'save_raw_data'
    payload = {'file_id': import_id,
               'organization_id': organization_id}
    try:
        result = requests.post(main_url + '/app/save_raw_data/',
                               headers=header,
                               data=json.dumps(payload))
        # progress = check_progress(main_url, header, result.json()['progress_key'])
        check_status(result, partmsg, log)
    except:
        log.error("Following API could not be tested")
        return

    # Get the mapping suggestions
    print ('API Function: get_column_mapping_suggestions\n'),
    partmsg = 'get_column_mapping_suggestions'
    payload = {'import_file_id': import_id,
               'org_id': organization_id}
    try:
        result = requests.get(main_url + '/app/get_column_mapping_suggestions/',
                              headers=header,
                              data=json.dumps(payload))
        check_status(result, partmsg, log, PIIDflag='mappings')
    except:
        pass

    # Save the column mappings
    print ('API Function: save_column_mappings\n'),
    partmsg = 'save_column_mappings'
    payload = {'import_file_id': import_id,
               'organization_id': organization_id}
    payload['mappings'] = read_map_file(mappingfilepath)
    try:
        result = requests.get(main_url + '/app/save_column_mappings/',
                              headers=header,
                              data=json.dumps(payload))
        check_status(result, partmsg, log)
    except:
        log.error("Following API could not be tested")
        return

    # Map the buildings with new column mappings.
    print ('API Function: remap_buildings\n'),
    partmsg = 'remap_buildings'
    payload = {'file_id': import_id,
               'organization_id': organization_id}
    try:
        result = requests.get(main_url + '/app/remap_buildings/',
                              headers=header,
                              data=json.dumps(payload))

        # progress = check_progress(main_url, header, result.json()['progress_key'])
        check_status(result, partmsg, log)
    except:
        log.error("Following API could not be tested")
        return

    # Get Data Cleansing Message
    print ('API Function: cleansing\n'),
    partmsg = 'cleansing'
    try:
        result = requests.get(main_url + '/cleansing/results/',
                              headers=header,
                              params={'import_file_id': import_id})
        check_status(result, partmsg, log, PIIDflag='cleansing')
    except:
        pass

    # Match uploaded buildings with buildings already in the organization.
    print ('API Function: start_system_matching\n'),
    partmsg = 'start_system_matching'
    payload = {'file_id': import_id,
               'organization_id': organization_id}
    try:
        result = requests.post(main_url + '/app/start_system_matching/',
                               headers=header,
                               data=json.dumps(payload))

        # progress = check_progress(main_url, header, result.json()['progress_key'])
        check_status(result, partmsg, log)
    except:
        pass

    # Check number of matched and unmatched BuildingSnapshots
    print ('API Function: get_PM_filter_by_counts\n'),
    partmsg = 'get_PM_filter_by_counts'
    try:
        result = requests.get(main_url + '/app/get_PM_filter_by_counts/',
                              headers=header,
                              params={'import_file_id': import_id})
        check_status(result, partmsg, log, PIIDflag='PM_filter')
    except:
        pass

    return
Exemple #14
0
# -- Cycles
print('\n\n|-------Cycles-------|')
cycle_id = cycles(header, main_url, organization_id, log)
report_memory()

# Create a dataset
print('\n\n|-------Create Dataset-------|')
partmsg = 'create_dataset'
params = {'organization_id': organization_id}
payload = {'name': 'API Test'}
result = requests.post(main_url + '/api/v2/datasets/',
                       headers=header,
                       params=params,
                       data=payload)
check_status(result, partmsg, log)

# Get the dataset id to be used
dataset_id = result.json()['id']
report_memory()

# Upload and test the raw building file
print('\n|---Covered Building File---|\n')
upload_match_sort(header, main_url, organization_id, dataset_id, cycle_id, raw_building_file,
                  'Assessed Raw',
                  raw_map_file, log)
report_memory()

# Upload and test the portfolio manager file
# print ('\n|---Portfolio Manager File---|\n')
# upload_match_sort(header, main_url, organization_id, dataset_id, cycle_id, pm_building_file, 'Portfolio Raw',
Exemple #15
0
def account(header, main_url, username, log):
    # Retrieve the user profile
    print ('API Function: get_user_profile\n'),
    partmsg = 'get_user_profile'
    result = requests.get(main_url + '/app/accounts/get_user_profile',
                          headers=header)
    check_status(result, partmsg, log)

    # Retrieve the organizations
    print ('API Function: get_organizations\n'),
    partmsg = 'get_organizations'
    result = requests.get(main_url + '/app/accounts/get_organizations/',
                          headers=header)
    check_status(result, partmsg, log, PIIDflag='organizations')

    # # Get the organization id to be used.
    # # NOTE: Loop through the organizations and get the org_id
    # # where the organization owner is 'username' else get the first organization.
    orgs_result = result.json()

    for org in orgs_result['organizations']:
        try:
            if org['owners'][0]['email'] == username:
                organization_id = org['org_id']
                break
        except IndexError:
            pass
    else:
        organization_id = orgs_result['organizations'][0]['org_id']

    # Get the organization details
    partmsg = 'get_organization (2)'
    mod_url = main_url + '/app/accounts/get_organization/?organization_id=' + str(organization_id)
    result = requests.get(mod_url,
                          headers=header)
    check_status(result, partmsg, log)

    # Change user profile
    # NOTE: Make sure these credentials are ok.
    print ('API Function: update_user\n'),
    partmsg = 'update_user'
    user_payload = {'user': {'first_name': 'Sherlock',
                             'last_name': 'Holmes',
                             'email': username}}
    result = requests.post(main_url + '/app/accounts/update_user/',
                           headers=header,
                           data=json.dumps(user_payload))
    check_status(result, partmsg, log)

    # Get organization users
    print ('API Function: get_organizations_users\n'),
    partmsg = 'get_organizations_users'
    org_payload = {'organization_id': organization_id}
    result = requests.post(main_url + '/app/accounts/get_organizations_users/',
                           headers=header,
                           data=json.dumps(org_payload))
    check_status(result, partmsg, log, PIIDflag='users')

    # Get organizations settings
    print ('API Function: get_query_treshold\n'),
    partmsg = 'get_query_threshold'
    result = requests.get(main_url + '/app/accounts/get_query_threshold/',
                          headers=header,
                          params={'organization_id': organization_id})
    check_status(result, partmsg, log)

    # Get shared fields
    print ('API Function: get_shared_fields\n'),
    partmsg = 'get_shared_fields'
    result = requests.get(main_url + '/app/accounts/get_shared_fields/',
                          headers=header,
                          params={'organization_id': organization_id})
    check_status(result, partmsg, log)

    return organization_id
Exemple #16
0
def upload_match_sort(header, main_url, organization_id, dataset_id, filepath, filetype, mappingfilepath, log):

    # Upload the covered-buildings-sample file
    print ('API Function: upload_file\n'),
    partmsg = 'upload_file'
    result = upload_file(header, filepath, main_url, dataset_id, filetype)
    check_status(result, partmsg, log)

    # Get import ID
    import_id = result.json()['import_file_id']

    # Save the data to BuildingSnapshots
    print ('API Function: save_raw_data\n'),
    partmsg = 'save_raw_data'
    payload = {'file_id': import_id,
               'organization_id': organization_id}
    result = requests.post(main_url + '/app/save_raw_data/',
                           headers=header,
                           data=json.dumps(payload))
    # progress = check_progress(main_url, header, result.json()['progress_key'])
    check_status(result, partmsg, log)

    time.sleep(20)

    # Get the mapping suggestions
    print ('API Function: get_column_mapping_suggestions\n'),
    partmsg = 'get_column_mapping_suggestions'
    payload = {'import_file_id': import_id,
               'org_id': organization_id}

    result = requests.get(main_url + '/app/get_column_mapping_suggestions/',
                          headers=header,
                          data=json.dumps(payload))
    check_status(result, partmsg, log, PIIDflag='mappings')

    # Save the column mappings
    print ('API Function: save_column_mappings\n'),
    partmsg = 'save_column_mappings'
    payload = {'import_file_id': import_id,
               'organization_id': organization_id}
    payload['mappings'] = read_map_file(mappingfilepath)

    result = requests.get(main_url + '/app/save_column_mappings/',
                          headers=header,
                          data=json.dumps(payload))
    check_status(result, partmsg, log)

    # Map the buildings with new column mappings.
    print ('API Function: remap_buildings\n'),
    partmsg = 'remap_buildings'
    payload = {'file_id': import_id,
               'organization_id': organization_id}

    result = requests.get(main_url + '/app/remap_buildings/',
                          headers=header,
                          data=json.dumps(payload))

    # progress = check_progress(main_url, header, result.json()['progress_key'])
    check_status(result, partmsg, log)

    # Get Data Cleansing Message
    print ('API Function: cleansing\n'),
    partmsg = 'cleansing'

    result = requests.get(main_url + '/cleansing/results/',
                          headers=header,
                          params={'import_file_id': import_id})
    check_status(result, partmsg, log, PIIDflag='cleansing')

    # Match uploaded buildings with buildings already in the organization.
    print ('API Function: start_system_matching\n'),
    partmsg = 'start_system_matching'
    payload = {'file_id': import_id,
               'organization_id': organization_id}

    count = 100
    while(count > 0):
        result = requests.post(main_url + '/app/start_system_matching/',
                               headers=header,
                               data=json.dumps(payload))
        if result.status_code == 200:
            break
        time.sleep(5)
        count -= 1

    check_status(result, partmsg, log)

    # Check number of matched and unmatched BuildingSnapshots
    print ('API Function: get_PM_filter_by_counts\n'),
    partmsg = 'get_PM_filter_by_counts'

    result = requests.get(main_url + '/app/get_PM_filter_by_counts/',
                          headers=header,
                          params={'import_file_id': import_id})
    check_status(result, partmsg, log, PIIDflag='PM_filter')
Exemple #17
0
def search_and_project(header, main_url, organization_id, log):
        # Search CanonicalBuildings
    print ('API Function: search_buildings\n'),
    partmsg = 'search_buildings'
    search_payload = {'filter_params': {u'address_line_1': u'94734 SE Honeylocust Street'}}

    result = requests.get(main_url + '/app/search_buildings/',
                          headers=header,
                          data=json.dumps(search_payload))
    check_status(result, partmsg, log)

    # Project
    print ('\n-------Project-------\n')

    # Create a Project for 'Condo' in 'use_description'
    print ('API Function: create_project\n'),
    partmsg = 'create_project'
    time1 = dt.datetime.now()
    newproject_payload = {'project': {'name': 'New Project_' + str(time1.day) + str(time1.second),
                                      'compliance_type': 'describe compliance type',
                                      'description': 'project description'},
                          'organization_id': organization_id}

    result = requests.post(main_url + '/app/projects/create_project/',
                           headers=header,
                           data=json.dumps(newproject_payload))
    check_status(result, partmsg, log)

    # Get project slug
    project_slug = result.json()['project_slug']

    # Get the projects for the organization
    print ('API Function: get_project\n'),
    partmsg = 'get_project'

    result = requests.get(main_url + '/app/projects/get_projects/',
                          headers=header,
                          params={'organization_id': organization_id})
    check_status(result, partmsg, log)

    # Populate project by search buildings result
    print ('API Function: add_buildings_to_project\n'),
    partmsg = 'add_buildings_to_project'
    projectbldg_payload = {'project': {'status': 'active',
                                       'project_slug': project_slug,
                                       'slug': project_slug,
                                       'select_all_checkbox': True,
                                       'selected_buildings': [],
                                       'filter_params': {'use_description': 'CONDO'}},
                           'organization_id': organization_id}

    result = requests.post(main_url + '/app/projects/add_buildings_to_project/',
                           headers=header,
                           data=json.dumps(projectbldg_payload))
    time.sleep(10)
    check_status(result, partmsg, log)

    # Get the percent/progress of buildings added to project
    progress = requests.post(main_url + '/app/projects/get_adding_buildings_to_project_status_percentage/',
                             headers=header,
                             data=json.dumps({'project_loading_cache_key': result.json()['project_loading_cache_key']}))
    log.debug(pprint.pformat(progress.json()))

    # Export
    print ('\n-------Export-------\n')

    # Export all buildings.
    print ('API Function: export_buildings\n'),
    partmsg = 'export_buildings'
    export_payload = {'export_name': 'project_buildings',
                      'export_type': "csv",
                      'select_all_checkbox': True,
                      'filter_params': {'project__slug': project_slug}}

    result = requests.post(main_url + '/app/export_buildings/',
                           headers=header,
                           data=json.dumps(export_payload))
    check_status(result, partmsg, log)
    if result.json()['total_buildings'] != 58:
        log.warning('Export Buildings: ' + str(result.json()['total_buildings']) + " ; expected 58")

        # Get exportID
    exportID = result.json()['export_id']

    progress = requests.post(main_url + '/app/export_buildings/progress/',
                             headers=header,
                             data=json.dumps({'export_id': exportID}))
    log.debug(pprint.pformat(progress.json()))

    print ('API Function: export_buildings_download\n'),
    partmsg = 'export_buildings_download'

    count = 100
    while(count > 0):
        result = requests.post(main_url + '/app/export_buildings/download/',
                               headers=header,
                               data=json.dumps({'export_id': exportID}))
        if result.status_code == 200:
            break
        time.sleep(5)
        count -= 1

    check_status(result, partmsg, log)

    return project_slug
Exemple #18
0
def search_and_project(header, main_url, organization_id, log):
        # Search CanonicalBuildings
    print ('API Function: search_buildings\n'),
    partmsg = 'search_buildings'
    search_payload = {'filter_params': {u'address_line_1': u'94734 SE Honeylocust Street'}}
    try:
        result = requests.get(main_url + '/app/search_buildings/',
                              headers=header,
                              data=json.dumps(search_payload))
        check_status(result, partmsg, log)
    except:
        pass

    # Project
    print ('\n-------Project-------\n')

    # Create a Project for 'Condo' in 'use_description'
    print ('API Function: create_project\n'),
    partmsg = 'create_project'
    time1 = dt.datetime.now()
    newproject_payload = {'project': {'name': 'New Project_' + str(time1.day) + str(time1.second),
                                      'compliance_type': 'describe compliance type',
                                      'description': 'project description'},
                          'organization_id': organization_id}
    try:
        result = requests.post(main_url + '/app/projects/create_project/',
                               headers=header,
                               data=json.dumps(newproject_payload))
        check_status(result, partmsg, log)
    except:
        log.error("Could not create a project. Following API in SEARCH_AND_PROJECT not tested")
        return

        # Get project slug
    project_slug = result.json()['project_slug']

    # Get the projects for the organization
    print ('API Function: get_project\n'),
    partmsg = 'get_project'
    try:
        result = requests.get(main_url + '/app/projects/get_projects/',
                              headers=header,
                              params={'organization_id': organization_id})
        check_status(result, partmsg, log)
    except:
        pass

        # Populate project by search buildings result
    print ('API Function: add_buildings_to_project\n'),
    partmsg = 'add_buildings_to_project'
    projectbldg_payload = {'project': {'status': 'active',
                                       'project_slug': project_slug,
                                       'slug': project_slug,
                                       'select_all_checkbox': True,
                                       'selected_buildings': [],
                                       'filter_params': {'use_description': 'CONDO'}},
                           'organization_id': organization_id}
    try:
        result = requests.post(main_url + '/app/projects/add_buildings_to_project/',
                               headers=header,
                               data=json.dumps(projectbldg_payload))
        time.sleep(10)
        check_status(result, partmsg, log)

        # Get the percent/progress of buildings added to project
        progress = requests.post(main_url + '/app/projects/get_adding_buildings_to_project_status_percentage/',
                                 headers=header,
                                 data=json.dumps({'project_loading_cache_key': result.json()['project_loading_cache_key']}))
        log.debug(pprint.pformat(progress.json()))
    except:
        pass

    # Export
    print ('\n-------Export-------\n')

    # Export all buildings.
    print ('API Function: export_buildings\n'),
    partmsg = 'export_buildings'
    export_payload = {'export_name': 'project_buildings',
                      'export_type': "csv",
                      'select_all_checkbox': True,
                      'filter_params': {'project__slug': project_slug}}
    try:
        result = requests.post(main_url + '/app/export_buildings/',
                               headers=header,
                               data=json.dumps(export_payload))
        check_status(result, partmsg, log)
        if result.json()['total_buildings'] != 58:
            log.warning('Export Buildings: ' + str(result.json()['total_buildings']) + " ; expected 58")

            # Get exportID
        exportID = result.json()['export_id']

        time.sleep(25)
        progress = requests.post(main_url + '/app/export_buildings/progress/',
                                 headers=header,
                                 data=json.dumps({'export_id': exportID}))
        log.debug(pprint.pformat(progress.json()))
        time.sleep(25)

    except:
        log.error("Could not export building. Following API in SEARCH_AND_PROJECT not tested")
        return project_slug

    print ('API Function: export_buildings_download\n'),
    partmsg = 'export_buildings_download'
    try:
        result = requests.post(main_url + '/app/export_buildings/download/',
                               headers=header,
                               data=json.dumps({'export_id': exportID}))
        check_status(result, partmsg, log)
    except:
        pass

    return project_slug
Exemple #19
0
pm_map_file = os.path.relpath(os.path.join(sample_dir, 'portfolio-manager-mapping.csv'))
assert (os.path.isfile(pm_map_file)), 'Missing file '+pm_map_file

# -- Accounts
print ('\n-------Accounts-------\n')
organization_id = account(header, main_url, username, log)

## Create a dataset
print ('API Function: create_dataset')
partmsg = 'create_dataset'
payload={'organization_id': organization_id,
		 'name': 'API Test'}
result = requests.post(main_url+'/app/create_dataset/',
					  headers=header,
					  data=json.dumps(payload))
check_status(result, partmsg, log)

# Get the dataset id to be used
dataset_id = result.json()['id']

# Upload and test the raw building file
print ('\n|---Covered Building File---|\n')
upload_match_sort(header, main_url, organization_id, dataset_id, raw_building_file, 'Assessed Raw', raw_map_file, log)

# Upload and test the portfolio manager file
print ('\n|---Portfolio Manager File---|\n')
upload_match_sort(header, main_url, organization_id, dataset_id, pm_building_file, 'Portfolio Raw', pm_map_file, log)

# Run search and project tests
project_slug = search_and_project(header, main_url, organization_id, log)
Exemple #20
0
def account(header, main_url, username, log):
    # Retrieve the user profile
    print ('API Function: get_user_profile\n'),
    partmsg = 'get_user_profile'
    result = requests.get(main_url + '/app/accounts/get_user_profile',
                          headers=header)
    check_status(result, partmsg, log)

    # Retrieve the organizations
    print ('API Function: get_organizations\n'),
    partmsg = 'get_organizations'
    result = requests.get(main_url + '/app/accounts/get_organizations/',
                          headers=header)
    check_status(result, partmsg, log, PIIDflag='organizations')

    # # Get the organization id to be used.
    # # NOTE: Loop through the organizations and get the org_id
    # # where the organization owner is 'username' else get the first organization.
    orgs_result = result.json()
    for ctr in range(len(orgs_result['organizations'])):
        if orgs_result['organizations'][ctr]['owners'][0]['email'] == username:
            organization_id = orgs_result['organizations'][ctr]['org_id']
            break
        else:
            organization_id = orgs_result['organizations'][0]['org_id']

    # Get the organization details
    partmsg = 'get_organization (2)'
    mod_url = main_url + '/app/accounts/get_organization/?organization_id=' + str(organization_id)
    result = requests.get(mod_url,
                          headers=header)
    check_status(result, partmsg, log)

    # Change user profile
    # NOTE: Make sure these credentials are ok.
    print ('API Function: update_user\n'),
    partmsg = 'update_user'
    user_payload = {'user': {'first_name': 'Sherlock',
                             'last_name': 'Holmes',
                             'email': username}}
    result = requests.post(main_url + '/app/accounts/update_user/',
                           headers=header,
                           data=json.dumps(user_payload))
    check_status(result, partmsg, log)

    # Get organization users
    print ('API Function: get_organizations_users\n'),
    partmsg = 'get_organizations_users'
    org_payload = {'organization_id': organization_id}
    result = requests.post(main_url + '/app/accounts/get_organizations_users/',
                           headers=header,
                           data=json.dumps(org_payload))
    check_status(result, partmsg, log, PIIDflag='users')

    # Get organizations settings
    print ('API Function: get_query_treshold\n'),
    partmsg = 'get_query_threshold'
    result = requests.get(main_url + '/app/accounts/get_query_threshold/',
                          headers=header,
                          params={'organization_id': organization_id})
    check_status(result, partmsg, log)

    # Get shared fields
    print ('API Function: get_shared_fields\n'),
    partmsg = 'get_shared_fields'
    result = requests.get(main_url + '/app/accounts/get_shared_fields/',
                          headers=header,
                          params={'organization_id': organization_id})
    check_status(result, partmsg, log)

    return organization_id
Exemple #21
0
def data_quality(header, main_url, organization_id, log):

    # get the data quality rules for the organization
    print('API Function: get_data_quality_rules\n')
    partmsg = 'get_data_quality_rules'
    params = {'organization_id': organization_id}
    result = requests.get(main_url +
                          '/api/v2/data_quality_checks/data_quality_rules',
                          headers=header,
                          params=params)
    check_status(result, partmsg, log)
    rules = result.json()['rules']
    prop_rules = rules['properties']
    tax_rules = rules['taxlots']

    # create a new rule
    print('API Function: create_data_quality_rule\n')
    partmsg = 'create_data_quality_rule'
    params = {'organization_id': organization_id}
    new_rule = {
        'field': 'city',
        'enabled': True,
        'data_type': 'string',
        'rule_type': 1,
        'required': False,
        'not_null': True,
        'min': None,
        'max': None,
        'text_match': None,
        'severity': 'warning',
        'units': '',
        'label': None
    }
    payload = {
        'data_quality_rules': {
            'properties': prop_rules + [new_rule],
            'taxlots': tax_rules
        }
    }
    result = requests.post(
        main_url + '/api/v2/data_quality_checks/save_data_quality_rules/',
        headers=header,
        params=params,
        json=payload)
    check_status(result, partmsg, log)

    # delete the new rule
    print('API Function: delete_data_quality_rule\n')
    partmsg = 'delete_data_quality_rule'
    params = {'organization_id': organization_id}
    payload = {
        'data_quality_rules': {
            'properties': prop_rules,
            'taxlots': tax_rules
        }
    }
    result = requests.post(
        main_url + '/api/v2/data_quality_checks/save_data_quality_rules/',
        headers=header,
        params=params,
        json=payload)
    check_status(result, partmsg, log)

    # get some property state ids
    result = requests.get(main_url + '/api/v2/property_states/',
                          headers=header)
    prop_state_ids = [prop['id'] for prop in result.json()['properties']]

    # create a new data quality check process
    print('API Function: create_data_quality_check\n')
    partmsg = 'create_data_quality_check'
    params = {'organization_id': organization_id}
    payload = {'property_state_ids': prop_state_ids, 'taxlot_state_ids': []}
    result = requests.post(main_url + '/api/v2/data_quality_checks/',
                           headers=header,
                           params=params,
                           json=payload)
    check_status(result, partmsg, log)
    data_quality_id = result.json()['progress']['unique_id']

    # perform the data quality check
    print('API Function: perform_data_quality_check\n')
    partmsg = 'perform_data_quality_check'
    params = {
        'organization_id': organization_id,
        'data_quality_id': data_quality_id
    }
    result = requests.get(main_url + '/api/v2/data_quality_checks/results/',
                          headers=header,
                          params=params)
    check_status(result, partmsg, log)