From 0f2d6c592c74d8425ff5146c1b74beab33c465d7 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Sun, 20 Sep 2020 18:18:41 +0300 Subject: [PATCH] Add separate --stats option --- package.json | 2 ++ src/index.ts | 43 ++++++++--------------- src/{ => stats}/average.ts | 0 src/{ => stats}/averageIntensity.ts | 2 +- src/stats/index.ts | 23 ++++++++++++ src/{ => stats}/intervalsToIntensities.ts | 2 +- src/{ => stats}/normalizedIntensity.ts | 2 +- src/{ => stats}/totalDuration.ts | 2 +- src/{ => stats}/tss.ts | 2 +- src/{ => stats}/tss2.ts | 0 10 files changed, 44 insertions(+), 34 deletions(-) rename src/{ => stats}/average.ts (100%) rename src/{ => stats}/averageIntensity.ts (88%) create mode 100644 src/stats/index.ts rename src/{ => stats}/intervalsToIntensities.ts (92%) rename src/{ => stats}/normalizedIntensity.ts (96%) rename src/{ => stats}/totalDuration.ts (78%) rename src/{ => stats}/tss.ts (96%) rename src/{ => stats}/tss2.ts (100%) diff --git a/package.json b/package.json index cfaff08..47622be 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.ts b/src/index.ts index 11dedd3..d871f31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 workout = parse(tokenize(fs.readFileSync(opts.filename, "utf8"))); -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()} -`); - -console.log(generateZwo(workout)); +if (opts.stats) { + console.log(stats(workout)); +} else { + console.log(generateZwo(workout)); +} diff --git a/src/average.ts b/src/stats/average.ts similarity index 100% rename from src/average.ts rename to src/stats/average.ts diff --git a/src/averageIntensity.ts b/src/stats/averageIntensity.ts similarity index 88% rename from src/averageIntensity.ts rename to src/stats/averageIntensity.ts index d7ae92d..3ed16cf 100644 --- a/src/averageIntensity.ts +++ b/src/stats/averageIntensity.ts @@ -1,5 +1,5 @@ import { pipe } from "ramda"; -import { Interval } from "./ast"; +import { Interval } from "../ast"; import { average } from "./average"; import { intervalsToIntensities } from "./intervalsToIntensities"; diff --git a/src/stats/index.ts b/src/stats/index.ts new file mode 100644 index 0000000..d89b6f0 --- /dev/null +++ b/src/stats/index.ts @@ -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()} +`; +}; diff --git a/src/intervalsToIntensities.ts b/src/stats/intervalsToIntensities.ts similarity index 92% rename from src/intervalsToIntensities.ts rename to src/stats/intervalsToIntensities.ts index 547d3d0..e365eb1 100644 --- a/src/intervalsToIntensities.ts +++ b/src/stats/intervalsToIntensities.ts @@ -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[] => { diff --git a/src/normalizedIntensity.ts b/src/stats/normalizedIntensity.ts similarity index 96% rename from src/normalizedIntensity.ts rename to src/stats/normalizedIntensity.ts index 1b5b0d0..22f0717 100644 --- a/src/normalizedIntensity.ts +++ b/src/stats/normalizedIntensity.ts @@ -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"; diff --git a/src/totalDuration.ts b/src/stats/totalDuration.ts similarity index 78% rename from src/totalDuration.ts rename to src/stats/totalDuration.ts index 334296a..0ed3777 100644 --- a/src/totalDuration.ts +++ b/src/stats/totalDuration.ts @@ -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)); diff --git a/src/tss.ts b/src/stats/tss.ts similarity index 96% rename from src/tss.ts rename to src/stats/tss.ts index fc741e4..028fc98 100644 --- a/src/tss.ts +++ b/src/stats/tss.ts @@ -1,4 +1,4 @@ -import { Interval } from "./ast"; +import { Interval } from "../ast"; // Training Stress Score formula from Training and Racing with a Power Meter: // diff --git a/src/tss2.ts b/src/stats/tss2.ts similarity index 100% rename from src/tss2.ts rename to src/stats/tss2.ts