Commit 2e6257f2 authored by redmitry@list.ru's avatar redmitry@list.ru
Browse files

moved from github

parents
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2020 ELIXIR ES, Spanish National Bioinformatics Institute (INB)
and Barcelona Supercomputing Center (BSC)
Modifications to the initial code base are copyright of their respective
authors, or their employers as appropriate.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>es.bsc.inb.elixir.elixibilitas</groupId>
<artifactId>elixibilitas-rest</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<description>
Tools Monitoring REST API
</description>
<organization>
<name>Barcelona Supercomputing Center</name>
<url>https://www.bsc.es/</url>
</organization>
<developers>
<developer>
<id>redmitry</id>
<name>Dmitry Repchevsky</name>
<email>redmitry@list.ru</email>
</developer>
</developers>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise.concurrent</groupId>
<artifactId>javax.enterprise.concurrent-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>es.elixir.bsc.json.schema</groupId>
<artifactId>jaronuinga</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>es.bsc.inb.elixir.elixibilitas</groupId>
<artifactId>elixibilitas-dao</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>es.bsc.inb.elixir.elixibilitas</groupId>
<artifactId>elixibilitas-meta</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.0.10</version>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-rdfxml</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-sail-memory</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-sail-lucene</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>inb-bsc-maven</id>
<url>https://gitlab.bsc.es/inb/maven/raw/master</url>
</repository>
<repository>
<id>jaronuinga</id>
<url>https://raw.github.com/inab/jaronuinga/maven/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<webResources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</webResources>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
/**
* *****************************************************************************
* Copyright (C) 2020 ELIXIR ES, Spanish National Bioinformatics Institute (INB)
* and Barcelona Supercomputing Center (BSC)
*
* Modifications to the initial code base are copyright of their respective
* authors, or their employers as appropriate.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*****************************************************************************
*/
package es.bsc.inb.elixir.elixibilitas.rest;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.ext.Provider;
/**
* @author Dmitry Repchevsky
*/
@Provider
@PreMatching
public class CorsResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
responseContext.getHeaders().add("Access-Control-Allow-Origin","*");
responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, OPTIONS");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "X-Requested-With");
}
}
\ No newline at end of file
/**
* *****************************************************************************
* Copyright (C) 2017 ELIXIR ES, Spanish National Bioinformatics Institute (INB)
* and Barcelona Supercomputing Center (BSC)
*
* Modifications to the initial code base are copyright of their respective
* authors, or their employers as appropriate.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*****************************************************************************
*/
package es.bsc.inb.elixir.elixibilitas.rest;
import java.io.IOException;
import java.util.List;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
/**
* The filter that add "application/json" or "application/ld+json" content to be accepted.
* For instance some browsers expect "text/html" or "application/xml" what
* makes impossible to get JSON back from the REST services.
*
* @author Dmitry Repchevsky
*/
@Provider
@PreMatching
public class JSONContentTypeFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext rc) throws IOException {
boolean json = false;
final MultivaluedMap<String, String> headers = rc.getHeaders();
final List<String> list = headers.get("Accept");
if (list != null) {
for (String header : list) {
final String[] ranges = header.split(",");
for (String range : ranges) {
final String[] nodes = range.split(";");
final String mime = nodes[0].trim();
if (MediaType.APPLICATION_XML.equals(mime)) {
if (nodes.length <= 1) {
headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
} else {
final String qs = nodes[1].replace(" ", "");
if (qs.startsWith("q=")) {
try {
final Float q = Float.parseFloat(qs.substring(2));
headers.add(HttpHeaders.ACCEPT, q > 0.5 ?
MediaType.APPLICATION_JSON :
"application/ld+json");
} catch(NumberFormatException ex) {
}
}
}
return;
} else if (MediaType.APPLICATION_JSON.equals(mime)) {
json = true;
}
}
}
}
if (!json) {
headers.add(HttpHeaders.ACCEPT, "application/json;q=.5");
}
headers.add(HttpHeaders.ACCEPT, "application/ld+json;q=.9");
}
}
/**
* *****************************************************************************
* Copyright (C) 2017 ELIXIR ES, Spanish National Bioinformatics Institute (INB)
* and Barcelona Supercomputing Center (BSC)
*
* Modifications to the initial code base are copyright of their respective
* authors, or their employers as appropriate.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*****************************************************************************
*/
package es.bsc.inb.elixir.elixibilitas.rest;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.servlet.ServletContext;
/**
* MongoDB driver producer (allows MongoClient injection).
*
* @author Dmitry Repchevsky
*/
@ApplicationScoped
public class MongoDBProducer implements Serializable {
@Inject
private ServletContext ctx;
private MongoClient mc;
@PostConstruct
public void init() {
mc = new MongoClient(new MongoClientURI(ctx.getInitParameter("mongodb.url")));
}
@PreDestroy
public void destroy() {
mc.close();
}
@Produces
public MongoClient mongoClient() {
return mc;
}
}
\ No newline at end of file
/**
* *****************************************************************************
* Copyright (C) 2017 ELIXIR ES, Spanish National Bioinformatics Institute (INB)
* and Barcelona Supercomputing Center (BSC)
*
* Modifications to the initial code base are copyright of their respective
* authors, or their employers as appropriate.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*****************************************************************************
*/
package es.bsc.inb.elixir.elixibilitas.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
* Benchmarking JAX-RS configuration (match everything from the root).
*
* @author Dmitry Repchevsky
*/
@ApplicationPath("/")
public class OpenEBenchMonitorApplication extends Application {
}
/**
* *****************************************************************************
* Copyright (C) 2016 Spanish National Bioinformatics Institute (INB) and
* Barcelona Supercomputing Center
*
* Modifications to the initial code base are copyright of their respective
* authors, or their employers as appropriate.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*****************************************************************************
*/
package es.bsc.inb.elixir.elixibilitas.rest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.NameBinding;
/**
* @author Dmitry Repchevsky
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PATCH")
@NameBinding
public @interface PATCH {
}
/**
* *****************************************************************************
* Copyright (C) 2020 ELIXIR ES, Spanish National Bioinformatics Institute (INB)
* and Barcelona Supercomputing Center (BSC)
*
* Modifications to the initial code base are copyright of their respective
* authors, or their employers as appropriate.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*****************************************************************************
*/
package es.bsc.inb.elixir.elixibilitas.rest;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import es.bsc.inb.elixir.elixibilitas.dao.MetricsDAO;
import es.bsc.inb.elixir.elixibilitas.dao.ToolsDAO;
import es.bsc.inb.elixir.openebench.model.metrics.HomepageAccess;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.info.License;
import io.swagger.v3.oas.annotations.servers.Server;
import java.io.BufferedWriter;
import java.io.CharArrayWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import static java.time.temporal.ChronoUnit.DAYS;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
/**
* REST Service to get some statistics.
*
* @author Dmitry Repchevsky
*/
@OpenAPIDefinition(info = @Info(title = "OpenEBench Statistics API services",
version = "0.1",
description = "OpenEBench Statistics API services",
license = @License(name = "LGPL 2.1",
url = "https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"),
contact = @Contact(url = "https://openebench.bsc.es")
),
//security = @SecurityRequirement(name = "openid-connect"),
servers = {@Server(url = "https://openebench.bsc.es/")})
@Path("/rest/")
@ApplicationScoped
public class StatisticsServices {
@Inject
private MongoClient mc;
@Inject
private ServletContext ctx;
@Context
private UriInfo uriInfo;
@Resource
private ManagedExecutorService executor;
private ToolsDAO toolsDAO;
private MetricsDAO metricsDAO;
private volatile long tools_uptime;
private volatile long metrics_uptime;
private CharArrayWriter tools_stat;
private CharArrayWriter metrics_stat;
@PostConstruct
public void init() {
final String toolsBaseURI = UriBuilder.fromUri(uriInfo.getBaseUri()).path(ToolsServices.class).build().toString();
final String metricsBaseURI = UriBuilder.fromUri(uriInfo.getBaseUri()).path(MetricsServices.class).build().toString();
final MongoClientURI mongodbURI = new MongoClientURI(ctx.getInitParameter("mongodb.url"));
toolsDAO = new ToolsDAO(mc.getDatabase(mongodbURI.getDatabase()), toolsBaseURI);
metricsDAO = new MetricsDAO(mc.getDatabase(mongodbURI.getDatabase()), metricsBaseURI);
}
@GET
@Path("/metrics/statistics/")
@Produces(MediaType.APPLICATION_JSON)
public void getMetricsStatistics(
@HeaderParam("Cache-Control") final String cache,
@Suspended final AsyncResponse asyncResponse) {
executor.submit(() -> {
asyncResponse.resume(getMetricsStatisticsAsync(cache).build());
});
}
private Response.ResponseBuilder getMetricsStatisticsAsync(final String cache) {
StreamingOutput stream = (OutputStream out) -> {
if (metrics_stat == null || "no-cache".equalsIgnoreCase(cache) || System.currentTimeMillis() - metrics_uptime > 86400000) {
final CharArrayWriter stat = metricsDAO.statistics(new CharArrayWriter());