Class: GitWakaTime::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/gitwakatime/query.rb

Overview

Integrates the nested hash from mapper with actions api

Instance Method Summary collapse

Constructor Details

#initialize(commits, project, _path = nil) ⇒ Query

Returns a new instance of Query.



8
9
10
11
12
13
# File 'lib/gitwakatime/query.rb', line 8

def initialize(commits, project, _path = nil)
  @commits   = commits
  @api_limit = 35
  @project   = project
  @requests   = time_params
end

Instance Method Details

#actionsObject



24
25
26
# File 'lib/gitwakatime/query.rb', line 24

def actions
  Action.where('time >= ? and time <= ? ', @start_at, @end_at).where(project: @project)
end

#getObject



15
16
17
18
19
20
21
22
# File 'lib/gitwakatime/query.rb', line 15

def get
  @requests.each do |params|
    Log.new "Requesting actions #{params[:start].to_date} to #{params[:end].to_date}".red
    Durations.new(params).load_actions
  end

  Durations.new(actions: actions).actions_to_durations
end

#time_paramsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gitwakatime/query.rb', line 28

def time_params
  commits = @commits.map(&:date)
  d_commits = CommitedFile.select(:dependent_date).all.map { |f| f.values[:dependent_date] }.compact
  timestamps = (commits + d_commits.flatten).uniq.sort

  # Don't query before the Wakatime Epoch
  @start_at = timestamps.first >= Time.new(2013, 5, 1) ? timestamps.first : Time.new(2013, 5, 1)
  @end_at = timestamps.last

  # Always have a date range great than 1 as the num request will be 0/1 otherwise
  num_requests = ((@end_at.to_date + 1) - @start_at.to_date) / @api_limit
  i = 0

  request_params = num_requests.to_f.ceil.times.map do

    params = {
      start: (@start_at.to_date + (i * @api_limit)).to_time.beginning_of_day,
      end:  (@start_at.to_date + ((i + 1) * @api_limit)).to_time.end_of_day,
      project: @project,
      show: 'file,branch,project,time,id'
    }
    i += 1
    params

  end
  request_params
end