Search in sources :

Example 21 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class RunAllSparqlQueriesApp method main.

// private static final String PREFIXES_URL = "http://build-api.nextprot.org/sparql-prefixes";
/**
 * Run all SPARQL queries that are public (excludes *.unpub files)
 * unless queries to be run are specified in args
 *
 * @param args optional query name(s) to be run separated by a space i.e NXQ_00005 NXQ_00006
 *
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    String prefixes = getSparqlPrefixes();
    Map queryMap = new HashMap();
    for (int i = 0; i < args.length; i++) queryMap.put(args[i], null);
    boolean testFailed = false;
    List<UserQuery> queries = getSparqlQueries();
    System.out.println("Found " + queries.size() + " queries");
    Date d1 = new Date(System.currentTimeMillis());
    int exceptionCount = 0;
    int zeroResultsCount = 0;
    int cnt = 0;
    for (UserQuery q : queries) {
        // ---------------------------------------------------------------------------
        if (args.length > 0 && !queryMap.containsKey(q.getPublicId()))
            continue;
        // ---------------------------------------------------------------------------
        cnt++;
        long start = System.currentTimeMillis();
        String errorMessage = "";
        int resultsCount = 0;
        try {
            String sparqlQuery = prefixes + "\n" + q.getSparql();
            System.out.println("\nRequesting " + q.getPublicId() + " (" + q.getDescription() + ")... \n\n" + sparqlQuery + "\n\n");
            // System.out.println("\n\nsparqlQuery=\n"+sparqlQuery +1
            // "\n---------------");
            resultsCount = executeQuery(sparqlQuery);
            if (resultsCount == 0)
                zeroResultsCount++;
        } catch (Exception e) {
            e.printStackTrace();
            exceptionCount++;
            errorMessage = e.getLocalizedMessage();
        } finally {
            if (resultsCount == 0) {
                testFailed = true;
            }
            long timeSpent = ((System.currentTimeMillis() - start) / 1000);
            String qn = UserQueryUtils.getTutoQueryNameFromId(q.getUserQueryId());
            // This will log on release-info folder in a file called sparql-queries.tsv
            LOGGER.info(qn + "\t" + timeSpent + "\t" + resultsCount + "\t" + q.getTitle() + "\t" + errorMessage);
        }
    }
    LOGGER.info("Summary");
    LOGGER.info("Started at       : " + d1);
    LOGGER.info("Ended at         : " + new Date(System.currentTimeMillis()));
    LOGGER.info("Run query count  : " + cnt);
    LOGGER.info("OK count         : " + (queries.size() - exceptionCount - zeroResultsCount));
    LOGGER.info("Exception count  : " + exceptionCount);
    LOGGER.info("ZeroResult count : " + zeroResultsCount);
    if (testFailed) {
        throw new IllegalStateException("test failed!");
    }
}
Also used : HashMap(java.util.HashMap) UserQuery(org.nextprot.api.user.domain.UserQuery) HashMap(java.util.HashMap) Map(java.util.Map) Date(java.util.Date)

Example 22 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class RunAllSparqlQueriesAppViaRemoteApi method main.

/**
 * Run all SPARQL queries that are public (excludes *.unpub files and snorql-only ones)
 * unless queries to be run are specified in args
 *
 * @param args
 * arg1 is optional: api server name, example: http://build-api.nextprot.org
 * arg2 is optional: comma separated list of query ids, example NXQ_00001,NXQ_00002
 *
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    if (args.length > 0 && (args[0] == "-h" || args[0] == "--help")) {
        System.out.println("Usage:");
        System.out.println("arg1 is optional: api server name, example: http://build-api.nextprot.org - default: http://localhost:8080/nextprot-api-web");
        System.out.println("arg2 is optional: comma separated list of specific query ids t obe run, example NXQ_00001,NXQ_00002");
        System.exit(0);
    }
    String server = "http://localhost:8080/nextprot-api-web";
    if (args.length > 0)
        server = args[0];
    Map<String, String> queryMap = new HashMap<String, String>();
    if (args.length == 2) {
        String[] ids = args[1].split(",");
        for (int i = 0; i < ids.length; i++) queryMap.put(ids[i], null);
    }
    List<UserQuery> queries = getSparqlQueries(server);
    Date d1 = new Date(System.currentTimeMillis());
    int exceptionCount = 0;
    int zeroResultsCount = 0;
    int cnt = 0;
    for (UserQuery q : queries) {
        // ---------------------------------------------------------------------------
        if (args.length == 2 && !queryMap.containsKey(q.getPublicId()))
            continue;
        // ---------------------------------------------------------------------------
        cnt++;
        long start = System.currentTimeMillis();
        int resultsCount = 0;
        System.out.println("Running query: " + q.getPublicId());
        ObjectMapper mapper = new ObjectMapper();
        URL runQueryUrl = new URL(server + "/sparql/run.json?queryId=" + q.getPublicId());
        Map<String, Object> result = mapper.readValue(runQueryUrl, new TypeReference<HashMap<String, Object>>() {
        });
        if (result.containsKey("error"))
            exceptionCount++;
        if (result.containsKey("entryCount") && (Integer) result.get("entryCount") == 0)
            zeroResultsCount++;
        for (Map.Entry<String, Object> e : result.entrySet()) System.out.println(e.getKey() + ": " + e.getValue());
        System.out.println();
    }
    System.out.println("Summary");
    System.out.println("Started at       : " + d1);
    System.out.println("Ended at         : " + new Date(System.currentTimeMillis()));
    System.out.println("Run query count  : " + cnt);
    System.out.println("OK count         : " + (cnt - exceptionCount - zeroResultsCount));
    System.out.println("Exception count  : " + exceptionCount);
    System.out.println("ZeroResult count : " + zeroResultsCount);
}
Also used : HashMap(java.util.HashMap) Date(java.util.Date) URL(java.net.URL) UserQuery(org.nextprot.api.user.domain.UserQuery) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 23 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class UserQueryDaoTest method testCreateUserQuery.

@Test
public void testCreateUserQuery() {
    UserQuery query = new UserQuery();
    query.setTitle("ma requete");
    query.setSparql("yet another sparql query");
    query.setPublicId("00000002");
    query.setOwnerId(24);
    long id = userQueryDao.createUserQuery(query);
    assertTrue(id > 0);
    UserQuery query2 = userQueryDao.getUserQueryById(id);
    assertExpectedUserQuery(query2, id, "tahitibob", "ma requete", null, false, "yet another sparql query", "00000002", Sets.<String>newHashSet());
}
Also used : UserQuery(org.nextprot.api.user.domain.UserQuery) UserResourceBaseTest(org.nextprot.api.user.dao.test.base.UserResourceBaseTest) Test(org.junit.Test)

Example 24 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class UserQueryDaoTest method testCreateUserQuery2.

@Test
public void testCreateUserQuery2() {
    UserQuery query = new UserQuery();
    query.setTitle("ma requete");
    query.setSparql("yet another sparql query");
    query.setPublicId("00000002");
    query.setOwnerId(24);
    long id = userQueryDao.createUserQuery(query);
    assertTrue(id > 0);
    UserQuery query2 = userQueryDao.getUserQueryById(id);
    assertExpectedUserQuery(query2, id, "tahitibob", "ma requete", null, false, "yet another sparql query", "00000002", Sets.<String>newHashSet());
}
Also used : UserQuery(org.nextprot.api.user.domain.UserQuery) UserResourceBaseTest(org.nextprot.api.user.dao.test.base.UserResourceBaseTest) Test(org.junit.Test)

Example 25 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class UserQueryDaoTest method testCreateUserQueryTags.

@Test
public void testCreateUserQueryTags() {
    userQueryDao.createUserQueryTags(16, Sets.newHashSet("great", "heavy"));
    UserQuery query = userQueryDao.getUserQueryById(16);
    assertExpectedUserQuery(query, 16, "spongebob", "myquery2", "my second query", true, "another sparql query", "ZZZZZU8V", Sets.newHashSet("public", "great", "heavy"));
}
Also used : UserQuery(org.nextprot.api.user.domain.UserQuery) UserResourceBaseTest(org.nextprot.api.user.dao.test.base.UserResourceBaseTest) Test(org.junit.Test)

Aggregations

UserQuery (org.nextprot.api.user.domain.UserQuery)33 Test (org.junit.Test)20 UserResourceBaseTest (org.nextprot.api.user.dao.test.base.UserResourceBaseTest)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 MVCBaseSecurityTest (org.nextprot.api.web.dbunit.base.mvc.MVCBaseSecurityTest)4 ResultSet (java.sql.ResultSet)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 NextProtException (org.nextprot.api.commons.exception.NextProtException)3 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ApiMethod (org.jsondoc.core.annotation.ApiMethod)2 AbstractUnitBaseTest (org.nextprot.api.commons.dbunit.AbstractUnitBaseTest)2 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)2 PrintWriter (java.io.PrintWriter)1 URL (java.net.URL)1 List (java.util.List)1 SearchQueryException (org.nextprot.api.commons.exception.SearchQueryException)1 SparqlEndpoint (org.nextprot.api.rdf.service.SparqlEndpoint)1