<commit_before>Template.residentFeelings.onCreated(function () {
  // Get reference to template instance
  const templateInstance = this;

  // Get resident ID from template instance
  const residentId = templateInstance.data.residentId;

  // Set up reactive variable to hold resident feelings counts
  templateInstance.residentFeelingsCounts = new ReactiveVar();

  // Fetch resident feelings counts from server
  Meteor.call('getFeelingsCountsByResidentId', residentId, function (error, residentFeelingsCounts) {
    // Update resident feelings counts with returned value from method call
    templateInstance.residentFeelingsCounts.set(residentFeelingsCounts);
  });
});

Template.residentFeelings.onRendered(function () {
  // Get resident ID
  const residentId = this.data.residentId;

  this.autorun(() => {
    // Make sure subscription data is ready
    if (this.subscriptionsReady()) {
      // Get all feelings
      const residentFeelings = Feelings.find({ residentId }).fetch();

      // Group feelings by type and calculate percentage for each feeling
      const residentFeelingsCounts = d3.nest()
        // TODO: add localization to the feeling names (possibly below)
        .key(function (datum) {
          // Get feeling
          const feeling = datum.feeling;

          // Get localization string for feeling
          const feelingL10n = TAPi18n.__(`residentFeelings-chart-feelingName-${ feeling }`);

          return feelingL10n;
        })
        // TODO: Determine whether to display counts or percentages
        .rollup(function (values) { return values.length / residentFeelings.length })
        .entries(residentFeelings);

      // Make sure feeling counts are available (not an empty array)
      if (residentFeelingsCounts.length > 0) {
        // clear any previous chart
        this.$('#residentFeelingsChart').empty();

        // Render chart with current feeling counts
        MG.data_graphic({
          title: TAPi18n.__('residentFeelings-chart-title'),
          data: residentFeelingsCounts,
          chart_type: 'bar',
          x_accessor: 'values',
          // TODO: Determine whether to display counts or percentages
          format: 'percentage',
          xax_format: d3.format('.0%'),
          y_accessor: 'key',
          height:300,
          full_width: true,
          target: '#residentFeelingsChart',
        });
      }
    }
  });
});

Template.residentFeelings.helpers({
  residentFeelingsCount () {
    // Get reference to template instance
    const templateInstance = Template.instance();

    // Get resident ID from template instance
    const residentId = templateInstance.data.residentId;

    // return a count of resident feelings
    return Feelings.find({ residentId }).count();
  }
});
<commit_msg>Refactor to use data returned from server aggregation<commit_after>Template.residentFeelings.onCreated(function () {
  // Get reference to template instance
  const templateInstance = this;

  // Get resident ID from template instance
  const residentId = templateInstance.data.residentId;

  // Set up reactive variable to hold resident feelings counts
  templateInstance.residentFeelingsCounts = new ReactiveVar();

  // Method to clear and render chart
  templateInstance.clearAndRenderChart = function (chartData) {
    // clear any previous chart
    this.$('#residentFeelingsChart').empty();

    // Render chart with current feeling counts
    MG.data_graphic({
      title: TAPi18n.__('residentFeelings-chart-title'),
      data: chartData,
      chart_type: 'bar',
      x_accessor: 'value',
      format: 'percentage',
      xax_format: d3.format('.0%'),
      y_accessor: 'localizedFeeling',
      height:300,
      full_width: true,
      target: '#residentFeelingsChart',
    });
  };

  templateInstance.localizeChartData = function (chartData) {
    // Create localized data for chart
    const localizedChartData = _.map(chartData, function (datum) {
      // Get feeling
      const feeling = datum.key;

      // Get localization string for feeling
      const feelingL10n = TAPi18n.__(`residentFeelings-chart-feelingName-${ feeling }`);

      // Update feeling with localized name
      datum.localizedFeeling = feelingL10n;

      return datum;
    });

    return localizedChartData
  };

  // Fetch resident feelings counts from server
  Meteor.call('getFeelingsCountsByResidentId', residentId, function (error, residentFeelingsCounts) {
    // Update resident feelings counts with returned value from method call
    templateInstance.residentFeelingsCounts.set(residentFeelingsCounts);
  });
});

Template.residentFeelings.onRendered(function () {
  // Get reference to template instance
  const templateInstance = this;

  templateInstance.autorun(() => {
    // Get resident feelings counts
    const residentFeelingsCounts = templateInstance.residentFeelingsCounts.get();

    // Make sure resident feelings counts data is available
    if (residentFeelingsCounts) {
      // Create localized data for chart
      const localizedChartData = templateInstance.localizeChartData(residentFeelingsCounts);

      // Render chart with localized data
      templateInstance.clearAndRenderChart(localizedChartData);
    }
  });
});

Template.residentFeelings.helpers({
  residentFeelingsCount () {
    // Get reference to template instance
    const templateInstance = Template.instance();

    // Get resident ID from template instance
    const residentId = templateInstance.data.residentId;

    // return a count of resident feelings
    return Feelings.find({ residentId }).count();
  }
});

{'fraction_non_alphanumeric': 0.0934763181411975, 'fraction_numerical': 0.005004468275245755, 'mean_word_length': 3.1575037147102525, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '2223062', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1462, 'n_words': 418}