Skip to content

Commit

Permalink
feat(logs): display the total of stats for mutations and queries API …
Browse files Browse the repository at this point in the history
…calls

Created a nested view
  • Loading branch information
C0ZEN committed Jan 29, 2022
1 parent 9f7700d commit 86261dd
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 56 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

105 changes: 89 additions & 16 deletions src/core/statistics/abstract-statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LoggerService } from '@utils/loggers/logger.service';
import { getMapLastKey } from '@utils/maps/get-map-last-key';
import { getMapLongestKey } from '@utils/maps/get-map-longest-key';
import { mapFilter } from '@utils/maps/map-filter';
import { isFiniteNumber } from '@utils/numbers/is-finite-number';
import { ETreeRows } from '@utils/trees/tree-rows.enum';
import _ from 'lodash';

Expand All @@ -19,7 +20,8 @@ export abstract class AbstractStatisticsService<TStatistic extends string> {
/**
* @description
* Log the statistics from this service
* @returns {AbstractStatisticsService} The service
* @template TStatistic
* @returns {AbstractStatisticsService<TStatistic>} The service
*/
public logsAllStatistics(): AbstractStatisticsService<TStatistic> {
LoggerService.startGroup(`${_.upperFirst(this._statisticsName)} statistics`);
Expand Down Expand Up @@ -57,35 +59,92 @@ export abstract class AbstractStatisticsService<TStatistic extends string> {
/**
* @description
* Internal logger to humanized and improve the logs when logging them all
* @returns {AbstractStatisticsService} The service
* @template TStatistic
* @returns {AbstractStatisticsService<TStatistic>} The service
* @private
*/
private _logsAllStatistics(): AbstractStatisticsService<TStatistic> {
const allStatistics: Map<TStatistic, number> = this._getAllFilteredStatisticsMap();
const lastStatistic: TStatistic | undefined = getMapLastKey(allStatistics);
const longestStatisticLength: number = getMapLongestKey(allStatistics);
const allStatistics: Map<TStatistic, Map<TStatistic, number> | number> = this._getAllFilteredStatisticsMap();

this._logStatisticsMap(allStatistics);

allStatistics.forEach((count: Readonly<number>, statistic: TStatistic): void => {
return this;
}

/**
* @description
* Log the statistics for a map
* @template TStatistic
* @param {Readonly<Map<TStatistic, Map<TStatistic, number> | number>>} statisticsMap The map of statistics to log
* @private
*/
private _logStatisticsMap(statisticsMap: Readonly<Map<TStatistic, Map<TStatistic, number> | number>>): void {
const lastStatistic: TStatistic | undefined = getMapLastKey(statisticsMap);
const longestStatisticLength: number = getMapLongestKey(statisticsMap);

statisticsMap.forEach((data: Readonly<Map<TStatistic, number> | number>, statistic: TStatistic): void => {
const prefix: ETreeRows = statistic === lastStatistic ? ETreeRows.LAST : ETreeRows.ANY;
const count: number = isFiniteNumber(data) ? data : this._getStatisticsMapCount(data);

this._log(prefix, _.padEnd(statistic, longestStatisticLength), count);

// Data is a map - we need to add a level
if (!isFiniteNumber(data)) {
this._logStatisticsSubMap(data, prefix);
}
});
}

return this;
/**
* @description
* Log the statistics for a sub map recursively
* @template TStatistic
* @param {Readonly<Map<TStatistic, number>>} statisticsMap The map of statistics to log
* @param {Readonly<ETreeRows>} prefix The parent statistic prefix
* @private
*/
private _logStatisticsSubMap(statisticsMap: Readonly<Map<TStatistic, number>>, prefix: Readonly<ETreeRows>): void {
const lastStatistic: TStatistic | undefined = getMapLastKey(statisticsMap);
const longestStatisticLength: number = getMapLongestKey(statisticsMap);

statisticsMap.forEach((count: Readonly<number>, statistic: TStatistic): void => {
const parentPrefix: string = prefix === ETreeRows.LAST ? ` ` : `${ETreeRows.EMPTY} `;
const subPrefix: ETreeRows = statistic === lastStatistic ? ETreeRows.LAST : ETreeRows.ANY;

this._log(`${parentPrefix}${subPrefix}`, _.padEnd(statistic, longestStatisticLength), count);
});
}

/**
* @description
* Return the sum for the given statistics
* @template TStatistic
* @param {Readonly<Map<TStatistic, number>>} statisticsMap The map of statistics to sum
* @returns {number} The sum of the statistics
* @private
*/
private _getStatisticsMapCount(statisticsMap: Readonly<Map<TStatistic, number>>): number {
let count: number = 0;

statisticsMap.forEach((statistic: Readonly<number>): void => {
count += statistic;
});

return count;
}

/**
* @description
* Log a single statistic
* @template TStatistic
* @param {Readonly<ETreeRows>} prefix The prefix of the statistic name as a tree row
* @param {Readonly<string | ETreeRows>} prefix The prefix of the statistic name as a tree row
* @param {Readonly<string | TStatistic>} statistic The name of the statistic
* @param {Readonly<number>} count The statistic count
* @returns {AbstractStatisticsService} The service
* @returns {AbstractStatisticsService<TStatistic>} The service
* @private
*/
private _log(
prefix: Readonly<ETreeRows>,
prefix: Readonly<string | ETreeRows>,
statistic: Readonly<TStatistic | string>,
count: Readonly<number>
): AbstractStatisticsService<TStatistic> {
Expand All @@ -102,26 +161,40 @@ export abstract class AbstractStatisticsService<TStatistic extends string> {
* @description
* Get the map of filtered statistics excluding all bellow 1
* @template TStatistic
* @returns {Map<TStatistic, number>} The filtered map of statistics
* @returns {Map<TStatistic, Map<TStatistic, number> | number>} The filtered map of statistics
* @private
*/
private _getAllFilteredStatisticsMap(): Map<TStatistic, number> {
return mapFilter<TStatistic, number>(this._getAllStatisticsMap(), ([_statistic, count]): boolean => count > 0);
private _getAllFilteredStatisticsMap(): Map<TStatistic, Map<TStatistic, number> | number> {
return mapFilter<TStatistic, Map<TStatistic, number> | number>(
this._getAllStatisticsMap(),
([_statistic, data]): boolean => {
// Data is a count
if (isFiniteNumber(data)) {
return data > 0;
}

const subMapStatisticsCount: number = this._getStatisticsMapCount(data);

// Data is a map - we need to add a level
return subMapStatisticsCount > 0;
}
);
}

/**
* @description
* Only used for the tests to reset the state
* @returns {AbstractStatisticsService} The service
* @template TStatistic
* @returns {AbstractStatisticsService<TStatistic>} The service
*/
public abstract initialize(): AbstractStatisticsService<TStatistic>;

/**
* @description
* Get all the statistics as a map
* @template TStatistic
* @returns {Map<TStatistic, number>} The map of statistics
* @returns {Map<TStatistic, Map<TStatistic, number> | number>} The map of statistics
* @protected
*/
protected abstract _getAllStatisticsMap(): Map<TStatistic, number>;
protected abstract _getAllStatisticsMap(): Map<TStatistic, Map<TStatistic, number> | number>;
}
32 changes: 19 additions & 13 deletions src/core/statistics/issues-statistics.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,68 +373,74 @@ describe(`IssuesStatisticsService`, (): void => {
});

it(`should log the statistics`, (): void => {
expect.assertions(11);
expect.assertions(12);

service.logsAllStatistics();

expect(loggerServiceInfoSpy).toHaveBeenCalledTimes(10);
expect(loggerServiceInfoSpy).toHaveBeenCalledTimes(11);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
1,
`white-├──`,
`whiteBright-Processed issues `,
`whiteBright-Processed issues `,
`value-1`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
2,
`white-├──`,
`whiteBright-Ignored issues `,
`whiteBright-Ignored issues `,
`value-2`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
3,
`white-├──`,
`whiteBright-Stale issues `,
`whiteBright-Stale issues `,
`value-3`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
4,
`white-├──`,
`whiteBright-Already stale issues `,
`whiteBright-Already stale issues `,
`value-4`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
5,
`white-├──`,
`whiteBright-Remove stale issues `,
`whiteBright-Remove stale issues `,
`value-5`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
6,
`white-├──`,
`whiteBright-Closed issues `,
`whiteBright-Closed issues `,
`value-6`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
7,
`white-├──`,
`whiteBright-Added issues comments `,
`whiteBright-Added issues comments`,
`value-7`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
8,
`white-├──`,
`whiteBright-Added issues labels `,
`whiteBright-Added issues labels `,
`value-8`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
9,
`white-├──`,
`white-└──`,
`whiteBright-Called API issues `,
`value-19`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
10,
`white- ├──`,
`whiteBright-Called API issues queries `,
`value-9`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
10,
`white-└──`,
11,
`white- └──`,
`whiteBright-Called API issues mutations`,
`value-10`
);
Expand Down
13 changes: 9 additions & 4 deletions src/core/statistics/issues-statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type IStat =
| 'Closed issues'
| 'Added issues comments'
| 'Added issues labels'
| 'Called API issues'
| 'Called API issues queries'
| 'Called API issues mutations';

Expand Down Expand Up @@ -144,8 +145,8 @@ export class IssuesStatisticsService extends AbstractStatisticsService<IStat> {
return this;
}

protected _getAllStatisticsMap(): Map<IStat, number> {
return new Map<IStat, number>()
protected _getAllStatisticsMap(): Map<IStat, Map<IStat, number> | number> {
return new Map<IStat, Map<IStat, number> | number>()
.set(`Processed issues`, this.processedIssuesCount)
.set(`Ignored issues`, this.ignoredIssuesCount)
.set(`Unaltered issues`, this.unalteredIssuesCount)
Expand All @@ -155,7 +156,11 @@ export class IssuesStatisticsService extends AbstractStatisticsService<IStat> {
.set(`Closed issues`, this.closedIssuesCount)
.set(`Added issues comments`, this.addedIssuesCommentsCount)
.set(`Added issues labels`, this.addedIssuesLabelsCount)
.set(`Called API issues queries`, this.calledApiIssuesQueriesCount)
.set(`Called API issues mutations`, this.calledApiIssuesMutationsCount);
.set(
`Called API issues`,
new Map<IStat, number>()
.set(`Called API issues queries`, this.calledApiIssuesQueriesCount)
.set(`Called API issues mutations`, this.calledApiIssuesMutationsCount)
);
}
}
36 changes: 21 additions & 15 deletions src/core/statistics/pull-requests-statistics.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,80 +425,86 @@ describe(`PullRequestsStatisticsService`, (): void => {
});

it(`should log the statistics`, (): void => {
expect.assertions(13);
expect.assertions(14);

service.logsAllStatistics();

expect(loggerServiceInfoSpy).toHaveBeenCalledTimes(12);
expect(loggerServiceInfoSpy).toHaveBeenCalledTimes(13);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
1,
`white-├──`,
`whiteBright-Processed pull requests `,
`whiteBright-Processed pull requests `,
`value-1`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
2,
`white-├──`,
`whiteBright-Ignored pull requests `,
`whiteBright-Ignored pull requests `,
`value-2`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
3,
`white-├──`,
`whiteBright-Stale pull requests `,
`whiteBright-Stale pull requests `,
`value-3`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
4,
`white-├──`,
`whiteBright-Already stale pull requests `,
`whiteBright-Already stale pull requests `,
`value-4`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
5,
`white-├──`,
`whiteBright-Remove stale pull requests `,
`whiteBright-Remove stale pull requests `,
`value-5`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
6,
`white-├──`,
`whiteBright-Closed pull requests `,
`whiteBright-Closed pull requests `,
`value-6`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
7,
`white-├──`,
`whiteBright-Deleted pull requests branches `,
`whiteBright-Deleted pull requests branches`,
`value-7`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
8,
`white-├──`,
`whiteBright-Added pull requests comments `,
`whiteBright-Added pull requests comments `,
`value-8`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
9,
`white-├──`,
`whiteBright-Added pull requests labels `,
`whiteBright-Added pull requests labels `,
`value-9`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
10,
`white-├──`,
`whiteBright-Draft pull requests `,
`whiteBright-Draft pull requests `,
`value-10`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
11,
`white-├──`,
`white-└──`,
`whiteBright-Called API pull requests `,
`value-23`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
12,
`white- ├──`,
`whiteBright-Called API pull requests queries `,
`value-11`
);
expect(loggerServiceInfoSpy).toHaveBeenNthCalledWith(
12,
`white-└──`,
13,
`white- └──`,
`whiteBright-Called API pull requests mutations`,
`value-12`
);
Expand Down

0 comments on commit 86261dd

Please sign in to comment.