Skip to content

Commit

Permalink
moving metric computation in entites
Browse files Browse the repository at this point in the history
  • Loading branch information
HLAD Nicolas committed Jul 23, 2024
1 parent 703f312 commit 3f51497
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 49 deletions.
72 changes: 63 additions & 9 deletions src/GitLabHealth-Model-Analysis/GitMetric.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,61 @@ Class {
#category : #'GitLabHealth-Model-Analysis'
}

{ #category : #setup }
GitMetric class >> setupGroupedDateFrom: since to: until over: aDateWeekMonthOrYear [

| groupedByDate start end over increment |
groupedByDate := OrderedDictionary new.

increment := 1.
start := self transformDate: since to: aDateWeekMonthOrYear.
end := self transformDate: until to: aDateWeekMonthOrYear.

groupedByDate
at: start printString
ifAbsentPut: [ OrderedCollection new ].

over := aDateWeekMonthOrYear name asLowercase asSymbol.
over = #date ifTrue: [ over := #day ].
over = #month ifTrue: [
increment := 32.
over := #day ].


[ groupedByDate keys last asDateAndTime < end ] whileTrue: [
| index |
index := groupedByDate keys last asDateAndTime
+ (increment perform: over).
index := self transformDate: index to: aDateWeekMonthOrYear.
groupedByDate
at: index printString
ifAbsentPut: [ OrderedCollection new ] ].


over = #day ifTrue: [
groupedByDate keysDo: [ :date |
| aWeekday |
aWeekday := date asDate weekday.
(aWeekday = #Sunday or: [ aWeekday = #Saturday ]) ifTrue: [
groupedByDate removeKey: date ] ] ].


groupedByDate
at: end printString
ifAbsentPut: [ OrderedCollection new ].

^ groupedByDate
]

{ #category : #'as yet unclassified' }
GitMetric class >> transformDate: date to: aWeekOrMonthOrYear [

aWeekOrMonthOrYear = Month ifTrue: [ ^ date asDate month asDate ].

^ (date asDate perform: ('as' , aWeekOrMonthOrYear name) asSymbol)
asDate
]

{ #category : #'as yet unclassified' }
GitMetric >> cacheSymbolFor: anEntityType since: since until: until [

Expand All @@ -25,10 +80,15 @@ GitMetric >> findUserNamed: aUsername [
^ user
]

{ #category : #accessing }
GitMetric >> glhImporter: aGLPHModelImporter [
glhImporter := aGLPHModelImporter
]

{ #category : #churn }
GitMetric >> loadCommitOfProjects: aCollection since: since until: until [

| allCommits period i size|
| allCommits period size |
period := self cacheSymbolFor: GLHCommit since: since until: until.

"download commits unless project cache is not empty"
Expand All @@ -45,16 +105,10 @@ GitMetric >> loadCommitOfProjects: aCollection since: since until: until [

allCommits := allCommits flatten.

i := 1.
size := allCommits size.
allCommits do: [ :commit |
(' ' join: {
'import creators '.
i.
'/'.
size }) recordInfo.
glhImporter importCreatorOfCommit: commit.
i := i + 1 ].
MyLogs logProgress: 'import creators ' over: size.
glhImporter importCreatorOfCommit: commit ].

glhImporter chainsCommitsFrom: allCommits.

Expand Down
40 changes: 15 additions & 25 deletions src/GitLabHealth-Model-Analysis/GitMetric4User.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ Class {
GitMetric4User >> codeChurnSince: since until: until onACommitWindowOf: commitLimit overA: aDateWeekMonthOrYear [

| commits res groupedByDate totalContributions userCommits |
"for each commit, we get its churn result (a dictionnary)""debug halt""percentage := (((totalChurn * 100) / totalContribution) asFloat)"" (
#churn -> ((groupedByDate sum: [ :frac | frac numerator ]) ->
(groupedByDate sum: [ :frac | frac denominator ]))
)."
"for each commit, we get its churn result (a dictionnary)"
'GitMetric4User: code churn' recordInfo.
totalContributions := OrderedCollection new.
groupedByDate := self
Expand Down Expand Up @@ -103,9 +100,7 @@ GitMetric4User >> codeContributionsSince: since until: until overA: aDateWeekMon

| commits contributions groupedByDate |
'GitMetric4User: code contribution' recordInfo.




groupedByDate := self
setupGroupedDateFrom: since
to: until
Expand Down Expand Up @@ -149,7 +144,6 @@ GitMetric4User >> codeContributionsSince: since until: until overA: aDateWeekMon
(#addition -> totalAdd).
(#deletion -> totalDele) } asDictionary ].


^ {
(#overEach -> aDateWeekMonthOrYear name).
(#forOver -> (groupedByDate keys size printString
Expand Down Expand Up @@ -230,7 +224,6 @@ GitMetric4User >> commitFrequencySince: since until: until overA: aDateWeekMonth
| periods total groupedByDate userCommits |
'GitMetric4User: commit frequency' recordInfo.


groupedByDate := self
setupGroupedDateFrom: since
to: until
Expand Down Expand Up @@ -297,19 +290,19 @@ GitMetric4User >> commitsProducedOnProject: projectId since: sinceDate until: un

{ #category : #metrics }
GitMetric4User >> commitsProducedOnProject: aProjectId since: since until: until overA: aWeekOrMonthOrYear [
"'aWeekOrMonthOrYear' should be the class of Week, Month or Year"

"self commitFrequencyOnProject: 6462 since: (Date today - 60 day) until: (Date today) overA: Week. "
"'aWeekOrMonthOrYear' should be the class of Data, Week, Month or Year"

| frequency aggregatedFrequencies |
aggregatedFrequencies := self setupGroupedDateFrom: since to: until over: aWeekOrMonthOrYear.

aggregatedFrequencies := self
setupGroupedDateFrom: since
to: until
over: aWeekOrMonthOrYear.

frequency := self
commitsProducedOnProject: aProjectId
since: since
until: until.


frequency associations do: [ :assoc |
| date commits overDate |
date := assoc key.
Expand Down Expand Up @@ -496,7 +489,7 @@ GitMetric4User >> delayUntilFirstChurnSince: since until: until overA: aDateWeek
{ #category : #churn }
GitMetric4User >> foundSuccessorOf: userCommits andCompleteImportForMax: commitLimit [

| analyzingCommits i|
| analyzingCommits |
analyzingCommits := Set new.
glhImporter withCommitDiffs: true.

Expand All @@ -505,19 +498,16 @@ GitMetric4User >> foundSuccessorOf: userCommits andCompleteImportForMax: commitL
visitChildCommits: c childCommits
toStoreThemIn: analyzingCommits
upto: commitLimit ].
i := 1.
analyzingCommits do: [ :c |
(' ' join: { 'import commit creators: ' .(i) . '/' . analyzingCommits size }) recordInfo.
i := i + 1.

analyzingCommits do: [ :c |
MyLogs
logProgress: 'import commit creators: '
over: analyzingCommits size.
glhImporter completeImportedCommit: c ].

^ analyzingCommits
]

{ #category : #accessing }
GitMetric4User >> glhImporter: aGLPHModelImporter [
glhImporter := aGLPHModelImporter
]

{ #category : #initialization }
GitMetric4User >> initialize [

Expand Down
7 changes: 5 additions & 2 deletions src/GitLabHealth-Model-Analysis/GitMetricExporter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ GitMetricExporter >> addEntitiesFromUserNames: userNames [
metrics
glhImporter: glhImporter;
findUserNamed: username.

metrics itsProjects: (projects collect: [ :p |
MyLogs logProgress: 'complete import of project #' over: projects.

p id -> (glhImporter completeImportProject: p) ]) asDictionary.

metrics ]).
Expand Down Expand Up @@ -410,8 +413,8 @@ GitMetricExporter >> initialize [

entities := OrderedCollection new.
"set up the minimal date from which we are looking for the commits a particular user in projects"
sinceTimeLimit := '1 january 2023' asDateAndTime.
runningPeriods := OrderedCollection new.
sinceTimeLimit := '1 january 2024' asDateAndTime.
runningPeriods := OrderedCollection new.
maxCommitWindow := 3.
over := Date
]
Expand Down
47 changes: 47 additions & 0 deletions src/GitLabHealth-Model-Analysis/MyLogs.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Class {
#name : #MyLogs,
#superclass : #Object,
#classVars : [
'i',
'size'
],
#category : #'GitLabHealth-Model-Analysis'
}

{ #category : #accessing }
MyLogs class >> i [
^ i
]

{ #category : #accessing }
MyLogs class >> increment [

^ i := i + 1
]

{ #category : #progress }
MyLogs class >> logProgress: aString over: aCollection [

('' join: {
aString.
' '.
self increment printString.
'/'.
aCollection size }) recordInfo.
i >= aCollection size ifTrue: [ self reset ]
]

{ #category : #'instance creation' }
MyLogs class >> newLogNamed: aName [

self reset.
TinyLogger default addFileLoggerNamed:
'gitlabhealth-' , aName , '.log'

]

{ #category : #initialization }
MyLogs class >> reset [
i := 0.
size := 0.
]
9 changes: 9 additions & 0 deletions src/GitLabHealth-Model-Extension/GLHCommit.extension.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
Extension { #name : #GLHCommit }

{ #category : #'*GitLabHealth-Model-Extension' }
GLHCommit >> computeChurnForMaxCommits: aLimit [

^ GitAnalyzer new
fromCommit: self;
maxChildCommit: aLimit;
analyseChurn
]

{ #category : #'*GitLabHealth-Model-Extension' }
GLHCommit >> displayStringOn: aStream [

Expand Down
14 changes: 14 additions & 0 deletions src/GitLabHealth-Model-Extension/GLHUser.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Extension { #name : #GLHUser }

{ #category : #'*GitLabHealth-Model-Extension' }
GLHUser >> computeCommitFrequencyOver: aDateWeekMonthOrYear [

| orderedCommits |
orderedCommits := self commits sortBy: #created_at.

^ GitMetric4User new
user: self;
commitFrequencySince: orderedCommits first created_at asDateAndTime
until: orderedCommits last created_at asDateAndTime
overA: aDateWeekMonthOrYear
]
26 changes: 25 additions & 1 deletion src/GitLabHealth-Model-Importer/GLHApi.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ Class {
'baseAPIUrl',
'client'
],
#classVars : [
'currentAPI'
],
#category : #'GitLabHealth-Model-Importer'
}

{ #category : #accessing }
GLHApi class >> current [

^ currentAPI
]

{ #category : #accessing }
GLHApi >> baseAPIUrl [

Expand All @@ -23,6 +32,20 @@ GLHApi >> baseAPIUrl: anObject [
baseAPIUrl := anObject
]

{ #category : #'as yet unclassified' }
GLHApi >> blamesOfProject: aProjectId forFile: filePath onBranch: branchRef onStartRange: start toEndRange: end [

self client path:
self baseAPIUrl , '/projects/' , aProjectId printString
, '/repository/files/' , filePath urlEncoded , '/blame'.

branchRef ifNotNil: [ self client queryAt: #'ref' put: branchRef ].
start ifNotNil: [ self client queryAt: #'range[start]' put: start ].
end ifNotNil: [ self client queryAt: #'range[end]' put: end ].

^ self client get
]

{ #category : #'as yet unclassified' }
GLHApi >> blamesOfProject: aProjectId forFile: filePath onStartRange: start toEndRange: end [

Expand Down Expand Up @@ -353,7 +376,8 @@ GLHApi >> initialize [

self client: (ZnClient new
accept: ZnMimeType applicationJson;
yourself)
yourself).
currentAPI:= self
]

{ #category : #'api - jobs' }
Expand Down
Loading

0 comments on commit 3f51497

Please sign in to comment.