Projekt

Obecné

Profil

Stáhnout (5.03 KB) Statistiky
| Větev: | Tag: | Revize:
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

    
17
	public class CommandsAcceptor
18
	{
19
		private DataDownloader dd;
20
		private IPredictionController model;
21

    
22
		public CommandsAcceptor(DataDownloader dd, IPredictionController controller)
23
		{
24
			this.dd = dd;
25
			this.model = controller;
26
		}
27

    
28
		public void AcceptCommand()
29
		{
30
			while (true) {
31
				Console.Write(">");
32
				string command = Console.ReadLine();
33
				if(command != null && command.Length > 0)
34
				{
35
					Console.WriteLine("received admin command: " + command);
36
					Command c = ParseString(command);
37
					CheckCommand(c);
38
					//Program.NotifyUserMessage(command);
39
				}
40

    
41
			}
42
		}
43

    
44
		private static Command ParseString(string str)
45
		{
46

    
47
			string[] splits = str.Trim().Split(' ');
48
			Command command = new Command();
49

    
50
			command.WholeCommand = str.Trim();
51
			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
		private void CheckCommand(Command c)
71
		{
72

    
73
			switch (c.MainCommand)
74
			{
75
				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
			}
88
		}
89

    
90
		private void PrintUnknown(string command)
91
		{
92
			Console.WriteLine("Unknown command: " + command);
93
		}
94

    
95
		private void HandleDataCommand(Command c)
96
		{
97
			// 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

    
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
			}
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

    
127
				if (start == null || end == null)
128
				{
129
					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
					return;
131
				}
132

    
133
				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
			}
154
		}
155

    
156
		private void HandleModelCommand(Command c)
157
		{
158
			// TODO:
159
			// model -files
160
			// model -retrain
161
			// model -rollback
162
		}
163

    
164
		private void HandleHelpCommand(Command c)
165
		{
166
			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
		}
187

    
188
		private void PrintUsage(Command c)
189
		{
190
			
191
		}
192
	}
193
}
(2-2/2)