Add separate --stats option

This commit is contained in:
Rene Saarsoo 2020-09-20 18:18:41 +03:00
parent 5c34c7c167
commit 0f2d6c592c
10 changed files with 44 additions and 34 deletions

View File

@ -6,6 +6,8 @@
"lint:ts": "tsc --noEmit",
"test": "ts-node src/index.ts examples/threshold-pushing.txt",
"test:2": "ts-node src/index.ts examples/darth-vader.txt",
"test:stats": "ts-node src/index.ts --stats examples/threshold-pushing.txt",
"test:stats:2": "ts-node src/index.ts --stats examples/darth-vader.txt",
"format:js": "prettier --write src/"
},
"dependencies": {

View File

@ -1,36 +1,21 @@
import * as fs from "fs";
import { averageIntensity } from "./averageIntensity";
import { generateZwo } from "./generateZwo";
import { normalizedIntensity } from "./normalizedIntensity";
import { parse } from "./parser";
import { stats } from "./stats";
import { tokenize } from "./tokenizer";
import { totalDuration } from "./totalDuration";
import { tss } from "./tss";
import { tss2 } from "./tss2";
const filename = process.argv[2];
const opts = { stats: false, filename: "" };
if (process.argv[2] === "--stats") {
opts.stats = true;
opts.filename = process.argv[3];
} else {
opts.filename = process.argv[2];
}
console.log(`Parsing: ${filename}`);
const file = fs.readFileSync(filename, "utf8");
const workout = parse(tokenize(file));
const { intervals } = workout;
const duration = totalDuration(intervals);
const avgIntensity = averageIntensity(intervals);
const normIntensity = normalizedIntensity(intervals);
console.log(intervals);
console.log(`
Total duration: ${(duration / 60).toFixed()} minutes
Average intensity: ${(avgIntensity * 100).toFixed()}%
Normalized intensity: ${(normIntensity * 100).toFixed()}%
TSS #1: ${tss(intervals).toFixed()}
TSS #2: ${tss2(duration, normIntensity).toFixed()}
`);
const workout = parse(tokenize(fs.readFileSync(opts.filename, "utf8")));
if (opts.stats) {
console.log(stats(workout));
} else {
console.log(generateZwo(workout));
}

View File

@ -1,5 +1,5 @@
import { pipe } from "ramda";
import { Interval } from "./ast";
import { Interval } from "../ast";
import { average } from "./average";
import { intervalsToIntensities } from "./intervalsToIntensities";

23
src/stats/index.ts Normal file
View File

@ -0,0 +1,23 @@
import { Workout } from "../ast";
import { averageIntensity } from "./averageIntensity";
import { normalizedIntensity } from "./normalizedIntensity";
import { totalDuration } from "./totalDuration";
import { tss } from "./tss";
import { tss2 } from "./tss2";
// Generates statistics
export const stats = ({ intervals }: Workout): string => {
const duration = totalDuration(intervals);
const avgIntensity = averageIntensity(intervals);
const normIntensity = normalizedIntensity(intervals);
return `
Total duration: ${(duration / 60).toFixed()} minutes
Average intensity: ${(avgIntensity * 100).toFixed()}%
Normalized intensity: ${(normIntensity * 100).toFixed()}%
TSS #1: ${tss(intervals).toFixed()}
TSS #2: ${tss2(duration, normIntensity).toFixed()}
`;
};

View File

@ -1,5 +1,5 @@
import { chain } from "ramda";
import { Interval } from "./ast";
import { Interval } from "../ast";
// Converts interval to array of intensity values for each second
const intervalToIntensities = ({ duration, intensity }: Interval): number[] => {

View File

@ -1,5 +1,5 @@
import { pipe, sum } from "ramda";
import { Interval } from "./ast";
import { Interval } from "../ast";
import { average } from "./average";
import { intervalsToIntensities } from "./intervalsToIntensities";

View File

@ -1,5 +1,5 @@
import { pluck, sum } from "ramda";
import { Interval } from "./ast";
import { Interval } from "../ast";
export const totalDuration = (intervals: Interval[]) =>
sum(pluck("duration", intervals));

View File

@ -1,4 +1,4 @@
import { Interval } from "./ast";
import { Interval } from "../ast";
// Training Stress Score formula from Training and Racing with a Power Meter:
//