healthycode 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,327 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/aboutcode-org/healthycode/blob/main/src/grimoirelab_metrics/metrics.schema.json",
4
+ "title": "healthycode metrics output",
5
+ "description": "Output of the healthycode CLI: commit-activity metrics and an unhealthy-probability score for each analyzed package. Metric descriptions and CHAOSS references when available are provided.",
6
+ "type": "object",
7
+ "required": ["packages", "metadata"],
8
+ "additionalProperties": false,
9
+ "properties": {
10
+ "packages": {
11
+ "type": "object",
12
+ "description": "Metrics and score for each analyzed package.",
13
+ "additionalProperties": { "$ref": "#/$defs/packageEntry" }
14
+ },
15
+ "metadata": { "$ref": "#/$defs/runMetadata" }
16
+ },
17
+ "$defs": {
18
+ "packageEntry": {
19
+ "description": "Either a full metrics/score record, or {\"metrics\": null} when the package's repository could not be matched to any GrimoireLab result.",
20
+ "oneOf": [
21
+ {
22
+ "type": "object",
23
+ "required": ["metrics"],
24
+ "additionalProperties": false,
25
+ "properties": {
26
+ "metrics": { "const": null }
27
+ }
28
+ },
29
+ {
30
+ "type": "object",
31
+ "required": ["metrics", "metadata", "repository", "score"],
32
+ "additionalProperties": false,
33
+ "properties": {
34
+ "metrics": { "$ref": "#/$defs/metrics" },
35
+ "metadata": { "$ref": "#/$defs/commitMetadata" },
36
+ "repository": {
37
+ "type": "string",
38
+ "format": "uri",
39
+ "description": "Git repository URL the package was resolved to."
40
+ },
41
+ "score": { "$ref": "#/$defs/score" }
42
+ }
43
+ }
44
+ ]
45
+ },
46
+ "metrics": {
47
+ "type": "object",
48
+ "additionalProperties": false,
49
+ "required": [
50
+ "total_commits",
51
+ "total_contributors",
52
+ "total_organizations",
53
+ "pony_factor",
54
+ "elephant_factor",
55
+ "recent_organizations",
56
+ "recent_contributors",
57
+ "recent_commits",
58
+ "contributor_growth",
59
+ "contributor_growth_rate",
60
+ "active_branches",
61
+ "days_since_last_commit",
62
+ "casual_regular_contributors_rate",
63
+ "returning_contributors",
64
+ "commits_over_periods_rate",
65
+ "coefficient_of_variation",
66
+ "file_types_code",
67
+ "file_types_binary",
68
+ "file_types_other",
69
+ "commit_size_added_lines",
70
+ "commit_size_removed_lines",
71
+ "message_size_total",
72
+ "message_size_mean",
73
+ "message_size_median",
74
+ "developer_categories_core",
75
+ "developer_categories_regular",
76
+ "developer_categories_casual",
77
+ "commits_per_week",
78
+ "commits_per_month",
79
+ "commits_per_year",
80
+ "found_file_license",
81
+ "found_file_adopters"
82
+ ],
83
+ "properties": {
84
+ "total_commits": {
85
+ "type": "integer",
86
+ "minimum": 0,
87
+ "description": "Number of commits per repository: activity for the Git repository analyzed, within the analyzed period.",
88
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-code-changes-commits/"
89
+ },
90
+ "total_contributors": {
91
+ "type": "integer",
92
+ "minimum": 0,
93
+ "description": "Number of developers per repository: unique participants producing commits in the repository.",
94
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-contributors/"
95
+ },
96
+ "total_organizations": {
97
+ "type": "integer",
98
+ "minimum": 0,
99
+ "description": "Number of contributing organizations: total number of distinct organizations whose members have made contributions to the project over the analyzed period. The organization is determined by the email address of the commit author.",
100
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-organizational-diversity/"
101
+ },
102
+ "pony_factor": {
103
+ "type": "integer",
104
+ "minimum": 0,
105
+ "description": "Pony factor: number of individuals who produce up to the first pony_threshold share (default 50%) of the total number of code contributions (in descending order) within the analyzed period. A low value implies a high dependency on these individuals, making the project vulnerable if they were to leave. Also known as Lottery Factor, Bus Factor, or Contributor Absence Factor.",
106
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-contributor-absence-factor/"
107
+ },
108
+ "elephant_factor": {
109
+ "type": "integer",
110
+ "minimum": 0,
111
+ "description": "Elephant factor: number of unique organizations producing up to the first elephant_threshold share (default 50%) of the total number of code contributions (in descending order) within the analyzed period. Applies the Pony Factor concept to contributing organizations, determined by the email address of the commit author. First defined by Bitergia.",
112
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-elephant-factor/"
113
+ },
114
+ "recent_organizations": {
115
+ "type": "integer",
116
+ "minimum": 0,
117
+ "description": "Number of organizations contributing recently: unique organizations whose members have actively made contributions within the last 90 days, determined by the email address of the commit author.",
118
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-organizational-diversity/"
119
+ },
120
+ "recent_contributors": {
121
+ "type": "integer",
122
+ "minimum": 0,
123
+ "description": "Number of recent contributors: total count of unique individuals who contributed within the last 90 days.",
124
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-contributors/"
125
+ },
126
+ "recent_commits": {
127
+ "type": "integer",
128
+ "minimum": 0,
129
+ "description": "Number of recent commits: total number of commits made to the project within the last 90 days.",
130
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-code-changes-commits/"
131
+ },
132
+ "contributor_growth": {
133
+ "type": "integer",
134
+ "description": "Contributor growth: growth of active contributors (people sending one or more commits) between two halves of the analyzed period, i.e. second-half count minus first-half count. Can be negative."
135
+ },
136
+ "contributor_growth_rate": {
137
+ "type": "number",
138
+ "description": "Contributor growth rate: growth rate of active contributors between two halves of the analyzed period, i.e. (second-half count minus first-half count) divided by the first-half count. Can be negative."
139
+ },
140
+ "active_branches": {
141
+ "type": "integer",
142
+ "minimum": 0,
143
+ "description": "Number of active branches: count of branches in the Git repository that have seen recent development activity, usually indicated by new commits.",
144
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-branch-lifecycle/"
145
+ },
146
+ "days_since_last_commit": {
147
+ "type": "integer",
148
+ "minimum": 0,
149
+ "description": "Days since last commit was submitted to the repository or the project. 99999 is used as a sentinel when there is no last-commit date.",
150
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-code-changes-commits/"
151
+ },
152
+ "casual_regular_contributors_rate": {
153
+ "type": "number",
154
+ "minimum": 0,
155
+ "description": "Rate of contributors contributing infrequently vs. regularly: ratio of casual contributors to core-plus-regular contributors, categorizing contributors by the frequency, consistency and intensity of their contributions. The CHAOSS community defines \"Occasional Contributors\" as people who make contributions to a project on an irregular basis.",
156
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-occasional-contributors/"
157
+ },
158
+ "returning_contributors": {
159
+ "type": "integer",
160
+ "minimum": 0,
161
+ "description": "Number of contributors who have contributed in previous periods (\"returning contributors\"/contributor retention): unique individuals active in the last 90 days who had also made contributions in one or more defined previous periods."
162
+ },
163
+ "commits_over_periods_rate": {
164
+ "type": "number",
165
+ "minimum": 0,
166
+ "description": "Rate of commits over specified periods: rate of commits between the last 90 days and the last year, giving an estimation of how distributed contributions are and the project's momentum."
167
+ },
168
+ "coefficient_of_variation": {
169
+ "type": "number",
170
+ "minimum": 0,
171
+ "description": "Coefficient of variation of the number of commits per month (standard deviation divided by the mean), used as a measure of how consistent commit activity is period over period."
172
+ },
173
+ "file_types_code": {
174
+ "type": "integer",
175
+ "minimum": 0,
176
+ "description": "File type metrics: number of touched files classified as code (matching code_file_pattern), part of the split of developer activity into code, binary, and other files."
177
+ },
178
+ "file_types_binary": {
179
+ "type": "integer",
180
+ "minimum": 0,
181
+ "description": "File type metrics: number of touched files classified as binary (matching binary_file_pattern), part of the split of developer activity into code, binary, and other files."
182
+ },
183
+ "file_types_other": {
184
+ "type": "integer",
185
+ "minimum": 0,
186
+ "description": "File type metrics: number of touched files that are neither code nor binary (e.g. documentation, config), part of the split of developer activity into code, binary, and other files."
187
+ },
188
+ "commit_size_added_lines": {
189
+ "type": "integer",
190
+ "minimum": 0,
191
+ "description": "Commit size metrics: total number of lines added across analyzed commits, giving an overview of the usual size of code review submissions.",
192
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-change-request-commits/"
193
+ },
194
+ "commit_size_removed_lines": {
195
+ "type": "integer",
196
+ "minimum": 0,
197
+ "description": "Commit size metrics: total number of lines removed across analyzed commits, giving an overview of the usual size of code review submissions.",
198
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-change-request-commits/"
199
+ },
200
+ "message_size_total": {
201
+ "type": "integer",
202
+ "minimum": 0,
203
+ "description": "Message size metrics: sum of the character length of all analyzed commit messages, indicating how extensively developers document their changes."
204
+ },
205
+ "message_size_mean": {
206
+ "type": "number",
207
+ "minimum": 0,
208
+ "description": "Message size metrics: mean character length of analyzed commit messages, indicating how extensively developers document their changes."
209
+ },
210
+ "message_size_median": {
211
+ "type": "integer",
212
+ "minimum": 0,
213
+ "description": "Message size metrics: median character length of analyzed commit messages, indicating how extensively developers document their changes."
214
+ },
215
+ "developer_categories_core": {
216
+ "type": "integer",
217
+ "minimum": 0,
218
+ "description": "Developer categories: number of core contributors, i.e. those producing the most consistent/substantial share of contributions, classified using dev_categories_thresholds."
219
+ },
220
+ "developer_categories_regular": {
221
+ "type": "integer",
222
+ "minimum": 0,
223
+ "description": "Developer categories: number of regular contributors, classified using dev_categories_thresholds."
224
+ },
225
+ "developer_categories_casual": {
226
+ "type": "integer",
227
+ "minimum": 0,
228
+ "description": "Developer categories: number of casual contributors, i.e. those who contribute episodically, classified using dev_categories_thresholds."
229
+ },
230
+ "commits_per_week": {
231
+ "type": ["number", "null"],
232
+ "minimum": 0,
233
+ "description": "Frequency metrics for commits: mean number of commits per week over the analyzed period, used to understand the consistency of developer activity. null when the analyzed period is shorter than 7 days."
234
+ },
235
+ "commits_per_month": {
236
+ "type": ["number", "null"],
237
+ "minimum": 0,
238
+ "description": "Frequency metrics for commits: mean number of commits per month over the analyzed period, used to understand the consistency of developer activity. null when the analyzed period is shorter than 30 days."
239
+ },
240
+ "commits_per_year": {
241
+ "type": ["number", "null"],
242
+ "minimum": 0,
243
+ "description": "Frequency metrics for commits: mean number of commits per year over the analyzed period, used to understand the consistency of developer activity. null when the analyzed period is shorter than 365 days."
244
+ },
245
+ "found_file_license": {
246
+ "type": "integer",
247
+ "enum": [0, 1],
248
+ "description": "Presence of a license file in a standard location: 1 if a file containing the project's chosen open source license (LICENSE, LICENSE.md, LICENSE.txt, or COPYING) exists at the root of the repository, else 0.",
249
+ "chaoss_reference": "https://www.chaoss.community/kb/metric-licenses-declared/"
250
+ },
251
+ "found_file_adopters": {
252
+ "type": "integer",
253
+ "enum": [0, 1],
254
+ "description": "Presence of an adopters file in a standard location: 1 if a file containing the full text of the project's adopters (ADOPTERS, ADOPTERS.md, or ADOPTERS.txt) exists at the root of the repository, else 0."
255
+ }
256
+ }
257
+ },
258
+ "commitMetadata": {
259
+ "type": "object",
260
+ "additionalProperties": false,
261
+ "required": ["first_commit", "last_commit", "first_commit_date", "last_commit_date"],
262
+ "properties": {
263
+ "first_commit": { "type": ["string", "null"], "description": "First commit hash within the analyzed period." },
264
+ "last_commit": { "type": ["string", "null"], "description": "Last commit hash within the analyzed period." },
265
+ "first_commit_date": { "type": ["string", "null"], "format": "date-time" },
266
+ "last_commit_date": { "type": ["string", "null"], "format": "date-time" }
267
+ }
268
+ },
269
+ "score": {
270
+ "type": "object",
271
+ "additionalProperties": false,
272
+ "required": ["value", "scoring_model"],
273
+ "properties": {
274
+ "value": {
275
+ "type": "number",
276
+ "minimum": 0,
277
+ "maximum": 1,
278
+ "description": "Probability of the repository being 'Unhealthy' (0.0 healthy .. 1.0 unhealthy), calculated from a pruned logistic regression model over the metrics above."
279
+ },
280
+ "scoring_model": {
281
+ "type": "string",
282
+ "pattern": "^[a-z0-9]+-[a-z0-9]+-[0-9]+(\\.[0-9]+)*$",
283
+ "description": "Ecosystem-model-version identifier, e.g. \"npm-health-0.1\"."
284
+ }
285
+ }
286
+ },
287
+ "runMetadata": {
288
+ "type": "object",
289
+ "additionalProperties": false,
290
+ "required": ["version", "started_at", "finished_at", "configuration"],
291
+ "properties": {
292
+ "version": { "type": "string", "description": "healthycode package version." },
293
+ "started_at": { "type": "string", "format": "date-time" },
294
+ "finished_at": { "type": "string", "format": "date-time" },
295
+ "configuration": { "$ref": "#/$defs/configuration" }
296
+ }
297
+ },
298
+ "configuration": {
299
+ "type": "object",
300
+ "additionalProperties": false,
301
+ "required": [
302
+ "from_date",
303
+ "to_date",
304
+ "code_file_pattern",
305
+ "binary_file_pattern",
306
+ "pony_threshold",
307
+ "elephant_threshold",
308
+ "dev_categories_thresholds"
309
+ ],
310
+ "properties": {
311
+ "from_date": { "type": "string", "format": "date-time" },
312
+ "to_date": { "type": "string", "format": "date-time" },
313
+ "code_file_pattern": { "type": "string", "description": "Regex used to classify code files (feeds file_types_code)." },
314
+ "binary_file_pattern": { "type": "string", "description": "Regex used to classify binary files (feeds file_types_binary)." },
315
+ "pony_threshold": { "type": "number", "minimum": 0, "maximum": 1, "description": "Cumulative commit-share threshold used to compute pony_factor." },
316
+ "elephant_threshold": { "type": "number", "minimum": 0, "maximum": 1, "description": "Cumulative commit-share threshold used to compute elephant_factor." },
317
+ "dev_categories_thresholds": {
318
+ "type": "array",
319
+ "items": { "type": "number", "minimum": 0, "maximum": 1 },
320
+ "minItems": 2,
321
+ "maxItems": 2,
322
+ "description": "[core/regular cutoff, regular/casual cutoff] cumulative commit-share thresholds used for developer_categories_*."
323
+ }
324
+ }
325
+ }
326
+ }
327
+ }
@@ -0,0 +1,83 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) AboutCode
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ #
18
+
19
+ import math
20
+
21
+ from typing import Dict
22
+
23
+ # These coefficients were calculated with the notebooks and data available
24
+ # at https://github.com/aboutcode-org/healthycode/blob/main/model/npm/README.md
25
+
26
+
27
+ class npmModel:
28
+
29
+ # Ecosystem Name
30
+ ECOSYSTEM_NAME = "npm"
31
+ # Model Name
32
+ MODEL_NAME = "health"
33
+ # Model Version
34
+ MODEL_VERSION = "0.2"
35
+
36
+ # We have dropped the low-impact metrics, those with a coefficient close to 0
37
+ COEFFICIENTS = {
38
+ "active_branches": -0.834069,
39
+ "commits_over_periods_rate": -0.841944,
40
+ "commit_size_added_lines": -0.389225,
41
+ "contributor_growth_rate": 0.114363,
42
+ "days_since_last_commit": 1.081051,
43
+ "developer_categories_casual": 0.958613,
44
+ "developer_categories_core": -1.277484,
45
+ "developer_categories_regular": 0.193106,
46
+ "file_types_code": -0.905957,
47
+ "found_file_license": 0.376334,
48
+ "returning_contributors": -1.167053,
49
+ }
50
+ # Model Intercept
51
+ Z = -1.023426
52
+
53
+ def __init__(self):
54
+ self.coefficients = self.COEFFICIENTS.copy()
55
+ self.z = self.Z
56
+
57
+ def calculate_score(self, metrics: Dict[str, float]) -> float:
58
+ """
59
+ Calculates the probability of a repository being 'Unhealthy' based on
60
+ the pruned logistic regression model metrics.
61
+ Parameters:
62
+ metrics (dict): Dictionary containing the project feature names and values.
63
+ Returns:
64
+ float: Probability score between 0.0 (Healthy) and 1.0 (Unhealthy).
65
+ """
66
+
67
+ z = self.z
68
+ # Calculate the linear combination (log-odds)
69
+ for metric, coef in self.coefficients.items():
70
+ # FIXME. We set by default 0 if a metric is missing. Is this safe?
71
+ value = metrics.get(metric, 0.0)
72
+ z += coef * value
73
+ # Apply the Sigmoid function to get the final probability
74
+ try:
75
+ probability = 1 / (1 + math.exp(-z))
76
+ except OverflowError:
77
+ # Safeguard against extreme values of z
78
+ # FIXME Is this correct?
79
+ probability = 0.0 if z < 0 else 1.0
80
+ return {
81
+ "value": probability,
82
+ "metadata": {"ecosystem": self.ECOSYSTEM_NAME, "model": self.MODEL_NAME, "version": self.MODEL_VERSION},
83
+ }