<commit_before>import moment from 'moment';

function queryResultLink() {
  return {
    restrict: 'A',
    link(scope, element, attrs) {
      const fileType = attrs.fileType ? attrs.fileType : 'csv';
      scope.$watch('queryResult && queryResult.getData()', (data) => {
        if (!data) {
          return;
        }

        if (scope.queryResult.getId() == null) {
          element.attr('href', '');
        } else {
          let url;
          if (scope.query.id) {
            url = `api/queries/${scope.query.id}/results/${scope.queryResult.getId()}.${fileType}${scope.embed
              ? `?api_key=${scope.apiKey}`
              : ''}`;
          } else {
            url = `api/query_results/${scope.queryResult.getId()}.${fileType}`;
          }
          element.attr('href', url);
          element.attr(
            'download',
            `${scope.query.name.replace(' ', '_') +
              moment(scope.queryResult.getUpdatedAt()).format('_YYYY_MM_DD')}.${fileType}`,
          );
        }
      });
    },
  };
}

export default function init(ngModule) {
  ngModule.directive('queryResultLink', queryResultLink);
}<commit_msg>Fix export query results output file name (#2916)<commit_after>import moment from 'moment';

function queryResultLink() {
  return {
    restrict: 'A',
    link(scope, element, attrs) {
      const fileType = attrs.fileType ? attrs.fileType : 'csv';
      scope.$watch('queryResult && queryResult.getData()', (data) => {
        if (!data) {
          return;
        }

        if (scope.queryResult.getId() == null) {
          element.attr('href', '');
        } else {
          let url;
          if (scope.query.id) {
            url = `api/queries/${scope.query.id}/results/${scope.queryResult.getId()}.${fileType}${scope.embed
              ? `?api_key=${scope.apiKey}`
              : ''}`;
          } else {
            url = `api/query_results/${scope.queryResult.getId()}.${fileType}`;
          }
          element.attr('href', url);
          element.attr(
            'download',
            `${scope.query.name.replace(/ /g, '_') +
              moment(scope.queryResult.getUpdatedAt()).format('_YYYY_MM_DD')}.${fileType}`,
          );
        }
      });
    },
  };
}

export default function init(ngModule) {
  ngModule.directive('queryResultLink', queryResultLink);
}
{'fraction_non_alphanumeric': 0.17300299017513884, 'fraction_numerical': 0.0017086715079026058, 'mean_word_length': 2.0494791666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '820848', 'n_tokens_mistral': 698, 'n_tokens_neox': 724, 'n_words': 108}