Use GeneratedPluginData
Getting ready to use the createdAt to determine if reindexing should happen.
This commit is contained in:
parent
f2364a34a3
commit
bcf0eb6a09
|
@ -255,7 +255,7 @@ public class GeneratePluginData {
|
|||
private void writePluginsToFile(List<Plugin> plugins) {
|
||||
final File data = Paths.get(System.getProperty("user.dir"), "target", "plugins.json.gzip").toFile();
|
||||
try(final Writer writer = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(data)), "utf-8"))) {
|
||||
JsonObjectMapper.getObjectMapper().writeValue(writer, plugins);
|
||||
JsonObjectMapper.getObjectMapper().writeValue(writer, new GeneratedPluginData(plugins));
|
||||
} catch (Exception e) {
|
||||
logger.error("Problem writing plugin data to file", e);
|
||||
throw new RuntimeException(e);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package io.jenkins.plugins.datastore;
|
||||
|
||||
import io.jenkins.plugins.commons.JsonObjectMapper;
|
||||
import io.jenkins.plugins.models.GeneratedPluginData;
|
||||
import io.jenkins.plugins.models.Plugin;
|
||||
import io.jenkins.plugins.services.ConfigurationService;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
@ -73,17 +74,15 @@ public class EmbeddedElasticsearchServer {
|
|||
|
||||
private void createAndPopulateIndex() {
|
||||
try {
|
||||
final String data = configurationService.getIndexData();
|
||||
reIndex(data);
|
||||
final GeneratedPluginData data = configurationService.getIndexData();
|
||||
populateIndex(data);
|
||||
} catch (Exception e) {
|
||||
logger.error("Problem creating and populating index", e);
|
||||
throw new RuntimeException("Problem creating and populating index", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void reIndex(String data) {
|
||||
private void populateIndex(GeneratedPluginData data) {
|
||||
final ClassLoader cl = getClass().getClassLoader();
|
||||
final String index = String.format("plugins_%s", DateTimeFormatter.ofPattern("yyyy.mm.dd_HH.mm.ss").format(LocalDateTime.now()));
|
||||
try {
|
||||
|
@ -94,16 +93,16 @@ public class EmbeddedElasticsearchServer {
|
|||
.addMapping("plugins", mappingContent)
|
||||
.get();
|
||||
logger.info(String.format("Index '%s' created", index));
|
||||
final JSONArray json = new JSONArray(data);
|
||||
final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
|
||||
for (int i = 0; i < json.length(); i++) {
|
||||
// Seems redundant but it's actually a good test to ensure the generation process is working. If we can read
|
||||
// a plugin from the JSON then it's good.
|
||||
final Plugin plugin = JsonObjectMapper.getObjectMapper().readValue(json.getJSONObject(i).toString(), Plugin.class);
|
||||
final IndexRequest indexRequest = client.prepareIndex(index, "plugins", plugin.getName())
|
||||
.setSource(JsonObjectMapper.getObjectMapper().writeValueAsString(plugin)).request();
|
||||
bulkRequestBuilder.add(indexRequest);
|
||||
}
|
||||
data.getPlugins().forEach((plugin) -> {
|
||||
try {
|
||||
final IndexRequest indexRequest = client.prepareIndex(index, "plugins", plugin.getName())
|
||||
.setSource(JsonObjectMapper.getObjectMapper().writeValueAsString(plugin)).request();
|
||||
bulkRequestBuilder.add(indexRequest);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
final BulkResponse response = bulkRequestBuilder.get();
|
||||
if (response.hasFailures()) {
|
||||
for (BulkItemResponse item : response.getItems()) {
|
||||
|
@ -111,7 +110,7 @@ public class EmbeddedElasticsearchServer {
|
|||
}
|
||||
throw new ElasticsearchException("Problem bulk indexing");
|
||||
}
|
||||
logger.info(String.format("Indexed %d plugins", json.length()));
|
||||
logger.info(String.format("Indexed %d plugins", data.getPlugins().size()));
|
||||
client.admin().indices().prepareAliases().addAlias(index, "plugins").get();
|
||||
client.admin().indices().prepareRefresh("plugins").execute().get();
|
||||
logger.info(String.format("Alias plugins points to index %s", index));
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package io.jenkins.plugins.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class GeneratedPluginData {
|
||||
|
||||
@JsonProperty("plugins")
|
||||
private List<Plugin> plugins;
|
||||
|
||||
@JsonProperty("createdAt")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SS'Z'")
|
||||
@JsonSerialize(using = LocalDateTimeSerializer.class)
|
||||
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
public GeneratedPluginData() {
|
||||
}
|
||||
|
||||
public GeneratedPluginData(List<Plugin> plugins) {
|
||||
this.plugins = plugins;
|
||||
this.createdAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public List<Plugin> getPlugins() {
|
||||
return plugins;
|
||||
}
|
||||
|
||||
public void setPlugins(List<Plugin> plugins) {
|
||||
this.plugins = plugins;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(LocalDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package io.jenkins.plugins.services;
|
||||
|
||||
import io.jenkins.plugins.models.GeneratedPluginData;
|
||||
|
||||
/**
|
||||
* <p>Get various configuration pieces for the application</p>
|
||||
*/
|
||||
|
@ -8,9 +10,9 @@ public interface ConfigurationService {
|
|||
/**
|
||||
* <p>Get index data need to populating Elasticsearch</p>
|
||||
*
|
||||
* @return JSON content
|
||||
* @return GeneratedPluginData
|
||||
* @throws ServiceException in case something goes wrong
|
||||
*/
|
||||
String getIndexData() throws ServiceException;
|
||||
GeneratedPluginData getIndexData() throws ServiceException;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package io.jenkins.plugins.services.impl;
|
||||
|
||||
import io.jenkins.plugins.commons.JsonObjectMapper;
|
||||
import io.jenkins.plugins.models.GeneratedPluginData;
|
||||
import io.jenkins.plugins.services.ConfigurationService;
|
||||
import io.jenkins.plugins.services.ServiceException;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
@ -25,7 +27,7 @@ public class DefaultConfigurationService implements ConfigurationService {
|
|||
private final Logger logger = LoggerFactory.getLogger(DefaultConfigurationService.class);
|
||||
|
||||
@Override
|
||||
public String getIndexData() throws ServiceException {
|
||||
public GeneratedPluginData getIndexData() throws ServiceException {
|
||||
final CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
try {
|
||||
final String url = getDataFileUrl();
|
||||
|
@ -36,7 +38,8 @@ public class DefaultConfigurationService implements ConfigurationService {
|
|||
final InputStream inputStream = entity.getContent();
|
||||
final File dataFile = File.createTempFile("plugins", ".json.gzip");
|
||||
FileUtils.copyToFile(inputStream, dataFile);
|
||||
return readGzipFile(dataFile);
|
||||
final String data = readGzipFile(dataFile);
|
||||
return JsonObjectMapper.getObjectMapper().readValue(data, GeneratedPluginData.class);
|
||||
} else {
|
||||
logger.error("Data file not found");
|
||||
throw new RuntimeException("Data file not found");
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package io.jenkins.plugins.services;
|
||||
|
||||
import io.jenkins.plugins.commons.JsonObjectMapper;
|
||||
import io.jenkins.plugins.models.GeneratedPluginData;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -11,12 +13,13 @@ public class MockConfigurationService implements ConfigurationService {
|
|||
private final Logger logger = LoggerFactory.getLogger(MockConfigurationService.class);
|
||||
|
||||
@Override
|
||||
public String getIndexData() throws ServiceException {
|
||||
public GeneratedPluginData getIndexData() throws ServiceException {
|
||||
try {
|
||||
logger.info("Using test plugin data");
|
||||
final ClassLoader cl = getClass().getClassLoader();
|
||||
final File mappingFile = new File(cl.getResource("plugins.json").getFile());
|
||||
return FileUtils.readFileToString(mappingFile, "utf-8");
|
||||
final File dataFile = new File(cl.getResource("plugins.json").getFile());
|
||||
final String data = FileUtils.readFileToString(dataFile, "utf-8");
|
||||
return JsonObjectMapper.getObjectMapper().readValue(data, GeneratedPluginData.class);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Can't get test plugin data");
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue