Skip to content

Commit 089b70c

Browse files
author
Vivek Patel
committed
Added History Functionality
1 parent 614611a commit 089b70c

File tree

11 files changed

+359
-97
lines changed

11 files changed

+359
-97
lines changed

models/table.js

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component, useState } from "react";
1+
import React, { Component } from "react";
22
import {
33
StyleSheet,
44
View,
@@ -9,7 +9,9 @@ import {
99
Switch,
1010
} from "react-native";
1111

12+
import AsyncStorage from "@react-native-async-storage/async-storage";
1213
import { Table, TableWrapper, Row, Cell } from "react-native-table-component";
14+
1315
export default class InputTable extends Component {
1416
constructor(props) {
1517
super(props);
@@ -33,6 +35,7 @@ export default class InputTable extends Component {
3335
isAnimationDone: false,
3436
isIoEnabled: false,
3537
time_Quantam: 2, // Time Quantam , Initially '2' (Added for Round robin Algorithm)
38+
from_history: false, // If Input Data comes from history
3639
};
3740
this.addProcess = this.addProcess.bind(this);
3841
this.deleteProcess = this.deleteProcess.bind(this);
@@ -43,7 +46,101 @@ export default class InputTable extends Component {
4346
this.timeQuantamTextInput = this.timeQuantamTextInput.bind(this);
4447
this.toggleButton = this.toggleButton.bind(this);
4548
}
49+
50+
checkForStorageKey = async () => {
51+
try {
52+
const jsonValue = await AsyncStorage.getItem("storage_key");
53+
54+
if (jsonValue != null) {
55+
} else {
56+
AsyncStorage.setItem("storage_key", JSON.stringify(0));
57+
}
58+
} catch (e) {
59+
console.log("Couldn't Check For The Storage_Key Value");
60+
console.log(e);
61+
}
62+
};
63+
64+
componentDidMount() {
65+
if (this.props.from_history == true) {
66+
// If the Input comes from History
67+
var history_data = this.props.inpt_data;
68+
var state = this.state;
69+
state.tableData = history_data["tableData"];
70+
state.tableHead = history_data["tableHead"];
71+
state.totalProcess = history_data["totalProcess"];
72+
state.processes = history_data["processes"];
73+
state.from_history = true;
74+
state.isIoEnabled = history_data["isIoEnabled"];
75+
state.time_Quantam = history_data["time_Quantam"];
76+
this.setState({ state });
77+
// console.log(this.state);
78+
}
79+
this.checkForStorageKey(); // If there is not storage key Place a storage Key
80+
}
81+
getDate = () => {
82+
let today = new Date();
83+
var date =
84+
today.getFullYear() +
85+
"-" +
86+
(today.getMonth() + 1) +
87+
"-" +
88+
today.getDate();
89+
var time =
90+
today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
91+
today = date + " @ " + time;
92+
93+
return today;
94+
};
95+
96+
storeData = async (id, data) => {
97+
var inpt_data = data;
98+
if (id != "storage_key") {
99+
inpt_data = { ...inpt_data, date: this.getDate() };
100+
}
101+
102+
try {
103+
const jsonValue = id != "storage_key" ? JSON.stringify(inpt_data) : data;
104+
// console.log(jsonValue);
105+
await AsyncStorage.setItem(id, jsonValue);
106+
// return jsonValue;
107+
} catch (e) {
108+
console.log("Couldn't Store the Value");
109+
console.log(e);
110+
}
111+
};
112+
46113
getAnswer = () => {
114+
if (this.props.from_history != true) {
115+
var inpt_data = {
116+
algorithm: this.props.algorithm,
117+
totalProcess: this.state.totalProcess,
118+
tableHead: this.state.tableHead,
119+
tableData: this.state.tableData,
120+
processes: this.state.processes,
121+
isIoEnabled: this.state.isIoEnabled,
122+
time_Quantam: this.state.time_Quantam,
123+
};
124+
var current_storage_key;
125+
126+
AsyncStorage.getItem("storage_key", (err, value) => {
127+
if (err) {
128+
console.log(err);
129+
} else {
130+
current_storage_key = JSON.parse(value);
131+
inpt_data = { ...inpt_data, date: this.getDate() }; // Store the Date
132+
AsyncStorage.setItem(
133+
current_storage_key.toString(),
134+
JSON.stringify(inpt_data)
135+
); // Store the Data
136+
AsyncStorage.setItem(
137+
"storage_key",
138+
JSON.stringify(current_storage_key + 1)
139+
); // Update the Storage Key
140+
}
141+
});
142+
}
143+
47144
(this.state.currentCpuPID = "IDLE"),
48145
(this.state.currentQueuePID = []),
49146
(this.state.currentSecond = 0),
@@ -52,14 +149,15 @@ export default class InputTable extends Component {
52149
this.state.avgtat = parseFloat(this.state.avgtat).toFixed(2);
53150
this.state.avgwaiting = parseFloat(this.state.avgwaiting).toFixed(2);
54151
};
152+
55153
showAnimation() {
56154
setTimeout(() => {
57155
let total_seconds = this.state.ganntChartArray.length;
58156
if (total_seconds == this.state.currentSecond) {
59157
this.state.isAnimationDone = true;
60158
}
61159

62-
console.log(this.state.currentSecond);
160+
// console.log(this.state.currentSecond);
63161
var temp_cpu_id = this.state.ganntChartArray[this.state.currentSecond];
64162
if (temp_cpu_id == "/") temp_cpu_id = "IDLE";
65163
this.setState({
@@ -71,9 +169,11 @@ export default class InputTable extends Component {
71169
});
72170
}, 2000);
73171
}
172+
74173
animationCompleted() {
75174
alert("Process Completed!");
76175
}
176+
77177
ganntChart() {
78178
if (this.state.ganntChartArray.length == 0) {
79179
alert("No Processes!");
@@ -481,6 +581,7 @@ export default class InputTable extends Component {
481581
</View>
482582
);
483583
}
584+
484585
showAnswer() {
485586
if (this.state.gotAnswer == true) {
486587
return (
@@ -540,6 +641,7 @@ export default class InputTable extends Component {
540641
);
541642
}
542643
}
644+
543645
addProcess() {
544646
const state = this.state;
545647
(state.currentCpuPID = "IDLE"),
@@ -558,6 +660,7 @@ export default class InputTable extends Component {
558660
this.state.ganntChartArray = [];
559661
this.setState({ state });
560662
}
663+
561664
deleteProcess() {
562665
const state = this.state;
563666
if (state.totalProcess == -1) {
@@ -576,6 +679,7 @@ export default class InputTable extends Component {
576679
this.state.ganntChartArray = [];
577680
this.setState({ state });
578681
}
682+
579683
toggleButton() {
580684
const toggleSwitch = () => {
581685
var isIoEnabled = this.state.isIoEnabled;
@@ -614,6 +718,7 @@ export default class InputTable extends Component {
614718
}
615719
}
616720
totalProcess = tableData.length - 1;
721+
// processes = ["P0"];
617722
this.setState({
618723
isIoEnabled,
619724
tableHead,
@@ -691,7 +796,11 @@ export default class InputTable extends Component {
691796
const element = (index, cellIndex) => (
692797
<TextInput
693798
editable={true}
694-
placeholder={"edit"}
799+
placeholder={
800+
this.state.from_history
801+
? this.state.tableData[index][cellIndex].toString()
802+
: "edit"
803+
}
695804
onChangeText={(text) => (this.state.tableData[index][cellIndex] = text)}
696805
keyboardType="number-pad"
697806
style={{

routes/homeStack.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ljf from "../screens/ljf";
99
import lrtf from "../screens/lrtf";
1010
import prio_p from "../screens/prio_p";
1111
import prio_np from "../screens/prio_np";
12+
import history from "../models/history";
1213
const screens = {
1314
"CPU Scheduling Algorithms": {
1415
screen: home,
@@ -37,6 +38,9 @@ const screens = {
3738
"PRIORITY SCHEDULING(NP)": {
3839
screen: prio_np,
3940
},
41+
"Input History": {
42+
screen: history,
43+
},
4044
};
4145
const screenStack = createStackNavigator(screens);
4246
export default createAppContainer(screenStack);

screens/fcfs.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class Fcfs extends InputTable {
1010
var newState = state;
1111
var tuple = [];
1212
var n = state.tableData.length;
13-
console.log(state.tableData);
13+
// console.log(state.tableData);
1414
for (let i = 0; i < n; i++) {
1515
var tempPid = state.tableData[i][0].substring(1);
1616
tempPid = parseInt(tempPid) + 1;
@@ -23,7 +23,7 @@ export default class Fcfs extends InputTable {
2323
});
2424
// console.log(tuple);
2525
}
26-
console.log(tuple);
26+
// console.log(tuple);
2727

2828
// var tuple = [
2929
// {pid:1,bt1:6,art:0,io:10,bt2:4},
@@ -129,10 +129,10 @@ export default class Fcfs extends InputTable {
129129
}
130130
}
131131
}
132-
console.log(que.length);
133-
for (var i = 0; i < 50; i++) {
134-
console.log(i + " " + final_ans[i]);
135-
}
132+
// console.log(que.length);
133+
// for (var i = 0; i < 50; i++) {
134+
// console.log(i + " " + final_ans[i]);
135+
// }
136136
var cmp_time = [];
137137
for (var i = 0; i < tuple.length; i++) {
138138
cmp_time[i] = -1;
@@ -172,8 +172,8 @@ export default class Fcfs extends InputTable {
172172
if (final_ans[i] != "/") break;
173173
final_ans.pop();
174174
}
175-
console.log(total_wt / n + " " + total_tat / n);
176-
console.log(que);
175+
// console.log(total_wt / n + " " + total_tat / n);
176+
// console.log(que);
177177
newState.queueAnimationArray = que;
178178
newState.tatarr = tat;
179179
newState.waitingarr = wt;
@@ -186,6 +186,7 @@ export default class Fcfs extends InputTable {
186186
this.setState({ newState });
187187
};
188188
getAnswer = (state) => {
189+
// console.log(state);
189190
if (state.isIoEnabled) {
190191
this.getIoEnabledAnswer(state);
191192
return;
@@ -295,10 +296,10 @@ export default class Fcfs extends InputTable {
295296
}
296297
}
297298
}
298-
console.log(que.length);
299-
for (var i = 0; i < 50; i++) {
300-
console.log(final_ans[i]);
301-
}
299+
// console.log(que.length);
300+
// for (var i = 0; i < 50; i++) {
301+
// console.log(final_ans[i]);
302+
// }
302303
var cmp_time = [];
303304
for (var i = 0; i < tuple.length; i++) {
304305
cmp_time[i] = -1;
@@ -336,8 +337,8 @@ export default class Fcfs extends InputTable {
336337
if (final_ans[i] != "/") break;
337338
final_ans.pop();
338339
}
339-
console.log(total_wt / n + " " + total_tat / n);
340-
console.log(que);
340+
// console.log(total_wt / n + " " + total_tat / n);
341+
// console.log(que);
341342
// var que = [];
342343
// for (let i = 0; i < 50; i++) {
343344
// que.push(["-Dummy"]);
@@ -355,6 +356,25 @@ export default class Fcfs extends InputTable {
355356
this.setState({ newState });
356357
};
357358
render() {
358-
return <InputTable onPress={(state) => this.getAnswer(state)} />;
359+
if (
360+
typeof this.props.navigation.state.params["from_history"] != "undefined"
361+
) {
362+
return (
363+
<InputTable
364+
onPress={(state) => this.getAnswer(state)}
365+
from_history={true}
366+
inpt_data={this.props.navigation.state.params}
367+
algorithm={"FCFS Algorithm"}
368+
/>
369+
);
370+
} else {
371+
return (
372+
<InputTable
373+
onPress={(state) => this.getAnswer(state)}
374+
from_history={false}
375+
algorithm={"FCFS Algorithm"}
376+
/>
377+
);
378+
}
359379
}
360380
}

0 commit comments

Comments
 (0)