Example #1
0
  /**
   * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsSummary(java.lang.String,
   *     java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
   */
  @Override
  @SuppressWarnings("nls")
  public ResponseStatsSummaryBean getResponseStatsSummary(
      String organizationId, String serviceId, String version, DateTime from, DateTime to) {
    ResponseStatsSummaryBean rval = new ResponseStatsSummaryBean();

    try {
      String query =
          "{"
              + "  \"query\": {"
              + "    \"filtered\" : {"
              + "      \"query\" : {"
              + "        \"range\" : {"
              + "          \"requestStart\" : {"
              + "            \"gte\": \"${from}\","
              + "            \"lte\": \"${to}\""
              + "          }"
              + "        }"
              + "      },"
              + "      \"filter\": {"
              + "        \"and\" : ["
              + "          { \"term\" : { \"serviceOrgId\" : \"${serviceOrgId}\" } },"
              + "          { \"term\" : { \"serviceId\" : \"${serviceId}\" } },"
              + "          { \"term\" : { \"serviceVersion\" : \"${serviceVersion}\" } }"
              + "        ]"
              + "      }"
              + "    }"
              + "  },"
              + "  \"size\": 0, "
              + "  \"aggs\" : {"
              + "    \"total_failures\" : {"
              + "      \"filter\" : { \"term\": { \"failure\": true } }"
              + "    },"
              + "    \"total_errors\" : {"
              + "      \"filter\" : { \"term\": { \"error\": true } }"
              + "    }"
              + "  }"
              + "}";
      Map<String, String> params = new HashMap<>();
      params.put("from", formatDate(from));
      params.put("to", formatDate(to));
      params.put("serviceOrgId", organizationId.replace('"', '_'));
      params.put("serviceId", serviceId.replace('"', '_'));
      params.put("serviceVersion", version.replace('"', '_'));
      StrSubstitutor ss = new StrSubstitutor(params);
      query = ss.replace(query);

      Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
      SearchResult response = getEsClient().execute(search);

      rval.setTotal(response.getTotal());
      rval.setFailures(
          response.getAggregations().getFilterAggregation("total_failures").getCount());
      rval.setErrors(response.getAggregations().getFilterAggregation("total_errors").getCount());
    } catch (IOException e) {
      log.error(e);
    }

    return rval;
  }
  @Test
  public void testQuery() throws IOException {
    createIndex("query_facet");
    String query =
        "{\n"
            + "            \"facets\" : {\n"
            + "            \"wow_facet\" : {\n"
            + "                \"query\" : {\n"
            + "                    \"term\" : { \"tag\" : \"wow\" }\n"
            + "                }\n"
            + "            },\n"
            + "            \"none_user_facet\" : {\n"
            + "                \"query\" : {\n"
            + "                    \"term\" : { \"user\" : \"none\" }\n"
            + "                }\n"
            + "            }\n"
            + "        }\n"
            + "        }";

    for (int i = 0; i < 2; i++) {
      Index index =
          new Index.Builder("{\"tag\":\"wow\", \"user\":\"root\"}")
              .index("query_facet")
              .type("document")
              .refresh(true)
              .build();
      client.execute(index);
    }

    Index index =
        new Index.Builder("{\"tag\":\"test\", \"user\":\"none\"}")
            .index("query_facet")
            .type("document")
            .refresh(true)
            .build();
    client.execute(index);

    Search search = new Search.Builder(query).addIndex("query_facet").addType("document").build();
    SearchResult result = client.execute(search);
    List<QueryFacet> filterFacets = result.getFacets(QueryFacet.class);

    assertEquals(2, filterFacets.size());
    QueryFacet queryFacetFirst = filterFacets.get(0);

    assertEquals("wow_facet", queryFacetFirst.getName());
    assertEquals(2L, queryFacetFirst.getCount().longValue());

    QueryFacet queryFacetSecond = filterFacets.get(1);

    assertEquals("none_user_facet", queryFacetSecond.getName());
    assertEquals(1L, queryFacetSecond.getCount().longValue());
  }
 public int getNbrInstance() {
   String query =
       "{\n"
           + "   \"query\": {\n"
           + "       \"match_all\": {}\n"
           + "   },\n"
           + "   \"size\": "
           + Integer.MAX_VALUE
           + "\n"
           + "}";
   String elasticType = "test-ES";
   SearchResult searchResult = performSearchOnType(query, elasticType);
   System.out.println(elasticType);
   JsonObject jsonObject = searchResult.getJsonObject();
   JsonArray jsonHits = jsonObject.get("hits").getAsJsonObject().get("hits").getAsJsonArray();
   System.out.println();
   return jsonHits.size();
 }
  private <T> List<T> getAll(Class<T> clazz, String elasticType) {
    String query =
        "{\n"
            + "   \"query\": {\n"
            + "      \"match_all\": {}\n"
            + "   },\n"
            + "   \"size\": "
            + Integer.MAX_VALUE
            + "\n"
            + "}";
    SearchResult searchResult = performSearchOnType(query, elasticType);

    return StreamSupport.stream(
            Spliterators.spliteratorUnknownSize(
                searchResult.getHits(clazz).iterator(), Spliterator.ORDERED),
            false)
        .map(hitResult -> hitResult.source)
        .collect(Collectors.toList());
  }
Example #5
0
 @RequestMapping(value = "/performSearch", method = RequestMethod.POST)
 public String performSearch(
     @ModelAttribute("RdfModel") RdfModel rdfModel, Model model, RedirectAttributes attrs) {
   SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
   QueryBuilder qb = QueryBuilders.matchQuery("hasTitle", rdfModel.getHasTitle());
   searchSourceBuilder.query(qb);
   Search search =
       new Search.Builder(searchSourceBuilder.toString()).addIndex("publications").build();
   try {
     SearchResult searchresult = jc.execute(search);
     ArrayList<RdfModel> publications =
         new ArrayList<RdfModel>(searchresult.getSourceAsObjectList(RdfModel.class));
     model.addAttribute("searchResults", publications);
     model.addAttribute("rdfModel", new RdfModel());
   } catch (Exception e) {
     e.printStackTrace();
   }
   return "index";
 }
  private <T> List<GeolocResult<T>> find(
      double lat, double lon, int size, Class<T> clazz, String elasticType) {
    if (size == 0) {
      size = 10;
    }

    String query =
        "{\n"
            + "  \"query\": {\n"
            + "    \"filtered\" : {\n"
            + "        \"filter\" : {\n"
            + "            \"geo_distance\" : {\n"
            + "                \"distance\" : \"200km\",\n"
            + "                \"location\" : {\n"
            + "                    \"lat\" : "
            + lat
            + ",\n"
            + "                    \"lon\" : "
            + lon
            + "\n"
            + "                }\n"
            + "            }\n"
            + "        }\n"
            + "    }\n"
            + "  },\n"
            + "  \"size\" : "
            + size
            + ",\n"
            + "  \"sort\": [\n"
            + "    {\n"
            + "      \"_geo_distance\": {\n"
            + "        \"location\": { \n"
            + "            \"lat\" : "
            + lat
            + ",\n"
            + "            \"lon\" : "
            + lon
            + "\n"
            + "        },\n"
            + "        \"order\":         \"asc\",\n"
            + "        \"unit\":          \"km\", \n"
            + "        \"distance_type\": \"plane\" \n"
            + "      }\n"
            + "    }\n"
            + "  ]\n"
            + "}";

    SearchResult searchResult = performSearchOnType(query, elasticType);

    JsonObject jsonObject = searchResult.getJsonObject();
    JsonArray jsonHits = jsonObject.get("hits").getAsJsonObject().get("hits").getAsJsonArray();

    Gson gson = new Gson();

    return StreamSupport.stream(jsonHits.spliterator(), false)
        .map(
            jsonElement -> {
              JsonObject hit = jsonElement.getAsJsonObject();
              double distance = hit.get("sort").getAsDouble();
              T result = gson.fromJson(hit.get("_source").getAsJsonObject(), clazz);

              return new GeolocResult<T>(result, (int) (distance * 1000));
            })
        .collect(Collectors.toList());
  }
Example #7
0
  /**
   * @see io.apiman.manager.api.core.IMetricsAccessor#getUsage(java.lang.String, java.lang.String,
   *     java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType,
   *     org.joda.time.DateTime, org.joda.time.DateTime)
   */
  @SuppressWarnings("nls")
  @Override
  public UsageHistogramBean getUsage(
      String organizationId,
      String serviceId,
      String version,
      HistogramIntervalType interval,
      DateTime from,
      DateTime to) {
    UsageHistogramBean rval = new UsageHistogramBean();
    Map<String, UsageDataPoint> index =
        generateHistogramSkeleton(rval, from, to, interval, UsageDataPoint.class);

    try {
      String query =
          "{"
              + "  \"query\": {"
              + "    \"filtered\" : {"
              + "      \"query\" : {"
              + "        \"range\" : {"
              + "          \"requestStart\" : {"
              + "            \"gte\": \"${from}\","
              + "            \"lte\": \"${to}\""
              + "          }"
              + "        }"
              + "      },"
              + "      \"filter\": {"
              + "        \"and\" : ["
              + "          { \"term\" : { \"serviceOrgId\" : \"${serviceOrgId}\" } },"
              + "          { \"term\" : { \"serviceId\" : \"${serviceId}\" } },"
              + "          { \"term\" : { \"serviceVersion\" : \"${serviceVersion}\" } }"
              + "        ]"
              + "      }"
              + "    }"
              + "  },"
              + "  \"size\": 0, "
              + "  \"aggs\" : {"
              + "      \"histogram\" : {"
              + "          \"date_histogram\" : {"
              + "              \"field\" : \"requestStart\","
              + "              \"interval\" : \"${interval}\""
              + "          }"
              + "      }"
              + "  }"
              + "}";
      Map<String, String> params = new HashMap<>();
      params.put("from", formatDate(from));
      params.put("to", formatDate(to));
      params.put("serviceOrgId", organizationId.replace('"', '_'));
      params.put("serviceId", serviceId.replace('"', '_'));
      params.put("serviceVersion", version.replace('"', '_'));
      params.put("interval", interval.name());
      StrSubstitutor ss = new StrSubstitutor(params);
      query = ss.replace(query);

      Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
      SearchResult response = getEsClient().execute(search);
      MetricAggregation aggregations = response.getAggregations();
      DateHistogramAggregation aggregation = aggregations.getDateHistogramAggregation("histogram");
      if (aggregation != null) {
        List<DateHistogram> buckets = aggregation.getBuckets();
        for (DateHistogram entry : buckets) {
          String keyAsString = entry.getTimeAsString();
          if (index.containsKey(keyAsString)) {
            index.get(keyAsString).setCount(entry.getCount());
          }
        }
      }
    } catch (IOException e) {
      log.error(e);
    }

    return rval;
  }
Example #8
0
  /**
   * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerPlan(java.lang.String,
   *     java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
   */
  @Override
  @SuppressWarnings("nls")
  public ResponseStatsPerPlanBean getResponseStatsPerPlan(
      String organizationId, String serviceId, String version, DateTime from, DateTime to) {
    ResponseStatsPerPlanBean rval = new ResponseStatsPerPlanBean();

    try {
      String query =
          "{"
              + "  \"query\": {"
              + "    \"filtered\" : {"
              + "      \"query\" : {"
              + "        \"range\" : {"
              + "          \"requestStart\" : {"
              + "            \"gte\": \"${from}\","
              + "            \"lte\": \"${to}\""
              + "          }"
              + "        }"
              + "      },"
              + "      \"filter\": {"
              + "        \"and\" : ["
              + "          { \"term\" : { \"serviceOrgId\" : \"${serviceOrgId}\" } },"
              + "          { \"term\" : { \"serviceId\" : \"${serviceId}\" } },"
              + "          { \"term\" : { \"serviceVersion\" : \"${serviceVersion}\" } }"
              + "        ]"
              + "      }"
              + "    }"
              + "  },"
              + "  \"size\": 0, "
              + "  \"aggs\" : {"
              + "      \"by_plan\" : {"
              + "        \"terms\" : {"
              + "          \"field\" : \"planId\""
              + "        },"
              + "        \"aggs\" : {"
              + "          \"total_failures\" : {"
              + "            \"filter\" : { \"term\": { \"failure\": true } }"
              + "          },"
              + "          \"total_errors\" : {"
              + "            \"filter\" : { \"term\": { \"error\": true } }"
              + "          }"
              + "        }"
              + "      }"
              + "  }"
              + "}";
      Map<String, String> params = new HashMap<>();
      params.put("from", formatDate(from));
      params.put("to", formatDate(to));
      params.put("serviceOrgId", organizationId.replace('"', '_'));
      params.put("serviceId", serviceId.replace('"', '_'));
      params.put("serviceVersion", version.replace('"', '_'));
      StrSubstitutor ss = new StrSubstitutor(params);
      query = ss.replace(query);

      Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
      SearchResult response = getEsClient().execute(search);
      MetricAggregation aggregations = response.getAggregations();
      ApimanTermsAggregation aggregation =
          aggregations.getAggregation("by_plan", ApimanTermsAggregation.class); // $NON-NLS-1$
      if (aggregation != null) {
        List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
        int counter = 0;
        for (ApimanTermsAggregation.Entry entry : buckets) {
          ResponseStatsDataPoint point = new ResponseStatsDataPoint();
          point.setTotal(entry.getCount());
          rval.addDataPoint(
              entry.getKey(),
              entry.getCount(),
              entry.getFilterAggregation("total_failures").getCount(),
              entry.getFilterAggregation("total_errors").getCount());
          counter++;
          if (counter > 10) {
            break;
          }
        }
      }
    } catch (IOException e) {
      log.error(e);
    }

    return rval;
  }