Example #1
0
    def add_period(self, timestamp, cpu_usage, mem_usage):
        cpu_usage = round(cpu_usage, 4)
        mem_usage = round(mem_usage, 4)

        bulk = self.collection.initialize_unordered_bulk_op()

        for period in ('1m', '5m', '30m', '2h', '1d'):
            spec = {
                'host_id': self.host_id,
                'period': period,
                'timestamp':
                usage_utils.get_period_timestamp(period, timestamp),
            }
            doc = {
                '$inc': {
                    'count': 1,
                    'cpu': cpu_usage,
                    'mem': mem_usage,
                }
            }
            rem_spec = {
                'host_id': self.host_id,
                'period': period,
                'timestamp': {
                    '$lt':
                    usage_utils.get_period_max_timestamp(period, timestamp),
                },
            }

            bulk.find(spec).upsert().update(doc)
            bulk.find(rem_spec).remove()

        bulk.execute()
Example #2
0
    def add_period(self, timestamp, cpu_usage, mem_usage):
        cpu_usage = round(cpu_usage, 4)
        mem_usage = round(mem_usage, 4)

        bulk = self.collection.initialize_unordered_bulk_op()

        for period in ('1m', '5m', '30m', '2h', '1d'):
            spec = {
                'host_id': self.host_id,
                'period': period,
                'timestamp': usage_utils.get_period_timestamp(
                    period, timestamp),
            }
            doc = {'$inc': {
                'count': 1,
                'cpu': cpu_usage,
                'mem': mem_usage,
            }}
            rem_spec = {
                'host_id': self.host_id,
                'period': period,
                'timestamp': {
                    '$lt': usage_utils.get_period_max_timestamp(
                        period, timestamp),
                },
            }

            bulk.find(spec).upsert().update(doc)
            bulk.find(rem_spec).remove()

        bulk.execute()
Example #3
0
    def get_period(self, period):
        date_end = usage_utils.get_period_timestamp(period,
                                                    datetime.datetime.utcnow())

        if period == '1m':
            date_start = date_end - datetime.timedelta(hours=6)
            date_step = datetime.timedelta(minutes=1)
        elif period == '5m':
            date_start = date_end - datetime.timedelta(days=1)
            date_step = datetime.timedelta(minutes=5)
        elif period == '30m':
            date_start = date_end - datetime.timedelta(days=7)
            date_step = datetime.timedelta(minutes=30)
        elif period == '2h':
            date_start = date_end - datetime.timedelta(days=30)
            date_step = datetime.timedelta(hours=2)
        elif period == '1d':
            date_start = date_end - datetime.timedelta(days=365)
            date_step = datetime.timedelta(days=1)
        date_cur = date_start

        data = {
            'cpu': [],
            'mem': [],
        }

        results = self.collection.aggregate([{
            '$match': {
                'host_id': self.host_id,
                'period': period,
            }
        }, {
            '$project': {
                'timestamp': True,
                'cpu': {
                    '$divide': ['$cpu', '$count']
                },
                'mem': {
                    '$divide': ['$mem', '$count']
                },
            }
        }, {
            '$sort': {
                'timestamp': pymongo.ASCENDING,
            }
        }])['result']

        for doc in results:
            if date_cur > doc['timestamp']:
                continue

            while date_cur < doc['timestamp']:
                timestamp = int(date_cur.strftime('%s'))
                data['cpu'].append((timestamp, 0))
                data['mem'].append((timestamp, 0))
                date_cur += date_step

            timestamp = int(doc['timestamp'].strftime('%s'))
            data['cpu'].append((timestamp, doc['cpu']))
            data['mem'].append((timestamp, doc['mem']))
            date_cur += date_step

        while date_cur <= date_end:
            timestamp = int(date_cur.strftime('%s'))
            data['cpu'].append((timestamp, 0))
            data['mem'].append((timestamp, 0))
            date_cur += date_step

        return data
Example #4
0
    def get_period(self, period):
        date_end = usage_utils.get_period_timestamp(period, utils.now())

        if period == '1m':
            date_start = date_end - datetime.timedelta(hours=6)
            date_step = datetime.timedelta(minutes=1)
        elif period == '5m':
            date_start = date_end - datetime.timedelta(days=1)
            date_step = datetime.timedelta(minutes=5)
        elif period == '30m':
            date_start = date_end - datetime.timedelta(days=7)
            date_step = datetime.timedelta(minutes=30)
        elif period == '2h':
            date_start = date_end - datetime.timedelta(days=30)
            date_step = datetime.timedelta(hours=2)
        elif period == '1d':
            date_start = date_end - datetime.timedelta(days=365)
            date_step = datetime.timedelta(days=1)
        date_cur = date_start

        data = {
            'cpu': [],
            'mem': [],
        }

        results = self.collection.aggregate([
            {'$match': {
                'host_id': self.host_id,
                'period': period,
            }},
            {'$project': {
                'timestamp': True,
                'cpu': {'$divide': ['$cpu', '$count']},
                'mem': {'$divide': ['$mem', '$count']},
            }},
            {'$sort': {
                'timestamp': pymongo.ASCENDING,
            }}
        ])

        for doc in results:
            if date_cur > doc['timestamp']:
                continue

            while date_cur < doc['timestamp']:
                timestamp = int(date_cur.strftime('%s'))
                data['cpu'].append((timestamp, 0))
                data['mem'].append((timestamp, 0))
                date_cur += date_step

            timestamp = int(doc['timestamp'].strftime('%s'))
            data['cpu'].append((timestamp, doc['cpu']))
            data['mem'].append((timestamp, doc['mem']))
            date_cur += date_step

        while date_cur <= date_end:
            timestamp = int(date_cur.strftime('%s'))
            data['cpu'].append((timestamp, 0))
            data['mem'].append((timestamp, 0))
            date_cur += date_step

        return data