From ddad067e666deeb88143e8e4fe10ac41ab264641 Mon Sep 17 00:00:00 2001 From: Echo Date: Mon, 7 Jul 2025 15:45:51 -0400 Subject: [PATCH] dedicated projects api --- app/controllers/api/v1/stats_controller.rb | 36 +++++++++++++++++++++- config/routes.rb | 2 ++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/stats_controller.rb b/app/controllers/api/v1/stats_controller.rb index f6fc4d0..85fa59b 100644 --- a/app/controllers/api/v1/stats_controller.rb +++ b/app/controllers/api/v1/stats_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 1b72da1..2f5856f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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"