Skip to content

Commit

Permalink
feat: added getAgeHrs function
Browse files Browse the repository at this point in the history
This commit adds the `getAgeHrs` function which will return the age/duration between two `time.Time` objects in hours.
  • Loading branch information
4lch4 committed Nov 29, 2023
1 parent 8c28c4a commit 592c053
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,26 @@ func (ghs *githubScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
if pr.Merged {
merged++

age := int64(pr.MergedAt.Sub(pr.CreatedAt).Hours())
age := getAgeHrs(pr.CreatedAt, pr.MergedAt)

ghs.mb.RecordGitRepositoryPullRequestMergeTimeDataPoint(now, age, name, pr.HeadRefName)

if pr.MergeCommit.Deployments.TotalCount > 0 {
deploymentAgeUpperBound := pr.MergeCommit.Deployments.Nodes[0].CreatedAt
deploymentAge := int64(deploymentAgeUpperBound.Sub(pr.CreatedAt).Hours())
age = getAgeHrs(pr.CreatedAt, pr.MergeCommit.Deployments.Nodes[0].CreatedAt)

ghs.mb.RecordGitRepositoryPullRequestDeploymentTimeDataPoint(now, deploymentAge, name, pr.HeadRefName)
ghs.mb.RecordGitRepositoryPullRequestDeploymentTimeDataPoint(now, age, name, pr.HeadRefName)
}
} else {
open++

age := int64(now.AsTime().Sub(pr.CreatedAt).Hours())
age := getAgeHrs(pr.CreatedAt, now.AsTime())

ghs.mb.RecordGitRepositoryPullRequestOpenTimeDataPoint(now, age, name, pr.HeadRefName)

if pr.Reviews.TotalCount > 0 {
approvedAt := pr.Reviews.Nodes[0].CreatedAt
approvalAge := int64(approvedAt.Sub(pr.CreatedAt).Hours())
age := getAgeHrs(pr.CreatedAt, pr.Reviews.Nodes[0].CreatedAt)

ghs.mb.RecordGitRepositoryPullRequestApprovalTimeDataPoint(now, approvalAge, name, pr.HeadRefName)
ghs.mb.RecordGitRepositoryPullRequestApprovalTimeDataPoint(now, age, name, pr.HeadRefName)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package githubscraper // import "github.com/open-telemetry/opentelemetry-collect
import (
"context"
"fmt"
"math"
"net/url"
"time"

"github.com/Khan/genqlient/graphql"
"github.com/google/go-github/v53/github"
Expand Down Expand Up @@ -204,3 +206,9 @@ func (ghs *githubScraper) getPullRequests(

return pullRequests, nil
}

// Get the age/duration between two times in hours. The hours will be rounded up
// if the minutes are greater than 30.
func getAgeHrs(start time.Time, end time.Time) int64 {
return int64(math.Round(end.Sub(start).Hours()))
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/Khan/genqlient/graphql"
"github.com/google/go-github/v53/github"
Expand Down Expand Up @@ -115,6 +116,51 @@ func TestGenDefaultSearchQueryUser(t *testing.T) {
assert.Equal(t, expected, actual)
}

func TestGetDurationHrs(t *testing.T) {
testCases := []struct {
desc string
hrsAdd time.Duration
minsAdd time.Duration
expected float64
}{
{
desc: "TestSubHalfHour",
hrsAdd: 0 * time.Hour,
minsAdd: time.Duration(25) * time.Minute,
expected: 0,
},
{
desc: "TestOverHalfHour",
hrsAdd: 0 * time.Hour,
minsAdd: time.Duration(45) * time.Minute,
expected: 1,
},
{
desc: "TestSubHalfHourWithHrs",
hrsAdd: time.Duration(1) * time.Hour,
minsAdd: time.Duration(25) * time.Minute,
expected: 1,
},
{
desc: "TestOverHalfHourWithHrs",
hrsAdd: time.Duration(1) * time.Hour,
minsAdd: time.Duration(45) * time.Minute,
expected: 2,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
min := time.Now()
max := min.Add(tc.hrsAdd).Add(tc.minsAdd)

actual := getAgeHrs(min, max)

assert.Equal(t, int64(tc.expected), actual)
})
}
}

func TestCheckOwnerExists(t *testing.T) {
testCases := []struct {
desc string
Expand Down Expand Up @@ -166,7 +212,6 @@ func TestCheckOwnerExists(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {

factory := Factory{}
defaultConfig := factory.CreateDefaultConfig()
settings := receivertest.NewNopCreateSettings()
Expand Down Expand Up @@ -392,7 +437,6 @@ func TestGetContributors(t *testing.T) {
desc: "TestListContributorsResponse",
server: restMockServer(responses{
contribs: []*github.Contributor{

{
ID: github.Int64(1),
},
Expand Down

0 comments on commit 592c053

Please sign in to comment.