You are here

function rec_transfer_import_results in Recommender API 7.5

Same name and namespace in other branches
  1. 6.3 rec_transfer/rec_transfer.module \rec_transfer_import_results()
  2. 7.4 rec_transfer/rec_transfer.module \rec_transfer_import_results()
1 call to rec_transfer_import_results()
rec_transfer_download in rec_transfer/rec_transfer.module

File

rec_transfer/rec_transfer.module, line 355
This is the module file for Recommender Data Transfer

Code

function rec_transfer_import_results($what, $command_id, $recommender_id) {
  $apikey = variable_get('rec_transfer_apikey');
  if ($what == 'similarity') {
    $table = 'recommender_similarity';
    $suffix = 'simi';
    $field = 'number3';
  }
  else {
    if ($what == 'prediction') {
      $table = 'recommender_prediction';
      $suffix = 'pred';
      $field = 'number4';
    }
    else {
      assert(FALSE);
    }
  }
  $download_file = "{$apikey}-{$command_id}.{$suffix}";
  $save_file = variable_get('file_temporary_path', file_directory_temp()) . '/' . $download_file;
  $download_success = rec_transfer_download_results($download_file, $save_file);
  if ($download_success) {

    // FIXME: if new data import fails, we'll need to re-import old data.
    // also, perhaps need to think about incremental update. then we don't want to delete old data.
    db_delete($table)
      ->condition('app_id', $recommender_id)
      ->execute();
    $count = 0;
    $insert = db_insert($table)
      ->fields(array(
      'app_id',
      'source_id',
      'target_id',
      'score',
      'updated',
    ));
    $fp = fopen($save_file, 'r');
    while (($row = fgetcsv($fp)) !== FALSE) {

      // TODO: summarize data stats
      $count++;
      $insert
        ->values(array(
        $recommender_id,
        $row[0],
        $row[1],
        $row[2],
        $row[3],
      ));
      if ($count % 1000 == 0) {
        $insert
          ->execute();

        // periodically flush the results.
      }
    }
    $insert
      ->execute();
    fclose($fp);
    async_command_update_command($command_id, array(
      $field => $count,
    ));
    return TRUE;
  }
  else {
    return FALSE;
  }
}