Separate formatting of stats

This commit is contained in:
Rene Saarsoo 2020-10-03 17:35:09 +03:00
parent c1a435e4d0
commit 77c6a721d4
5 changed files with 31 additions and 14 deletions

View File

@ -1,14 +1,14 @@
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require("fs");
const { generateZwo, parse, stats, parseCliOptions } = require("../dist/index");
const { generateZwo, parse, stats, formatStats, parseCliOptions } = require("../dist/index");
const opts = parseCliOptions();
const workout = parse(fs.readFileSync(opts.file, "utf8"));
if (opts.stats) {
console.log(stats(workout));
console.log(formatStats(stats(workout)));
} else {
console.log(generateZwo(workout));
}

View File

@ -1,6 +1,6 @@
export { generateZwo } from "./generateZwo";
export { parse } from "./parser";
export { stats } from "./stats";
export { stats, formatStats } from "./stats";
export { parseCliOptions } from "./parseCliOptions";
// types

View File

@ -1,24 +1,41 @@
import { Workout } from "../ast";
import { Duration } from "../Duration";
import { Intensity } from "../Intensity";
import { averageIntensity } from "./averageIntensity";
import { normalizedIntensity } from "./normalizedIntensity";
import { totalDuration } from "./totalDuration";
import { tss } from "./tss";
import { zoneDistribution } from "./zoneDistribution";
import { zoneDistribution, ZoneDuration } from "./zoneDistribution";
export type Stats = {
totalDuration: Duration;
averageIntensity: Intensity;
normalizedIntensity: Intensity;
tss: number;
zones: ZoneDuration[];
};
// Generates statistics
export const stats = ({ intervals }: Workout): string => {
export const stats = ({ intervals }: Workout): Stats => {
const duration = totalDuration(intervals);
const avgIntensity = averageIntensity(intervals);
const normIntensity = normalizedIntensity(intervals);
const zones = zoneDistribution(intervals);
return {
totalDuration: totalDuration(intervals),
averageIntensity: averageIntensity(intervals),
normalizedIntensity: normalizedIntensity(intervals),
tss: tss(duration, normIntensity),
zones: zoneDistribution(intervals),
};
};
export const formatStats = ({ totalDuration, averageIntensity, normalizedIntensity, tss, zones }: Stats) => {
return `
Total duration: ${(duration.seconds / 60).toFixed()} minutes
Total duration: ${(totalDuration.seconds / 60).toFixed()} minutes
Average intensity: ${(avgIntensity.value * 100).toFixed()}%
Normalized intensity: ${(normIntensity.value * 100).toFixed()}%
Average intensity: ${(averageIntensity.value * 100).toFixed()}%
Normalized intensity: ${(normalizedIntensity.value * 100).toFixed()}%
TSS: ${tss(duration, normIntensity).toFixed()}
TSS: ${tss.toFixed()}
Zone Distribution:
${zones.map(({ name, duration }) => `${(duration.seconds / 60).toFixed().padStart(3)} min - ${name}`).join("\n")}

View File

@ -5,7 +5,7 @@ import { intensityValueToZoneType, ZoneType } from "../ZoneType";
import { intervalsToIntensityNumbers } from "./intervalsToIntensityNumbers";
type NumericZoneDuration = { name: string; duration: number };
type ZoneDuration = { name: string; duration: Duration };
export type ZoneDuration = { name: string; duration: Duration };
const emptyZones = (): Record<ZoneType, NumericZoneDuration> => ({
Z1: { name: "Z1: Recovery", duration: 0 },

View File

@ -1,9 +1,9 @@
import * as fs from "fs";
import { generateZwo } from "../src/generateZwo";
import { parse } from "../src/parser";
import { stats } from "../src/stats";
import { formatStats, stats } from "../src/stats";
const createStats = (filename: string) => stats(parse(fs.readFileSync(filename, "utf8")));
const createStats = (filename: string) => formatStats(stats(parse(fs.readFileSync(filename, "utf8"))));
const createZwo = (filename: string) => generateZwo(parse(fs.readFileSync(filename, "utf8")));
const filenames = [