experimentStartupInfo.ts 3.1 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;
Deshui Yu's avatar
Deshui Yu committed
20

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

        if (logDir !== undefined && logDir.length > 0) {
SparkSnail's avatar
SparkSnail committed
30
            this.logDir = path.join(path.normalize(logDir), getExperimentId());
31
32
33
34
35
36
37
        } else {
            this.logDir = path.join(os.homedir(), 'nni', 'experiments', getExperimentId());
        }

        if (logLevel !== undefined && logLevel.length > 1) {
            this.logLevel = logLevel;
        }
SparkSnail's avatar
SparkSnail committed
38
39
40
41

        if (readonly !== undefined) {
            this.readonly = readonly;
        }
Deshui Yu's avatar
Deshui Yu committed
42
43
44
45
46
47
48
49
    }

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

        return this.experimentId;
    }

50
51
52
53
54
55
    public getBasePort(): number {
        assert(this.initialized);

        return this.basePort;
    }

Deshui Yu's avatar
Deshui Yu committed
56
57
58
59
60
    public isNewExperiment(): boolean {
        assert(this.initialized);

        return this.newExperiment;
    }
61

62
63
64
65
66
67
68
69
70
71
72
73
    public getLogDir(): string {
        assert(this.initialized);

        return this.logDir;
    }

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

        return this.logLevel;
    }

SparkSnail's avatar
SparkSnail committed
74
75
76
77
78
    public isReadonly(): boolean {
        assert(this.initialized);

        return this.readonly;
    }
Deshui Yu's avatar
Deshui Yu committed
79
80
81
82
83
84
}

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

85
86
87
88
function getBasePort(): number {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).getBasePort();
}

Deshui Yu's avatar
Deshui Yu committed
89
90
91
92
function isNewExperiment(): boolean {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo).isNewExperiment();
}

93
94
95
96
97
function getExperimentStartupInfo(): ExperimentStartupInfo {
    return component.get<ExperimentStartupInfo>(ExperimentStartupInfo);
}

function setExperimentStartupInfo(
SparkSnail's avatar
SparkSnail committed
98
    newExperiment: boolean, experimentId: string, basePort: number, logDir?: string, logLevel?: string, readonly?: boolean): void {
99
    component.get<ExperimentStartupInfo>(ExperimentStartupInfo)
SparkSnail's avatar
SparkSnail committed
100
101
102
103
104
    .setStartupInfo(newExperiment, experimentId, basePort, logDir, logLevel, readonly);
}

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

107
export { ExperimentStartupInfo, getBasePort, getExperimentId, isNewExperiment, getExperimentStartupInfo,
108
    setExperimentStartupInfo, isReadonly };