dedicated projects api

This commit is contained in:
Echo
2025-07-07 15:45:51 -04:00
parent 2bdcf4c97f
commit ddad067e66
2 changed files with 37 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
class Api::V1::StatsController < ApplicationController
before_action :ensure_authenticated!, only: [ :show ], unless: -> { Rails.env.development? }
before_action :set_user, only: [ :user_stats, :user_spans, :trust_factor ]
before_action :set_user, only: [ :user_stats, :user_spans, :trust_factor, :user_projects, :user_project ]
def show
# take either user_id with a start date & end date
@@ -99,6 +99,40 @@ class Api::V1::StatsController < ApplicationController
}
end
def user_projects
return render json: { error: "User not found" }, status: :not_found unless @user
since = 30.days.ago.beginning_of_day
projects = @user.heartbeats
.where("time >= ?", since)
.where.not(project: [ nil, "" ])
.select(:project)
.distinct
.pluck(:project)
render json: { projects: projects }
end
def user_project
return render json: { error: "User not found" }, status: :not_found unless @user
project_name = params[:project_name]
return render json: { error: "whats the name?" }, status: :bad_request unless project_name.present?
heartbeats = @user.heartbeats.where(project: project_name)
return render json: { error: "found nuthin" }, status: :not_found if heartbeats.empty?
repo_url = heartbeats.where.not(repo_url: [ nil, "" ]).order(time: :desc).limit(1).pluck(:repo_url).first
last_commit = heartbeats.where.not(commit: [ nil, "" ]).order(time: :desc).limit(1).pluck(:commit).first
languages = heartbeats.where.not(language: [ nil, "" ]).distinct.pluck(:language)
render json: {
project: project_name,
repo_url: repo_url,
last_commit: last_commit,
languages: languages
}
end
private
def set_user

View File

@@ -129,6 +129,8 @@ Rails.application.routes.draw do
get "users/:username/stats", to: "stats#user_stats"
get "users/:username/heartbeats/spans", to: "stats#user_spans"
get "users/:username/trust_factor", to: "stats#trust_factor"
get "users/:username/projects", to: "stats#user_projects"
get "users/:username/project/:project_name", to: "stats#user_project"
get "users/lookup_email/:email", to: "users#lookup_email", constraints: { email: /[^\/]+/ }
get "users/lookup_slack_uid/:slack_uid", to: "users#lookup_slack_uid"