All files / reporter/xray xray.ts

85.19% Statements 23/27
35% Branches 7/20
71.43% Functions 5/7
85.19% Lines 23/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 741x 1x                             1x   1x 2x 2x 1x   1x   1x 1x 1x   1x     1x       1x                   1x 1x     2x             1x       1x 1x 1x   1x 1x              
import { addFeatureChangeObserver } from '../../src/feature/feature';
import { createConsoleReporter } from '../console';
import { ExperimentsObserver } from '../../src/feature/feature.typings';
 
export type LikeXRay = {
	send: (t: string, p: {i: {[key:string]: string}}) => void;
	addSplit: (v: string) => void;
}
 
export type XRayReporterInit = {
	send: LikeXRay['send'];
	prepare?: (key: string) => string;
	glue?: string;
	verbose?: boolean;
}
 
export function createXRayReporter(init: XRayReporterInit): ExperimentsObserver {
	const {
		send,
		glue = '_',
		prepare = defaultPrepare,
		verbose,
	} = init;
	const log = verbose ? createConsoleReporter() : null as (ExperimentsObserver | null);
 
	return (feature, xevt) => {
		const chain = [feature.id].concat(xevt.target.$path());
		const metrics = {};
 
		createIntervals(metrics, xevt.data, glue),
 
		(log !== null) && log(feature, xevt);
		send(chain.map(prepare).join(glue), {i: metrics});
	};
}
 
export function xraySplitAutoConfiguration(xray: LikeXRay) {
	return addFeatureChangeObserver((feature) => {
		if (feature.enabled && feature.split) {
			xray.addSplit(`s${feature.split}s`);
		} else {
			// todo: removeSplit???
		}
	});
}
 
const R_CAMEL = /([A-Z]+\w)/g;
const R_LIKE_CALLBACK = /^on([A-Z])/;
 
function defaultPrepare(key: string) {
	return key
		.replace(R_LIKE_CALLBACK, '$1')
		.replace(R_CAMEL, toKebabCase)
	;
}
 
function toKebabCase(_: string, match: string, offset: number) {
	return (offset ? '-' : '') + match.toLowerCase();
}
 
function createIntervals(metrics: object, data: object, glue: string): void {
	for (const key in data) {
		Eif (data.hasOwnProperty(key)) {
			const val = data[key];
 
			Eif (typeof val === 'string') {
				metrics[key + glue + val] = 1;
			} else {
				metrics[key] = +val >= 0 ? +val : 1;
			}
		}
	}
}