experimentStartupInfo.ts 3.37 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

'use strict';

import * as assert from 'assert';
7
8
import * as os from 'os';
import * as path from 'path';
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
35
36

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
37
38
        this.newExperiment = newExperiment;
        this.experimentId = experimentId;
39
        this.basePort = basePort;
40
        this.platform = platform;
41
42

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

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

        if (readonly !== undefined) {
            this.readonly = readonly;
        }
55
56
57
58

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

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

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

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

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

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
    );
100
101
}

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

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

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

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

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

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

126
127
export function getAPIRootUrl(): string {
    return getExperimentStartupInfo().apiRootUrl;
128
}