joshsoftware/code-curiosity

View on GitHub
app/services/multi_line_chart/contribution.rb

Summary

Maintainability
A
35 mins
Test Coverage
class MultiLineChart::Contribution
  class << self
    def get
      contributions = Commit.collection.aggregate([match, project, group, sort])
      contributions.collect do |r|
        [
          "#{Date::ABBR_MONTHNAMES[r['_id']['month']]} #{r['_id']['year']}",
          r['total']
        ]
      end
    end

    private

    def match
      {
        '$match' => {
        'commit_date' => { '$gt' => Date.parse('Apr 2016') }
      }
      }
    end

    def project
      {
        '$project' => {
        'month' => { '$month' => '$commit_date'},
        'year' => {'$year' => '$commit_date'},
        'score' => 1
      }
      }
    end

    def group
      {
        '$group' => {
        _id: {'month' => '$month', 'year' => '$year'},
        total: { '$sum' => '$score' }
      }
      }
    end

    def sort
      {
        '$sort' => {
        '_id.year' => 1,
        '_id.month' => 1
      }
      }
    end
  end
end