Example #1
0
    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
Example #2
0
    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
Example #3
0
 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]
Example #4
0
 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]