// update datapoint // Cosm documentation says, it's a post. It is in fact a PUT public void updateDatapoint(Integer feedid, String datastreamid, Datapoint datapoint) throws CosmException { try { HttpPut request = new HttpPut( API_BASE_URL_V2 + API_RESOURCE_FEEDS + "/" + feedid + "/datastreams/" + datastreamid + "/datapoints/" + datapoint.getAt() + JSON_FILE_EXTENSION); JSONObject jo = new JSONObject(); jo.put("value", datapoint.getValue()); request.setEntity(new StringEntity(jo.toString())); HttpResponse response = this.client.execute(request); StatusLine statusLine = response.getStatusLine(); String body = this.client.getBody(response); if (statusLine.getStatusCode() != 200) { if (body.length() > 0) { JSONObject ej = new JSONObject(body); throw new CosmException(ej.getString("errors")); } throw new CosmException(statusLine.toString()); } } catch (Exception e) { throw new CosmException("Caught exception in update datapoint: " + e.getMessage()); } }
private ArrayList<Datapoint> convertMetricStatisticsToDataoints( Statistics statistics, Collection<MetricStatistics> metrics) { ArrayList<Datapoint> datapoints = Lists.newArrayList(); boolean wantsAverage = statistics.getMember().contains("Average"); boolean wantsSum = statistics.getMember().contains("Sum"); boolean wantsSampleCount = statistics.getMember().contains("SampleCount"); boolean wantsMaximum = statistics.getMember().contains("Maximum"); boolean wantsMinimum = statistics.getMember().contains("Minimum"); for (MetricStatistics metricStatistics : metrics) { Datapoint datapoint = new Datapoint(); datapoint.setTimestamp(metricStatistics.getTimestamp()); datapoint.setUnit(metricStatistics.getUnits().toString()); if (wantsSum) { datapoint.setSum(metricStatistics.getSampleSum()); } if (wantsSampleCount) { datapoint.setSampleCount(metricStatistics.getSampleSize()); } if (wantsMaximum) { datapoint.setMaximum(metricStatistics.getSampleMax()); } if (wantsMinimum) { datapoint.setMinimum(metricStatistics.getSampleMin()); } if (wantsAverage) { datapoint.setAverage( MetricUtils.average(metricStatistics.getSampleSum(), metricStatistics.getSampleSize())); } datapoints.add(datapoint); } return datapoints; }
// create datapoint public void createDatapoint(Integer feedid, String datastreamid, Datapoint datapoint) throws CosmException { try { HttpPost request = new HttpPost( API_BASE_URL_V2 + API_RESOURCE_FEEDS + "/" + feedid + "/datastreams/" + datastreamid + "/datapoints" + JSON_FILE_EXTENSION); JSONObject jo = new JSONObject(); JSONArray ja = new JSONArray(); ja.put(datapoint.toJSONObject()); jo.put("datapoints", ja); request.setEntity(new StringEntity(jo.toString())); HttpResponse response = this.client.execute(request); StatusLine statusLine = response.getStatusLine(); this.client.getBody(response); if (statusLine.getStatusCode() != 200) { throw new CosmException(response.getStatusLine().toString()); } } catch (Exception e) { e.printStackTrace(); throw new CosmException("Caught exception in create datapoint" + e.getMessage()); } }
private ArrayList<Datapoint> aggregateDatapoints(List<Datapoint> list, String type) { Datapoint accumulator = null; Datapoint d = null; ArrayList<Datapoint> newList = new ArrayList<Datapoint>(); if (list == null) return newList; if (mAggregationMs == 0) { newList.addAll(list); return newList; } for (int i = 0; i < list.size(); i++) { d = list.get(i); if (i == 0) { accumulator = new Datapoint(d); continue; } if (inSameAggregationPeriod(accumulator, d) == false) { newList.add(accumulator); accumulator = new Datapoint(d); accumulator.mNEntries = 1; } else { if (type.equals(CategoryDbTable.KEY_TYPE_SUM)) { accumulator.mValue.y += d.mValue.y; accumulator.mNEntries++; } else if (type.equals(CategoryDbTable.KEY_TYPE_AVERAGE)) { if (accumulator.mNEntries + d.mNEntries != 0) { accumulator.mNEntries++; float oldMean = accumulator.mValue.y; accumulator.mValue.y += ((d.mValue.y - oldMean) / accumulator.mNEntries); } } } } if (accumulator != null) newList.add(accumulator); return newList; }
private void generateSynthetic(TimeSeries synth) { Formula formula = mFormulaCache.getFormula(synth.getDbRow().getId()); long ms; long firstVisibleMs = Long.MAX_VALUE; long lastVisibleMs = Long.MIN_VALUE; for (int j = 0; j < synth.getDependents().size(); j++) { TimeSeries ts = synth.getDependents().get(j); List<Datapoint> range = ts.getVisible(); if (range != null) { ms = range.get(0).mMillis; if (ms < firstVisibleMs) firstVisibleMs = ms; ms = range.get(range.size() - 1).mMillis; if (ms > lastVisibleMs) lastVisibleMs = ms; } } ArrayList<Datapoint> calculated = formula.apply(synth.getDependents()); ArrayList<Datapoint> pre = new ArrayList<Datapoint>(); ArrayList<Datapoint> visible = new ArrayList<Datapoint>(); ArrayList<Datapoint> post = new ArrayList<Datapoint>(); for (int j = 0; j < calculated.size(); j++) { Datapoint d = calculated.get(j); d.mCatId = synth.getDbRow().getId(); d.mSynthetic = true; if (d.mMillis < firstVisibleMs) pre.add(d); else if (d.mMillis <= lastVisibleMs) visible.add(d); else post.add(d); } pre = aggregateDatapoints(pre, synth.getDbRow().getType()); visible = aggregateDatapoints(visible, synth.getDbRow().getType()); post = aggregateDatapoints(post, synth.getDbRow().getType()); synth.setDatapoints(pre, visible, post, true); }
public boolean hasChanged() throws IOException { int address = datapoint.getAddress(); Object x = null; log.debug("Checking if {} has changed", datapoint.getName()); switch (datapoint.getType()) { case bool: x = modbus.readBoolean(address); break; case float16bit: x = modbus.readFloat16bit(address); break; case unsigned16bit: x = modbus.readUnsigned16bit(address); break; } if (x != null && !x.equals(value)) { log.info("{} has changed from {} to {}", datapoint.getName(), value, x); value = x; return true; } return false; }