Skip to content

Commit

Permalink
Scan metrics collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
michelcaradec committed Nov 5, 2019
1 parent f73608e commit a23e60a
Show file tree
Hide file tree
Showing 22 changed files with 512 additions and 135 deletions.
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ vsc-extension-quickstart.md
**/*.map
**/*.ts
_NOTEPAD.md
_ROADMAP.md
readme_assets/**
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ Here are a few common issues.

## [Unreleased]

## [0.1.0] - 2019-11-05

### Added

- Scan metrics are collected and displayed in a dedicated output console.
- New configuration setting `string-checker-js.output.show-on-completion` to show output console on scan completion.

### Changed

- Configuration settings grouped under name "String Checker JS".
- Code refactoring to allow metrics collection.

## [0.0.4] - 2019-11-02

### Changed
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"url": "https://github.com/michelcaradec/string-checker-js"
},
"license": "MIT",
"version": "0.0.4",
"version": "0.1.0",
"engines": {
"vscode": "^1.38.0"
},
Expand Down Expand Up @@ -200,7 +200,7 @@
]
},
"configuration": {
"title": "string-checker-js",
"title": "String Checker JS",
"properties": {
"string-checker-js.file-extension": {
"type": "[string]",
Expand Down Expand Up @@ -279,6 +279,11 @@
"type": "string",
"default": "$",
"description": "jQuery identifier (default is '$')."
},
"string-checker-js.output.show-on-completion": {
"type": "boolean",
"default": true,
"description": "Show output console on scan completion."
}
}
}
Expand Down
21 changes: 20 additions & 1 deletion src/checker/providerCollection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { IDetectProvider } from "./providers/detectProvider";
import { ConfidenceLevel } from "../enumerations";
import { IStatsEmiter } from "../stats/statsEmiter";

export class ProviderCollection {
private _providers: IDetectProvider[];
private _statsEmiter: IStatsEmiter;

constructor(providers: IDetectProvider[]) {
constructor(providers: IDetectProvider[], statsEmiter: IStatsEmiter) {
this._providers = providers ? providers : [];
this._statsEmiter = statsEmiter;
}

check(text: string): [IDetectProvider | undefined, ConfidenceLevel, string] {
Expand All @@ -30,10 +33,26 @@ export class ProviderCollection {
break;
}
}

this.emitStats(bestProvider, bestLevel);

return [bestProvider, bestLevel, bestMessage];
}

private emitStats(provider: IDetectProvider | undefined, level: ConfidenceLevel): void {
if (provider) {
switch (level) {
case ConfidenceLevel.Technical:
this._statsEmiter.emit(provider.eventWhenTechnical);
break;

case ConfidenceLevel.Message:
this._statsEmiter.emit(provider.eventWhenMessage);
break;
}
}
}

test(text: string): [IDetectProvider | undefined, ConfidenceLevel, string][] {
return this._providers.map(provider => {
let [result, message] = provider.check(text);
Expand Down
10 changes: 6 additions & 4 deletions src/checker/providerCollectionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import { LanguageDetect } from "./providers/languageDetect";
import { EntropyDetect } from "./providers/entropyDetect";
import { ProviderCollection } from "./providerCollection";
import { ClassNameDetect } from "./providers/classNameProvider";
import { IStatsEmiter } from "../stats/statsEmiter";

export class ProviderCollectionFactory {
static createIncludeAll(): ProviderCollection {
return new ProviderCollection([new StringDetect()]);
static createIncludeAll(statsEmiter: IStatsEmiter): ProviderCollection {
return new ProviderCollection([new StringDetect()], statsEmiter);
}

static createInstance(): ProviderCollection {
static createInstance(statsEmiter: IStatsEmiter): ProviderCollection {
return new ProviderCollection([
new KeywordsDetect(),
new ClassNameDetect(),
new CodeDetect(),
new LanguageDetect(),
new EntropyDetect()]);
new EntropyDetect()],
statsEmiter);
}
}
10 changes: 8 additions & 2 deletions src/checker/providers/classNameProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { IDetectProvider } from "./detectProvider";
import { ConfidenceLevel } from "../../enumerations";
import { ConfidenceLevel, StatsEventType } from "../../enumerations";
import { ProviderName } from "../../constants";

export class ClassNameDetect implements IDetectProvider {
readonly name: string = 'Class provider';
//#region IDetectProvider

readonly name: string = ProviderName.ClassName;
readonly eventWhenTechnical: StatsEventType = StatsEventType.DetectedAsTechnicalByClassName;
readonly eventWhenMessage: StatsEventType = StatsEventType.DetectedAsMessageByClassName;
readonly isStopOnEval: boolean = true;

check(text: string): [ConfidenceLevel, string] {
Expand Down Expand Up @@ -55,4 +59,6 @@ export class ClassNameDetect implements IDetectProvider {

return [ConfidenceLevel.Unknown, ''];
}

//#endregion
}
16 changes: 11 additions & 5 deletions src/checker/providers/codeDetect.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import * as vscode from 'vscode';
import { IDetectProvider } from "./detectProvider";
import { ConfidenceLevel } from "../../enumerations";
import { Constants } from '../../constants';
import { ConfidenceLevel, StatsEventType } from "../../enumerations";
import { Constants, ProviderName } from '../../constants';
import { isCamelCase, isPascalCase, getNonAlphaRatio } from '../../helpers/utils';

export class CodeDetect implements IDetectProvider {
private _minWordLength: number;
private _maxWordLength: number;
private _nonAlphaThreshold: number;
readonly name: string = 'Code provider';

readonly isStopOnEval: boolean = true;

constructor() {
this._minWordLength = vscode.workspace.getConfiguration(Constants.ExtensionID).get<number>('variable.word-min-length')!;
this._maxWordLength = vscode.workspace.getConfiguration(Constants.ExtensionID).get<number>('variable.word-max-length')!;
this._nonAlphaThreshold = vscode.workspace.getConfiguration(Constants.ExtensionID).get<number>('variable.non-alpha-ratio-threshold')!;
}

//#region IDetectProvider

readonly name: string = ProviderName.Code;
readonly eventWhenTechnical: StatsEventType = StatsEventType.DetectedAsTechnicalByCode;
readonly eventWhenMessage: StatsEventType = StatsEventType.DetectedAsMessageByCode;
readonly isStopOnEval: boolean = true;

private static readonly _startChars = [
['..', '..*'], // Path
['#', '#*'], // Reference
Expand Down Expand Up @@ -124,4 +128,6 @@ export class CodeDetect implements IDetectProvider {

return [ConfidenceLevel.Unknown, ''];
}

//#endregion
}
4 changes: 3 additions & 1 deletion src/checker/providers/detectProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ConfidenceLevel } from "../../enumerations";
import { ConfidenceLevel, StatsEventType } from "../../enumerations";

export interface IDetectProvider {
readonly name: string;
readonly eventWhenTechnical: StatsEventType;
readonly eventWhenMessage: StatsEventType;
readonly isStopOnEval: boolean;

check(text: string): [ConfidenceLevel, string];
Expand Down
11 changes: 8 additions & 3 deletions src/checker/providers/entropyDetect.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import { IDetectProvider } from "./detectProvider";
import { ConfidenceLevel } from "../../enumerations";
import { Constants } from '../../constants';
import { ConfidenceLevel, StatsEventType } from "../../enumerations";
import { Constants, ProviderName } from '../../constants';
import { stripNonAlphaCharacters } from '../../helpers/utils';

export class EntropyDetect implements IDetectProvider {
Expand All @@ -11,8 +11,11 @@ export class EntropyDetect implements IDetectProvider {
this._threshold = vscode.workspace.getConfiguration(Constants.ExtensionID).get<number>('entropy.threshold')!;
}

readonly name: string = 'Entropy provider';
//#region IDetectProvider

readonly name: string = ProviderName.Entropy;
readonly eventWhenTechnical: StatsEventType = StatsEventType.DetectedAsTechnicalByEntropy;
readonly eventWhenMessage: StatsEventType = StatsEventType.DetectedAsMessageByEntropy;
readonly isStopOnEval: boolean = false;

check(text: string): [ConfidenceLevel, string] {
Expand All @@ -21,6 +24,8 @@ export class EntropyDetect implements IDetectProvider {
return [entropy > this._threshold ? ConfidenceLevel.Message : ConfidenceLevel.Unknown, `entropy=${entropy.toFixed(2)}`];
}

//#endregion

private getShannonEntropy(text: string): number {
let map: {[id: string]: number; } = {};

Expand Down
10 changes: 8 additions & 2 deletions src/checker/providers/keywordsDetect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IDetectProvider } from "./detectProvider";
import { ConfidenceLevel, DictionaryType } from "../../enumerations";
import { ConfidenceLevel, DictionaryType, StatsEventType } from "../../enumerations";
import { UserDictionaryFactory } from '../../user-dictionary/userDictionaryFactory';
import { UserDictionary } from "../../user-dictionary/userDictionary";
import { ProviderName } from "../../constants";

export class KeywordsDetect implements IDetectProvider {
private _technicalList: UserDictionary;
Expand All @@ -12,8 +13,11 @@ export class KeywordsDetect implements IDetectProvider {
this._messageList = UserDictionaryFactory.createInstance(DictionaryType.IncludeToken) || new UserDictionary();
}

readonly name: string = 'Keywords provider';
//#region IDetectProvider

readonly name: string = ProviderName.Keywords;
readonly eventWhenTechnical: StatsEventType = StatsEventType.DetectedAsTechnicalByKeyword;
readonly eventWhenMessage: StatsEventType = StatsEventType.DetectedAsMessageByKeyword;
readonly isStopOnEval: boolean = true;

check(text: string): [ConfidenceLevel, string] {
Expand All @@ -29,4 +33,6 @@ export class KeywordsDetect implements IDetectProvider {

return [ConfidenceLevel.Unknown, ''];
}

//#endregion
}
13 changes: 9 additions & 4 deletions src/checker/providers/languageDetect.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import * as vscode from 'vscode';
import { IDetectProvider } from "./detectProvider";
import { ConfidenceLevel } from "../../enumerations";
import { Constants } from '../../constants';
import { ConfidenceLevel, StatsEventType } from "../../enumerations";
import { Constants, ProviderName } from '../../constants';
var franc = require('franc');

const UndefinedLanguage: string = 'und';

export class LanguageDetect implements IDetectProvider {
readonly name: string = 'Natural language provider';

//#region IDetectProvider

readonly name: string = ProviderName.Language;
readonly eventWhenTechnical: StatsEventType = StatsEventType.DetectedAsTechnicalByLanguage;
readonly eventWhenMessage: StatsEventType = StatsEventType.DetectedAsMessageByLanguage;
readonly isStopOnEval: boolean = false;

check(text: string): [ConfidenceLevel, string] {
Expand All @@ -18,4 +21,6 @@ export class LanguageDetect implements IDetectProvider {

return language === UndefinedLanguage ? [ConfidenceLevel.Unknown, ''] : [ConfidenceLevel.Message, `lang=${language}`];
}

//#endregion
}
10 changes: 8 additions & 2 deletions src/checker/providers/stringDetect.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { IDetectProvider } from "./detectProvider";
import { ConfidenceLevel } from "../../enumerations";
import { ConfidenceLevel, StatsEventType } from "../../enumerations";
import { ProviderName } from "../../constants";

export class StringDetect implements IDetectProvider {
readonly name: string = 'String provider';
//#region IDetectProvider

readonly name: string = ProviderName.String;
readonly eventWhenTechnical: StatsEventType = StatsEventType.DetectedAsTechnicalByString;
readonly eventWhenMessage: StatsEventType = StatsEventType.DetectedAsMessageByString;
readonly isStopOnEval: boolean = false;

check(_text: string): [ConfidenceLevel, string] {
return [ConfidenceLevel.Message, '*'];
}

//#endregion
}
Loading

0 comments on commit a23e60a

Please sign in to comment.