def initialize_data(self): begin = ck_utils.get_month_start_timestamp() for i in range(begin, begin + 3600 * 12, 3600): data = self.create_fake_data(i, i + 3600) self.storage.append(data, '3d9a1b33-482f-42fd-aef9-b575a3da9369')
def initialize_data(self): begin = ck_utils.get_month_start_timestamp() for i in range(begin, begin + 3600 * 12, 3600): project_id = '3d9a1b33-482f-42fd-aef9-b575a3da9369' data = self.create_fake_data(i, i + 3600, project_id) self.storage.push(data, project_id)
def initialize_data(self): begin = ck_utils.get_month_start_timestamp() for i in range(begin, begin + 3600 * 12, 3600): project_id = '3d9a1b33-482f-42fd-aef9-b575a3da9369' data = self.create_fake_data(i, i + 3600, project_id) self.storage.push(data, project_id)
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') ] if not begin: begin = ck_utils.get_month_start_timestamp() if not end: end = ck_utils.get_next_month_timestamp() # Boundary calculation 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(sqlalchemy.sql.text(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 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') ] if not begin: begin = ck_utils.get_month_start_timestamp() if not end: end = ck_utils.get_next_month_timestamp() # Boundary calculation 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 get_time_frame(self, begin, end, **filters): if not begin: begin = ck_utils.get_month_start_timestamp() if not end: end = ck_utils.get_next_month_timestamp() 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 NoTimeFrame() r = q.all() return [entry.to_cloudkitty(self._collector) for entry in r]
def get_time_frame(self, begin, end, **filters): if not begin: begin = ck_utils.get_month_start_timestamp() if not end: end = ck_utils.get_next_month_timestamp() 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 NoTimeFrame() r = q.all() return [entry.to_cloudkitty(self._collector) for entry in r]