@Test public void testQuerySeries() throws IOException { MemStorageEngine engine = new MemStorageEngine(); engine.configure(new HashMap<>(), null); engine.startup(); Builder dp = Point.newBuilder(); dp.setDbName("test1"); dp.addFp(false); dp.addValue(1L); dp.setMeasurementName("cpu"); dp.setTimestamp(System.currentTimeMillis()); dp.addAllTags(Arrays.asList(Tag.newBuilder().setTagKey("host").setTagValue("1").build(), Tag.newBuilder().setTagKey("vm").setTagValue("1").build())); dp.addValueFieldName("value"); engine.writeDataPointWithLock(dp.build(), false); DatabaseOpsApi api = new DatabaseOpsApi(engine); String querySeries = api.querySeries("test1", "2000-12-10T10:10:10<cpu.value.host=1|te=2&vm=1<2020-12-10T10:10:10"); JsonArray results = new Gson().fromJson(querySeries, JsonArray.class); assertEquals(1, results.size()); querySeries = api.querySeries("test1", "2000-12-10T10:10:10<cpu.value.host=2<2020-12-10T10:10:10"); results = new Gson().fromJson(querySeries, JsonArray.class); assertEquals(0, results.size()); querySeries = api.querySeries("test1", "2000-12-10T10:10:10<cpu.value.vm=1<2020-12-10T10:10:10"); results = new Gson().fromJson(querySeries, JsonArray.class); assertEquals(1, results.size()); System.out.println(results); }
@Path("/{" + DB_NAME + "}/query") @POST @Produces({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.TEXT_PLAIN }) public String querySeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query) { try { String[] queryParts = query.split("<=?"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); long endTs = System.currentTimeMillis(); long startTs = endTs; String startTime = queryParts[0]; String endTime = queryParts[2]; if (startTime.contains("now")) { String[] split = startTime.split("-"); int offset = Integer.parseInt(split[1].charAt(0) + ""); startTs = startTs - (offset * 3600 * 1000); } else if (startTime.matches("\\d+")) { startTs = Long.parseLong(startTime); endTs = Long.parseLong(endTime); } else { startTs = sdf.parse(startTime).getTime(); endTs = sdf.parse(endTime).getTime(); } TargetSeries tagSeries = MiscUtils.extractTargetFromQuery(query); List<SeriesOutput> points = storageEngine.queryDataPoints(dbName, tagSeries.getMeasurementName(), tagSeries.getFieldName(), startTs, endTs, tagSeries.getTagFilter(), null, tagSeries.getAggregationFunction()); return new Gson().toJson(points); } catch (ItemNotFoundException e) { throw new NotFoundException(e); } catch (BadRequestException e) { throw e; } catch (Exception e) { e.printStackTrace(); throw new InternalServerErrorException(e); } }
DatabaseOpsApi { @Path("/{" + DB_NAME + "}/query") @POST @Produces({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.TEXT_PLAIN }) public String querySeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query) { try { String[] queryParts = query.split("<=?"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); long endTs = System.currentTimeMillis(); long startTs = endTs; String startTime = queryParts[0]; String endTime = queryParts[2]; if (startTime.contains("now")) { String[] split = startTime.split("-"); int offset = Integer.parseInt(split[1].charAt(0) + ""); startTs = startTs - (offset * 3600 * 1000); } else if (startTime.matches("\\d+")) { startTs = Long.parseLong(startTime); endTs = Long.parseLong(endTime); } else { startTs = sdf.parse(startTime).getTime(); endTs = sdf.parse(endTime).getTime(); } TargetSeries tagSeries = MiscUtils.extractTargetFromQuery(query); List<SeriesOutput> points = storageEngine.queryDataPoints(dbName, tagSeries.getMeasurementName(), tagSeries.getFieldName(), startTs, endTs, tagSeries.getTagFilter(), null, tagSeries.getAggregationFunction()); return new Gson().toJson(points); } catch (ItemNotFoundException e) { throw new NotFoundException(e); } catch (BadRequestException e) { throw e; } catch (Exception e) { e.printStackTrace(); throw new InternalServerErrorException(e); } } }
DatabaseOpsApi { @Path("/{" + DB_NAME + "}/query") @POST @Produces({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.TEXT_PLAIN }) public String querySeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query) { try { String[] queryParts = query.split("<=?"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); long endTs = System.currentTimeMillis(); long startTs = endTs; String startTime = queryParts[0]; String endTime = queryParts[2]; if (startTime.contains("now")) { String[] split = startTime.split("-"); int offset = Integer.parseInt(split[1].charAt(0) + ""); startTs = startTs - (offset * 3600 * 1000); } else if (startTime.matches("\\d+")) { startTs = Long.parseLong(startTime); endTs = Long.parseLong(endTime); } else { startTs = sdf.parse(startTime).getTime(); endTs = sdf.parse(endTime).getTime(); } TargetSeries tagSeries = MiscUtils.extractTargetFromQuery(query); List<SeriesOutput> points = storageEngine.queryDataPoints(dbName, tagSeries.getMeasurementName(), tagSeries.getFieldName(), startTs, endTs, tagSeries.getTagFilter(), null, tagSeries.getAggregationFunction()); return new Gson().toJson(points); } catch (ItemNotFoundException e) { throw new NotFoundException(e); } catch (BadRequestException e) { throw e; } catch (Exception e) { e.printStackTrace(); throw new InternalServerErrorException(e); } } DatabaseOpsApi(StorageEngine storageEngine); }
DatabaseOpsApi { @Path("/{" + DB_NAME + "}/query") @POST @Produces({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.TEXT_PLAIN }) public String querySeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query) { try { String[] queryParts = query.split("<=?"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); long endTs = System.currentTimeMillis(); long startTs = endTs; String startTime = queryParts[0]; String endTime = queryParts[2]; if (startTime.contains("now")) { String[] split = startTime.split("-"); int offset = Integer.parseInt(split[1].charAt(0) + ""); startTs = startTs - (offset * 3600 * 1000); } else if (startTime.matches("\\d+")) { startTs = Long.parseLong(startTime); endTs = Long.parseLong(endTime); } else { startTs = sdf.parse(startTime).getTime(); endTs = sdf.parse(endTime).getTime(); } TargetSeries tagSeries = MiscUtils.extractTargetFromQuery(query); List<SeriesOutput> points = storageEngine.queryDataPoints(dbName, tagSeries.getMeasurementName(), tagSeries.getFieldName(), startTs, endTs, tagSeries.getTagFilter(), null, tagSeries.getAggregationFunction()); return new Gson().toJson(points); } catch (ItemNotFoundException e) { throw new NotFoundException(e); } catch (BadRequestException e) { throw e; } catch (Exception e) { e.printStackTrace(); throw new InternalServerErrorException(e); } } DatabaseOpsApi(StorageEngine storageEngine); @GET @Produces({ MediaType.APPLICATION_JSON }) Set<String> showDatabases(); @Path("/{" + DB_NAME + "}") @PUT void createDatabase(@PathParam(DB_NAME) String dbName,
			@DefaultValue("28") @QueryParam("retentionPolicy") String retentionPolicy); @Path("/{" + DB_NAME + "}") @DELETE void dropDatabase(@PathParam(DB_NAME) String dbName); @Path("/{" + DB_NAME + "}") @GET @Produces({ MediaType.APPLICATION_JSON }) Set<String> listMeasurements(@PathParam(DB_NAME) String dbName); @Path("/{" + DB_NAME + "}/check") @GET @Produces({ MediaType.APPLICATION_JSON }) String checkIfExists(@PathParam(DB_NAME) String dbName); @DELETE void dropDatabases(); @Path("/{" + DB_NAME + "}/query") @POST @Produces({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.TEXT_PLAIN }) String querySeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query); @Path("/{" + DB_NAME + "}/gc") @POST @Produces({ MediaType.APPLICATION_JSON }) int collectGarbage(@PathParam(DatabaseOpsApi.DB_NAME) String dbName); @Path("/{" + DB_NAME + "}/compact") @POST @Produces({ MediaType.APPLICATION_JSON }) int compact(@PathParam(DatabaseOpsApi.DB_NAME) String dbName); }
DatabaseOpsApi { @Path("/{" + DB_NAME + "}/query") @POST @Produces({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.TEXT_PLAIN }) public String querySeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query) { try { String[] queryParts = query.split("<=?"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); long endTs = System.currentTimeMillis(); long startTs = endTs; String startTime = queryParts[0]; String endTime = queryParts[2]; if (startTime.contains("now")) { String[] split = startTime.split("-"); int offset = Integer.parseInt(split[1].charAt(0) + ""); startTs = startTs - (offset * 3600 * 1000); } else if (startTime.matches("\\d+")) { startTs = Long.parseLong(startTime); endTs = Long.parseLong(endTime); } else { startTs = sdf.parse(startTime).getTime(); endTs = sdf.parse(endTime).getTime(); } TargetSeries tagSeries = MiscUtils.extractTargetFromQuery(query); List<SeriesOutput> points = storageEngine.queryDataPoints(dbName, tagSeries.getMeasurementName(), tagSeries.getFieldName(), startTs, endTs, tagSeries.getTagFilter(), null, tagSeries.getAggregationFunction()); return new Gson().toJson(points); } catch (ItemNotFoundException e) { throw new NotFoundException(e); } catch (BadRequestException e) { throw e; } catch (Exception e) { e.printStackTrace(); throw new InternalServerErrorException(e); } } DatabaseOpsApi(StorageEngine storageEngine); @GET @Produces({ MediaType.APPLICATION_JSON }) Set<String> showDatabases(); @Path("/{" + DB_NAME + "}") @PUT void createDatabase(@PathParam(DB_NAME) String dbName,
			@DefaultValue("28") @QueryParam("retentionPolicy") String retentionPolicy); @Path("/{" + DB_NAME + "}") @DELETE void dropDatabase(@PathParam(DB_NAME) String dbName); @Path("/{" + DB_NAME + "}") @GET @Produces({ MediaType.APPLICATION_JSON }) Set<String> listMeasurements(@PathParam(DB_NAME) String dbName); @Path("/{" + DB_NAME + "}/check") @GET @Produces({ MediaType.APPLICATION_JSON }) String checkIfExists(@PathParam(DB_NAME) String dbName); @DELETE void dropDatabases(); @Path("/{" + DB_NAME + "}/query") @POST @Produces({ MediaType.APPLICATION_JSON }) @Consumes({ MediaType.TEXT_PLAIN }) String querySeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query); @Path("/{" + DB_NAME + "}/gc") @POST @Produces({ MediaType.APPLICATION_JSON }) int collectGarbage(@PathParam(DatabaseOpsApi.DB_NAME) String dbName); @Path("/{" + DB_NAME + "}/compact") @POST @Produces({ MediaType.APPLICATION_JSON }) int compact(@PathParam(DatabaseOpsApi.DB_NAME) String dbName); static final String DB_NAME; }