1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.dhp;
|
2
|
import java.text.SimpleDateFormat;
|
3
|
import java.util.Date;
|
4
|
import java.util.Iterator;
|
5
|
import java.util.Map;
|
6
|
import java.util.Optional;
|
7
|
import java.util.stream.Collectors;
|
8
|
|
9
|
import eu.dnetlib.common.logging.DnetLogger;
|
10
|
import eu.dnetlib.dhp.collector.worker.model.ApiDescriptor;
|
11
|
import eu.dnetlib.dhp.model.mdstore.Provenance;
|
12
|
import eu.dnetlib.enabling.datasources.common.Datasource;
|
13
|
import eu.dnetlib.enabling.datasources.common.DsmNotFoundException;
|
14
|
import eu.dnetlib.msro.rmi.MSROException;
|
15
|
import org.apache.commons.lang.math.NumberUtils;
|
16
|
import org.apache.commons.lang3.StringUtils;
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
|
21
|
import com.google.gson.Gson;
|
22
|
import com.googlecode.sarasvati.Arc;
|
23
|
import com.googlecode.sarasvati.NodeToken;
|
24
|
|
25
|
import eu.dnetlib.enabling.datasources.common.ApiParam;
|
26
|
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
|
27
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
28
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
29
|
import org.springframework.beans.factory.annotation.Value;
|
30
|
|
31
|
import javax.annotation.Resource;
|
32
|
|
33
|
public class PrepareEnvCollectHadoopJobNode extends SimpleJobNode {
|
34
|
|
35
|
private static final Log log = LogFactory.getLog(PrepareEnvCollectHadoopJobNode.class);
|
36
|
|
37
|
public static final String METADATA_IDENTIFIER_PATH = "metadata_identifier_path";
|
38
|
public static final String DATE_FORMAT = "yyyy-MM-dd";
|
39
|
|
40
|
private long ONE_DAY = 1000 * 60 * 60 * 24;
|
41
|
|
42
|
@Resource(name = "msroWorkflowLogger")
|
43
|
private DnetLogger dnetLogger;
|
44
|
|
45
|
@Autowired
|
46
|
private LocalDatasourceManager<?, ?> dsManager;
|
47
|
|
48
|
/**
|
49
|
* MDStore identifier
|
50
|
*/
|
51
|
private String mdId;
|
52
|
|
53
|
/**
|
54
|
* REFRESH | INCREMENTAL
|
55
|
*/
|
56
|
private String collectionMode;
|
57
|
|
58
|
/**
|
59
|
* used in INCREMENTAL mode
|
60
|
*/
|
61
|
private String fromDateOverride;
|
62
|
|
63
|
/**
|
64
|
* XML | JSON, default = XML
|
65
|
*/
|
66
|
private String metadataEncoding = "XML";
|
67
|
|
68
|
@Value("${dnet.openaire.dhp.collection.app.path}")
|
69
|
private String oozieWfPath;
|
70
|
|
71
|
@Value("${dnet.openaire.dhp.dnetMessageManagerURL}")
|
72
|
private String dnetMessageManagerURL;
|
73
|
|
74
|
@Override
|
75
|
protected String execute(final NodeToken token) throws Exception {
|
76
|
|
77
|
final String dsId = token.getEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_ID);
|
78
|
log.info("dsId: " + dsId);
|
79
|
final String apiId = token.getEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE);
|
80
|
log.info("apiId: " + apiId);
|
81
|
|
82
|
final Optional<ApiDescriptor> opt = dsManager.getApis(dsId)
|
83
|
.stream()
|
84
|
.filter(a -> a.getId().equals(apiId))
|
85
|
.map(a -> {
|
86
|
final ApiDescriptor res = new ApiDescriptor();
|
87
|
res.setBaseUrl(a.getBaseurl());
|
88
|
res.setId(a.getId());
|
89
|
res.setProtocol(a.getProtocol());
|
90
|
res.getParams().put(METADATA_IDENTIFIER_PATH, a.getMetadataIdentifierPath());
|
91
|
res.getParams().putAll(a.getApiParams()
|
92
|
.stream()
|
93
|
.map(o -> (ApiParam) o)
|
94
|
.collect(Collectors.toMap(ApiParam::getParam, ApiParam::getValue)));
|
95
|
return res;
|
96
|
})
|
97
|
.findFirst();
|
98
|
|
99
|
if (opt.isPresent()) {
|
100
|
token.getEnv().setAttribute("mdId", getMdId());
|
101
|
token.getEnv().setAttribute("collectionMode", getCollectionMode());
|
102
|
token.getEnv().setAttribute("metadataEncoding", getMetadataEncoding());
|
103
|
token.getEnv().setAttribute("oozieWfPath", getOozieWfPath());
|
104
|
token.getEnv().setAttribute("dnetMessageManagerURL", getDnetMessageManagerURL());
|
105
|
|
106
|
final ApiDescriptor api = opt.get();
|
107
|
if ("INCREMENTAL".equals(getCollectionMode())) {
|
108
|
final String fromDate = calculateFromDate(token);
|
109
|
|
110
|
log.info("Incremental Harvesting from: " + fromDate);
|
111
|
|
112
|
if (StringUtils.isNotBlank(fromDate)) {
|
113
|
api.getParams().put("fromDate", fromDate);
|
114
|
}
|
115
|
}
|
116
|
|
117
|
token.getEnv().setAttribute("apiDescription", new Gson().toJson(api));
|
118
|
|
119
|
final Provenance provenance = new Provenance();
|
120
|
provenance.setDatasourceId(dsId);
|
121
|
final Datasource<?, ?> ds = dsManager.getDs(dsId);
|
122
|
provenance.setDatasourceName(ds.getOfficialname());
|
123
|
provenance.setNsPrefix(ds.getNamespaceprefix());
|
124
|
final String dsProvenance = new Gson().toJson(provenance);
|
125
|
log.info("datasource provenance: " + dsProvenance);
|
126
|
|
127
|
token.getEnv().setAttribute("dataSourceInfo", dsProvenance);
|
128
|
token.getEnv().setAttribute("timestamp", ""+System.currentTimeMillis());
|
129
|
token.getEnv().setAttribute("identifierPath",api.getParams().get(METADATA_IDENTIFIER_PATH));
|
130
|
token.getEnv().setAttribute("workflowId",token.getProcess().getEnv().getAttribute("system:processId"));
|
131
|
|
132
|
token.getEnv().setAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE_BASEURL, api.getBaseUrl());
|
133
|
token.getEnv().setAttribute(WorkflowsConstants.DATAPROVIDER_PREFIX + "protocol", api.getProtocol());
|
134
|
final Map<String, String> params = api.getParams();
|
135
|
if (params != null) {
|
136
|
for(Map.Entry<String, String> e : params.entrySet()) {
|
137
|
token.getEnv().setAttribute(WorkflowsConstants.DATAPROVIDER_PREFIX + e.getKey(), e.getValue());
|
138
|
}
|
139
|
}
|
140
|
|
141
|
return Arc.DEFAULT_ARC;
|
142
|
} else {
|
143
|
throw new DsmNotFoundException("cannot find ds interface: " + apiId);
|
144
|
}
|
145
|
}
|
146
|
|
147
|
private String findCurrentWfProfileId(NodeToken token) throws MSROException {
|
148
|
final String p1 = token.getEnv().getAttribute(WorkflowsConstants.SYSTEM_WF_PROFILE_ID);
|
149
|
if (p1 != null && !p1.isEmpty()) {
|
150
|
return p1;
|
151
|
}
|
152
|
final String p2 = token.getFullEnv().getAttribute(WorkflowsConstants.SYSTEM_WF_PROFILE_ID);
|
153
|
if (p2 != null && !p2.isEmpty()) {
|
154
|
return p2;
|
155
|
}
|
156
|
final String p3 = token.getProcess().getEnv().getAttribute(WorkflowsConstants.SYSTEM_WF_PROFILE_ID);
|
157
|
if (p3 != null && !p3.isEmpty()) {
|
158
|
return p3;
|
159
|
}
|
160
|
throw new MSROException("Missing property in env: " + WorkflowsConstants.SYSTEM_WF_PROFILE_ID);
|
161
|
}
|
162
|
|
163
|
private String calculateFromDate(final NodeToken token) throws MSROException {
|
164
|
|
165
|
if (StringUtils.isNotBlank(getFromDateOverride())) {
|
166
|
log.info("using override FROM_DATE for incremental harvesting: " + getFromDateOverride());
|
167
|
return getFromDateOverride();
|
168
|
}
|
169
|
final String profId = findCurrentWfProfileId(token);
|
170
|
|
171
|
final long d = findLastSuccessStartDate(profId);
|
172
|
return (d > 0) ? (new SimpleDateFormat(DATE_FORMAT)).format(new Date(d - ONE_DAY)) : null;
|
173
|
}
|
174
|
|
175
|
private long findLastSuccessStartDate(String profId) {
|
176
|
long res = -1;
|
177
|
|
178
|
final Iterator<Map<String, String>> iter = dnetLogger.find(WorkflowsConstants.SYSTEM_WF_PROFILE_ID, profId);
|
179
|
while (iter.hasNext()) {
|
180
|
final Map<String, String> map = iter.next();
|
181
|
if ("true".equalsIgnoreCase(map.get(WorkflowsConstants.SYSTEM_COMPLETED_SUCCESSFULLY))) {
|
182
|
final long curr = NumberUtils.toLong(map.get(WorkflowsConstants.SYSTEM_START_DATE), -1);
|
183
|
if (curr > res) {
|
184
|
res = curr;
|
185
|
}
|
186
|
}
|
187
|
}
|
188
|
return res;
|
189
|
}
|
190
|
|
191
|
|
192
|
public String getMdId() {
|
193
|
return mdId;
|
194
|
}
|
195
|
|
196
|
public void setMdId(String mdId) {
|
197
|
this.mdId = mdId;
|
198
|
}
|
199
|
|
200
|
public String getCollectionMode() {
|
201
|
return collectionMode;
|
202
|
}
|
203
|
|
204
|
public void setCollectionMode(String collectionMode) {
|
205
|
this.collectionMode = collectionMode;
|
206
|
}
|
207
|
|
208
|
public String getOozieWfPath() {
|
209
|
return oozieWfPath;
|
210
|
}
|
211
|
|
212
|
public void setOozieWfPath(String oozieWfPath) {
|
213
|
this.oozieWfPath = oozieWfPath;
|
214
|
}
|
215
|
|
216
|
public String getMetadataEncoding() {
|
217
|
return metadataEncoding;
|
218
|
}
|
219
|
|
220
|
public void setMetadataEncoding(String metadataEncoding) {
|
221
|
this.metadataEncoding = metadataEncoding;
|
222
|
}
|
223
|
|
224
|
public String getFromDateOverride() {
|
225
|
return fromDateOverride;
|
226
|
}
|
227
|
|
228
|
public void setFromDateOverride(String fromDateOverride) {
|
229
|
this.fromDateOverride = fromDateOverride;
|
230
|
}
|
231
|
|
232
|
public String getDnetMessageManagerURL() {
|
233
|
return dnetMessageManagerURL;
|
234
|
}
|
235
|
|
236
|
public void setDnetMessageManagerURL(String dnetMessageManagerURL) {
|
237
|
this.dnetMessageManagerURL = dnetMessageManagerURL;
|
238
|
}
|
239
|
}
|