def get_tenants(self, begin, end): session = db.get_session() q = utils.model_query(self.frame_model, session) q = q.filter(self.frame_model.begin >= begin, self.frame_model.end <= end) tenants = q.distinct().values(self.frame_model.tenant_id) return [tenant.tenant_id for tenant in tenants]
def delete_mapping(self, service): session = db.get_session() q = utils.model_query(models.ServiceToCollectorMapping, session) q = q.filter(models.ServiceToCollectorMapping.service == service) r = q.delete() if not r: raise api.NoSuchMapping(service)
def list_mappings(self, collector=None): session = db.get_session() q = utils.model_query(models.ServiceToCollectorMapping, session) if collector: q = q.filter( models.ServiceToCollectorMapping.collector == collector) res = q.all() return res
def list_services(self, collector=None): session = db.get_session() q = utils.model_query(models.ServiceToCollectorMapping, session) if collector: q = q.filter( models.ServiceToCollectorMapping.collector == collector) res = q.distinct().values(models.ServiceToCollectorMapping.service) return res
def get_mapping(self, service): session = db.get_session() try: q = utils.model_query(models.ServiceToCollectorMapping, session) q = q.filter(models.ServiceToCollectorMapping.service == service) return q.one() except sqlalchemy.orm.exc.NoResultFound: raise api.NoSuchMapping(service)
def get_state(self, name): session = db.get_session() try: q = utils.model_query(models.ModuleStateInfo, session) q = q.filter(models.ModuleStateInfo.name == name) res = q.value(models.ModuleStateInfo.state) return bool(res) except sqlalchemy.orm.exc.NoResultFound: return None
def get_priority(self, name): session = db.get_session() q = utils.model_query(models.ModuleStateInfo, session) q = q.filter(models.ModuleStateInfo.name == name) res = q.value(models.ModuleStateInfo.priority) if res: return int(res) else: return 1
def get_state(self, tenant_id=None): session = db.get_session() q = utils.model_query(self.frame_model, session) if tenant_id: q = q.filter(self.frame_model.tenant_id == tenant_id) q = q.order_by(self.frame_model.begin.desc()) r = q.first() if r: return ck_utils.dt2ts(r.begin)
def set_metadata(self, name, metadata): session = db.get_session() with session.begin(): try: q = utils.model_query(models.StateInfo, session) q = q.filter(models.StateInfo.name == name) q = q.with_lockmode('update') db_state = q.one() db_state.s_metadata = metadata except sqlalchemy.orm.exc.NoResultFound: db_state = models.StateInfo(name=name, s_metadata=metadata) session.add(db_state)
def set_state(self, name, state): session = db.get_session() with session.begin(): try: q = utils.model_query(models.ModuleStateInfo, session) q = q.filter(models.ModuleStateInfo.name == name) q = q.with_lockmode('update') db_state = q.one() db_state.state = state except sqlalchemy.orm.exc.NoResultFound: db_state = models.ModuleStateInfo(name=name, state=state) session.add(db_state) return bool(db_state.state)
def get_total(self, begin=None, end=None, tenant_id=None, service=None, groupby=None): session = db.get_session() querymodels = [ sqlalchemy.func.sum(self.frame_model.rate).label('rate') ] # Boundary calculation if not begin: begin = ck_utils.get_month_start() if not end: end = ck_utils.get_next_month() if tenant_id: querymodels.append(self.frame_model.tenant_id) if service: querymodels.append(self.frame_model.res_type) if groupby: groupbyfields = groupby.split(",") for field in groupbyfields: field_obj = self.frame_model.__dict__.get(field, None) if field_obj and field_obj not in querymodels: querymodels.append(field_obj) q = session.query(*querymodels) if tenant_id: q = q.filter(self.frame_model.tenant_id == tenant_id) if service: q = q.filter(self.frame_model.res_type == service) q = q.filter(self.frame_model.begin >= begin, self.frame_model.end <= end, self.frame_model.res_type != '_NO_DATA_') if groupby: q = q.group_by(groupby) # Order by sum(rate) q = q.order_by(sqlalchemy.func.sum(self.frame_model.rate)) results = q.all() totallist = [] for r in results: total = {model.name: value for model, value in zip(querymodels, r)} total["begin"] = begin total["end"] = end totallist.append(total) return totallist
def set_mapping(self, service, collector): session = db.get_session() with session.begin(): try: q = utils.model_query(models.ServiceToCollectorMapping, session) q = q.filter( models.ServiceToCollectorMapping.service == service) q = q.with_lockmode('update') db_mapping = q.one() db_mapping.collector = collector except sqlalchemy.orm.exc.NoResultFound: model = models.ServiceToCollectorMapping db_mapping = model(service=service, collector=collector) session.add(db_mapping) return db_mapping
def get_time_frame(self, begin, end, **filters): session = db.get_session() q = utils.model_query(self.frame_model, session) q = q.filter(self.frame_model.begin >= ck_utils.ts2dt(begin), self.frame_model.end <= ck_utils.ts2dt(end)) for filter_name, filter_value in filters.items(): if filter_value: q = q.filter( getattr(self.frame_model, filter_name) == filter_value) if not filters.get('res_type'): q = q.filter(self.frame_model.res_type != '_NO_DATA_') count = q.count() if not count: raise storage.NoTimeFrame() r = q.all() return [entry.to_elnino(self._collector) for entry in r]
def get_metadata(self, name): session = db.get_session() q = utils.model_query(models.StateInfo, session) q.filter(models.StateInfo.name == name) return q.value(models.StateInfo.s_metadata)
def _check_session(self, tenant_id): session = self._session.get(tenant_id) if not session: self._session[tenant_id] = db.get_session() self._session[tenant_id].begin()