def get_metric_id(day, probe_id, instrument_id): """ Returns a string that can be used as the document id for storing metric data. Metric data is stored at daily granularity, the id is a composite key of the day, probe id and instrument (sensor/actuator) id. ex: YYYYMMDD-probe_id-instrument_id """ mm = date_util.pad_month_day_value(day.month) dd = date_util.pad_month_day_value(day.day) return "%s%s%s-%s-%s" % (day.year, mm, dd, probe_id, instrument_id)
def update_daily_metrics(term, yyyy, mm, dd, first_of_month, days_in_curr_month, term_count): global docs_created_daily, dr_time, dw_time, dc_time # Create the metric identifier id_daily_metric = { "_id" : { "term" : term, "yyyy" : yyyy, "mm" : date_util.pad_month_day_value(mm) } } # Check if a doc for this identifier already exists, if not # allocate the doc r_time = datetime.now() if (db_metric_data_daily.find(id_daily_metric).count() == 0): dr_time += (datetime.now() - r_time) c_time = datetime.now() docs_created_daily += 1 metric_doc_daily = { "_id" : id_daily_metric["_id"], "term" : term, "date" : first_of_month, "daily": {} } for day in range(1, days_in_curr_month + 1): metric_doc_daily["daily"][str(day)] = 0 db_metric_data_daily.insert(metric_doc_daily) dc_time += (datetime.now() - c_time) else: dr_time += (datetime.now() - r_time) # Update the daily metric data with this value w_time = datetime.now() metric_update_daily = {"$inc" : {"daily." + str(dd) : term_count}} db_metric_data_daily.update(id_daily_metric, metric_update_daily, True) # True for upsert dw_time += (datetime.now() - w_time)