Skip to content
Open
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
3 changes: 2 additions & 1 deletion cmd/oss-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ func main() {
r.Get("/small-mrs/{org}/{repo}", handler.SmallMRsHandler)

// New endpoints
r.Get("/{org}/{repo}/code-change", handler.CodeChangeHandler)
r.Get(
"/{org}/{repo}/code-change", handler.CodeChangeHandler)
r.Get("/{org}/{repo}/code-change.md", handler.CodeChangeMarkdownHandler)
r.Get("/{org}/{repo}/coding-time", handler.CodingTimeHandler)
r.Get("/{org}/{repo}/commits", handler.CommitsHandler)
Expand Down
11 changes: 11 additions & 0 deletions internal/data/aggregated_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ type AggregatedStatistics = OverallWeeklyData[Statistics]
type WeeklyStatisticsData = WeeklyData[Statistics]
type AggregatedStatisticsQuery = Query[AggregatedStatisticsKey]

func (AggregatedStatisticsKey) Execute(
ctx context.Context,
db *DB,
q AggregatedStatisticsQuery,
org, repo string,
weeks []string,
team *int64,
) (any, error) {
return db.GetAggregatedStatistics(ctx, q, org, repo, weeks, team)
}

func (d DB) GetAggregatedStatistics(
ctx context.Context,
query AggregatedStatisticsQuery,
Expand Down
11 changes: 11 additions & 0 deletions internal/data/aggregated_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ type AggregatedValues = OverallWeeklyData[Value]
type WeeklyValueData = WeeklyData[Value]
type AggregatedValuesQuery = Query[AggregatedValuesKey]

func (AggregatedValuesKey) Execute(
ctx context.Context,
db *DB,
q AggregatedValuesQuery,
org, repo string,
weeks []string,
team *int64,
) (any, error) {
return db.GetAggregatedValues(ctx, q, org, repo, weeks, team)
}

func (d DB) GetAggregatedValues(
ctx context.Context,
query AggregatedValuesQuery,
Expand Down
24 changes: 23 additions & 1 deletion internal/data/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,37 @@
db *sql.DB
}

type Query[T any] struct {
type QueryKeys interface {
AggregatedStatisticsKey | AggregatedValuesKey | CycleTimeStatisticsKey
}

type Query[T QueryKeys] struct {
value string
_ T
}

func NewQuery[T QueryKeys](query string) Query[T] {

Check failure on line 26 in internal/data/db.go

View workflow job for this annotation

GitHub Actions / test

unreachable func: NewQuery
return Query[T]{
value: query,
}
}

func (q *Query[T]) Get() string {
return q.value
}

type Executable[T QueryKeys] interface {
QueryKeys
Execute(ctx context.Context,
db *DB,
q Query[T],
org string,
repo string,
weeks []string,
team *int64,
) (any, error)
}

var dbPool sync.Map

func NewDB(ctx context.Context, tenantRepo TenantRepo) (DB, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/data/deploy_freq.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

func BuildDeployFrequencyQuery(weeks []string) AggregatedValuesQuery {
func BuildDeployFrequencyQuery(weeks []string, _ *int64) AggregatedValuesQuery {

weeksPlaceholder := getWeeksPlaceholder(len(weeks))

Expand Down
15 changes: 13 additions & 2 deletions internal/data/detailed_cycle_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ type CycleTimeStatistics struct {

type AggregatedCycleTimeStatistics = OverallWeeklyData[CycleTimeStatistics]
type WeeklyCycleTimeStatistics = WeeklyData[CycleTimeStatistics]
type CycleTimeStatisticsKey struct {}
type CycleTimeStatisticsKey struct{}
type CycleTimeStatisticsQuery = Query[CycleTimeStatisticsKey]

func (CycleTimeStatisticsKey) Execute(
ctx context.Context,
db *DB,
q CycleTimeStatisticsQuery,
org, repo string,
weeks []string,
team *int64,
) (any, error) {
return db.GetDetailedCycleTime(ctx, q, org, repo, weeks, team)
}

func BuildDetailedCycleTimeQuery(weeks []string, team *int64) CycleTimeStatisticsQuery {
teamQuery := ""

Expand All @@ -28,7 +39,7 @@ func BuildDetailedCycleTimeQuery(weeks []string, team *int64) CycleTimeStatistic

weeksPlaceholder := getWeeksPlaceholder(len(weeks))

return CycleTimeStatisticsQuery{ value :fmt.Sprintf(`
return CycleTimeStatisticsQuery{value: fmt.Sprintf(`
WITH has_deployment AS (
SELECT DISTINCT repository_external_id, forge_type
FROM tenant_deployment_environments
Expand Down
40 changes: 2 additions & 38 deletions internal/oss-api/handler/code_chage.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handler

import (
"encoding/json"
"net/http"

"github.com/dxta-dev/app/internal/data"
Expand All @@ -10,6 +9,8 @@ import (
"github.com/dxta-dev/app/internal/util"
)

var CodeChangeHandler = OSSMetricHandler(data.BuildCodeChangeQuery)

func CodeChangeMarkdownHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

Expand Down Expand Up @@ -64,40 +65,3 @@ func CodeChangeMarkdownHandler(w http.ResponseWriter, r *http.Request) {
return
}
}

func CodeChangeHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

apiState, err := api.NewAPIState(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

weekParam := r.URL.Query().Get("weeks")

weeksArray := util.GetWeeksArray(weekParam)
weeksSorted := util.SortISOWeeks(weeksArray)

query := data.BuildCodeChangeQuery(weeksSorted, apiState.TeamId)

result, err := apiState.DB.GetAggregatedValues(
ctx,
query,
apiState.Org,
apiState.Repo,
weeksSorted,
apiState.TeamId,
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(result); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
42 changes: 1 addition & 41 deletions internal/oss-api/handler/coding_time.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
package handler

import (
"encoding/json"
"net/http"

"github.com/dxta-dev/app/internal/data"
api "github.com/dxta-dev/app/internal/oss-api"
"github.com/dxta-dev/app/internal/util"
)

func CodingTimeHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

apiState, err := api.NewAPIState(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

weekParam := r.URL.Query().Get("weeks")

weeksArray := util.GetWeeksArray(weekParam)
weeksSorted := util.SortISOWeeks(weeksArray)

query := data.BuildCodingTimeQuery(weeksSorted, apiState.TeamId)

result, err := apiState.DB.GetAggregatedStatistics(
ctx,
query,
apiState.Org,
apiState.Repo,
weeksSorted,
apiState.TeamId,
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(result); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
var CodingTimeHandler = OSSMetricHandler(data.BuildCodingTimeQuery)
42 changes: 2 additions & 40 deletions internal/oss-api/handler/commits.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handler

import (
"encoding/json"
"net/http"

"github.com/dxta-dev/app/internal/data"
Expand All @@ -10,6 +9,8 @@ import (
"github.com/dxta-dev/app/internal/util"
)

var CommitsHandler = OSSMetricHandler(data.BuildCommitsQuery)

func CommitsMarkdownHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

Expand Down Expand Up @@ -57,42 +58,3 @@ func CommitsMarkdownHandler(w http.ResponseWriter, r *http.Request) {
return
}
}

func CommitsHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

apiState, err := api.NewAPIState(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

weekParam := r.URL.Query().Get("weeks")

weeksArray := util.GetWeeksArray(weekParam)

weeksSorted := util.SortISOWeeks(weeksArray)

query := data.BuildCommitsQuery(weeksSorted, apiState.TeamId)

result, err := apiState.DB.GetAggregatedValues(
ctx,
query,
apiState.Org,
apiState.Repo,
weeksSorted,
apiState.TeamId,
)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(result); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
40 changes: 1 addition & 39 deletions internal/oss-api/handler/cycle_time.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
package handler

import (
"encoding/json"
"net/http"

"github.com/dxta-dev/app/internal/data"
api "github.com/dxta-dev/app/internal/oss-api"
"github.com/dxta-dev/app/internal/util"
)

func CycleTimeHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

apiState, err := api.NewAPIState(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

weekParam := r.URL.Query().Get("weeks")
weeksArray := util.GetWeeksArray(weekParam)
weeksSorted := util.SortISOWeeks(weeksArray)

query := data.BuildCycleTimeQuery(weeksSorted, apiState.TeamId)
result, err := apiState.DB.GetAggregatedStatistics(
ctx,
query,
apiState.Org,
apiState.Repo,
weeksSorted,
apiState.TeamId,
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(result); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
var CycleTimeHandler = OSSMetricHandler(data.BuildCycleTimeQuery)
Loading
Loading