Add api key pulling for desktop client (#563)

This commit is contained in:
Max Wofford 2025-10-03 21:05:28 -04:00 committed by GitHub
parent ae76f20946
commit 6ce033c98d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 13 deletions

View 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

View file

@ -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 }

View file

@ -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

View file

@ -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