Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/app/app-config.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
thumbnailFetchLimitPerPage: 500,
maxFileUploadSizeInMb: "16mb",
maxDirectDownloadSize: 5000000000,
metadataFloatFormatEnabled: true,
metadataFloatFormat: {
significantDigits: 3,
minCutoff: 0.001,
maxCutoff: 1000,
},
metadataPreviewEnabled: true,
metadataStructure: "",
multipleDownloadAction: "http://localhost:3012/zip",
Expand Down Expand Up @@ -325,7 +331,7 @@
backendError = false,
) => {
spyOn(service["http"], "get").and.callFake(
(url: string): Observable<any> => {

Check warning on line 334 in src/app/app-config.service.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
if (url === "/api/v3/admin/config") {
if (backendError) {
return new Observable((sub) =>
Expand Down
16 changes: 16 additions & 0 deletions src/app/app-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
authenticatedUser: MainMenuOptions;
}

export class MetadataFloatFormat {
significantDigits: number;
minCutoff: number; // using scientific notation below this cutoff
maxCutoff: number; // using scientific notation above this cutoff
}

export interface AppConfigInterface {
allowConfigOverrides?: boolean;
skipSciCatLoginPageEnabled?: boolean;
Expand All @@ -74,7 +80,7 @@
datasetReduceEnabled: boolean;
datasetDetailsShowMissingProposalId: boolean;
datafilesActionsEnabled: boolean;
datafilesActions: any[];

Check warning on line 83 in src/app/app-config.service.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
editDatasetEnabled: boolean;
editDatasetSampleEnabled: boolean;
editMetadataEnabled: boolean;
Expand Down Expand Up @@ -103,6 +109,8 @@
maxDirectDownloadSize: number | null;
metadataPreviewEnabled: boolean;
metadataStructure: string;
metadataFloatFormat?: MetadataFloatFormat;
metadataFloatFormatEnabled?: boolean;
multipleDownloadAction: string | null;
multipleDownloadEnabled: boolean;
multipleDownloadUseAuthToken: boolean;
Expand Down Expand Up @@ -146,7 +154,7 @@
checkBoxFilterClickTrigger?: boolean;
}

function isMainPageConfiguration(obj: any): obj is MainPageConfiguration {

Check warning on line 157 in src/app/app-config.service.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
const validKeys = Object.keys(MainPageOptions);
return (
obj &&
Expand Down Expand Up @@ -205,7 +213,7 @@
.pipe(timeout(2000))
.toPromise();
this.appConfig = Object.assign({}, this.appConfig, config);
} catch (err) {

Check warning on line 216 in src/app/app-config.service.ts

View workflow job for this annotation

GitHub Actions / eslint

'err' is defined but never used
console.log("No config available in backend, trying with local config.");
const config = await this.mergeConfig();
this.appConfig = Object.assign({}, this.appConfig, config);
Expand Down Expand Up @@ -237,6 +245,14 @@
config.dateFormat = "yyyy-MM-dd HH:mm";
}

if (!config.metadataFloatFormat) {
config.metadataFloatFormat = {
significantDigits: 3,
minCutoff: 0.001,
maxCutoff: 1000,
};
}

this.appConfig = config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,42 @@ import { waitForAsync, ComponentFixture, TestBed } from "@angular/core/testing";
import { FormatNumberPipe } from "shared/pipes/format-number.pipe";
import { PrettyUnitPipe } from "shared/pipes/pretty-unit.pipe";
import { ScientificMetadataTreeModule } from "../scientific-metadata-tree.module";

import { TreeViewComponent } from "./tree-view.component";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { AppConfigService } from "app-config.service";
import { MetadataValueService } from "shared/services/metadata-value.service";
import { provideHttpClient } from "@angular/common/http";

describe("TreeViewComponent", () => {
let component: TreeViewComponent;
let fixture: ComponentFixture<TreeViewComponent>;

beforeEach(waitForAsync(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
declarations: [TreeViewComponent],
imports: [ScientificMetadataTreeModule, BrowserAnimationsModule],
providers: [DatePipe, PrettyUnitPipe, FormatNumberPipe],
providers: [
DatePipe,
PrettyUnitPipe,
FormatNumberPipe,
MetadataValueService,
AppConfigService,
provideHttpClient(),
],
}).compileComponents();
}));

beforeEach(() => {
const appConfigService = TestBed.inject(AppConfigService);
(appConfigService as any).appConfig = {
metadataFloatFormatEnabled: true,
metadataFloatFormat: {
significantDigits: 3,
minCutoff: 0.001,
maxCutoff: 1000,
},
};
fixture = TestBed.createComponent(TreeViewComponent);
component = fixture.componentInstance;
component.metadata = {
Expand All @@ -39,4 +58,10 @@ describe("TreeViewComponent", () => {
it("should create", () => {
expect(component).toBeTruthy();
});

it("should format flatNode.value when float formatting is enabled", () => {
const nodes = component.treeControl.dataNodes;
const focusNode = nodes.find((node) => node.key === "focus");
expect(focusNode.value).toBe("-0.272");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TreeNode,
} from "shared/modules/scientific-metadata-tree/base-classes/tree-base";
import { DatePipe } from "@angular/common";
import { MetadataValueService } from "shared/services/metadata-value.service";
@Component({
selector: "tree-view",
templateUrl: "./tree-view.component.html",
Expand All @@ -27,9 +28,11 @@ export class TreeViewComponent
implements OnInit, OnChanges
{
@Input() metadata: any;
constructor(datePipe: DatePipe) {
constructor(
public datePipe: DatePipe,
private metadataValueService: MetadataValueService,
) {
super();
this.datePipe = datePipe;
this.treeFlattener = new MatTreeFlattener(
this.transformer,
this.getLevel,
Expand Down Expand Up @@ -69,7 +72,7 @@ export class TreeViewComponent
: new FlatNode();
flatNode.key = node.key;
flatNode.level = level;
flatNode.value = node.value;
flatNode.value = this.metadataValueService.valueFormat(node.value);
flatNode.unit = node.unit;
flatNode.expandable = node.children?.length > 0;
flatNode.visible = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { ReplaceUnderscorePipe } from "shared/pipes/replace-underscore.pipe";
import { LinkyPipe } from "ngx-linky";
import { DatePipe, TitleCasePipe } from "@angular/common";
import { PrettyUnitPipe } from "shared/pipes/pretty-unit.pipe";
import { AppConfigService } from "app-config.service";
import { provideHttpClient } from "@angular/common/http";

describe("MetadataViewComponent", () => {
let component: MetadataViewComponent;
Expand All @@ -23,12 +25,24 @@ describe("MetadataViewComponent", () => {
DatePipe,
LinkyPipe,
PrettyUnitPipe,
AppConfigService,
provideHttpClient(),
],
declarations: [MetadataViewComponent],
}).compileComponents();
}));

beforeEach(() => {
const appConfigService = TestBed.inject(AppConfigService);
spyOn(appConfigService as any, "getConfig").and.returnValue({
metadataFloatFormatEnabled: true,
metadataFloatFormat: {
significantDigits: 3,
minCutoff: 0.001,
maxCutoff: 1000,
},
});

fixture = TestBed.createComponent(MetadataViewComponent);
component = fixture.componentInstance;
fixture.detectChanges();
Expand All @@ -53,6 +67,19 @@ describe("MetadataViewComponent", () => {
expect(metadataArray[0]["unit"]).toEqual("");
});

it("should round float value if float formatting enabled", () => {
const testMetadata = {
someMetadata: {
value: 12.39421321511,
unit: "m",
},
};
const metadataArray = component.createMetadataArray(testMetadata);

expect(metadataArray[0]["value"]).toEqual("12.4");
expect(metadataArray[0]["unit"]).toEqual("m");
});

it("should parse an untyped metadata object to an array", () => {
const testMetadata = {
untypedTestName: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { DateTime } from "luxon";
import { MetadataTypes } from "../metadata-edit/metadata-edit.component";
import { actionMenu } from "shared/modules/dynamic-material-table/utilizes/default-table-settings";
import { TablePaginationMode } from "shared/modules/dynamic-material-table/models/table-pagination.model";
import { MetadataValueService } from "shared/services/metadata-value.service";

@Component({
selector: "metadata-view",
Expand Down Expand Up @@ -173,6 +174,7 @@ export class MetadataViewComponent implements OnInit, OnChanges {
private replaceUnderscore: ReplaceUnderscorePipe,
private titleCase: TitleCasePipe,
private datePipe: DatePipe,
private metadataValueService: MetadataValueService,
public linkyPipe: LinkyPipe,
public prettyUnit: PrettyUnitPipe,
) {}
Expand All @@ -194,9 +196,13 @@ export class MetadataViewComponent implements OnInit, OnChanges {
typeof metadata[key] === "object" &&
"value" in (metadata[key] as ScientificMetadata)
) {
const formattedValue = this.metadataValueService.valueFormat(
metadata[key]["value"],
);

metadataObject = {
name: key,
value: metadata[key]["value"],
value: formattedValue,
unit: metadata[key]["unit"],
human_name: humanReadableName,
type: metadata[key]["type"],
Expand All @@ -215,9 +221,12 @@ export class MetadataViewComponent implements OnInit, OnChanges {
? metadata[key]
: JSON.stringify(metadata[key]);

const formattedValue =
this.metadataValueService.valueFormat(metadataValue);

metadataObject = {
name: key,
value: metadataValue,
value: formattedValue,
unit: "",
human_name: humanReadableName,
type: metadata[key]["type"],
Expand Down
105 changes: 105 additions & 0 deletions src/app/shared/services/metadata-value.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { TestBed } from "@angular/core/testing";
import { MetadataValueService } from "./metadata-value.service";
import { AppConfigInterface, AppConfigService } from "app-config.service";

describe("MetadataValueService", () => {
let service: MetadataValueService;
let mockConfigService: jasmine.SpyObj<AppConfigService>;

beforeEach(() => {
mockConfigService = jasmine.createSpyObj("AppConfigService", ["getConfig"]);

TestBed.configureTestingModule({
providers: [
MetadataValueService,
{ provide: AppConfigService, useValue: mockConfigService },
],
});
});

function setConfig(
options?: Partial<{
enabled: boolean;
significantDigits: number;
minCutoff: number;
maxCutoff: number;
}>,
) {
const mockConfig: Partial<AppConfigInterface> = {
metadataFloatFormatEnabled: options?.enabled ?? true,
metadataFloatFormat: {
significantDigits: options?.significantDigits ?? 3,
minCutoff: options?.minCutoff ?? 0.001,
maxCutoff: options?.maxCutoff ?? 1000,
},
};

mockConfigService.getConfig.and.returnValue(
mockConfig as AppConfigInterface,
);
}

it("should not format float if not enabled", () => {
setConfig({ enabled: false });
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat(1.23456789)).toBe("1.23456789");
});

it("should return string as-is for non-number values", () => {
setConfig();
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat("abc")).toBe("abc");
expect(service.valueFormat("")).toBe("");
});

it("should return string representation for non-finite numbers", () => {
setConfig();
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat(NaN)).toBe("NaN");
expect(service.valueFormat(Infinity)).toBe("Infinity");
expect(service.valueFormat(-Infinity)).toBe("-Infinity");
});

it("should return string representation for integers", () => {
setConfig();
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat(42)).toBe("42");
expect(service.valueFormat(-10)).toBe("-10");
});

it("should omit decimals when significant digits fit within the integer part", () => {
setConfig({ significantDigits: 2 });
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat(12.3456)).toBe("12");
});

it("should use exponential notation for very small numbers", () => {
setConfig({ significantDigits: 3, minCutoff: 0.001 });
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat(0.0000123)).toBe("1.23e-5");
});

it("should use exponential notation for very large numbers", () => {
setConfig({ significantDigits: 4, maxCutoff: 1e6 });
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat(50004313.487)).toBe("5.000e+7");
});

it("should handle values just above minCutoff correctly", () => {
setConfig({ minCutoff: 0.001, significantDigits: 4 });
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat(0.0023456)).toBe("0.002346");
});

it("should handle values just below maxCutoff correctly", () => {
setConfig({ maxCutoff: 1e6, significantDigits: 3 });
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat(999999.99)).toBe("1.00e+6");
});

it("should respect significantDigits when using exponential notation", () => {
setConfig({ significantDigits: 5, minCutoff: 1e-3 });
service = TestBed.inject(MetadataValueService);
expect(service.valueFormat(0.0000123)).toBe("1.2300e-5");
});
});
47 changes: 47 additions & 0 deletions src/app/shared/services/metadata-value.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Injectable } from "@angular/core";
import { AppConfigService } from "app-config.service";

@Injectable({
providedIn: "root",
})
export class MetadataValueService {
private enabled: boolean;
private significantDigits: number;
private minCutoff: number;
private maxCutoff: number;

constructor(private configService: AppConfigService) {
const config = this.configService.getConfig();
this.enabled = config.metadataFloatFormatEnabled ?? false;
if (this.enabled) {
const metadataFloatFormat = config.metadataFloatFormat;
this.significantDigits = metadataFloatFormat.significantDigits;
this.minCutoff = metadataFloatFormat.minCutoff;
this.maxCutoff = metadataFloatFormat.maxCutoff;
}
}

valueFormat(value: string | number): string {
if (!this.enabled) {
return String(value);
}
if (typeof value !== "number" || !Number.isFinite(value)) {
// value is not a finite number
return String(value);
}

// Do not format integers
if (Number.isInteger(value)) {
return String(value);
}

// use scientific notation if float value is large or small
const absoluteValue = Math.abs(value);
if (absoluteValue < this.minCutoff || absoluteValue > this.maxCutoff) {
// use scientific notation with (significantDigits - 1) decimals
return value.toExponential(this.significantDigits - 1);
}

return value.toPrecision(this.significantDigits);
}
}
Loading