Skip to content

Commit

Permalink
feat: remove empty data columns from results
Browse files Browse the repository at this point in the history
  • Loading branch information
thraizz committed Dec 17, 2023
1 parent 46e6cf5 commit d23d78d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/SessionPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { Label } from "./Label";
import { SessionContext } from "./SessionContext";
import { UserContext } from "./UserProvider";
import { db } from "./firebaseConfig";
import { Sessions } from "./types/Sessions";
import { Session, Sessions } from "./types/Sessions";
import { reduceSessionToDefinedValues } from "./utils";

export const SessionPicker = () => {
const [selected, setSelected] = useState<string[]>([]);
Expand All @@ -21,8 +22,7 @@ export const SessionPicker = () => {
collection(db, "r10data", uuid, "data"),
);
const fetchedSessions = querySnapshot.docs.reduce((acc, curr) => {
const data = curr.data();
// @ts-expect-error - The results property will always be set as we upload it this way
const data = reduceSessionToDefinedValues(curr.data() as Session);
acc[curr.id] = { ...data, selected: true };
return acc;
}, {} as Sessions);
Expand Down
79 changes: 46 additions & 33 deletions src/types/GolfSwingData.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,4 @@
export type GolfSwingData = {
Schlagflächenstellung: null | number | undefined;
Luftdruck: number | null | undefined;
Schlagfläche: null | number;
Datum: string | null;
Temperatur: number | null;
Markierung: null | string;
Luftdichte: number | null;
Schwungbahn: null | number;
Drehrate: number | null;
"Smash Factor": number | null;
Drehratentyp: string | null;
Ballgeschwindigkeit: number | null;
Gesamtabweichungswinkel: number | null;
"Höhe des Scheitelpunkts": number | null;
Gesamtabweichungsdistanz: number | null;
Drehachse: number | null;
"Carry-Abweichungsdistanz": number | null;
Abflugrichtung: number | null;
Backspin: number | null;
Schlägername: null | string;
"Carry-Abweichungswinkel": number | null;
Sidespin: number | null;
Gesamtstrecke: number | null;
Spieler: string | null;
Abflugwinkel: number | null;
"Relative Luftfeuchtigkeit": number | null;
"Schl.gsch.": number | null;
Notiz: null | string;
Schlägerart: string | null;
Anstellwinkel: null | number;
"Carry-Distanz": number | null;
} & {
type GolfSwingDataEN = {
"Club Speed": number;
"Launch Direction": number;
"Carry Distance": number;
Expand Down Expand Up @@ -64,17 +32,62 @@ export type GolfSwingData = {
Backspin: number;
};

type GolfSwingDataDE = {
Schlagflächenstellung: null | number | undefined;
Luftdruck: number | null | undefined;
Schlagfläche: null | number;
Datum: string | null;
Temperatur: number | null;
Markierung: null | string;
Luftdichte: number | null;
Schwungbahn: null | number;
Drehrate: number | null;
"Smash Factor": number | null;
Drehratentyp: string | null;
Ballgeschwindigkeit: number | null;
Gesamtabweichungswinkel: number | null;
"Höhe des Scheitelpunkts": number | null;
Gesamtabweichungsdistanz: number | null;
Drehachse: number | null;
"Carry-Abweichungsdistanz": number | null;
Abflugrichtung: number | null;
Backspin: number | null;
Schlägername: null | string;
"Carry-Abweichungswinkel": number | null;
Sidespin: number | null;
Gesamtstrecke: number | null;
Spieler: string | null;
Abflugwinkel: number | null;
"Relative Luftfeuchtigkeit": number | null;
"Schl.gsch.": number | null;
Notiz: null | string;
Schlägerart: string | null;
Anstellwinkel: null | number;
"Carry-Distanz": number | null;
};

export type GolfSwingData = GolfSwingDataEN | GolfSwingDataDE;

export const golfSwingDataKeysInMeters: Array<keyof GolfSwingData> = [
"Carry Distance",
"Carry-Distanz",
"Carry Deviation Distance",
"Carry-Abweichungsdistanz",
"Total Distance",
"Gesamtstrecke",
"Total Deviation Distance",
"Gesamtabweichungsdistanz",
"Apex Height",
"Höhe des Scheitelpunkts",
];

export const golfSwingDataKeysInDegrees: Array<keyof GolfSwingData> = [
"Carry Deviation Angle",
"Carry-Abweichungswinkel",
"Total Deviation Angle",
"Gesamtabweichungswinkel",
"Launch Direction",
"Abflugrichtung",
"Club Face",
"Abflugwinkel",
];
10 changes: 5 additions & 5 deletions src/types/Sessions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GolfSwingData } from "./GolfSwingData";

export type Session = {
results: GolfSwingData[];
selected: boolean;
};
export type Sessions = {
[key: string]: {
results: GolfSwingData[];
selected: boolean;
};
[key: string]: Session;
};
31 changes: 31 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { GolfSwingData } from "./types/GolfSwingData";
import { Session } from "./types/Sessions";

export const sortGolfSwingKeysForHeader = (a: string, b: string) => {
// We want to prioritize Carry Distance, Total Distance, Club Name, Ball Spped, and Date
// over all other keys. This is because these keys are the most important ones to
Expand Down Expand Up @@ -62,3 +65,31 @@ export const sortGolfSwingKeysForHeader = (a: string, b: string) => {
// If both keys are not in the list of prioritized keys, sort them alphabetically
return a.localeCompare(b);
};

export const reduceSessionToDefinedValues: (session: Session) => Session = (
session,
) => {
const fieldValueCount = session.results.reduce((accumulator, curr) => {
// @ts-expect-error - We know that the type will be correct
Object.keys(curr).forEach((key: keyof GolfSwingData) => {
if (accumulator[key] === undefined) accumulator[key] = 0;
if (curr[key] !== undefined && curr[key] !== null)
accumulator[key] = (accumulator[key] ?? 0) + 1;
});
return accumulator;
}, {} as GolfSwingData);

const fieldsWithoutValues = Object.keys(fieldValueCount).filter(
(key) => fieldValueCount[key as keyof GolfSwingData] === 0,
);

const reducedResults = session.results.map((result) => {
const newResult = { ...result };
fieldsWithoutValues.forEach((key) => {
delete newResult[key as keyof GolfSwingData];
});
return newResult;
});

return { ...session, results: reducedResults };
};

0 comments on commit d23d78d

Please sign in to comment.