Projekt

Obecné

Profil

Stáhnout (5.03 KB) Statistiky
| Větev: | Tag: | Revize:
1 40f56e57 Eliška Mourycová
//
2
//	Author: Eliska Mourycova
3
//
4
5
using ServerApp.DataDownload;
6 c4383c00 Eliška Mourycová
using ServerApp.Predictor;
7
using System;
8 4a417b8b Eliška Mourycová
using System.Collections.Generic;
9
using System.Linq;
10
using System.Text;
11
using System.Threading.Tasks;
12
13
namespace ServerApp.User
14
{
15 4129ce12 Eliška Mourycová
	
16
17 4a417b8b Eliška Mourycová
	public class CommandsAcceptor
18
	{
19 c4383c00 Eliška Mourycová
		private DataDownloader dd;
20
		private IPredictionController model;
21 4a417b8b Eliška Mourycová
22 c4383c00 Eliška Mourycová
		public CommandsAcceptor(DataDownloader dd, IPredictionController controller)
23
		{
24
			this.dd = dd;
25
			this.model = controller;
26
		}
27
28
		public void AcceptCommand()
29 4a417b8b Eliška Mourycová
		{
30
			while (true) {
31 5cb4249a Eliška Mourycová
				Console.Write(">");
32 4a417b8b Eliška Mourycová
				string command = Console.ReadLine();
33 cf35739b Eliška Mourycová
				if(command != null && command.Length > 0)
34 5cb4249a Eliška Mourycová
				{
35
					Console.WriteLine("received admin command: " + command);
36
					Command c = ParseString(command);
37
					CheckCommand(c);
38
					//Program.NotifyUserMessage(command);
39
				}
40
41 4a417b8b Eliška Mourycová
			}
42
		}
43 4129ce12 Eliška Mourycová
44
		private static Command ParseString(string str)
45
		{
46 c4383c00 Eliška Mourycová
47
			string[] splits = str.Trim().Split(' ');
48 4129ce12 Eliška Mourycová
			Command command = new Command();
49
50 c4383c00 Eliška Mourycová
			command.WholeCommand = str.Trim();
51 4129ce12 Eliška Mourycová
			command.MainCommand = splits[0];
52
53
			for(int i = 1; i < splits.Length; i++)
54
			{
55
56
				string current = splits[i];
57
				if (current.StartsWith("-"))
58
				{
59
					command.AddFlag(current.Substring(1));
60
				}
61
				else
62
				{
63
					command.AddData(current);
64
				}
65
			}
66
67
			return command;
68
		}
69
70 c4383c00 Eliška Mourycová
		private void CheckCommand(Command c)
71 4129ce12 Eliška Mourycová
		{
72
73
			switch (c.MainCommand)
74
			{
75 c4383c00 Eliška Mourycová
				case "data":
76
					HandleDataCommand(c);
77
					break;
78
				case "model":
79
					HandleModelCommand(c);
80
					break;
81
				case "help":
82
					HandleHelpCommand(c);
83
					break;
84
				default:
85
					PrintUnknown(c.MainCommand);
86
					break;
87 4129ce12 Eliška Mourycová
			}
88
		}
89
90 c4383c00 Eliška Mourycová
		private void PrintUnknown(string command)
91
		{
92
			Console.WriteLine("Unknown command: " + command);
93
		}
94
95
		private void HandleDataCommand(Command c)
96 4129ce12 Eliška Mourycová
		{
97 c4383c00 Eliška Mourycová
			// data -list
98
			// data -dl 1-2019 12-2020
99
100
			if(c.WholeCommand.Equals("data -list"))
101
			{
102
				Console.WriteLine("Listing all downloaded data files...");
103 40f56e57 Eliška Mourycová
104
				var subdirs = dd.DataSubDirectories.Values.ToArray();
105
106
				foreach (string sub in subdirs)
107
				{
108
					List<string> files = dd.GetData(sub, null, null);
109
					foreach (string f in files)
110
					{
111
						Console.WriteLine(f);
112
					}
113
				}
114 c4383c00 Eliška Mourycová
			}
115
			else if (c.FlagsAndData.ContainsKey("dl"))
116
			{
117
				List<string> dates = c.FlagsAndData["dl"];
118
				if (dates.Count != 2)
119
				{
120
					Console.WriteLine("2 date arguments needed!");
121
					return;
122
				}
123
124
				Date start = Date.ParseDate(dates[0]);
125
				Date end = Date.ParseDate(dates[1]);
126 4129ce12 Eliška Mourycová
127 c4383c00 Eliška Mourycová
				if (start == null || end == null)
128
				{
129 40f56e57 Eliška Mourycová
					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.");
130 c4383c00 Eliška Mourycová
					return;
131
				}
132
133 40f56e57 Eliška Mourycová
				Console.WriteLine("Downloading jis, login and weather data from " + start.ToString() + " to " + end.ToString() + "...");
134
135
				try
136
				{
137
					List<string> files = dd.DownloadData(DataType.JIS, DataFormat.CSV, start, end);
138
					files.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, start, end));
139
					files.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, start, end));
140
141
					Console.WriteLine("Data download complete, saved files: ");
142
					foreach (string f in files)
143
						Console.WriteLine(f);
144
145
				}
146
				catch(Exception ex)
147
				{
148
					Console.WriteLine("Data download failed, please ensure the start date is before or same as end date.");
149
					return;
150
				}
151
				
152
				
153 c4383c00 Eliška Mourycová
			}
154
		}
155
156
		private void HandleModelCommand(Command c)
157
		{
158 40f56e57 Eliška Mourycová
			// TODO:
159 c4383c00 Eliška Mourycová
			// model -files
160
			// model -retrain
161
			// model -rollback
162
		}
163
164
		private void HandleHelpCommand(Command c)
165
		{
166 40f56e57 Eliška Mourycová
			Console.WriteLine("List of available commands and their description: ");
167
			Console.WriteLine("--------------------------------------------------");
168
			// data
169
			Console.WriteLine("data [OPTION] [DATE]...");
170
			Console.WriteLine("   Description: Capable of showing saved data files and downloading new ones.");
171
			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!");
172
			Console.WriteLine("      Example: data -list");
173
			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].");
174
			Console.WriteLine("      Example: data -dl 1-2019 12-2020");
175
			Console.WriteLine("");
176
177
			// model
178
			Console.WriteLine("model [OPTION]");
179
			Console.WriteLine("   Description: Capable of retraining the prediction model, going back to the previous version and listing files on which the predictor was trained. ");
180
			Console.WriteLine("   -files: Lists the data files on which the currently used model was trained.");
181
			Console.WriteLine("      Example: model -files");
182
			Console.WriteLine("   -retrain: Retrains the model using the files currently saved in the data directory.");
183
			Console.WriteLine("      Example: model -retrain");
184
			Console.WriteLine("   -rollback: Switches the model back to the previous version.");
185
			Console.WriteLine("      Example: model -rollback");
186 c4383c00 Eliška Mourycová
		}
187
188
		private void PrintUsage(Command c)
189
		{
190
			
191 4129ce12 Eliška Mourycová
		}
192 4a417b8b Eliška Mourycová
	}
193
}