bianzhenhua123 hace 1 mes
commit
1ba61cd809

+ 22 - 0
PyCharmMiscProject/.gitignore

@@ -0,0 +1,22 @@
+
+.idea
+*.iml
+.venv
+# Compiled class file
+*.class
+*.zip
+
+/target/*
+
+### Java template
+
+
+# Log file
+*.log
+
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
+hs_err_pid*
+*.springBeans
+*.project
+*.classpath
+__pycache__*

+ 182 - 0
PyCharmMiscProject/keysCount.py

@@ -0,0 +1,182 @@
+import time
+from tarfile import data_filter
+
+import pandas as pd
+import matplotlib.pyplot as plt
+from datetime import datetime
+
+from pandas.io.sas.sas_constants import dataset_length
+
+# 示例数据(一组数据即可)
+data = [
+    {"time": 1114, "params": {"key1": 190, "key2": 328, "key3": 312}},
+    {"time": 1113, "params": {"key1": 150, "key2": 318, "key3": 334}},
+    {"time": 1112, "params": {"key1": 126, "key2": 299, "key3": 340}},
+    {"time": 1111, "params": {"key1": 119, "key2": 270, "key3": 350}},
+    {"time": 1110, "params": {"key1": 110, "key2": 200, "key3": 100}}
+]
+
+
+class DataVisualizer:
+    def __init__(self, data_list):
+        self.data_list = data_list
+        self.dfs = self._process_data_list()
+        self.keys = self._get_keys()  # 自动获取所有key
+        # 添加中文字体配置
+        self._setup_chinese_font()
+
+    def _setup_chinese_font(self):
+        """配置中文字体支持"""
+        try:
+            plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
+            plt.rcParams['axes.unicode_minus'] = False
+        except Exception as e:
+            print(f"字体配置警告: {e}")
+
+    def _process_data_list(self):
+        """处理多个原始数据为DataFrame列表"""
+        dfs = []
+        print(self.data_list)
+        for data in self.data_list:
+            df_data = []
+            for item in data:
+                row = {"time": item["time"]}
+                row.update(item["params"])
+                df_data.append(row)
+            df = pd.DataFrame(df_data)
+            df = df.sort_values('time')  # 按时间升序排列
+            dfs.append(df)
+        return dfs
+
+    def _get_keys(self):
+        """自动获取所有key(即params中的键)"""
+        if self.dfs:
+            return list(self.dfs[0].columns)  # 假设所有数据的key一致
+        return []
+
+    def get_data_summary(self):
+        """生成多个数据数组的统计摘要"""
+        summaries = []
+        for df in self.dfs:
+            summary = {'数据点数量': len(df), '时间范围': f"{df['time'].min()} - {df['time'].max()}"}
+            for key in self.keys:
+                start = df[key].iloc[0]
+                end = df[key].iloc[-1]
+                diff = end - start
+                rate = (diff / start) * 100 if start != 0 else 0
+                summary[f'{key}变化'] = f"{start} → {end} (Δ{diff}, {rate:.1f}%)"
+            summaries.append(summary)
+        return summaries
+
+    def convertTime(timesMs):
+        local_time = time.localtime(timesMs)
+        return f"{local_time.tm_hour:02d}:{local_time.tm_min:02d}"
+
+    def create_individual_key_plots11(self):
+        """为每个key单独生成一个独立图表"""
+        for key in self.keys:
+            if key == 'time':
+                continue
+            plt.figure(figsize=(10, 6))
+            for i, df in enumerate(self.dfs):
+                plt.plot(df['time'], df[key],
+                         marker='o', linewidth=2, markersize=8,
+                         label=f'数据组{i + 1} - {key}变化趋势')
+                print('xx = ' , df['time'])
+                print('mm = ',  df[key])
+                for j, (time, value) in enumerate(zip(df['time'], df[key])):
+                    plt.annotate(f'{value}', (time, value), textcoords="offset points", xytext=(0, 10), ha='center', fontsize=12)
+            plt.xlabel('时间', fontsize=12)
+            plt.ylabel('参数值', fontsize=12)
+            plt.title(f'{key}在不同数据组随时间的变化对比', fontsize=14, fontweight='bold')
+            plt.legend()
+            plt.grid(True, alpha=0.3)
+            plt.xticks(self.dfs[0]['time'])
+            plt.tight_layout()
+            plt.show()
+
+    def create_individual_key_plots(self):
+        """为每个key单独生成一个独立图表,时间戳转换为年月日时分秒格式"""
+        for key in self.keys:
+            if key == 'time':
+                continue
+            plt.figure(figsize=(10, 6))
+
+            # 存储所有格式化后的时间,用于统一x轴刻度
+            all_formatted_times = []
+
+            for i, df in enumerate(self.dfs):
+                # 将时间戳转换为年月日时分秒格式
+                formatted_times = []
+                for timestamp in df['time']:
+                    # 假设时间戳为整数或浮点数
+                    dt_obj = datetime.fromtimestamp(timestamp / 1000)
+                    formatted_time = dt_obj.strftime("%H:%M:%S")
+                    formatted_times.append(formatted_time)
+
+                # 如果是第一个数据组,保存格式化后的时间用于x轴刻度
+                if i == 0:
+                    all_formatted_times = formatted_times
+
+                # 使用格式化后的时间进行绘图
+                plt.plot(formatted_times, df[key],
+                         marker='o', linewidth=2, markersize=8,
+                         label=f'数据组{i + 1} - {key}变化趋势')
+
+                print('原始时间戳 = ', df['time'])
+                print('格式化时间 = ', formatted_times)
+                print('数值数据 = ', df[key])
+
+                # 为每个数据点添加标注
+                for j, (formatted_time, value) in enumerate(zip(formatted_times, df[key])):
+                    plt.annotate(f'{value}',
+                                 (formatted_time, value),
+                                 textcoords="offset points",
+                                 xytext=(0, 10),
+                                 ha='center',
+                                 fontsize=12)
+
+            plt.xlabel('时间', fontsize=12)
+            plt.ylabel('参数值', fontsize=12)
+            plt.title(f'{key}在不同数据组随时间的变化对比', fontsize=14, fontweight='bold')
+            plt.legend()
+            plt.grid(True, alpha=0.3)
+
+            # 设置x轴刻度为格式化后的时间
+            plt.xticks(all_formatted_times, rotation=45)
+            plt.tight_layout()
+            plt.show()
+
+def show(data1):
+    # 创建数据数组(示例:1个数据数组)
+    data_list = [data1]
+
+    # 创建可视化器实例
+    visualizer = DataVisualizer(data_list)
+
+    print("=" * 50)
+    print("数据表格展示")
+    print("=" * 50)
+    for i, df in enumerate(visualizer.dfs):
+        print(f"数据组{i + 1}表格:")
+        print(df)
+        print()
+
+    print("=" * 50)
+    print("数据统计摘要")
+    print("=" * 50)
+    summaries = visualizer.get_data_summary()
+    for i, summary in enumerate(summaries):
+        print(f"数据组{i + 1}统计摘要:")
+        for key, value in summary.items():
+            print(f"  {key}: {value}")
+        print()
+
+    # 为每个key单独生成图表
+    print("正在生成每个key的独立对比图表...")
+    visualizer.create_individual_key_plots()
+
+'''
+if __name__ == "__main__":
+    main()
+'''

+ 182 - 0
PyCharmMiscProject/keysFromEs.py

@@ -0,0 +1,182 @@
+import json
+
+import pip
+from elasticsearch import Elasticsearch
+from datetime import datetime
+import sys
+
+from fontTools.misc.cython import returns
+
+import timeSelect
+import keysCount
+
+
+class ElasticsearchQuery:
+    def __init__(self, hosts=None):
+        """
+        初始化Elasticsearch客户端
+        :param hosts: ES服务器地址,默认为['http://localhost:9200']
+        :param http_auth: 认证信息,格式为('username', 'password')
+        """
+        self.hosts = hosts
+        self.es = None
+        self.connect()
+
+    def connect(self):
+        """建立Elasticsearch连接"""
+        try:
+            self.es = Elasticsearch(
+                hosts=self.hosts,
+                verify_certs=False,  # 开发环境可关闭证书验证
+            )
+            if self.es.ping():
+                print("✅ Elasticsearch连接成功")
+            else:
+                print("❌ Elasticsearch连接失败")
+                sys.exit(1)
+        except Exception as e:
+            print(f"❌ 连接Elasticsearch时发生错误: {e}")
+            sys.exit(1)
+
+    def execute_query(self, index_name, distinct_id, start_time, end_time):
+        """
+        执行查询
+        :param index_name: 索引名称
+        :param distinct_id: 用户ID
+        :param start_time: 开始时间戳
+        :param end_time: 结束时间戳
+        :return: 查询结果
+        """
+        query_body = {
+            "query": {
+                "bool": {
+                    "must": [
+                        {
+                            "term": {
+                                "distinctId": distinct_id
+                            }
+                        },
+                        {
+                            "range": {
+                                "time": {
+                                    "gte": start_time,
+                                    "lte": end_time
+                                }
+                            }
+                        },
+                        {
+                            "exists": {
+                                "field": "properties.params.log"
+                            }
+                        }
+                    ]
+                }
+            }
+        }
+
+        try:
+            response = self.es.search(
+                index=index_name,
+                body=query_body
+            )
+            return response
+        except Exception as e:
+            print(f"❌ 查询执行失败: {e}")
+            return None
+
+    def format_results(self, response):
+        """格式化查询结果"""
+        if not response or 'hits' not in response:
+            print("❌ 未找到匹配的结果")
+            return
+
+        hits = response['hits']['hits']
+        total = response['hits']['total']['value']
+
+        print(f"\n📊 查询结果统计:")
+        print(f"   匹配文档总数: {total}")
+        print(f"   返回文档数量: {len(hits)}")
+        print("\n" + "=" * 80)
+
+        result = []
+        for i, hit in enumerate(hits, 1):
+            source = hit['_source']
+            score = hit['_score']
+
+            print(f"\n📄 文档 {i} (得分: {score:.2f}):")
+            print(f"   文档ID: {hit['_id']}")
+            print(f"   用户ID: {source.get('distinctId', 'N/A')}")
+            print(f"   时间戳: {source.get('time', 'N/A')}")
+
+            # 格式化时间显示
+            if 'time' in source:
+                try:
+                    dt = datetime.fromtimestamp(source['time'] / 1000)
+                    print(f"   格式化时间: {dt.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]}")
+                except:
+                    pass
+
+            # 显示properties.params.log内容
+            properties = source.get('properties', {})
+            params = properties.get('params', {})
+            log_content = params.get('log', 'N/A')
+            print(f"   日志内容: {log_content}")
+            print("-" * 60)
+            try:
+                params_dict = json.loads(log_content)
+            except Exception as e:
+                print(f"JSON解析错误: {e}, 跳过该条目")
+                continue
+            converted_item = {
+                "time": source['time'],
+                "params": params_dict
+            }
+            result.append(converted_item)
+        return result
+
+
+def main():
+    """主函数"""
+    print("🚀 Elasticsearch查询工具")
+    print("=" * 50)
+
+    # 配置信息 - 请根据实际情况修改
+    ES_HOSTS = ['http://192.168.32.212:9200']  # ES服务器地址
+    now = datetime.now()
+    INDEX_NAME = "traces-" + str(now.year) + "-" + str(now.month)  + "-" + str(now.day).zfill(2)  # 索引名称
+
+    # 创建查询实例
+    es_query = ElasticsearchQuery(hosts=ES_HOSTS)
+
+    # 查询参数
+    distinct_id = "1902111"
+
+    start_time = timeSelect.enhanced_time_picker()
+    end_time = start_time + 7200000
+
+    print(f"\n🔍 执行查询:")
+    print(f"   索引: {INDEX_NAME}")
+    print(f"   用户ID: {distinct_id}")
+    print(f"   时间范围: {start_time} - {end_time}")
+
+    # 执行查询
+    response = es_query.execute_query(INDEX_NAME, distinct_id, start_time, end_time)
+
+    # 显示结果
+    if response:
+        data = es_query.format_results(response)
+        keysCount.show(data)
+
+    print("\n✅ 查询完成")
+
+
+'''
+请先行安装
+pip install elasticsearch
+pip install fontTools
+pip install pandas
+pip install matplotlib
+'''
+
+if __name__ == "__main__":
+    main()

+ 77 - 0
PyCharmMiscProject/script.py

@@ -0,0 +1,77 @@
+import matplotlib.pyplot as plt
+import numpy as np
+
+plt.rcParams['font.sans-serif'] = ['SimHei']  # 使用黑体
+plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题
+
+def generate_comparison_chart():
+    """
+    生成历史耗时与异步化耗时的对比折线图
+    """
+    # 数据定义
+    categories = ['耗时1', '耗时2', '耗时3', '耗时4', '耗时5']
+
+    # 历史耗时数据(耗时1无数据,用None表示)
+    history_times = [730, 88, 130, 1350, 168]
+
+    # 异步化耗时数据(耗时6无数据,用None表示)
+    async_times = [696, 155, 122, 662, 135]
+
+    # 创建图表
+    plt.figure(figsize=(12, 8))
+
+    # 绘制历史耗时折线
+    plt.plot(categories, history_times,
+             marker='o',
+             markersize=8,
+             linewidth=2.5,
+             label='历史耗时',
+             color='#3498db',
+             alpha=0.8)
+
+    # 绘制异步化耗时折线
+    plt.plot(categories, async_times,
+             marker='s',
+             markersize=8,
+             linewidth=2.5,
+             label='异步化耗时',
+             color='#e74c3c',
+             alpha=0.8)
+
+    # 图表美化
+    plt.title('历史耗时 vs 异步化耗时对比分析',
+              fontsize=16,
+              fontweight='bold',
+              pad=20)
+
+    plt.xlabel('测试维度', fontsize=12)
+    plt.ylabel('耗时 (ms)', fontsize=12)
+
+    # 设置网格和背景
+    plt.grid(True, linestyle='--', alpha=0.7)
+    plt.gca().set_facecolor('#f8f9fa')
+
+    # 添加图例
+    plt.legend(fontsize=11,
+               loc='upper right',
+               framealpha=0.9)
+
+    # 旋转x轴标签
+    plt.xticks(rotation=45)
+
+    # 自动调整布局
+    plt.tight_layout()
+
+    # 显示图表
+    plt.show()
+
+    # 打印数据统计
+    print("数据统计:")
+    print(
+        f"历史耗时范围: {min([x for x in history_times if x is not None])} - {max([x for x in history_times if x is not None])} ms")
+    print(
+        f"异步化耗时范围: {min([x for x in async_times if x is not None])} - {max([x for x in async_times if x is not None])} ms")
+
+
+if __name__ == "__main__":
+    generate_comparison_chart()

+ 59 - 0
PyCharmMiscProject/timeSelect.py

@@ -0,0 +1,59 @@
+import tkinter as tk
+from datetime import datetime, timedelta
+
+
+def enhanced_time_picker():
+    """增强版时间选择弹窗 - 直接返回选择时间"""
+    selected_time = [None]  # 使用列表实现类似引用传递
+
+    def on_button_click(minutes):
+        """按钮点击处理函数"""
+        target_time = datetime.now() - timedelta(minutes=minutes)
+        selected_time[0] = int(target_time.timestamp()) * 1000
+        root.destroy()
+
+    # 创建主窗口
+    root = tk.Tk()
+    root.title("快速时间选择")
+    root.geometry("250x300")
+    root.resizable(False, False)
+
+    # 居中显示
+    root.eval('tk::PlaceWindow . center')
+
+    # 当前时间显示
+    current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+    current_label = tk.Label(root, text=f"当前时间: {current_time}",
+                             font=("Arial", 9), pady=10)
+    current_label.pack()
+
+    # 按钮框架
+    button_frame = tk.Frame(root)
+    button_frame.pack(expand=True, pady=10)
+
+    # 快速选择按钮
+    time_options = [
+        ("30分钟前", 30),
+        ("1小时前", 60),
+        ("2小时前", 120),
+        ("3小时前", 180)
+    ]
+
+    for text, minutes in time_options:
+        tk.Button(button_frame, text=text,
+                  command=lambda m=minutes: on_button_click(m),
+                  width=15, height=1).pack(pady=5)
+
+    # 运行窗口
+    root.mainloop()
+    return selected_time[0]
+
+
+
+# 使用示例
+if __name__ == "__main__":
+    result = enhanced_time_picker()
+    if result:
+        print(f"选择的时间: {result}")
+    else:
+        print("未选择时间")