experimentStartupInfo.ts 3.98 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
13
14
import * as component from '../common/component';

@component.Singleton
class ExperimentStartupInfo {
    private experimentId: string = '';
    private newExperiment: boolean = true;
15
    private basePort: number = -1;
Deshui Yu's avatar
Deshui Yu committed
16
    private initialized: boolean = false;
17
18
    private logDir: string = '';
    private logLevel: string = '';
SparkSnail's avatar
SparkSnail committed
19
    private readonly: boolean = false;
20
    private dispatcherPipe: string | null = null;
21
    private platform: string = '';
Deshui Yu's avatar
Deshui Yu committed
22

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

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

        if (logLevel !== undefined && logLevel.length > 1) {
            this.logLevel = logLevel;
        }
SparkSnail's avatar
SparkSnail committed
41
42
43
44

        if (readonly !== undefined) {
            this.readonly = readonly;
        }
45
46
47
48

        if (dispatcherPipe != undefined && dispatcherPipe.length > 0) {
            this.dispatcherPipe = dispatcherPipe;
        }
Deshui Yu's avatar
Deshui Yu committed
49
50
51
52
53
54
55
56
    }

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

        return this.experimentId;
    }

57
58
59
60
61
62
    public getBasePort(): number {
        assert(this.initialized);

        return this.basePort;
    }

Deshui Yu's avatar
Deshui Yu committed
63
64
65
66
67
    public isNewExperiment(): boolean {
        assert(this.initialized);

        return this.newExperiment;
    }
68

69
70
71
72
73
74
    public getPlatform(): string {
        assert(this.initialized);

        return this.platform;
    }

75
76
77
78
79
80
81
82
83
84
85
86
    public getLogDir(): string {
        assert(this.initialized);

        return this.logDir;
    }

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

        return this.logLevel;
    }

SparkSnail's avatar
SparkSnail committed
87
88
89
90
91
    public isReadonly(): boolean {
        assert(this.initialized);

        return this.readonly;
    }
92
93
94
95
96

    public getDispatcherPipe(): string | null {
        assert(this.initialized);
        return this.dispatcherPipe;
    }
Deshui Yu's avatar
Deshui Yu committed
97
98
99
100
101
102
}

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

103
104
105
106
function getBasePort(): number {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).getBasePort();
}

Deshui Yu's avatar
Deshui Yu committed
107
108
109
110
function isNewExperiment(): boolean {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).isNewExperiment();
}

111
112
113
114
function getPlatform(): string {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).getPlatform();
}

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

function setExperimentStartupInfo(
120
    newExperiment: boolean, experimentId: string, basePort: number, platform: string, logDir?: string, logLevel?: string, readonly?: boolean, dispatcherPipe?: string): void {
121
    component.get<ExperimentStartupInfo>(ExperimentStartupInfo)
122
        .setStartupInfo(newExperiment, experimentId, basePort, platform, logDir, logLevel, readonly, dispatcherPipe);
SparkSnail's avatar
SparkSnail committed
123
124
125
126
}

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

129
130
131
132
function getDispatcherPipe(): string | null {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).getDispatcherPipe();
}

133
134
export {
    ExperimentStartupInfo, getBasePort, getExperimentId, isNewExperiment, getPlatform, getExperimentStartupInfo,
135
    setExperimentStartupInfo, isReadonly, getDispatcherPipe
136
};