Skip to content

Percent round js function #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions enums/enum-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { InformationSourceType } from "./enums";

export function informationSourceTypeToString(source: InformationSourceType, short: boolean, forDisplay: boolean = true) {
if (forDisplay) {
switch (source) {
case InformationSourceType.LABELING_FUNCTION: return short ? "LF" : "Labeling Function module";
case InformationSourceType.ACTIVE_LEARNING: return short ? "AL" : "Active Learning module";
case InformationSourceType.PRE_COMPUTED: return short ? "PC" : "Pre Computed module";
case InformationSourceType.ZERO_SHOT: return short ? "ZS" : "Zero Shot module";
case InformationSourceType.CROWD_LABELER: return short ? "CL" : "Crowd labeler";
default: return source;
}
}
return source;
}
7 changes: 7 additions & 0 deletions enums/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum InformationSourceType {
LABELING_FUNCTION = "LABELING_FUNCTION",
ACTIVE_LEARNING = "ACTIVE_LEARNING",
PRE_COMPUTED = "PRE_COMPUTED",
ZERO_SHOT = "ZERO_SHOT",
CROWD_LABELER = "CROWD_LABELER"
}
13 changes: 13 additions & 0 deletions general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,17 @@ export function getUserAvatarUri(user) {
avatarId = (user.firstName[0].charCodeAt(0) + user.lastName[0].charCodeAt(0)) % 5;
}
return avatarId + ".png";
}

export function percentRoundString(value: number | string, decimals: number = 0, isZeroToOne: boolean = true) {
if (typeof value == 'number') {
if (isNaN(value)) return "n/a";
if (!isFinite(value)) return "0 %";
if (isZeroToOne) value *= 100;
if (!decimals) return Math.round(value) + '%';
const dec = 10 ** decimals;
return Math.round(value * dec) / dec + ' %';
}
else if (typeof value == 'undefined' || value == null) return "n/a";
return value;
}
24 changes: 24 additions & 0 deletions logs-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { informationSourceTypeToString } from "./enums/enum-functions";
import { InformationSourceType } from "./enums/enums";

export function parseContainerLogsData(logs: string[], isType: InformationSourceType = null) {
if (!logs) {
if (isType) return [`Running ${informationSourceTypeToString(isType, false)}...`];
else return null;
}
if (!Array.isArray(logs)) return null;
if (logs.length == 0) return [];

const neededIDLength = String(logs.length)?.length;
return logs.map((wrapper, index) => {
const d: Date = new Date(wrapper.substr(0, wrapper.indexOf(' ')));
if (isNaN(d.getTime())) return wrapper;
return (
String(index + 1).padStart(neededIDLength, '0') +
': ' +
d.toLocaleString() +
' - ' +
wrapper.substr(wrapper.indexOf(' ') + 1)
);
});
}