|
1 | 1 | package ml.comet.experiment; |
2 | 2 |
|
3 | 3 | import lombok.NonNull; |
| 4 | +import lombok.experimental.UtilityClass; |
4 | 5 | import ml.comet.experiment.builder.ApiExperimentBuilder; |
5 | | -import ml.comet.experiment.config.CometConfig; |
6 | | -import ml.comet.experiment.http.Connection; |
7 | | -import ml.comet.experiment.http.ConnectionInitializer; |
8 | | -import ml.comet.experiment.utils.CometUtils; |
9 | | -import org.apache.commons.lang3.StringUtils; |
10 | | -import org.slf4j.Logger; |
11 | | -import org.slf4j.LoggerFactory; |
12 | | - |
13 | | -import java.io.File; |
14 | | -import java.time.Duration; |
15 | | -import java.util.Optional; |
16 | | - |
17 | | -import static ml.comet.experiment.config.CometConfig.COMET_API_KEY; |
18 | | -import static ml.comet.experiment.config.CometConfig.COMET_BASE_URL; |
19 | | -import static ml.comet.experiment.config.CometConfig.COMET_MAX_AUTH_RETRIES; |
20 | | -import static ml.comet.experiment.config.CometConfig.COMET_TIMEOUT_CLEANING_SECONDS; |
| 6 | +import ml.comet.experiment.impl.ApiExperimentImpl; |
21 | 7 |
|
22 | 8 | /** |
23 | | - * <p>Implementation of the {@link Experiment} that allows to read/update existing experiment.</p> |
| 9 | + * This is stub to support backward compatibility. |
24 | 10 | * |
25 | | - * <p>The ApiExperiment should be used to directly read/update data of the |
26 | | - * existing experiment from the Comet server.</p> |
| 11 | + * @deprecated It would be replaced in the future with new experiment creation API. |
27 | 12 | */ |
28 | | -public final class ApiExperiment extends BaseExperiment { |
29 | | - private final String baseUrl; |
30 | | - private final String experimentKey; |
31 | | - private final Duration cleaningTimeout; |
32 | | - private final Connection connection; |
33 | | - private Logger logger = LoggerFactory.getLogger(ApiExperiment.class); |
34 | | - |
35 | | - private ApiExperiment( |
36 | | - final String apiKey, |
37 | | - final String anExperimentKey, |
38 | | - final Logger logger, |
39 | | - final String baseUrl, |
40 | | - final int maxAuthRetries, |
41 | | - Duration cleaningTimeout) { |
42 | | - this.experimentKey = anExperimentKey; |
43 | | - this.baseUrl = baseUrl; |
44 | | - this.cleaningTimeout = cleaningTimeout; |
45 | | - if (logger != null) { |
46 | | - this.logger = logger; |
47 | | - } |
48 | | - this.connection = ConnectionInitializer.initConnection( |
49 | | - apiKey, this.baseUrl, maxAuthRetries, this.logger); |
50 | | - |
51 | | - CometUtils.printCometSdkVersion(); |
52 | | - } |
53 | | - |
| 13 | +@UtilityClass |
| 14 | +public final class ApiExperiment { |
54 | 15 | /** |
55 | | - * Returns builder to create ApiExperiment instance. |
| 16 | + * Returns builder to create {@link Experiment} instance. |
56 | 17 | * |
57 | 18 | * @param experimentKey the unique identifier of the existing experiment. |
58 | 19 | * @return the initialized ApiExperiment instance. |
59 | 20 | */ |
60 | | - public static ApiExperiment.ApiExperimentBuilderImpl builder(@NonNull final String experimentKey) { |
61 | | - return new ApiExperiment.ApiExperimentBuilderImpl(experimentKey); |
62 | | - } |
63 | | - |
64 | | - @Override |
65 | | - protected Connection getConnection() { |
66 | | - return this.connection; |
67 | | - } |
68 | | - |
69 | | - @Override |
70 | | - protected Logger getLogger() { |
71 | | - return this.logger; |
72 | | - } |
73 | | - |
74 | | - @Override |
75 | | - public String getContext() { |
76 | | - return StringUtils.EMPTY; |
77 | | - } |
78 | | - |
79 | | - @Override |
80 | | - public String getWorkspaceName() { |
81 | | - return getMetadata().getWorkspaceName(); |
82 | | - } |
83 | | - |
84 | | - @Override |
85 | | - public String getProjectName() { |
86 | | - return getMetadata().getProjectName(); |
87 | | - } |
88 | | - |
89 | | - @Override |
90 | | - public String getExperimentName() { |
91 | | - return getMetadata().getExperimentName(); |
92 | | - } |
93 | | - |
94 | | - @Override |
95 | | - public void end() { |
96 | | - // invoke end of the superclass for common cleanup routines with given timeout |
97 | | - super.end(this.cleaningTimeout); |
98 | | - } |
99 | | - |
100 | | - @Override |
101 | | - public String getExperimentKey() { |
102 | | - return this.experimentKey; |
103 | | - } |
104 | | - |
105 | | - @Override |
106 | | - public Optional<String> getExperimentLink() { |
107 | | - if (StringUtils.isEmpty(experimentKey)) { |
108 | | - return Optional.empty(); |
109 | | - } |
110 | | - try { |
111 | | - return Optional.of(CometUtils.createExperimentLink( |
112 | | - baseUrl, getWorkspaceName(), getProjectName(), experimentKey)); |
113 | | - } catch (Exception ex) { |
114 | | - this.logger.error("failed to build experiment link", ex); |
115 | | - return Optional.empty(); |
116 | | - } |
117 | | - } |
118 | | - |
119 | | - /** |
120 | | - * The builder to create properly configured ApiExperiment instance. |
121 | | - */ |
122 | | - public static final class ApiExperimentBuilderImpl implements ApiExperimentBuilder { |
123 | | - private final String experimentKey; |
124 | | - private String apiKey; |
125 | | - private Logger logger; |
126 | | - |
127 | | - private ApiExperimentBuilderImpl(final String anExperimentKey) { |
128 | | - this.experimentKey = anExperimentKey; |
129 | | - } |
130 | | - |
131 | | - @Override |
132 | | - public ApiExperiment.ApiExperimentBuilderImpl withApiKey(@NonNull final String anApiKey) { |
133 | | - this.apiKey = anApiKey; |
134 | | - return this; |
135 | | - } |
136 | | - |
137 | | - @Override |
138 | | - public ApiExperiment.ApiExperimentBuilderImpl withLogger(@NonNull final Logger logger) { |
139 | | - this.logger = logger; |
140 | | - return this; |
141 | | - } |
142 | | - |
143 | | - @Override |
144 | | - public ApiExperiment.ApiExperimentBuilderImpl withConfigOverride(@NonNull final File overrideConfig) { |
145 | | - CometConfig.applyConfigOverride(overrideConfig); |
146 | | - return this; |
147 | | - } |
148 | | - |
149 | | - @Override |
150 | | - public ApiExperiment build() { |
151 | | - if (StringUtils.isEmpty(this.apiKey)) { |
152 | | - this.apiKey = COMET_API_KEY.getString(); |
153 | | - } |
154 | | - return new ApiExperiment( |
155 | | - this.apiKey, this.experimentKey, this.logger, |
156 | | - COMET_BASE_URL.getString(), |
157 | | - COMET_MAX_AUTH_RETRIES.getInt(), |
158 | | - COMET_TIMEOUT_CLEANING_SECONDS.getDuration()); |
159 | | - } |
| 21 | + public static ApiExperimentBuilder builder(@NonNull final String experimentKey) { |
| 22 | + return ApiExperimentImpl.builder(experimentKey); |
160 | 23 | } |
161 | 24 | } |
0 commit comments