1
|
//
|
2
|
// Author: Eliska Mourycova
|
3
|
//
|
4
|
|
5
|
using ServerApp.DataDownload;
|
6
|
using ServerApp.Predictor;
|
7
|
using System;
|
8
|
using System.Collections.Generic;
|
9
|
using System.Linq;
|
10
|
using System.Text;
|
11
|
using System.Threading.Tasks;
|
12
|
|
13
|
namespace ServerApp.User
|
14
|
{
|
15
|
|
16
|
/// <summary>
|
17
|
/// This class tkes care of parsing and executing admin commands
|
18
|
/// </summary>
|
19
|
public class CommandsAcceptor
|
20
|
{
|
21
|
// the DataDownloader instance
|
22
|
private DataDownloader dd;
|
23
|
|
24
|
// the predictor instance
|
25
|
private IPredictionController model;
|
26
|
|
27
|
public CommandsAcceptor(DataDownloader dd, IPredictionController controller)
|
28
|
{
|
29
|
this.dd = dd;
|
30
|
this.model = controller;
|
31
|
}
|
32
|
|
33
|
/// <summary>
|
34
|
/// Prints a prompt and waits for input
|
35
|
/// </summary>
|
36
|
public void AcceptCommand()
|
37
|
{
|
38
|
while (true) {
|
39
|
Console.Write(">");
|
40
|
string command = Console.ReadLine();
|
41
|
if(command != null && command.Length > 0)
|
42
|
{
|
43
|
Console.WriteLine("received admin command: " + command);
|
44
|
Command c = ParseString(command);
|
45
|
CheckCommand(c);
|
46
|
}
|
47
|
|
48
|
}
|
49
|
}
|
50
|
|
51
|
/// <summary>
|
52
|
/// Parses the input string
|
53
|
/// </summary>
|
54
|
/// <param name="str">The whole string typed in by the user</param>
|
55
|
/// <returns>A new instance of the Command class</returns>
|
56
|
private Command ParseString(string str)
|
57
|
{
|
58
|
|
59
|
string[] splits = str.Trim().Split(' ');
|
60
|
Command command = new Command();
|
61
|
|
62
|
command.WholeCommand = str.Trim();
|
63
|
command.MainCommand = splits[0];
|
64
|
|
65
|
for(int i = 1; i < splits.Length; i++)
|
66
|
{
|
67
|
|
68
|
string current = splits[i];
|
69
|
if (current.StartsWith("-"))
|
70
|
{
|
71
|
command.AddFlag(current.Substring(1));
|
72
|
}
|
73
|
else
|
74
|
{
|
75
|
command.AddData(current);
|
76
|
}
|
77
|
}
|
78
|
|
79
|
return command;
|
80
|
}
|
81
|
|
82
|
/// <summary>
|
83
|
/// Checks the comamnd and decides what to do according to the main command
|
84
|
/// </summary>
|
85
|
/// <param name="c">The command</param>
|
86
|
private void CheckCommand(Command c)
|
87
|
{
|
88
|
|
89
|
switch (c.MainCommand)
|
90
|
{
|
91
|
case "data":
|
92
|
HandleDataCommand(c);
|
93
|
break;
|
94
|
case "model":
|
95
|
HandleModelCommand(c);
|
96
|
break;
|
97
|
case "help":
|
98
|
HandleHelpCommand(c);
|
99
|
break;
|
100
|
default:
|
101
|
PrintUnknown(c.MainCommand);
|
102
|
break;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
/// <summary>
|
107
|
/// Prints to the console, that the given command is unknown
|
108
|
/// </summary>
|
109
|
/// <param name="command">The input command</param>
|
110
|
private void PrintUnknown(string command)
|
111
|
{
|
112
|
Console.WriteLine("Unknown command: " + command);
|
113
|
}
|
114
|
|
115
|
/// <summary>
|
116
|
/// Handles the command linked to data listing and downloading
|
117
|
/// </summary>
|
118
|
/// <param name="c">The command</param>
|
119
|
private void HandleDataCommand(Command c)
|
120
|
{
|
121
|
// data -list
|
122
|
// data -dl 1-2019 12-2020
|
123
|
|
124
|
if(c.WholeCommand.Equals("data -list"))
|
125
|
{
|
126
|
Console.WriteLine("Listing all downloaded data files...");
|
127
|
|
128
|
var subdirs = dd.DataSubDirectories.Values.ToArray();
|
129
|
|
130
|
foreach (string sub in subdirs)
|
131
|
{
|
132
|
List<string> files = dd.GetData(sub, null, null);
|
133
|
foreach (string f in files)
|
134
|
{
|
135
|
Console.WriteLine(f);
|
136
|
}
|
137
|
}
|
138
|
}
|
139
|
else if (c.FlagsAndData.ContainsKey("dl"))
|
140
|
{
|
141
|
List<string> dates = c.FlagsAndData["dl"];
|
142
|
if (dates.Count != 2)
|
143
|
{
|
144
|
Console.WriteLine("2 date arguments needed!");
|
145
|
return;
|
146
|
}
|
147
|
|
148
|
Date start = Date.ParseDate(dates[0]);
|
149
|
Date end = Date.ParseDate(dates[1]);
|
150
|
|
151
|
if (start == null || end == null)
|
152
|
{
|
153
|
Console.WriteLine("Date parsing was not successful. Please ensure you enter the dates in the following format: [month]-[year] (e.g. 1-2020). Month must be greater than zero.");
|
154
|
return;
|
155
|
}
|
156
|
|
157
|
Console.WriteLine("Downloading jis, login and weather data from " + start.ToString() + " to " + end.ToString() + "...");
|
158
|
|
159
|
try
|
160
|
{
|
161
|
List<string> files = dd.DownloadData(DataType.JIS, DataFormat.CSV, start, end);
|
162
|
files.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, start, end));
|
163
|
files.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, start, end));
|
164
|
|
165
|
Console.WriteLine("Data download complete, saved files: ");
|
166
|
foreach (string f in files)
|
167
|
Console.WriteLine(f);
|
168
|
|
169
|
}
|
170
|
catch (Exception)
|
171
|
{
|
172
|
Console.WriteLine("Data download failed, please ensure the start date is before or same as end date.");
|
173
|
return;
|
174
|
}
|
175
|
|
176
|
|
177
|
}
|
178
|
else
|
179
|
{
|
180
|
PrintUnknown(c.WholeCommand);
|
181
|
HandleHelpCommand(c);
|
182
|
}
|
183
|
}
|
184
|
|
185
|
/// <summary>
|
186
|
/// Handles the command linked to predictor training, rollback and files listing
|
187
|
/// </summary>
|
188
|
/// <param name="c">The command</param>
|
189
|
private void HandleModelCommand(Command c)
|
190
|
{
|
191
|
|
192
|
// todo if the command has something else after the flag - abort?
|
193
|
if (c.FlagsAndData.ContainsKey("files"))
|
194
|
{
|
195
|
IEnumerable<string> files = model.GetDataFileNames();
|
196
|
Console.WriteLine("Listing current model's trianing data...");
|
197
|
|
198
|
foreach (string f in files)
|
199
|
Console.WriteLine(f);
|
200
|
}
|
201
|
else if (c.FlagsAndData.ContainsKey("retrain"))
|
202
|
{
|
203
|
Console.WriteLine("Starting to retrain the predictor...");
|
204
|
model.Train();
|
205
|
Console.WriteLine("Predictor retrained.");
|
206
|
}
|
207
|
else if (c.FlagsAndData.ContainsKey("rollback"))
|
208
|
{
|
209
|
Console.WriteLine("Switching back to the previous version of the predictor...");
|
210
|
int s = model.Rollback();
|
211
|
switch (s)
|
212
|
{
|
213
|
case 0:
|
214
|
Console.WriteLine("Rollback succesful.");
|
215
|
break;
|
216
|
case 1:
|
217
|
Console.WriteLine("No previous version of the predictor exists!");
|
218
|
break;
|
219
|
case 2:
|
220
|
Console.WriteLine("Error occured while trying to perform rollback - corrupted or removed Predictor.config file!");
|
221
|
break;
|
222
|
default:
|
223
|
Console.WriteLine("Unknown error occured when performing rollback.");
|
224
|
break;
|
225
|
}
|
226
|
}
|
227
|
else
|
228
|
{
|
229
|
PrintUnknown(c.WholeCommand);
|
230
|
HandleHelpCommand(c);
|
231
|
}
|
232
|
|
233
|
|
234
|
//model.Rollback() // 0 -ok, 1 nejde, 2 - error (kdyz nekdo smaze soubor config)
|
235
|
//model.GetDataFileNames();
|
236
|
}
|
237
|
|
238
|
/// <summary>
|
239
|
/// Prints help to the console
|
240
|
/// </summary>
|
241
|
/// <param name="c"></param>
|
242
|
private void HandleHelpCommand(Command c)
|
243
|
{
|
244
|
Console.WriteLine("List of available commands and their description: ");
|
245
|
Console.WriteLine("--------------------------------------------------");
|
246
|
// data
|
247
|
Console.WriteLine("data [OPTION] [DATE]...");
|
248
|
Console.WriteLine(" Description: Capable of showing saved data files and downloading new ones.");
|
249
|
Console.WriteLine(" -list: Lists currently saved data files. This list does not have to match the files which any of the predictors is trained on!");
|
250
|
Console.WriteLine(" Example: data -list");
|
251
|
Console.WriteLine(" -dl [startDate] [endDate]: Downloads jis, login and weather files from a specified time span. Both [startDate] and [endDate] are inclusive. Date format must be entered as [month]-[year].");
|
252
|
Console.WriteLine(" Example: data -dl 1-2019 12-2020");
|
253
|
Console.WriteLine("");
|
254
|
|
255
|
// model
|
256
|
Console.WriteLine("model [OPTION]");
|
257
|
Console.WriteLine(" Description: Capable of retraining the prediction model, going back to the previous version and listing files on which the predictor was trained. ");
|
258
|
Console.WriteLine(" -files: Lists the data files on which the currently used model was trained.");
|
259
|
Console.WriteLine(" Example: model -files");
|
260
|
Console.WriteLine(" -retrain: Retrains the model using the files currently saved in the data directory.");
|
261
|
Console.WriteLine(" Example: model -retrain");
|
262
|
Console.WriteLine(" -rollback: Switches the model back to the previous version.");
|
263
|
Console.WriteLine(" Example: model -rollback");
|
264
|
}
|
265
|
|
266
|
}
|
267
|
}
|