Skip to content

Commit

Permalink
feat: add org and group contribution analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
justlorain committed Jun 27, 2024
1 parent 0b50b4e commit 16f6941
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 0 deletions.
124 changes: 124 additions & 0 deletions cron/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ func InitTask(ctx context.Context, db *gorm.DB) error {
bar := progressbar.Default(int64(len(group.Orgs)+len(group.Repos)), "HANDLING GROUP: "+group.Name)

var groupCount Count
groupContributions := make(map[string]model.AnalyzedGroupContribution)

// handle orgs in groups
for _, login := range group.Orgs {
// increase bar
_ = bar.Add(1)

var orgCount Count
orgContributions := make(map[string]model.AnalyzedOrgContribution)

// org data
org, err := graphql.QueryOrgInfo(ctx, login)
if err != nil {
Expand Down Expand Up @@ -100,6 +104,37 @@ func InitTask(ctx context.Context, db *gorm.DB) error {
orgCount.StarCount += rd.Repo.Stargazers.TotalCount
orgCount.ForkCount += rd.Repo.Forks.TotalCount
}
// handle new contributor
for _, contributor := range rd.Contributors {
if _, ok := orgContributions[contributor.NodeID]; !ok {
orgContributions[contributor.NodeID] = model.AnalyzedOrgContribution{
Login: contributor.Login,
NodeID: contributor.NodeID,
OrgLogin: org.Login,
OrgNodeID: org.ID,
Contributions: 0,
}
}
if _, ok := groupContributions[contributor.NodeID]; !ok {
groupContributions[contributor.NodeID] = model.AnalyzedGroupContribution{
Login: contributor.Login,
NodeID: contributor.NodeID,
GroupName: group.Name,
Contributions: 0,
}
}
}
// calculate contributor contribution by organization and group
for _, contributor := range rd.Contributors {
if orgContribution, ok := orgContributions[contributor.NodeID]; ok {
orgContribution.Contributions += contributor.Contributions
orgContributions[contributor.NodeID] = orgContribution
}
if groupContribution, ok := groupContributions[contributor.NodeID]; ok {
groupContribution.Contributions += contributor.Contributions
groupContributions[contributor.NodeID] = groupContribution
}
}
}
contributorCount, err := storage.QueryContributorCountByOrg(ctx, db, org.ID)
if err != nil {
Expand Down Expand Up @@ -132,6 +167,11 @@ func InitTask(ctx context.Context, db *gorm.DB) error {
groupCount.StarCount += orgCount.StarCount
groupCount.ForkCount += orgCount.ForkCount
}
// create analyzed org contributions
if err := storage.CreateAnalyzedOrgContributions(ctx, db, orgContributions); err != nil {
slog.Error("error create analyzed org contributions", "err", err.Error())
return err
}
}
// handle repos in group
for _, nameWithOwner := range group.Repos {
Expand Down Expand Up @@ -165,6 +205,24 @@ func InitTask(ctx context.Context, db *gorm.DB) error {
groupCount.StarCount += rd.Repo.Stargazers.TotalCount
groupCount.ForkCount += rd.Repo.Forks.TotalCount
}
// handle new contributor
for _, contributor := range rd.Contributors {
if _, ok := groupContributions[contributor.NodeID]; !ok {
groupContributions[contributor.NodeID] = model.AnalyzedGroupContribution{
Login: contributor.Login,
NodeID: contributor.NodeID,
GroupName: group.Name,
Contributions: 0,
}
}
}
// calculate contributor contribution by group
for _, contributor := range rd.Contributors {
if groupContribution, ok := groupContributions[contributor.NodeID]; ok {
groupContribution.Contributions += contributor.Contributions
groupContributions[contributor.NodeID] = groupContribution
}
}
}
contributorCount, err := storage.QueryContributorCountByGroup(ctx, db, group.Name)
if err != nil {
Expand All @@ -182,6 +240,10 @@ func InitTask(ctx context.Context, db *gorm.DB) error {
slog.Error("error create group", "err", err.Error())
return err
}
if err := storage.CreateAnalyzedGroupContributions(ctx, db, groupContributions); err != nil {
slog.Error("err create analyzed group contributions", "err", err.Error())
return err
}
}
// do cleaner, marker
if err := CleanContributorCompanyAndLocation(ctx, db); err != nil {
Expand All @@ -200,11 +262,15 @@ func UpdateTask(ctx context.Context, db *gorm.DB) error {
bar := progressbar.Default(int64(len(group.Orgs)+len(group.Repos)), "HANDLING GROUP: "+group.Name)

var groupCount Count
groupContributions := make(map[string]model.AnalyzedGroupContribution)

for _, login := range group.Orgs {
// increase bar
_ = bar.Add(1)

var orgCount Count
orgContributions := make(map[string]model.AnalyzedOrgContribution)

org, err := graphql.QueryOrgInfo(ctx, login)
if err != nil {
slog.Error("error query org info", "err", err.Error())
Expand Down Expand Up @@ -256,6 +322,37 @@ func UpdateTask(ctx context.Context, db *gorm.DB) error {
orgCount.StarCount += rd.Repo.Stargazers.TotalCount
orgCount.ForkCount += rd.Repo.Forks.TotalCount
}
// handle new contributor
for _, contributor := range rd.Contributors {
if _, ok := orgContributions[contributor.NodeID]; !ok {
orgContributions[contributor.NodeID] = model.AnalyzedOrgContribution{
Login: contributor.Login,
NodeID: contributor.NodeID,
OrgLogin: org.Login,
OrgNodeID: org.ID,
Contributions: 0,
}
}
if _, ok := groupContributions[contributor.NodeID]; !ok {
groupContributions[contributor.NodeID] = model.AnalyzedGroupContribution{
Login: contributor.Login,
NodeID: contributor.NodeID,
GroupName: group.Name,
Contributions: 0,
}
}
}
// calculate contributor contribution by organization and group
for _, contributor := range rd.Contributors {
if orgContribution, ok := orgContributions[contributor.NodeID]; ok {
orgContribution.Contributions += contributor.Contributions
orgContributions[contributor.NodeID] = orgContribution
}
if groupContribution, ok := groupContributions[contributor.NodeID]; ok {
groupContribution.Contributions += contributor.Contributions
groupContributions[contributor.NodeID] = groupContribution
}
}
}
contributorCount, err := storage.QueryContributorCountByOrg(ctx, db, org.ID)
if err != nil {
Expand All @@ -280,6 +377,11 @@ func UpdateTask(ctx context.Context, db *gorm.DB) error {
groupCount.StarCount += orgCount.StarCount
groupCount.ForkCount += orgCount.ForkCount
}
// create analyzed org contributions
if err := storage.CreateAnalyzedOrgContributions(ctx, db, orgContributions); err != nil {
slog.Error("error create analyzed org contributions", "err", err.Error())
return err
}
}
for _, nameWithOwner := range group.Repos {
// increase bar
Expand Down Expand Up @@ -310,6 +412,24 @@ func UpdateTask(ctx context.Context, db *gorm.DB) error {
groupCount.StarCount += rd.Repo.Stargazers.TotalCount
groupCount.ForkCount += rd.Repo.Forks.TotalCount
}
// handle new contributor
for _, contributor := range rd.Contributors {
if _, ok := groupContributions[contributor.NodeID]; !ok {
groupContributions[contributor.NodeID] = model.AnalyzedGroupContribution{
Login: contributor.Login,
NodeID: contributor.NodeID,
GroupName: group.Name,
Contributions: 0,
}
}
}
// calculate contributor contribution by group
for _, contributor := range rd.Contributors {
if groupContribution, ok := groupContributions[contributor.NodeID]; ok {
groupContribution.Contributions += contributor.Contributions
groupContributions[contributor.NodeID] = groupContribution
}
}
}
contributorCount, err := storage.QueryContributorCountByGroup(ctx, db, group.Name)
if err != nil {
Expand All @@ -326,6 +446,10 @@ func UpdateTask(ctx context.Context, db *gorm.DB) error {
slog.Error("error update group", "err", err.Error())
return err
}
if err := storage.CreateAnalyzedGroupContributions(ctx, db, groupContributions); err != nil {
slog.Error("err create analyzed group contributions", "err", err.Error())
return err
}
}
// do cleaner, marker
if err := CleanContributorCompanyAndLocation(ctx, db); err != nil {
Expand Down
63 changes: 63 additions & 0 deletions cron/task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2024 BINARY Members
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cron

import (
"fmt"
"testing"
)

type User struct {
ID string
Name string
Count int
}

func Test1(t *testing.T) {
m := make(map[string]User)
if _, ok := m["1"]; !ok {
m["1"] = User{ID: "1", Name: "lorain", Count: 0}
}
fmt.Println(m) // 0
if v, ok := m["1"]; ok {
v.Count += 10
}
fmt.Println(m) // 0
}

func Test2(t *testing.T) {
m := make(map[string]User)
if _, ok := m["1"]; !ok {
m["1"] = User{ID: "1", Name: "lorain", Count: 0}
}
fmt.Println(m) // 0
if v, ok := m["1"]; ok {
v.Count += 10
m["1"] = v
}
fmt.Println(m) // 10
}

func Test3(t *testing.T) {
m := make(map[string]*User)
if _, ok := m["1"]; !ok {
m["1"] = &User{ID: "1", Name: "lorain", Count: 0}
}
fmt.Println(m) // 0
if v, ok := m["1"]; ok {
v.Count += 10
}
fmt.Println(m["1"].Count) // 10
}
40 changes: 40 additions & 0 deletions model/analyser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 BINARY Members
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package model

import "gorm.io/gorm"

type AnalyzedOrgContribution struct {
gorm.Model

Login string
NodeID string

OrgLogin string
OrgNodeID string

Contributions int
}

type AnalyzedGroupContribution struct {
gorm.Model

Login string
NodeID string

GroupName string

Contributions int
}
1 change: 1 addition & 0 deletions model/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type IssueAssignees struct {
IssueNumber int
IssueURL string
// repo name with owner
// TODO: split to IssueRepoOwner and IssueRepoName, grafana dashboard need update
IssueRepoName string

AssigneeNodeID string
Expand Down
44 changes: 44 additions & 0 deletions storage/analyser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 BINARY Members
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package storage

import (
"context"

"github.com/B1NARY-GR0UP/openalysis/model"
"gorm.io/gorm"
)

func CreateAnalyzedOrgContributions(ctx context.Context, db *gorm.DB, contributions map[string]model.AnalyzedOrgContribution) error {
if len(contributions) == 0 {
return nil
}
var cs []model.AnalyzedOrgContribution
for _, contribution := range contributions {
cs = append(cs, contribution)
}
return db.WithContext(ctx).Create(&cs).Error
}

func CreateAnalyzedGroupContributions(ctx context.Context, db *gorm.DB, contributions map[string]model.AnalyzedGroupContribution) error {
if len(contributions) == 0 {
return nil
}
var cs []model.AnalyzedGroupContribution
for _, contribution := range contributions {
cs = append(cs, contribution)
}
return db.WithContext(ctx).Create(&cs).Error
}
2 changes: 2 additions & 0 deletions storage/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func Init() (err error) {
&model.GroupsRepositories{},
&model.IssueAssignees{},
&model.PullRequestAssignees{},
&model.AnalyzedOrgContribution{},
&model.AnalyzedGroupContribution{},
)
if err != nil {
return
Expand Down

0 comments on commit 16f6941

Please sign in to comment.