File size: 2,680 Bytes
05a4c82
 
626c449
05a4c82
 
3273528
 
 
05a4c82
3273528
f731ddb
 
05a4c82
3273528
 
 
05a4c82
f731ddb
 
05a4c82
3273528
 
05a4c82
3273528
 
 
 
f731ddb
3273528
 
05a4c82
f731ddb
05a4c82
3273528
 
 
05a4c82
f5f591a
 
 
 
 
 
 
 
c587d34
05a4c82
 
 
 
 
 
f5f591a
 
 
 
 
05a4c82
 
e904c97
 
626c449
e904c97
 
3d63627
 
 
 
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
import pandas as pd

async def machine_metrics(raw_data):
    df = pd.DataFrame(raw_data)

    datetime_cols = ['Timestamp', 'Downtime Start', 'Downtime End']
    for col in datetime_cols:
        df[col] = pd.to_datetime(df[col], errors='coerce', format="%Y-%m-%d %H:%M:%S")

    opening_time = df['Timestamp'].max() - df['Timestamp'].min()
    required_time = opening_time
    # planned_stop_time = 0 non implémenté

    downtime_df = df.dropna(subset=['Downtime Start', 'Downtime End'])
    unplanned_stop_time = (downtime_df['Downtime End'] - downtime_df['Downtime Start']).sum()
    operating_time = required_time - unplanned_stop_time

    net_time = operating_time
    # cadency_variance = 0 non implémenté

    nok_count = (df['Compliance'] != 'OK').sum()
    useful_time = net_time - pd.Timedelta(seconds=nok_count)

    operating_sec = operating_time.total_seconds()
    net_sec = net_time.total_seconds()
    required_sec = required_time.total_seconds()

    quality_rate = (useful_time / net_time) * 100 if net_time else 0
    operating_rate = (net_sec / operating_sec) * 100 if operating_sec > 0 else 0
    availability_rate = (operating_sec / required_sec) * 100 if required_sec > 0 else 0

    OEE = (quality_rate / 100) * (operating_rate / 100) * (availability_rate / 100) * 100

    downtime_count = len(downtime_df)
    mtbf = operating_time / downtime_count if downtime_count > 0 else pd.Timedelta(0)
    mttr = unplanned_stop_time / downtime_count if downtime_count > 0 else pd.Timedelta(0)

    # Quality rate per tool ID
    quality_by_tool = {}
    for tool_id in [1, 2]:
        tool_df = df[df["Tool ID"] == tool_id]
        total = len(tool_df)
        ok_count = (tool_df["Compliance"] == "OK").sum()
        quality_by_tool[f"quality_rate_tool_{tool_id}"] = round(((ok_count / total) * 100), 2) if total > 0 else 0

    return {
        "opening_time": str(opening_time),
        "required_time": str(required_time),
        "unplanned_stop_time": str(unplanned_stop_time),
        "operating_time": str(operating_time),
        "net_time": str(net_time),
        "useful_time": str(useful_time),
        "quality_rate": round(quality_rate, 2),
        **quality_by_tool,
        "operating_rate": round(operating_rate, 2),
        "availability_rate": round(availability_rate, 2),
        "OEE": round(OEE, 2),
        "MTBF": str(mtbf),
        "MTTR": str(mttr)
    }

async def fetch_issues(raw_data):
    df = pd.DataFrame(raw_data)
    issues = df[df["Event"] == "Machine Error"]
    selected_issues = issues[
        ["Timestamp", "Event", "Error Code", "Error Description", "Downtime Start", "Downtime End"]
    ]
    return selected_issues