experimentStartupInfo.ts 4.54 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';
Deshui Yu's avatar
Deshui Yu committed
9
10
11
12
import * as component from '../common/component';

@component.Singleton
class ExperimentStartupInfo {
13
14
    private readonly API_ROOT_URL: string = '/api/v1/nni';

Deshui Yu's avatar
Deshui Yu committed
15
16
    private experimentId: string = '';
    private newExperiment: boolean = true;
17
    private basePort: number = -1;
Deshui Yu's avatar
Deshui Yu committed
18
    private initialized: boolean = false;
19
20
    private logDir: string = '';
    private logLevel: string = '';
SparkSnail's avatar
SparkSnail committed
21
    private readonly: boolean = false;
22
    private dispatcherPipe: string | null = null;
23
    private platform: string = '';
24
    private urlprefix: string = '';
Deshui Yu's avatar
Deshui Yu committed
25

26
    public setStartupInfo(newExperiment: boolean, experimentId: string, basePort: number, platform: string, logDir?: string, logLevel?: string, readonly?: boolean, dispatcherPipe?: string, urlprefix?: string): void {
Deshui Yu's avatar
Deshui Yu committed
27
28
29
30
        assert(!this.initialized);
        assert(experimentId.trim().length > 0);
        this.newExperiment = newExperiment;
        this.experimentId = experimentId;
31
        this.basePort = basePort;
Deshui Yu's avatar
Deshui Yu committed
32
        this.initialized = true;
33
        this.platform = platform;
34
35

        if (logDir !== undefined && logDir.length > 0) {
chicm-ms's avatar
chicm-ms committed
36
            this.logDir = path.join(path.normalize(logDir), this.getExperimentId());
37
        } else {
chicm-ms's avatar
chicm-ms committed
38
            this.logDir = path.join(os.homedir(), 'nni-experiments', this.getExperimentId());
39
40
41
42
43
        }

        if (logLevel !== undefined && logLevel.length > 1) {
            this.logLevel = logLevel;
        }
SparkSnail's avatar
SparkSnail committed
44
45
46
47

        if (readonly !== undefined) {
            this.readonly = readonly;
        }
48
49
50
51

        if (dispatcherPipe != undefined && dispatcherPipe.length > 0) {
            this.dispatcherPipe = dispatcherPipe;
        }
52
53
54
55

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

    public getExperimentId(): string {
        assert(this.initialized);

        return this.experimentId;
    }

64
65
66
67
68
69
    public getBasePort(): number {
        assert(this.initialized);

        return this.basePort;
    }

Deshui Yu's avatar
Deshui Yu committed
70
71
72
73
74
    public isNewExperiment(): boolean {
        assert(this.initialized);

        return this.newExperiment;
    }
75

76
77
78
79
80
81
    public getPlatform(): string {
        assert(this.initialized);

        return this.platform;
    }

82
83
84
85
86
87
88
89
90
91
92
93
    public getLogDir(): string {
        assert(this.initialized);

        return this.logDir;
    }

    public getLogLevel(): string {
        assert(this.initialized);

        return this.logLevel;
    }

SparkSnail's avatar
SparkSnail committed
94
95
96
97
98
    public isReadonly(): boolean {
        assert(this.initialized);

        return this.readonly;
    }
99
100
101
102
103

    public getDispatcherPipe(): string | null {
        assert(this.initialized);
        return this.dispatcherPipe;
    }
104
105
106

    public getAPIRootUrl(): string {
        assert(this.initialized);
Ni Hao's avatar
Ni Hao committed
107
        return this.urlprefix==''?this.API_ROOT_URL:`/${this.urlprefix}${this.API_ROOT_URL}`;
108
    }
Deshui Yu's avatar
Deshui Yu committed
109
110
111
112
113
114
}

function getExperimentId(): string {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).getExperimentId();
}

115
116
117
118
function getBasePort(): number {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).getBasePort();
}

Deshui Yu's avatar
Deshui Yu committed
119
120
121
122
function isNewExperiment(): boolean {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).isNewExperiment();
}

123
124
125
126
function getPlatform(): string {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).getPlatform();
}

127
128
129
130
131
function getExperimentStartupInfo(): ExperimentStartupInfo {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo);
}

function setExperimentStartupInfo(
132
    newExperiment: boolean, experimentId: string, basePort: number, platform: string, logDir?: string, logLevel?: string, readonly?: boolean, dispatcherPipe?: string, urlprefix?: string): void {
133
    component.get<ExperimentStartupInfo>(ExperimentStartupInfo)
134
        .setStartupInfo(newExperiment, experimentId, basePort, platform, logDir, logLevel, readonly, dispatcherPipe, urlprefix);
SparkSnail's avatar
SparkSnail committed
135
136
137
138
}

function isReadonly(): boolean {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).isReadonly();
Deshui Yu's avatar
Deshui Yu committed
139
140
}

141
142
143
144
function getDispatcherPipe(): string | null {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).getDispatcherPipe();
}

145
146
147
148
function getAPIRootUrl(): string {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).getAPIRootUrl();
}

149
150
export {
    ExperimentStartupInfo, getBasePort, getExperimentId, isNewExperiment, getPlatform, getExperimentStartupInfo,
151
    setExperimentStartupInfo, isReadonly, getDispatcherPipe, getAPIRootUrl
152
};