Projekt

Obecné

Profil

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