experimentStartupInfo.ts 3.51 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3

4
5
6
import assert from 'assert';
import os from 'os';
import path from 'path';
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

const API_ROOT_URL: string = '/api/v1/nni';

let singleton: ExperimentStartupInfo | null = null;

export class ExperimentStartupInfo {

    public experimentId: string = '';
    public newExperiment: boolean = true;
    public basePort: number = -1;
    public initialized: boolean = false;
    public logDir: string = '';
    public logLevel: string = '';
    public readonly: boolean = false;
    public dispatcherPipe: string | null = null;
    public platform: string = '';
    public urlprefix: string = '';

    constructor(
            newExperiment: boolean,
            experimentId: string,
            basePort: number,
            platform: string,
            logDir?: string,
            logLevel?: string,
            readonly?: boolean,
            dispatcherPipe?: string,
            urlprefix?: string) {
Deshui Yu's avatar
Deshui Yu committed
35
36
        this.newExperiment = newExperiment;
        this.experimentId = experimentId;
37
        this.basePort = basePort;
38
        this.platform = platform;
39
40

        if (logDir !== undefined && logDir.length > 0) {
41
            this.logDir = path.join(path.normalize(logDir), experimentId);
42
        } else {
43
            this.logDir = path.join(os.homedir(), 'nni-experiments', experimentId);
44
45
46
47
48
        }

        if (logLevel !== undefined && logLevel.length > 1) {
            this.logLevel = logLevel;
        }
SparkSnail's avatar
SparkSnail committed
49
50
51
52

        if (readonly !== undefined) {
            this.readonly = readonly;
        }
53
54
55
56

        if (dispatcherPipe != undefined && dispatcherPipe.length > 0) {
            this.dispatcherPipe = dispatcherPipe;
        }
57
58
59
60

        if(urlprefix != undefined && urlprefix.length > 0){
            this.urlprefix = urlprefix;
        }
Deshui Yu's avatar
Deshui Yu committed
61
62
    }

63
64
    public get apiRootUrl(): string {
        return this.urlprefix === '' ? API_ROOT_URL : `/${this.urlprefix}${API_ROOT_URL}`;
65
66
    }

67
68
69
    public static getInstance(): ExperimentStartupInfo {
        assert(singleton !== null);
        return singleton!;
70
    }
Deshui Yu's avatar
Deshui Yu committed
71
72
}

73
74
export function getExperimentStartupInfo(): ExperimentStartupInfo {
    return ExperimentStartupInfo.getInstance();
Deshui Yu's avatar
Deshui Yu committed
75
76
}

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
export function setExperimentStartupInfo(
        newExperiment: boolean,
        experimentId: string,
        basePort: number,
        platform: string,
        logDir?: string,
        logLevel?: string,
        readonly?: boolean,
        dispatcherPipe?: string,
        urlprefix?: string): void {
    singleton = new ExperimentStartupInfo(
        newExperiment,
        experimentId,
        basePort,
        platform,
        logDir,
        logLevel,
        readonly,
        dispatcherPipe,
        urlprefix
    );
98
99
}

100
101
export function getExperimentId(): string {
    return getExperimentStartupInfo().experimentId;
Deshui Yu's avatar
Deshui Yu committed
102
103
}

104
105
export function getBasePort(): number {
    return getExperimentStartupInfo().basePort;
106
107
}

108
109
export function isNewExperiment(): boolean {
    return getExperimentStartupInfo().newExperiment;
110
111
}

112
113
export function getPlatform(): string {
    return getExperimentStartupInfo().platform;
SparkSnail's avatar
SparkSnail committed
114
115
}

116
117
export function isReadonly(): boolean {
    return getExperimentStartupInfo().readonly;
Deshui Yu's avatar
Deshui Yu committed
118
119
}

120
121
export function getDispatcherPipe(): string | null {
    return getExperimentStartupInfo().dispatcherPipe;
122
123
}

124
125
export function getAPIRootUrl(): string {
    return getExperimentStartupInfo().apiRootUrl;
126
}
127
128
129
130
131

export function getPrefixUrl(): string {
    const prefix = getExperimentStartupInfo().urlprefix === '' ? '' : `/${getExperimentStartupInfo().urlprefix}`;
    return prefix;
}