Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/controllers/api/v1/authenticated/api_keys_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Api
module V1
module Authenticated
class ApiKeysController < ApplicationController
def index
render json: { token: api_key.token }
end

private

def api_key
@api_key ||= current_user.api_keys.first || current_user.api_keys.create!
end
end
end
end
end
5 changes: 3 additions & 2 deletions app/controllers/api/v1/authenticated/heartbeats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ def latest
render json: {
id: heartbeat.id,
created_at: heartbeat.created_at,
time: heartbeat.time,
category: heartbeat.category,
project: heartbeat.project,
language: heartbeat.language,
editor: heartbeat.editor,
operating_system: heartbeat.operating_system,
machine: heartbeat.machine,
file: heartbeat.file,
duration: heartbeat.duration
entity: heartbeat.entity
}
else
render json: { heartbeat: nil }
Expand Down
24 changes: 13 additions & 11 deletions app/controllers/api/v1/authenticated/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ module V1
module Authenticated
class ProjectsController < ApplicationController
def index
projects = current_user.heartbeats
.where.not(project: [ nil, "" ])
.group(:project)
.map { |project|
projects = time_per_project.map { |project, _|
{
name: project,
total_seconds: time_per_project[project] || 0,
most_recent_heartbeat: most_recent_heartbeat_per_project[project] ? Time.at(most_recent_heartbeat_per_project[project]).strftime("%Y-%m-%dT%H:%M:%SZ") : nil,
percentage: time_per_project.sum { |_, secs| secs }.zero? ? 0 : ((time_per_project[project] || 0) / time_per_project.sum { |_, secs| secs }.to_f * 100).round(2),
repo: project_repo_mappings[project]&.repo
languages: languages_per_project[project] || []
}
}

Expand All @@ -21,11 +17,6 @@ def index

private

def project_repo_mappings
@project_repo_mappings ||= current_user.project_repo_mappings
.index_by(&:project)
end

def time_per_project
@time_per_project ||= current_user.heartbeats
.with_valid_timestamps
Expand All @@ -41,6 +32,17 @@ def most_recent_heartbeat_per_project
.group(:project)
.maximum(:time)
end

def languages_per_project
@languages_per_project ||= current_user.heartbeats
.with_valid_timestamps
.where.not(project: [ nil, "" ])
.where.not(language: [ nil, "" ])
.distinct
.pluck(:project, :language)
.group_by(&:first)
.transform_values { |pairs| pairs.map(&:last).uniq }
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def matches?(request)
get "projects", to: "projects#index"
# get "projects/:name", to: "projects#show", constraints: { name: /.+/ }
get "heartbeats/latest", to: "heartbeats#latest"
get "api_keys", to: "api_keys#index"
end
end

Expand Down