mirror of
https://github.com/System-End/hackatime.git
synced 2026-04-19 19:55:16 +00:00
Add api key pulling for desktop client (#563)
This commit is contained in:
parent
ae76f20946
commit
6ce033c98d
4 changed files with 34 additions and 13 deletions
17
app/controllers/api/v1/authenticated/api_keys_controller.rb
Normal file
17
app/controllers/api/v1/authenticated/api_keys_controller.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -12,13 +12,14 @@ module Api
|
|||
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 }
|
||||
|
|
|
|||
|
|
@ -3,16 +3,12 @@ module Api
|
|||
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] || []
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -21,11 +17,6 @@ module Api
|
|||
|
||||
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
|
||||
|
|
@ -41,6 +32,17 @@ module Api
|
|||
.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
|
||||
|
|
|
|||
|
|
@ -162,6 +162,7 @@ Rails.application.routes.draw do
|
|||
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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue