Projekt

Obecné

Profil

Stáhnout (4.95 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
				string command = Console.ReadLine();
32
				Console.WriteLine("received admin command: " + command);
33
				Command c = ParseString(command);
34
				CheckCommand(c);
35
				//Program.NotifyUserMessage(command);
36
			}
37
		}
38

    
39
		private static Command ParseString(string str)
40
		{
41

    
42
			string[] splits = str.Trim().Split(' ');
43
			Command command = new Command();
44

    
45
			command.WholeCommand = str.Trim();
46
			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
		private void CheckCommand(Command c)
66
		{
67

    
68
			switch (c.MainCommand)
69
			{
70
				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
			}
83
		}
84

    
85
		private void PrintUnknown(string command)
86
		{
87
			Console.WriteLine("Unknown command: " + command);
88
		}
89

    
90
		private void HandleDataCommand(Command c)
91
		{
92
			// 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

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

    
122
				if (start == null || end == null)
123
				{
124
					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
					return;
126
				}
127

    
128
				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
			}
149
		}
150

    
151
		private void HandleModelCommand(Command c)
152
		{
153
			// TODO:
154
			// model -files
155
			// model -retrain
156
			// model -rollback
157
		}
158

    
159
		private void HandleHelpCommand(Command c)
160
		{
161
			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
		}
182

    
183
		private void PrintUsage(Command c)
184
		{
185
			
186
		}
187
	}
188
}
(2-2/2)