统计elasticsearch数据写入excel表格,统计前一天的数据,加入定时任务每天进行自动统计

import xlrd, requests
from xlutils3.copy import copy
from elasticsearch import Elasticsearch
import time, datetime


# 统计所有系统的数据并写入Excel表格

class NewCount(object):
    def __init__(self):
        self.es = Elasticsearch(['192.17.5.148:9200'])
        self.es_505 = Elasticsearch(['192.169.31.12:9200'])
        self.plat_505 = '505'
        self.plat_506 = '506'
        self.plat_507 = '507'
        self.stat = '正常'
        self.now = datetime.datetime.now().date()
        self.today = datetime.date.today()
        self.yesterday = self.today - datetime.timedelta(days=1)
        self.start_time = str(self.yesterday) + ' ' + '00:00:00'
        self.end_time = str(self.yesterday) + ' ' + '23:59:59'
        self.index_time = str(self.yesterday).replace('-', '.')
        #self.month = str(time.localtime()[0]) + '.' + '%0.2s' % str(time.localtime()[1])
        self.year = str(time.localtime()[0])
        self.month = str(time.localtime()[1]).zfill(2)
        self.y_m = '{}.{:.2}'.format(self.year,self.month)
        self.query_index = 'cr-stat-%s' % self.index_time
        self.filename = r'D:\0_日常工作\运维记录\日常运维记录表.xls'

        # 获取上个月的月份和上个月最后一天
        self.last_month = self.get_Up_month(self.today, 1)
        self.last_day_index = 'cr-all-{}'.format(str(self.last_month).replace('-', '.'))
        self.last_day_of_last_month = datetime.date(self.today.year, self.today.month, 1) - datetime.timedelta(1)
        self.last_am_timestamp = int(
            time.mktime(time.strptime(str(self.last_day_of_last_month) + ' ' + '00:00:00', '%Y-%m-%d %H:%M:%S')))
        self.last_pm_timestamp = int(
            time.mktime(time.strptime(str(self.last_day_of_last_month) + ' ' + '23:59:59', '%Y-%m-%d %H:%M:%S')))

        if self.yesterday != self.last_day_of_last_month:
            self.conv_time_start = int(time.mktime(time.strptime(self.start_time, "%Y-%m-%d %X")))
            self.conv_time_end = int(time.mktime(time.strptime(self.end_time, "%Y-%m-%d %X")))
            self.index_505 = 'cr-all-{}'.format(self.y_m)
        else:
            self.conv_time_start = int(
                time.mktime(time.strptime(str(self.last_day_of_last_month) + ' ' + '00:00:00', '%Y-%m-%d %H:%M:%S')))
            self.conv_time_end = int(
                time.mktime(time.strptime(str(self.last_day_of_last_month) + ' ' + '23:59:59', '%Y-%m-%d %H:%M:%S')))
            self.index_505 = 'cr-all-{}'.format(str(self.last_month).replace('-', '.'))

    # 判断当天是否是本月最后一天
    def last_day_of_month(self):
        next_month = self.now.replace(day=28) + datetime.timedelta(days=4)
        last_day = next_month - datetime.timedelta(days=next_month.day)
        return last_day

    def get_Up_month(self, date, n):
        month = date.month
        year = date.year
        for i in range(n):
            if month == 1:
                year -= 1
                month = 12
            else:
                month -= 1
        up_month = datetime.date(year, month, 1).strftime('%Y-%m')
        return up_month

    # 每日vpr处理量
    def vpr_ret(self):
        vpr_query = \
            {
                "query": {
                    "match_all": {}
                },
                "_source": "{vprresult}"
            }
        vpr_ret = self.es.search(index=self.query_index, body=vpr_query)
        vpr_result = vpr_ret['hits']['total'] / 10000
        return vpr_result

    # 每日关键词处理量
    def kwd_ret(self):
        kwd_query = \
            {
                "size": 0,
                "query": {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "languagename.keyword": {
                                        "value": "lang1"
                                    }
                                }
                            },
                            {
                                "term": {
                                    "languagename.keyword": {
                                        "value": "lang2"
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        kwd_ret = self.es.search(index=self.query_index, body=kwd_query)
        kwd_result = kwd_ret['hits']['total'] / 10000
        return kwd_result

    # 4G处理量
    def video_ret(self):
        video_index = "cr-video-{}".format(self.y_m)
        video_query = \
            {
                "query": {
                    "match": {
                        "start_loc": self.index_time
                    }
                }
            }
        video_ret = self.es.search(index=video_index, body=video_query)
        video_result = video_ret['hits']['total'] / 10000
        return video_result

    # 507数据接入量
    def adapter_507_ret(self):
        adapter_507 = \
            {
                "size": 0,
                "query": {
                    "match": {
                        "platformid": self.plat_507
                    }
                }
            }
        ret_507 = self.es.search(index=self.query_index, body=adapter_507)
        result_507 = ret_507['hits']['total'] / 10000
        return result_507

    # 506数据接入量
    def adapter_506_ret(self):
        adapter_506 = \
            {
                "size": 0,
                "query": {
                    "match": {
                        "platformid": self.plat_506
                    }
                }
            }
        ret_506 = self.es.search(index=self.query_index, body=adapter_506)
        result_506 = ret_506['hits']['total'] / 10000
        return result_506

    def stat_505(self):
        all_query = \
            {
                "query": {
                    "range": {
                        "starttime": {
                            "gte": self.conv_time_start,
                            "lte": self.conv_time_end
                        }
                    }
                }
            }
        ret = self.es_505.search(index=self.index_505, body=all_query)

        return ret['hits']['total'] / 10000


    def result_set(self):
        ret_list = []
        vpr_value = self.vpr_ret()
        kwd_value = self.kwd_ret()
        video_value = self.video_ret()
        value_505 = self.stat_505()
        value_506 = self.adapter_506_ret()
        value_507 = self.adapter_507_ret()
        ret_list.append(self.index_time)
        ret_list.append(vpr_value)
        ret_list.append(kwd_value)
        ret_list.append(video_value)
        ret_list.append(value_505)
        ret_list.append(value_506)
        ret_list.append(value_507)
        ret_list.append(video_value)
        ret_list.append(self.stat)
        ret_list.append(self.stat)
        ret_list.append(self.stat)
        ret_list.append(self.stat)
        ret_list.append(self.stat)

        return ret_list


    def write_excel_xls_append(self):
        value = self.result_set()
        index = len(value)
        try:
            workbook = xlrd.open_workbook(self.filename)
            sheets = workbook.sheet_names()
            worksheet = workbook.sheet_by_name(sheets[0])
            rows_old = worksheet.nrows
            new_workbook = copy(workbook)
            new_worksheet = new_workbook.get_sheet(0)
            for i in range(0, index):
                new_worksheet.write(rows_old, i, value[i])
            new_workbook.save(self.filename)
            print('{} 写入数据成功'.format(self.yesterday))
        except Exception as e:
            print(e)


if __name__ == '__main__':
    res_ins = NewCount()
    res_ins.write_excel_xls_append()

你可能感兴趣的:(运维,python)