baoshiwei
2026-04-01 81b0ad0124847f083990d574dc8d20961ec6e713
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import streamlit as st
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
from datetime import datetime, timedelta
from app.services.main_process_service import MainProcessService
 
def show_main_process_dashboard():
    # 初始化服务
    service = MainProcessService()
 
    # 页面标题
    st.title("主流程控制数据分析")
 
    # 初始化会话状态用于日期同步
    if 'main_process_start_date' not in st.session_state:
        st.session_state['main_process_start_date'] = datetime.now().date() - timedelta(days=7)
    if 'main_process_end_date' not in st.session_state:
        st.session_state['main_process_end_date'] = datetime.now().date()
    if 'main_process_quick_select' not in st.session_state:
        st.session_state['main_process_quick_select'] = "最近7天"
 
    # 定义回调函数
    def update_dates(qs):
        st.session_state['main_process_quick_select'] = qs
        today = datetime.now().date()
        if qs == "今天":
            st.session_state['main_process_start_date'] = today
            st.session_state['main_process_end_date'] = today
        elif qs == "最近3天":
            st.session_state['main_process_start_date'] = today - timedelta(days=3)
            st.session_state['main_process_end_date'] = today
        elif qs == "最近7天":
            st.session_state['main_process_start_date'] = today - timedelta(days=7)
            st.session_state['main_process_end_date'] = today
        elif qs == "最近30天":
            st.session_state['main_process_start_date'] = today - timedelta(days=30)
            st.session_state['main_process_end_date'] = today
 
    def on_date_change():
        st.session_state['main_process_quick_select'] = "自定义"
 
    # 查询条件区域
    with st.expander("🔍 查询配置", expanded=True):
        # 添加自定义 CSS 实现响应式换行
        st.markdown("""
            <style>
            /* 强制列容器换行 */
            [data-testid="stExpander"] [data-testid="column"] {
                flex: 1 1 120px !important;
                min-width: 120px !important;
            }
            /* 针对日期输入框列稍微加宽一点 */
            @media (min-width: 768px) {
                [data-testid="stExpander"] [data-testid="column"]:nth-child(6),
                [data-testid="stExpander"] [data-testid="column"]:nth-child(7) {
                    flex: 2 1 180px !important;
                    min-width: 180px !important;
                }
            }
            </style>
            """, unsafe_allow_html=True)
            
        # 创建布局
        cols = st.columns([1, 1, 1, 1, 1, 1.5, 1.5, 1])
        
        options = ["今天", "最近3天", "最近7天", "最近30天", "自定义"]
        for i, option in enumerate(options):
            with cols[i]:
                # 根据当前选择状态决定按钮类型
                button_type = "primary" if st.session_state['main_process_quick_select'] == option else "secondary"
                if st.button(option, key=f"btn_main_{option}", width='stretch', type=button_type):
                    update_dates(option)
                    st.rerun()
 
        with cols[5]:
            st.date_input(
                "开始日期", 
                label_visibility="collapsed",
                key="main_process_start_date",
                on_change=on_date_change
            )
        
        with cols[6]:
            st.date_input(
                "结束日期", 
                label_visibility="collapsed",
                key="main_process_end_date",
                on_change=on_date_change
            )
 
        with cols[7]:
            query_button = st.button("🚀 查询", key="main_process_query", width='stretch')
 
    # 转换为datetime对象
    start_dt = datetime.combine(st.session_state['main_process_start_date'], datetime.min.time())
    end_dt = datetime.combine(st.session_state['main_process_end_date'], datetime.max.time())
 
    if query_button:
        with st.spinner("正在获取主流程数据..."):
            # 1. 获取主速度数据
            df_speed = service.get_cutting_setting_data(start_dt, end_dt)
            # 2. 获取电机监控数据
            df_motor = service.get_motor_monitoring_data(start_dt, end_dt)
            # 3. 获取温度控制数据
            df_temp = service.get_temperature_control_data(start_dt, end_dt)
 
            # --- 趋势图 1: 流程主速度 ---
            st.subheader("📈 流程主速度趋势")
            if not df_speed.empty:
                fig_speed = px.line(df_speed, x='time', y='process_main_speed', 
                                   title="流程主速度 (M/Min)",
                                   labels={'time': '时间', 'process_main_speed': '主速度 (M/Min)'})
                fig_speed.update_layout(
                    xaxis=dict(rangeslider=dict(visible=True), type='date'),
                                            yaxis=dict(fixedrange=False),
                                            hovermode='x unified',
                    dragmode='zoom',
                )
                st.plotly_chart(fig_speed, width='stretch', config={
                    'scrollZoom': True, 
                    'modeBarButtonsToAdd': ['zoom2d', 'zoomIn2d', 'zoomOut2d'], 
                    'doubleClick': 'reset', 
                    'displayModeBar': True,
                    'toImageButtonOptions': {'format': 'png'}
                })
            else:
                st.info("该时间段内无主速度数据")
 
            # --- 趋势图 2: 电机运行线速 ---
            st.subheader("📈 电机运行线速趋势")
            if not df_motor.empty:
                fig_motor = go.Figure()
                fig_motor.add_trace(go.Scatter(x=df_motor['time'], y=df_motor['m1_line_speed'], name='拉出一段线速'))
                fig_motor.add_trace(go.Scatter(x=df_motor['time'], y=df_motor['m2_line_speed'], name='拉出二段线速'))
                fig_motor.update_layout(
                    title="电机线速 (M/Min)", 
                    xaxis_title="时间", 
                    yaxis_title="线速 (M/Min)",
                    xaxis=dict(rangeslider=dict(visible=True), type='date'),
                                            yaxis=dict(fixedrange=False),
                                            hovermode='x unified',
                    dragmode='zoom'
                )
                st.plotly_chart(fig_motor, width='stretch', config={
                    'scrollZoom': True, 
                    'modeBarButtonsToAdd': ['zoom2d', 'zoomIn2d', 'zoomOut2d'], 
                    'doubleClick': 'reset', 
                    'displayModeBar': True
                })
            else:
                st.info("该时间段内无电机监控数据")
 
            # --- 趋势图 3: 中田挤出机温度控制 ---
            st.subheader("📈 中田挤出机温度趋势")
            if not df_temp.empty:
                fig_temp = go.Figure()
                
                # 螺杆温度
                fig_temp.add_trace(go.Scatter(x=df_temp['time'], y=df_temp['nakata_extruder_screw_set_temp'], 
                                            name='螺杆设定', line=dict(dash='dash')))
                fig_temp.add_trace(go.Scatter(x=df_temp['time'], y=df_temp['nakata_extruder_screw_display_temp'], 
                                            name='螺杆显示'))
                
                # 后机筒温度
                fig_temp.add_trace(go.Scatter(x=df_temp['time'], y=df_temp['nakata_extruder_rear_barrel_set_temp'], 
                                            name='后机筒设定', line=dict(dash='dash')))
                fig_temp.add_trace(go.Scatter(x=df_temp['time'], y=df_temp['nakata_extruder_rear_barrel_display_temp'], 
                                            name='后机筒显示'))
                
                # 前机筒温度
                fig_temp.add_trace(go.Scatter(x=df_temp['time'], y=df_temp['nakata_extruder_front_barrel_set_temp'], 
                                            name='前机筒设定', line=dict(dash='dash')))
                fig_temp.add_trace(go.Scatter(x=df_temp['time'], y=df_temp['nakata_extruder_front_barrel_display_temp'], 
                                            name='前机筒显示'))
                
                # 机头温度
                fig_temp.add_trace(go.Scatter(x=df_temp['time'], y=df_temp['nakata_extruder_head_set_temp'], 
                                            name='机头设定', line=dict(dash='dash')))
                fig_temp.add_trace(go.Scatter(x=df_temp['time'], y=df_temp['nakata_extruder_head_display_temp'], 
                                            name='机头显示'))
 
                fig_temp.update_layout(
                    title="中田挤出机温度 (°C)", 
                    xaxis_title="时间", 
                    yaxis_title="温度 (°C)",
                    xaxis=dict(rangeslider=dict(visible=True), type='date'),
                                            yaxis=dict(fixedrange=False),
                                            hovermode='x unified',
                    dragmode='zoom'
                )
                st.plotly_chart(fig_temp, width='stretch', config={
                    'scrollZoom': True, 
                    'modeBarButtonsToAdd': ['zoom2d', 'zoomIn2d', 'zoomOut2d'], 
                    'doubleClick': 'reset', 
                    'displayModeBar': True
                })
            else:
                st.info("该时间段内无温度控制数据")