<commit_before>  
  function Geocoding(address,table) {
    if (!georeferencing) {
      this.address = address;
      this.table = table_name;
			

      // Get the total and start loader
      this.getTotalRecords();
      
      // Georeferencing state -> true
      georeferencing = true;
      $('p.geo').addClass('loading');
    }
  }



	/*============================================================================*/
	/* Start worker geocoding	*/
	/*============================================================================*/
	Geocoding.prototype.getTotalRecords = function() {
	  var me = this;

		$.ajax({
      method: "GET",
      url: global_api_url+'queries?sql='+escape("SELECT cartodb_georef_status from "+this.table+" where cartodb_georef_status is NULL OR cartodb_georef_status=false"),
      headers: {'cartodbclient':true},
      dataType:'jsonp',
      data: {rows_per_page:100},
      success: function(result) {
				me.requestId = createUniqueId();
		    window.ops_queue.startGeoreferencing(me.requestId,result.total_rows);
		    me.getRecords();
      },
			error: function(e) {
				createGeoreferencedColumn();
			}
    });

		// Column to know if this record has been georeferenced or not
		function createGeoreferencedColumn() {
			var params = {};
			params['name'] = 'cartodb_georef_status';
			params['type'] = "boolean";
			$.ajax({
        dataType: 'json',
        type: 'POST',
        dataType: "text",
        headers: {"cartodbclient": true},
        url: '/api/v1/tables/'+me.table+'/columns',
        data: params,
        success: function(data) {
					me.getTotalRecords();
				},
        error: function(e, textStatus) {}
      });
		}
	}



  
  
  /*============================================================================*/
	/* Start worker geocoding	*/
	/*============================================================================*/
	Geocoding.prototype.getRecords = function() {
	  var me = this;
		
		var template = _.templateSettings = {
		  interpolate : /\{(.+?)\}/g
		};
		
	
		$.ajax({
      method: "GET",
      url: global_api_url+'queries?sql='+escape("SELECT * from "+this.table+" where cartodb_georef_status is NULL OR cartodb_georef_status=false"),
      headers: {'cartodbclient':true},
      dataType:'jsonp',
      data: {rows_per_page:100},
      success: function(result) {
        var rows = result.rows,
						addresses = [];
				_.each(rows,function(row,i){
					addresses.push({cartodb_id:row.cartodb_id,address:_.template(me.address,row)});
				});

        if (result.rows!=null && result.rows.length>0) {
          // Update loader
          me.processGeocoding(addresses);
        } else {
          window.ops_queue.finishGeoreferencing(me.requestId);
          $('p.geo').removeClass('loading');
          georeferencing = false;
        }
       }
    });
	}
	
	
	/*============================================================================*/
	/* Process geocoding	*/
	/*============================================================================*/
	Geocoding.prototype.processGeocoding = function(directions) {
    var me = this;
    var worker = new Worker("/javascripts/admin/show/view/geocoding_worker.js");

    worker.onmessage = function(event){
      
      if (event.data == "Finish" || event.data == "Stopped") {
        worker.terminate();
        delete worker;
        
        if (event.data == "Finish") {
          me.getRecords();
        } else {
          me.stopGeocoding();
        }
      } else {
        // Add new one to loader
        window.ops_queue.updateGeoreferencing(null);
        
        var params = {};
        if (event.data.query.results != undefined) {
          params['the_geom'] = {"type":"Point","coordinates":[event.data.query.results.ResultSet.Results.longitude,event.data.query.results.ResultSet.Results.latitude]};
        }
				params['cartodb_georef_status'] = true;
				$.ajax({
          dataType: 'json',
          type: 'PUT',
          dataType: "text",
          headers: {"cartodbclient": true},
          url: '/api/v1/tables/'+me.table+'/records/'+event.data.cartodb_id,
          data: params,
          success: function(data) {},
          error: function(e, textStatus) {}
        });
      }<commit_msg>catch up errors when geocoding fails, now working properly<commit_after>  
  function Geocoding(address,table) {
    if (!georeferencing) {
      this.address = address;
      this.table = table_name;
			

      // Get the total and start loader
      this.getTotalRecords();
      
      // Georeferencing state -> true
      georeferencing = true;
      $('p.geo').addClass('loading');
    }
  }



	/*============================================================================*/
	/* Start worker geocoding	*/
	/*============================================================================*/
	Geocoding.prototype.getTotalRecords = function() {
	  var me = this;

		$.ajax({
      method: "GET",
      url: global_api_url+'queries?sql='+escape("SELECT cartodb_georef_status from "+this.table+" where cartodb_georef_status is NULL OR cartodb_georef_status=false"),
      headers: {'cartodbclient':true},
      dataType:'jsonp',
      data: {rows_per_page:100},
      success: function(result) {
				me.requestId = createUniqueId();
		    window.ops_queue.startGeoreferencing(me.requestId,result.total_rows);
		    me.getRecords();
      },
			error: function(e) {
				createGeoreferencedColumn();
			}
    });

		// Column to know if this record has been georeferenced or not
		function createGeoreferencedColumn() {
			var params = {};
			params['name'] = 'cartodb_georef_status';
			params['type'] = "boolean";
			$.ajax({
        dataType: 'json',
        type: 'POST',
        dataType: "text",
        headers: {"cartodbclient": true},
        url: '/api/v1/tables/'+me.table+'/columns',
        data: params,
        success: function(data) {
					me.getTotalRecords();
				},
        error: function(e, textStatus) {}
      });
		}
	}



  
  
  /*============================================================================*/
	/* Start worker geocoding	*/
	/*============================================================================*/
	Geocoding.prototype.getRecords = function() {
	  var me = this;
		
		var template = _.templateSettings = {
		  interpolate : /\{(.+?)\}/g
		};
		
	
		$.ajax({
      method: "GET",
      url: global_api_url+'queries?sql='+escape("SELECT * from "+this.table+" where cartodb_georef_status is NULL OR cartodb_georef_status=false"),
      headers: {'cartodbclient':true},
      dataType:'jsonp',
      data: {rows_per_page:100},
      success: function(result) {
        var rows = result.rows,
						addresses = [];
				_.each(rows,function(row,i){
					addresses.push({cartodb_id:row.cartodb_id,address:_.template(me.address,row)});
				});

        if (result.rows!=null && result.rows.length>0) {
          // Update loader
          me.processGeocoding(addresses);
        } else {
          window.ops_queue.finishGeoreferencing(me.requestId);
          $('p.geo').removeClass('loading');
          georeferencing = false;
        }
       }
    });
	}
	
	
	/*============================================================================*/
	/* Process geocoding	*/
	/*============================================================================*/
	Geocoding.prototype.processGeocoding = function(directions) {
    var me = this;
    var worker = new Worker("/javascripts/admin/show/view/geocoding_worker.js?891892");

    worker.onmessage = function(event){
      
      if (event.data == "Finish" || event.data == "Stopped") {
        worker.terminate();
        delete worker;
        
        if (event.data == "Finish") {
          me.getRecords();
        } else {
          me.stopGeocoding();
        }
      } else {
        // Add new one to loader
        window.ops_queue.updateGeoreferencing(null);
        
        var params = {};
        if (event.data.query.results.ResultSet.Found != "0") {
          params['the_geom'] = {"type":"Point","coordinates":[event.data.query.results.ResultSet.Results.longitude,event.data.query.results.ResultSet.Results.latitude]};
        }
				params['cartodb_georef_status'] = true;
				$.ajax({
          dataType: 'json',
          type: 'PUT',
          dataType: "text",
          headers: {"cartodbclient": true},
          url: '/api/v1/tables/'+me.table+'/records/'+event.data.cartodb_id,
          data: params,
          success: function(data) {},
          error: function(e, textStatus) {}
        });
      }
{'fraction_non_alphanumeric': 0.24259388850011662, 'fraction_numerical': 0.0029157919290879403, 'mean_word_length': 2.7791978845306304, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3010293', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2428, 'n_words': 426}