File size: 5,947 Bytes
60b2d8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { ChartConfig } from "@/components/ui/chart";

// Define types for different chart data structures
export interface LineChartData {
  name: string;
  value: number;
}

export interface BarChartData {
  name: string;
  [key: string]: number | string;
}

export interface PieChartData {
  name: string;
  value: number;
}

// Generate random number within a range
export const randomInRange = (min: number, max: number, decimals = 0): number => {
  const value = Math.random() * (max - min) + min;
  const factor = Math.pow(10, decimals);
  return Math.round(value * factor) / factor;
};

// Generate random array of numbers within a range
export const randomArrayInRange = (
  min: number,
  max: number,
  length: number,
  decimals = 0
): number[] => {
  return Array.from({ length }, () => randomInRange(min, max, decimals));
};

// Create random trend (up, down, or stable with small variations)
export const generateTrend = (
  startValue: number,
  points: number,
  volatility: number,
  trend: "up" | "down" | "stable" = "stable",
  min = 0,
  max = 100
): number[] => {
  const data: number[] = [startValue];
  
  for (let i = 1; i < points; i++) {
    let trendFactor = 0;
    
    if (trend === "up") {
      trendFactor = volatility * 0.7;
    } else if (trend === "down") {
      trendFactor = -volatility * 0.7;
    }
    
    const randomChange = randomInRange(-volatility, volatility, 2);
    let newValue = data[i - 1] + randomChange + trendFactor;
    
    // Ensure the value stays within the specified range
    newValue = Math.max(min, Math.min(max, newValue));
    
    data.push(Number(newValue.toFixed(2)));
  }
  
  return data;
};

// Generate production metrics data
export const generateProductionMetrics = (): BarChartData[] => {
  const machines = ["Assembly", "Packaging", "Testing", "Processing", "Welding"];
  
  return machines.map(machine => {
    return {
      name: machine,
      efficiency: randomInRange(65, 95),
      output: randomInRange(70, 98),
      quality: randomInRange(85, 99)
    };
  });
};

// Generate energy consumption data
export const generateEnergyData = (): LineChartData[] => {
  const hours = Array.from({ length: 24 }, (_, i) => `${i}:00`);
  
  return hours.map(hour => {
    // Higher consumption during working hours (8-18)
    const hourNum = parseInt(hour);
    let baseValue = 30;
    
    if (hourNum >= 8 && hourNum <= 18) {
      baseValue = 70;
    } else if ((hourNum >= 6 && hourNum < 8) || (hourNum > 18 && hourNum <= 20)) {
      baseValue = 50;
    }
    
    return {
      name: hour,
      value: randomInRange(baseValue - 15, baseValue + 15)
    };
  });
};

// Generate quality metrics data
export const generateQualityMetrics = (): PieChartData[] => {
  const passed = randomInRange(90, 98);
  
  return [
    { name: "Passed", value: passed },
    { name: "Minor Issues", value: randomInRange(1, Math.floor((100 - passed) * 0.7)) },
    { name: "Major Issues", value: 100 - passed - randomInRange(1, Math.floor((100 - passed) * 0.3)) }
  ];
};

// Generate blockchain transaction data
export const generateBlockchainTransactions = (count: number = 5) => {
  const types = [
    "Product Verification",
    "Machine Maintenance",
    "Batch Production",
    "Quality Control",
    "Material Receipt",
    "Operator Authentication",
    "Process Change"
  ];
  
  const timestamps = [
    "Just now",
    "2 mins ago",
    "5 mins ago",
    "12 mins ago",
    "27 mins ago",
    "1 hour ago",
    "2 hours ago",
    "5 hours ago"
  ];
  
  const generateId = () => {
    const hex = "0123456789abcdef";
    let id = "0x";
    for (let i = 0; i < 4; i++) {
      id += hex[Math.floor(Math.random() * 16)];
    }
    id += "...";
    for (let i = 0; i < 4; i++) {
      id += hex[Math.floor(Math.random() * 16)];
    }
    return id;
  };
  
  const generateBlock = () => {
    return `#${(14_500_000 + Math.floor(Math.random() * 100_000)).toLocaleString()}`;
  };
  
  return Array.from({ length: count }, (_, i) => {
    return {
      id: generateId(),
      type: types[Math.floor(Math.random() * types.length)],
      timestamp: timestamps[Math.min(i, timestamps.length - 1)],
      status: Math.random() > 0.1 ? "Confirmed" : "Pending",
      block: generateBlock()
    };
  });
};

// Generate KPI data
export const generateKpiData = () => {
  return {
    oee: {
      value: `${randomInRange(70, 85, 1)}%`,
      trend: randomInRange(-3, 5, 1)
    },
    productionOrders: {
      value: String(randomInRange(10, 20)),
      trend: randomInRange(-2, 3),
      description: `${randomInRange(1, 5)} high priority`
    },
    connectedMachines: {
      value: String(randomInRange(25, 32)),
      trend: randomInRange(-1, 2),
      description: Math.random() > 0.9 ? `${randomInRange(1, 2)} offline` : "All machines online"
    },
    simulations: {
      value: String(randomInRange(2, 5)),
      trend: randomInRange(0, 2),
      description: `${randomInRange(1, 3)} optimizations running`
    }
  };
};

// Chart config for recharts
export const chartConfig: ChartConfig = {
  efficiency: {
    label: "Efficiency",
    theme: {
      light: "#10b981",
      dark: "#10b981"
    }
  },
  output: {
    label: "Output",
    theme: {
      light: "#3b82f6",
      dark: "#60a5fa"
    }
  },
  quality: {
    label: "Quality",
    theme: {
      light: "#8b5cf6",
      dark: "#a78bfa"
    }
  },
  energy: {
    label: "Energy",
    theme: {
      light: "#f59e0b",
      dark: "#fbbf24"
    }
  },
  temperature: {
    label: "Temperature",
    theme: {
      light: "#ef4444", 
      dark: "#f87171"
    }
  },
  Passed: {
    label: "Passed",
    theme: {
      light: "#22c55e",
      dark: "#4ade80"
    }
  },
  "Minor Issues": {
    label: "Minor Issues",
    theme: {
      light: "#f59e0b",
      dark: "#fbbf24"
    }
  },
  "Major Issues": {
    label: "Major Issues",
    theme: {
      light: "#ef4444",
      dark: "#f87171"
    }
  }
};