Skip to content

Commit 8f3797d

Browse files
committed
chore: git commit metrics
1 parent be770b7 commit 8f3797d

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/extensions/default/Git/src/Panel.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ define(function (require, exports) {
1818
ProjectManager = brackets.getModule("project/ProjectManager"),
1919
StringUtils = brackets.getModule("utils/StringUtils"),
2020
Strings = brackets.getModule("strings"),
21+
Metrics = brackets.getModule("utils/Metrics"),
2122
Constants = require("src/Constants"),
2223
Git = require("src/git/Git"),
2324
Events = require("./Events"),
@@ -87,6 +88,8 @@ define(function (require, exports) {
8788
const compiledTemplate = Mustache.render(gitCommitDialogTemplate, {Strings: Strings}),
8889
dialog = Dialogs.showModalDialogUsingTemplate(compiledTemplate),
8990
$dialog = dialog.getElement();
91+
Metrics.countEvent(Metrics.EVENT_TYPE.GIT, 'commit', "showDialog");
92+
let totalLintErrors = 0;
9093
inspectFiles(files, $dialog).then(function (lintResults) {
9194
// Flatten the error structure from various providers
9295
lintResults = lintResults || [];
@@ -111,8 +114,11 @@ define(function (require, exports) {
111114
ErrorHandler.logError("[brackets-git] lintResults contain object in unexpected format: " + JSON.stringify(lintResult));
112115
}
113116
lintResult.hasErrors = lintResult.errors.length > 0;
117+
totalLintErrors += lintResult.errors.length;
114118
});
115119

120+
Metrics.countEvent(Metrics.EVENT_TYPE.GIT, 'commit', "lintErr" + Metrics.getRangeName(totalLintErrors));
121+
116122
// Filter out only results with errors to show
117123
lintResults = _.filter(lintResults, function (lintResult) {
118124
return lintResult.hasErrors;
@@ -152,7 +158,11 @@ define(function (require, exports) {
152158
_makeDialogBig($dialog);
153159

154160
// Show nicely colored commit diff
155-
$dialog.find(".commit-diff").append(Utils.formatDiff(stagedDiff));
161+
const diff = Utils.formatDiff(stagedDiff);
162+
if(diff === Utils.FORMAT_DIFF_TOO_LARGE) {
163+
Metrics.countEvent(Metrics.EVENT_TYPE.GIT, 'commit', "diffTooLarge");
164+
}
165+
$dialog.find(".commit-diff").append(diff);
156166

157167
// Enable / Disable amend checkbox
158168
var toggleAmendCheckbox = function (bool) {
@@ -343,8 +353,10 @@ define(function (require, exports) {
343353
}
344354

345355
ErrorHandler.showError(err, Strings.ERROR_GIT_COMMIT_FAILED);
356+
Metrics.countEvent(Metrics.EVENT_TYPE.GIT, 'commit', "fail");
346357

347358
}).finally(function () {
359+
Metrics.countEvent(Metrics.EVENT_TYPE.GIT, 'commit', "success");
348360
EventEmitter.emit(Events.GIT_COMMITED);
349361
refresh();
350362
});
@@ -599,8 +611,8 @@ define(function (require, exports) {
599611
function inspectFiles(gitStatusResults, $dialog) {
600612
const lintResults = [];
601613
let totalFiles = gitStatusResults.length,
614+
totalFilesLinted = 0,
602615
filesDone = 0;
603-
604616
function showProgress() {
605617
const $progressBar = $dialog.find('.accordion-progress-bar-inner');
606618
if ($progressBar.length) {
@@ -651,13 +663,16 @@ define(function (require, exports) {
651663
resolve();
652664
}).finally(()=>{
653665
filesDone++;
666+
totalFilesLinted++;
654667
showProgress();
655668
});
656669
}, 0); // Delay of 0ms to defer to the next tick of the event loop
657670
});
658671
});
659672

660673
return Promise.all(_.compact(codeInspectionPromises)).then(function () {
674+
Metrics.countEvent(Metrics.EVENT_TYPE.GIT, 'commit', "files" + Metrics.getRangeName(totalFiles));
675+
Metrics.countEvent(Metrics.EVENT_TYPE.GIT, 'commit', "lint" + Metrics.getRangeName(totalFilesLinted));
661676
return lintResults;
662677
});
663678
}

src/extensions/default/Git/src/Utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ define(function (require, exports, module) {
2323
Constants = require("src/Constants"),
2424
Strings = brackets.getModule("strings");
2525

26+
const FORMAT_DIFF_TOO_LARGE = "<div>" + Strings.DIFF_TOO_LONG + "</div>";
27+
2628
// Module variables
2729
const formatDiffTemplate = require("text!templates/format-diff.html"),
2830
questionDialogTemplate = require("text!templates/git-question-dialog.html"),
@@ -69,7 +71,7 @@ define(function (require, exports, module) {
6971
var diffSplit = diff.split("\n");
7072

7173
if (diffSplit.length > DIFF_MAX_LENGTH) {
72-
return "<div>" + Strings.DIFF_TOO_LONG + "</div>";
74+
return "" + FORMAT_DIFF_TOO_LARGE; // create new str to return
7375
}
7476

7577
diffSplit.forEach(function (line) {
@@ -585,6 +587,7 @@ define(function (require, exports, module) {
585587
}
586588

587589
// Public API
590+
exports.FORMAT_DIFF_TOO_LARGE = FORMAT_DIFF_TOO_LARGE;
588591
exports.formatDiff = formatDiff;
589592
exports.getProjectRoot = getProjectRoot;
590593
exports.getExtensionDirectory = getExtensionDirectory;

src/utils/Metrics.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,33 @@ define(function (require, exports, module) {
426426
valueEvent(EVENT_TYPE.PERFORMANCE, "ms", action, Number(durationMs));
427427
}
428428

429+
/**
430+
* Get the range name for a given number.
431+
*
432+
* The function returns the first range that the number fits into, based on predefined ranges:
433+
* 0, 10, 25, 50, 100, 250, 500, 1000, 5000, 10000, and "10000+" for numbers exceeding 10000.
434+
*
435+
* @param {number} number - The number to determine the range for.
436+
* @returns {string} The range name that the number belongs to.
437+
*/
438+
function getRangeName(number) {
439+
// Define the ranges
440+
const ranges = [0, 5, 10, 25, 50, 100, 250, 500, 1000, 5000, 10000];
441+
442+
// Iterate through the ranges and return the first range that is greater than or equal to the number
443+
// small array, linear scan is most efficient than binary search in most cases comparing the overheads and
444+
// maintainability
445+
for (let i = 0; i < ranges.length; i++) {
446+
if (number <= ranges[i]) {
447+
return ""+ranges[i];
448+
}
449+
}
450+
451+
// If the number exceeds the largest range, return "10000+"
452+
return "10000+";
453+
}
454+
455+
429456
// Define public API
430457
exports.init = init;
431458
exports.setDisabled = setDisabled;
@@ -436,6 +463,7 @@ define(function (require, exports, module) {
436463
exports.valueEvent = valueEvent;
437464
exports.logPerformanceTime = logPerformanceTime;
438465
exports.flushMetrics = flushMetrics;
466+
exports.getRangeName = getRangeName;
439467
exports.EVENT_TYPE = EVENT_TYPE;
440468
exports.AUDIT_TYPE_COUNT = AUDIT_TYPE_COUNT;
441469
exports.AUDIT_TYPE_VALUE = AUDIT_TYPE_VALUE;

0 commit comments

Comments
 (0)