Ejemplo n.º 1
0
def geocode():
  start = time.time()
  jsonp_callback = request.args.get('callback', None)
  address = request.args.get('address', '')
  lat = request.args.get('lat', '')
  lng = request.args.get('lng', '')
  if address == '' and not (lat != '' and lng != ''):
    abort(400)
  if address:
    location_string = ', '.join([address, app.config['GEOCODING_DEFAULT_CITY']])
  else:
    location_string = ', '.join([lat, lng])
  obj = {
    'result': util.geocode(location_string)
  }
  obj['duration'] = int((time.time() - start) * 1000)
  json_output = json.dumps(obj, sort_keys=True)
  if jsonp_callback is not None:
    json_output = jsonp_callback + '(' + json_output + ')'
  response = make_response(json_output, 200)
  response.mimetype = 'application/json'
  response.headers['Pragma'] = 'no-cache'
  response.headers['Expires'] = -1
  response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
  return response
Ejemplo n.º 2
0
def process_worksheet_row(row, worksheet_row_index):
    notification = dict(cep = row[CEP_INDEX], address = row[ADDRESS_INDEX], number = row[NUMBER_INDEX], number_for_cep= row[NUMBER_FOR_CEP_INDEX], district = row[DISTRICT_INDEX], state = row[STATE_INDEX], city = row[CITY_INDEX], lat_lng = row[LAT_LNG_INDEX])
    
    if (notification['city'] == '' and notification['cep'] != ''):
        address_dict = cep_to_address(notification['cep'])
        notification['address'] = address_dict['address']
        notification['district'] = address_dict['district']
        notification['city'] = address_dict['city']
        notification['state'] = address_dict['state']
        
        update_cell(worksheet_row_index, ADDRESS_INDEX+1, notification['address'])
        update_cell(worksheet_row_index, DISTRICT_INDEX+1, notification['district'])
        update_cell(worksheet_row_index, CITY_INDEX+1, notification['city'])
        update_cell(worksheet_row_index, STATE_INDEX+1, notification['state'])
        
        if (notification['city'] == 'Desconhecida'):
            return
    
    if (notification['city'] != 'Desconhecida' and notification['lat_lng'] == ''):
        print(build_complete_address(notification))
        address_geocode = geocode(build_complete_address(notification))
        notification['lat_lng'] = address_geocode
        update_cell(worksheet_row_index, LAT_LNG_INDEX+1, address_geocode)
        
    return notification
Ejemplo n.º 3
0
def process_worksheet_row(row, worksheet_row_index):

    notification = dict(
        address=row[ADDRESS_INDEX],
        city=row[CITY_INDEX],
        lat_lng=row[LAT_LNG_INDEX],
        number=row[NUMBER_INDEX],
        number_for_cep=row[NUMBER_FOR_CEP_INDEX],
        district=row[DISTRICT_INDEX],
        state=row[STATE_INDEX],
        cep=row[CEP_INDEX],
    )

    if notification["city"] == "" and notification["cep"] != "":
        address_dict = cep_to_address(notification["cep"])
        notification["address"] = address_dict["address"]
        notification["district"] = address_dict["district"]
        notification["city"] = address_dict["city"]
        notification["state"] = address_dict["state"]

        update_cell(worksheet_row_index, ADDRESS_INDEX + 1, notification["address"])
        update_cell(worksheet_row_index, DISTRICT_INDEX + 1, notification["district"])
        update_cell(worksheet_row_index, CITY_INDEX + 1, notification["city"])
        update_cell(worksheet_row_index, STATE_INDEX + 1, notification["state"])

        if notification["city"] == "Desconhecida":
            return

    if notification["city"] != "Desconhecida" and notification["lat_lng"] == "":
        address_geocode = geocode(build_complete_address(notification))

        notification["lat_lng"] = address_geocode
        update_cell(worksheet_row_index, LAT_LNG_INDEX + 1, address_geocode)

    return notification
Ejemplo n.º 4
0
 def __init__(self, owner, resource_type, title, url, tags):
     self.resource_type = resource_type
     self.active = True
     self.title = title
     self.url = url
     self.owner = owner
     self.tags = tags
     self.latitude, self.longitude = util.geocode(url)
Ejemplo n.º 5
0
def processa_linha(linha, indice_linha):
    vazamento = dict(address = linha[ADDRESS_VAZAMENTOS_INDEX], district = linha[DISTRICT_VAZAMENTOS_INDEX], state = linha[STATE_VAZAMENTOS_INDEX],city = linha[CITY_VAZAMENTOS_INDEX], lat_lng = linha[LAT_LNG_VAZAMENTOS_INDEX] )
    
    if (vazamento['city'] != 'Desconhecida' and vazamento['lat_lng'] == ''):
        print(monta_endereco(vazamento))   
        address_geocode = geocode(monta_endereco(vazamento))
        vazamento['lat_lng'] = address_geocode
        atualiza_celula(indice_linha, LAT_LNG_VAZAMENTOS_INDEX+1, address_geocode)
Ejemplo n.º 6
0
 def __init__(self, owner, resource_type, title, url, tags):
     self.resource_type = resource_type
     self.active = True
     self.title = title
     self.url = url
     self.owner = owner
     self.tags = tags
     self.latitude, self.longitude = util.geocode(url)
Ejemplo n.º 7
0
    def __init__(self, owner, resource_type, title, url):
        self.resource_type = resource_type
        self.title = title
        self.url = url
        self.owner = owner

        # get latitude/longitude from hostname
        try:
            self.latitude, self.longitude = util.geocode(url)
        except Exception as err:  # skip storage
            LOGGER.exception('Could not derive coordinates: %s', err)
Ejemplo n.º 8
0
    def __init__(self, owner, resource_type, title, url):
        self.resource_type = resource_type
        self.title = title
        self.url = url
        self.owner = owner

        # get latitude/longitude from hostname
        try:
            self.latitude, self.longitude = util.geocode(url)
        except Exception as err:  # skip storage
            LOGGER.exception('Could not derive coordinates: %s', err)
Ejemplo n.º 9
0
def api_geocode():
    start = time.time()
    jsonp_callback = request.args.get('callback', None)
    street = request.args.get('street', '')
    if street == '':
        abort(400)
    obj = {'result': util.geocode(street)}
    obj['duration'] = int((time.time() - start) * 1000)
    json_output = json.dumps(obj, sort_keys=True)
    if jsonp_callback is not None:
        json_output = jsonp_callback + '(' + json_output + ')'
    response = make_response(json_output, 200)
    response.mimetype = 'application/json'
    response.headers['Expires'] = util.expires_date(hours=24)
    response.headers['Cache-Control'] = util.cache_max_age(hours=24)
    return response
Ejemplo n.º 10
0
def processa_linha(linha, indice_linha):
    vazamento = dict(
        address=linha[ADDRESS_VAZAMENTOS_INDEX],
        district=linha[DISTRICT_VAZAMENTOS_INDEX],
        state=linha[STATE_VAZAMENTOS_INDEX],
        city=linha[CITY_VAZAMENTOS_INDEX],
        lat_lng=linha[LAT_LNG_VAZAMENTOS_INDEX],
    )

    if vazamento["city"] != "Desconhecida" and vazamento["lat_lng"] == "":
        print(monta_endereco(vazamento))
        address_geocode = geocode(monta_endereco(vazamento))
        vazamento["lat_lng"] = address_geocode
        atualiza_celula(indice_linha, LAT_LNG_VAZAMENTOS_INDEX + 1, address_geocode)

    return vazamento
Ejemplo n.º 11
0
def api_geocode():
    start = time.time()
    jsonp_callback = request.args.get("callback", None)
    street = request.args.get("street", "")
    if street == "":
        abort(400)
    obj = {"result": util.geocode(street)}
    obj["duration"] = int((time.time() - start) * 1000)
    json_output = json.dumps(obj, sort_keys=True)
    if jsonp_callback is not None:
        json_output = jsonp_callback + "(" + json_output + ")"
    response = make_response(json_output, 200)
    response.mimetype = "application/json"
    response.headers["Expires"] = util.expires_date(hours=24)
    response.headers["Cache-Control"] = util.cache_max_age(hours=24)
    return response
Ejemplo n.º 12
0
def api_geocode():
    start = time.time()
    jsonp_callback = request.args.get('callback', None)
    street = request.args.get('street', '')
    if street == '':
        abort(400)
    obj = {
        'result': util.geocode(street)
    }
    obj['duration'] = int((time.time() - start) * 1000)
    json_output = json.dumps(obj, sort_keys=True)
    if jsonp_callback is not None:
        json_output = jsonp_callback + '(' + json_output + ')'
    response = make_response(json_output, 200)
    response.mimetype = 'application/json'
    response.headers['Expires'] = util.expires_date(hours=24)
    response.headers['Cache-Control'] = util.cache_max_age(hours=24)
    return response
Ejemplo n.º 13
0
def update(resource_identifier):
    """update a resource"""

    update_counter = 0
    status = 'success'

    try:
        resource_identifier_dict = request.get_json()

        resource = Resource.query.filter_by(
            identifier=resource_identifier).first()

        for key, value in resource_identifier_dict.items():
            if key == 'tags':
                resource_tags = [t.name for t in resource.tags]

                tags_to_add = set(value) - set(resource_tags)
                tags_to_delete = set(resource_tags) - set(value)

                # Existing Tags: create relation else add new Tag
                all_tag_objs = Tag.query.all()
                for tag in tags_to_add:
                    tag_add_obj = None
                    for tag_obj in all_tag_objs:
                        if tag == tag_obj.name:
                            # use existing
                            tag_add_obj = tag_obj
                            break

                    if not tag_add_obj:
                        # add new
                        tag_add_obj = Tag(name=tag)
                        DB.session.add(tag_add_obj)

                    resource.tags.append(tag_add_obj)

                for tag in tags_to_delete:
                    tag_to_delete = Tag.query.filter_by(name=tag).first()
                    resource.tags.remove(tag_to_delete)

                update_counter += 1
            elif key == 'probes':
                # Remove all existing ProbeVars for Resource
                for probe_var in resource.probe_vars:
                    resource.probe_vars.remove(probe_var)

                # Add ProbeVars anew each with optional CheckVars
                for probe in value:
                    LOGGER.info('adding Probe class=%s parms=%s' %
                                (probe['probe_class'], str(probe)))
                    probe_vars = ProbeVars(resource, probe['probe_class'],
                                           probe['parameters'])
                    for check in probe['checks']:
                        check_vars = CheckVars(probe_vars,
                                               check['check_class'],
                                               check['parameters'])
                        probe_vars.check_vars.append(check_vars)

                    resource.probe_vars.append(probe_vars)

                update_counter += 1
            elif key == 'notify_emails':
                resource.set_recipients('email',
                                        [v for v in value if v.strip()])
            elif key == 'notify_webhooks':
                resource.set_recipients('webhook',
                                        [v for v in value if v.strip()])
            elif getattr(resource, key) != resource_identifier_dict[key]:
                # Update other resource attrs, mainly 'name'
                setattr(resource, key, resource_identifier_dict[key])
                min_run_freq = CONFIG['GHC_MINIMAL_RUN_FREQUENCY_MINS']
                if int(resource.run_frequency) < min_run_freq:
                    resource.run_frequency = min_run_freq
                update_counter += 1

        # Always update geo-IP: maybe failure on creation or
        # IP-address of URL may have changed.
        latitude, longitude = geocode(resource.url)
        if latitude != 0.0 and longitude != 0.0:
            # Only update for valid lat/lon
            resource.latitude = latitude
            resource.longitude = longitude
            update_counter += 1

    except Exception as err:
        LOGGER.error("Cannot update resource: %s", err, exc_info=err)
        DB.session.rollback()
        status = str(err)
        update_counter = 0
    # finally:
    #     DB.session.close()

    if update_counter > 0:
        err = db_commit()
        if err:
            status = str(err)

    return jsonify({'status': status})
Ejemplo n.º 14
0
def update(resource_identifier):
    """update a resource"""

    update_counter = 0
    status = 'success'

    try:
        resource_identifier_dict = request.get_json()

        resource = Resource.query.filter_by(
            identifier=resource_identifier).first()

        for key, value in resource_identifier_dict.items():
            if key == 'tags':
                resource_tags = [t.name for t in resource.tags]

                tags_to_add = set(value) - set(resource_tags)
                tags_to_delete = set(resource_tags) - set(value)

                # Existing Tags: create relation else add new Tag
                all_tag_objs = Tag.query.all()
                for tag in tags_to_add:
                    tag_add_obj = None
                    for tag_obj in all_tag_objs:
                        if tag == tag_obj.name:
                            # use existing
                            tag_add_obj = tag_obj
                            break

                    if not tag_add_obj:
                        # add new
                        tag_add_obj = Tag(name=tag)
                        DB.session.add(tag_add_obj)

                    resource.tags.append(tag_add_obj)

                for tag in tags_to_delete:
                    tag_to_delete = Tag.query.filter_by(name=tag).first()
                    resource.tags.remove(tag_to_delete)

                update_counter += 1
            elif key == 'probes':
                # Remove all existing ProbeVars for Resource
                for probe_var in resource.probe_vars:
                    resource.probe_vars.remove(probe_var)

                # Add ProbeVars anew each with optional CheckVars
                for probe in value:
                    LOGGER.info('adding Probe class=%s parms=%s' %
                                (probe['probe_class'], str(probe)))
                    probe_vars = ProbeVars(resource, probe['probe_class'],
                                           probe['parameters'])
                    for check in probe['checks']:
                        check_vars = CheckVars(
                            probe_vars, check['check_class'],
                            check['parameters'])
                        probe_vars.check_vars.append(check_vars)

                    resource.probe_vars.append(probe_vars)

                update_counter += 1
            elif key == 'notify_emails':
                resource.set_recipients('email',
                                        [v for v in value if v.strip()])
            elif key == 'notify_webhooks':
                resource.set_recipients('webhook',
                                        [v for v in value if v.strip()])
            elif getattr(resource, key) != resource_identifier_dict[key]:
                # Update other resource attrs, mainly 'name'
                setattr(resource, key, resource_identifier_dict[key])
                min_run_freq = CONFIG['GHC_MINIMAL_RUN_FREQUENCY_MINS']
                if int(resource.run_frequency) < min_run_freq:
                    resource.run_frequency = min_run_freq
                update_counter += 1

        # Always update geo-IP: maybe failure on creation or
        # IP-address of URL may have changed.
        latitude, longitude = geocode(resource.url)
        if latitude != 0.0 and longitude != 0.0:
            # Only update for valid lat/lon
            resource.latitude = latitude
            resource.longitude = longitude
            update_counter += 1

    except Exception as err:
        LOGGER.error("Cannot update resource: %s", err, exc_info=err)
        DB.session.rollback()
        status = str(err)
        update_counter = 0
    # finally:
    #     DB.session.close()

    if update_counter > 0:
        err = db_commit()
        if err:
            status = str(err)

    return jsonify({'status': status})