def get(bucket, sensor, starttime, endtime, granularity=granularity.seconds): """ Returns data for chartkick.py - tuple of unordered dictionaries in following format: {timestamp: value, timestamp: value, ...} Firt dictionary is deviation data and second is pressure data """ pressure = {} deviation = {} utc = pytz.timezone("UTC") start_ts = int(time.mktime(starttime.timetuple())) end_ts = int(time.mktime(endtime.timetuple())) timestamps = [ts for ts in xrange(start_ts, end_ts + granularity, granularity)] minutes_ts = [] for ts in timestamps: minute = get_prev_minute_timestamp(ts) if minute not in minutes_ts: minutes_ts.append(minute) keys = ['{0}-{1}'.format(sensor, ts) for ts in minutes_ts] datas = bucket.get_multi(keys) for ts in timestamps: minute = get_prev_minute_timestamp(ts) seconds = ts - minute val = datas.get('{0}-{1}'.format(sensor, minute)).value if val: keyname = datetime.datetime.fromtimestamp(ts, tz=utc).strftime("%Y-%m-%d %H:%M:%S") try: deviation[keyname] = _avg_val(val[str(seconds)])[0] except KeyError: pass try: pressure[keyname] = _avg_val(val[str(seconds)])[1] except KeyError: pass return deviation, pressure
def add_measurements(bucket, data, sensor_id): """ Adds measurement data to DB. Data is list of dictionaries in format you can find in dataparser.py module """ if isinstance(data, list): # minute_ts = _get_prev_minute_timestamp(int(data[0]['timestamp'].split(".")[0])) # minute_db = bucket.get(str(minute_ts)) # if minute_db.value: sec_ts, ms_part = data[0]['timestamp'].split(".") old_ts_min = get_prev_minute_timestamp(int(sec_ts)) document = {} for m_record in data: sec_ts, ms_part = m_record['timestamp'].split(".") sec_part = int(sec_ts) - get_prev_minute_timestamp(int(sec_ts)) ts_min = get_prev_minute_timestamp(int(sec_ts)) if old_ts_min == ts_min: if sec_part not in document: document[sec_part] = {} document[sec_part][int(ms_part[0])] = [m_record['deviation'], m_record['pressure']] else: # if we are in new minute if document: try: key_measurement = '{0}-{1}'.format(sensor_id, old_ts_min) bucket.set(key_measurement, document) except exceptions.TemporaryFailError as e: logger.debug("{0}".format(e.message)) raise e document = {sec_part: {int(ms_part[0]): [m_record['deviation'], m_record['pressure']]}} old_ts_min = get_prev_minute_timestamp(int(sec_ts)) logger.debug(m_record) return True