Example #1
0
 private long getStartTime(Query request, String context) throws BeanValidationException {
   if (request.getStartAbsolute() != null) {
     return request.getStartAbsolute();
   } else if (request.getStartRelative() != null) {
     return request.getStartRelative().getTimeRelativeTo(System.currentTimeMillis());
   } else {
     throw new BeanValidationException(
         new SimpleConstraintViolation("start_time", "relative or absolute time must be set"),
         context);
   }
 }
Example #2
0
 private long getEndTime(Query request) {
   if (request.getEndAbsolute() != null) return request.getEndAbsolute();
   else if (request.getEndRelative() != null)
     return request.getEndRelative().getTimeRelativeTo(System.currentTimeMillis());
   return -1;
 }
Example #3
0
  private List<QueryMetric> parseQueryMetric(JsonObject obj, String contextPrefix)
      throws QueryException, BeanValidationException {
    List<QueryMetric> ret = new ArrayList<QueryMetric>();

    Query query;
    try {
      query = m_gson.fromJson(obj, Query.class);
      validateObject(query);
    } catch (ContextualJsonSyntaxException e) {
      throw new BeanValidationException(
          new SimpleConstraintViolation(e.getContext(), e.getMessage()), "query");
    }

    JsonArray metricsArray = obj.getAsJsonArray("metrics");
    if (metricsArray == null) {
      throw new BeanValidationException(
          new SimpleConstraintViolation("metric[]", "must have a size of at least 1"),
          contextPrefix + "query");
    }

    for (int I = 0; I < metricsArray.size(); I++) {
      String context =
          (!contextPrefix.isEmpty() ? contextPrefix + "." : contextPrefix)
              + "query.metric["
              + I
              + "]";
      try {
        Metric metric = m_gson.fromJson(metricsArray.get(I), Metric.class);

        validateObject(metric, context);

        long startTime = getStartTime(query, context);
        QueryMetric queryMetric =
            new QueryMetric(startTime, query.getCacheTime(), metric.getName());
        queryMetric.setExcludeTags(metric.isExcludeTags());
        queryMetric.setLimit(metric.getLimit());

        long endTime = getEndTime(query);
        if (endTime > -1) queryMetric.setEndTime(endTime);

        if (queryMetric.getEndTime() < startTime)
          throw new BeanValidationException(
              new SimpleConstraintViolation("end_time", "must be greater than the start time"),
              context);

        queryMetric.setCacheString(query.getCacheString() + metric.getCacheString());

        JsonObject jsMetric = metricsArray.get(I).getAsJsonObject();

        JsonElement group_by = jsMetric.get("group_by");
        if (group_by != null) {
          JsonArray groupBys = group_by.getAsJsonArray();
          parseGroupBy(context, queryMetric, groupBys);
        }

        JsonElement aggregators = jsMetric.get("aggregators");
        if (aggregators != null) {
          JsonArray asJsonArray = aggregators.getAsJsonArray();
          if (asJsonArray.size() > 0)
            parseAggregators(context, queryMetric, asJsonArray, query.getTimeZone());
        }

        JsonElement plugins = jsMetric.get("plugins");
        if (plugins != null) {
          JsonArray pluginArray = plugins.getAsJsonArray();
          if (pluginArray.size() > 0) parsePlugins(context, queryMetric, pluginArray);
        }

        JsonElement order = jsMetric.get("order");
        if (order != null) queryMetric.setOrder(Order.fromString(order.getAsString(), context));

        queryMetric.setTags(metric.getTags());

        ret.add(queryMetric);
      } catch (ContextualJsonSyntaxException e) {
        throw new BeanValidationException(
            new SimpleConstraintViolation(e.getContext(), e.getMessage()), context);
      }
    }

    return (ret);
  }
Example #4
0
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    int index;

    DatastoreService ds;
    MemcacheService ms;

    Cookie[] cookies;
    boolean insideFlag;

    String delpw;
    String paramOffset;
    String paramSize;
    int offset;
    int size;

    Key postObjGroupKey;
    Query q;
    List<Entity> postObjList;
    PostObj postObj;

    Gson gson;
    List<String> filelinkList;

    resp.setCharacterEncoding("UTF-8");
    resp.setContentType("text/plain");

    ds = DatastoreServiceFactory.getDatastoreService();
    ms = MemcacheServiceFactory.getMemcacheService();

    insideFlag = false;
    try {
      cookies = req.getCookies();
      if (cookies.length > 0) {
        if (ms.contains(cookies[0].getValue()) == true) {
          insideFlag = true;
        }
      }
    } catch (Exception e) {
      insideFlag = false;
    }

    delpw = req.getParameter("delpw");
    if (delpw != null) {
      if (delpw.equals("") == true) {
        delpw = null;
      }
    }

    paramOffset = req.getParameter("offset");
    if (paramOffset != null) {
      offset = Integer.valueOf(paramOffset);
    } else {
      offset = 0;
    }

    paramSize = req.getParameter("size");
    if (paramSize != null) {
      size = Integer.valueOf(paramSize);
    } else {
      size = 4096;
    }

    postObjGroupKey = KeyFactory.createKey("PostObjGroup", 1L);
    q = new Query("PostObj", postObjGroupKey);
    if (delpw != null) {
      q.addFilter("delpw", FilterOperator.EQUAL, delpw);
      q.addSort("delpw");
    }
    q.addSort("posttime", SortDirection.DESCENDING);
    postObjList = ds.prepare(q).asList(FetchOptions.Builder.withOffset(offset).limit(size));

    postObj = new PostObj();
    filelinkList = new ArrayList<String>();
    for (index = 0; index < postObjList.size(); index++) {
      postObj.getDB(postObjList.get(index));
      if ((postObj.flag.equals("showgallery") == false && insideFlag == true)
          || (postObj.flag.equals("showgallery") == true && insideFlag == false)
          || delpw != null) {
        filelinkList.add(postObj.filelink);
      }
    }

    gson = new Gson();
    resp.getWriter().print(gson.toJson(filelinkList));
  }