<commit_before>class Article < ActiveRecord::Base
  has_many :revisions

  ####################
  # Instance methods #
  ####################
  def url
    escaped_title = title.gsub(" ", "_")
    "https://en.wikipedia.org/wiki/#{escaped_title}"
  end

  def update(data={})
    if data.blank?
      # Implement method for single-article lookup
    end

    puts "Updating #{title}"
    self.title = data["page_title"].gsub("_", " ")
    if(self.revisions.count > 0)
      self.views = self.revisions.order('date ASC').first.views
    else
      self.views = 0
    end
    self.save
  end

  def update_views(all_time)
    since = all_time ? CourseList.start.to_date : Date.today

    if all_time
      self.revisions.each do |r|
        r.views = 0
        r.save
      end
    end

    # Update views on all revisions and the article
    puts "Getting views for #{self.title} since #{since.strftime('%Y-%m-%d')}"
    new_views = Grok.get_views_since_date_for_article(self.title, since)
    new_views.each do |date, view_count|
      updated = all_time ? Time.now.utc + 1.day : since
      puts "#{date} - #{updated.strftime('%Y-%m-%d')} - #{view_count}"
      self.revisions.where("date <= ?", date).where("updated_at < ?", updated.strftime('%Y-%m-%d')).find_each do |r|
        r.views = r.views.nil? ? view_count : r.views + view_count
        r.save
      end
    end
    if(self.revisions.count > 0)
      self.views = self.revisions.order('date ASC').first.views
    else
      self.views = 0
    end
    self.save
  end

  # Cache methods<commit_msg>View diffing is more robust<commit_after>class Article < ActiveRecord::Base
  has_many :revisions

  ####################
  # Instance methods #
  ####################
  def url
    escaped_title = title.gsub(" ", "_")
    "https://en.wikipedia.org/wiki/#{escaped_title}"
  end

  def update(data={})
    if data.blank?
      # Implement method for single-article lookup
    end

    puts "Updating #{title}"
    self.title = data["page_title"].gsub("_", " ")
    if(self.revisions.count > 0)
      self.views = self.revisions.order('date ASC').first.views
    else
      self.views = 0
    end
    self.save
  end

  def update_views(all_time)
    if(self.views_updated_at.nil?)
      self.views_updated_at = CourseList.start.to_date
    end

    if(self.views_updated_at < Date.today)
      since = all_time ? CourseList.start.to_date : self.views_updated_at + 1.day

      if all_time
        self.revisions.each do |r|
          r.views = 0
          r.save
        end
      end

      # Update views on all revisions and the article
      puts "Getting views for #{self.title} since #{since.strftime('%Y-%m-%d')}"
      new_views = Grok.get_views_since_date_for_article(self.title, since)
      last = since
      new_views.each do |date, view_count|
        puts "#{date} - #{view_count}"
        self.revisions.where("date <= ?", date).find_each do |r|
          r.views = r.views.nil? ? view_count : r.views + view_count
          r.save
        end
        last = date.to_date > last ? date.to_date : last
      end
      self.views_updated_at = last
    end

    if(self.revisions.count > 0)
      self.views = self.revisions.order('date ASC').first.views
    else
      self.views = 0
    end
    self.save
  end

  # Cache methods
{'fraction_non_alphanumeric': 0.14783398970009087, 'fraction_numerical': 0.0036352620418055137, 'mean_word_length': 2.494179894179894, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 2, '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': '3909164', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1094, 'n_words': 237}