示例#1
0
        /// Metoda wywołująca filtrowanie danych dla listy
        /// </summary>
        /// <param name="data">Lista z danymi wejściowymi</param>
        /// <returns>Listę z przefiltrowanymi danymi</returns>
        public List <CallRecord> FilterRequests(List <CallRecord> data)
        {
            MessageController.FilterMenuMessage();

            ConsoleKeyInfo request        = Console.ReadKey();
            Stopwatch      stopWatch      = new Stopwatch();
            bool           correctRequest = true;

            var filteredData = SingleRequest(GetRequestType(request.Key), stopWatch, data, out correctRequest);

            stopWatch.Stop();
            if (MessageController.FilterDataMessage(filteredData.Count, stopWatch.Elapsed, correctRequest))
            {
                MessageController.ShowListRecords(filteredData);
            }

            if (filteredData.Count > 0)
            {
                MessageController.SaveFilterMessage();
                ConsoleKeyInfo keyPressed = Console.ReadKey();

                if (keyPressed.Key == ConsoleKey.S)
                {
                    SaveData.SaveToFile(filteredData);
                }

                MessageController.KeepFilteredData();
                keyPressed = Console.ReadKey();
                if (keyPressed.Key == ConsoleKey.T)
                {
                    return(filteredData);
                }
            }
            return(data);
        }
示例#2
0
        /// <summary>
        /// Wyświetla wiadomość o błędzie i oczekuje na podjęcie dalszych akcji użytkownika
        /// </summary>
        /// <param name="message">Wiadomość o błędzie</param>
        /// <returns>False jeśli działanie programu ma być kontynuowane, inaczej true</returns>
        private bool ErrorReturn(string message)
        {
            MessageController.ParseError(message);
            errMessage += message + " ";

            ConsoleKeyInfo keyPressed = Console.ReadKey();

            if (keyPressed.Key == ConsoleKey.T)
            {
                return(false);
            }
            else
            {
                Environment.Exit(0);
            }
            return(true);
        }
示例#3
0
        /// <summary>
        /// Parsuje dane z pliku
        /// </summary>
        /// <returns>True w przypadku poprawnego parsowania, inaczej false</returns>
        public bool Parse()
        {
            SavedDataRows = new List <CallRecord>();
            try
            {
                string[]          lines;
                List <CallRecord> TempDbRows = new List <CallRecord>();
                if (!File.Exists(this.filePath))
                {
                    return(ErrorReturn("Plik nie istnieje"));
                }

                lines = System.IO.File.ReadAllLines(this.filePath, Encoding.GetEncoding(949));

                if (lines.Length < 1)
                {
                    throw new Exception("Plik jest pusty");
                }

                long      i         = 1;
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
                foreach (string line in lines)
                {
                    bool parseError = false;
                    parseMessage = "";
                    string[] valueLineParts = line.Split(',');

                    if (valueLineParts.Length != dataInLineNumber)
                    {
                        MessageController.ParseError(line, i, "\tNieprawidłowa ilość danych w linii");
                        continue;
                    }

                    if (!Int32.TryParse(valueLineParts[0], out int callId))
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowe id\n";
                    }
                    if (!IsNumeric(valueLineParts[1]))
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowy numer dzwoniącego\n";
                    }
                    if (!Int32.TryParse(valueLineParts[2], out int callLine))
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowy numer linii\n";
                    }
                    if (!IsNumeric(valueLineParts[3]))
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowy numer odbierającego\n";
                    }
                    if (!DateTime.TryParseExact(valueLineParts[4], "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out DateTime dateStart))
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowa data rozpoczęcia rozmowy\n";
                    }
                    if (DateTime.TryParse(valueLineParts[6], out DateTime timeStart))
                    {
                        dateStart = new DateTime(dateStart.Year, dateStart.Month, dateStart.Day, timeStart.Hour, timeStart.Minute, timeStart.Second);
                    }
                    else
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowy czas rozpoczęcia rozmowy\n";
                    }
                    if (!DateTime.TryParseExact(valueLineParts[5], "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out DateTime dateEnd))
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowa data zakończenia rozmowy\n";
                    }
                    if (DateTime.TryParse(valueLineParts[7], out DateTime timeEnd))
                    {
                        dateEnd = new DateTime(dateEnd.Year, dateEnd.Month, dateEnd.Day, timeEnd.Hour, timeEnd.Minute, timeEnd.Second);
                    }
                    else
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowy czas zakończenia rozmowy\n";
                    }

                    if (valueLineParts[8] == "National" || valueLineParts[8] == "Mobile" || valueLineParts[8] == "Local" ||
                        valueLineParts[8] == "Intl" || valueLineParts[8] == "PRS" || valueLineParts[8] == "Free")
                    {
                    }
                    else
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowy typ połączenia\n";
                    }
                    if (!float.TryParse(valueLineParts[9], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out float callCharge))
                    {
                        parseError    = true;
                        parseMessage += "\tNieprawidłowa opłata za połączenie\n";
                    }

                    if (!parseError)
                    {
                        CallRecord record = new CallRecord()
                        {
                            PhoneNumber     = valueLineParts[1],
                            CallLine        = callLine,
                            DestPhoneNumber = valueLineParts[3],
                            CallStart       = dateStart,
                            CallEnd         = dateEnd,
                            CallType        = valueLineParts[8],
                            CallCharge      = callCharge
                        };

                        if (DB.useDb)
                        {
                            if (i % 2000 == 0)
                            {
                                DB.CallRecords.InsertMany(TempDbRows);
                                TempDbRows = new List <CallRecord>();
                            }
                            else
                            {
                                TempDbRows.Add(record);
                            }
                        }
                        else
                        {
                            SavedDataRows.Add(record);
                        }

                        if (i % 1000 == 0)
                        {
                            MessageController.LoadingDataCurrentCount(i, lines.Length);
                        }
                    }
                    else
                    {
                        MessageController.ParseError(line, i, parseMessage);
                    }
                    i++;
                }
                MessageController.LoadingDataCurrentCount(i - 1, lines.Length);
                stopWatch.Stop();
                MessageController.LoadingDataTime(stopWatch.Elapsed);
                return(true);
            }
            catch (Exception e)
            {
                return(ErrorReturn("Wyjątek:" + e.Message));
            }
        }
示例#4
0
        /// <summary>
        /// Obsługuje pojedyncze zapytanie do bazy danych
        /// </summary>
        /// <param name="requestType">Typ zapytania</param>
        /// <param name="stopWatch">Stoper odmierzający czas zapytania</param>
        /// <param name="correctRequest">Określa czy zapytanie było prawidłowe</param>
        /// <returns>Listę przefiltrowanych danych</returns>
        List <CallRecord> SingleRequest(string requestType, Stopwatch stopWatch, out bool correctRequest)
        {
            correctRequest = true;
            string requestMsg = "";

            requestType = requestType.ToUpper();
            MessageController.FilterInstructionMessage(requestType);
            switch (requestType)
            {
            case "DZWONIACY":
                requestMsg = Console.ReadLine();
                MessageController.WaitForQuery();
                stopWatch.Start();
                if (IsPhoneNumber(requestMsg))
                {
                    return(DB.CallRecords.Find(t => t.PhoneNumber == requestMsg).ToList());
                }
                return(new List <CallRecord>());

            case "ODBIERAJACY":
                requestMsg = Console.ReadLine();
                MessageController.WaitForQuery();
                stopWatch.Start();
                if (IsPhoneNumber(requestMsg))
                {
                    return(DB.CallRecords.Find(t => t.DestPhoneNumber == requestMsg).ToList());
                }
                return(new List <CallRecord>());

            case "ROZPOCZECIE":
                requestMsg = Console.ReadLine();
                string[] startDateLine = requestMsg.Split(' ');
                if (startDateLine.Count() >= 2)
                {
                    if (DateTime.TryParse(startDateLine[1], out DateTime startDate))
                    {
                        if (startDateLine.Count() == 2)
                        {
                            if (startDateLine[0] == "<")
                            {
                                MessageController.WaitForQuery();
                                stopWatch.Start();
                                return(DB.CallRecords.Find(t => t.CallStart < startDate).ToList());
                            }
                            else if (startDateLine[0] == "=")
                            {
                                MessageController.WaitForQuery();
                                stopWatch.Start();
                                return(DB.CallRecords.Find(t => t.CallStart == startDate).ToList());
                            }
                            else if (startDateLine[0] == ">")
                            {
                                MessageController.WaitForQuery();
                                stopWatch.Start();
                                return(DB.CallRecords.Find(t => t.CallStart > startDate).ToList());
                            }
                        }
                        else if (startDateLine.Count() == 3)
                        {
                            if (DateTime.TryParse(startDateLine[2], out DateTime startTime))
                            {
                                startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, startTime.Hour, startTime.Minute, startTime.Second);
                                if (startDateLine[0] == "<")
                                {
                                    MessageController.WaitForQuery();
                                    stopWatch.Start();
                                    return(DB.CallRecords.Find(t => t.CallStart < startDate).ToList());
                                }
                                else if (startDateLine[0] == "=")
                                {
                                    MessageController.WaitForQuery();
                                    stopWatch.Start();
                                    return(DB.CallRecords.Find(t => t.CallStart == startDate).ToList());
                                }
                                else if (startDateLine[0] == ">")
                                {
                                    MessageController.WaitForQuery();
                                    stopWatch.Start();
                                    return(DB.CallRecords.Find(t => t.CallStart > startDate).ToList());
                                }
                            }
                        }
                    }
                }
                MessageController.FilterErrorMessage();
                stopWatch.Start();
                return(new List <CallRecord>());

            case "ZAKONCZENIE":
                requestMsg = Console.ReadLine();
                string[] endDateLine = requestMsg.Split(' ');
                if (endDateLine.Count() >= 2)
                {
                    if (DateTime.TryParse(endDateLine[1], out DateTime endDate))
                    {
                        if (endDateLine.Count() == 2)
                        {
                            if (endDateLine[0] == "<")
                            {
                                MessageController.WaitForQuery();
                                stopWatch.Start();
                                return(DB.CallRecords.Find(t => t.CallEnd.Date < endDate).ToList());
                            }
                            else if (endDateLine[0] == "=")
                            {
                                MessageController.WaitForQuery();
                                stopWatch.Start();
                                return(DB.CallRecords.Find(t => t.CallEnd.Date == endDate).ToList());
                            }
                            else if (endDateLine[0] == ">")
                            {
                                MessageController.WaitForQuery();
                                stopWatch.Start();
                                return(DB.CallRecords.Find(t => t.CallEnd.Date > endDate).ToList());
                            }
                        }
                        else if (endDateLine.Count() == 3)
                        {
                            if (DateTime.TryParse(endDateLine[2], out DateTime endTime))
                            {
                                endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, endTime.Hour, endTime.Minute, endTime.Second);
                                if (endDateLine[0] == "<")
                                {
                                    MessageController.WaitForQuery();
                                    stopWatch.Start();
                                    return(DB.CallRecords.Find(t => t.CallEnd < endDate).ToList());
                                }
                                else if (endDateLine[0] == "=")
                                {
                                    MessageController.WaitForQuery();
                                    stopWatch.Start();
                                    return(DB.CallRecords.Find(t => t.CallEnd == endDate).ToList());
                                }
                                else if (endDateLine[0] == ">")
                                {
                                    MessageController.WaitForQuery();
                                    stopWatch.Start();
                                    return(DB.CallRecords.Find(t => t.CallEnd > endDate).ToList());
                                }
                            }
                        }
                    }
                }
                MessageController.FilterErrorMessage();
                stopWatch.Start();
                return(new List <CallRecord>());

            case "RODZAJ":
                ConsoleKeyInfo request = Console.ReadKey();
                MessageController.WaitForQuery();
                stopWatch.Start();
                if (GetCallType(request.Key) != "")
                {
                    return(DB.CallRecords.Find(t => t.CallType == GetCallType(request.Key)).ToList());
                }
                return(new List <CallRecord>());

            case "OPLATA":
                requestMsg = Console.ReadLine();
                string[] chargeLine = requestMsg.Split(' ');
                if (chargeLine.Count() == 2)
                {
                    if (float.TryParse(chargeLine[1], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out float chargeAmount))
                    {
                        if (chargeLine[0] == "<")
                        {
                            MessageController.WaitForQuery();
                            stopWatch.Start();
                            return(DB.CallRecords.Find(t => t.CallCharge < chargeAmount).ToList());
                        }
                        else if (chargeLine[0] == "=")
                        {
                            MessageController.WaitForQuery();
                            stopWatch.Start();
                            return(DB.CallRecords.Find(t => t.CallCharge == chargeAmount).ToList());
                        }
                        else if (chargeLine[0] == ">")
                        {
                            MessageController.WaitForQuery();
                            stopWatch.Start();
                            return(DB.CallRecords.Find(t => t.CallCharge > chargeAmount).ToList());
                        }
                    }
                }
                MessageController.FilterErrorMessage();
                stopWatch.Start();
                return(new List <CallRecord>());

            default:
                stopWatch.Start();
                correctRequest = false;
                return(new List <CallRecord>());
            }
        }
示例#5
0
        /// <summary>
        /// Główna metoda programu, uruchamia się jako pierwsza
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var parserController  = new ParserController();
            var requestController = new RequestController();

            parserController.SetFilePath("..\\..\\cdr_small.txt"); // ścieżka względna
            //parserController.SetFilePath("D:\\Adrian Dokumenty\\Studia\\III rok\\VI semestr\\Techniki multimedialne\\Projekt\\cdr_big.txt"); // ścieżka bezwzględna
            DB.useDb = MessageController.HelloMessage();

            if (DB.useDb)
            {
                if (DB.CallRecords.Count(new BsonDocument()) == 0)
                {
                    if (MessageController.StartLoadData())
                    {
                        parserController.Parse();
                    }
                }
            }
            else
            {
                parserController.Parse();
            }


            while (true)
            {
                MessageController.MainMenuMessage();
                ConsoleKeyInfo keyPressed = Console.ReadKey();

                if (keyPressed.Key == ConsoleKey.F)
                {
                    //Filtruj
                    List <CallRecord> request = new List <CallRecord>();
                    if (DB.useDb)
                    {
                        request = requestController.FilterRequests();
                    }
                    else
                    {
                        request = requestController.FilterRequests(parserController.SavedDataRows);
                    }

                    parserController.SavedDataRows = request;
                    parserController.SavedDataRows.TrimExcess();
                    GC.Collect();
                }

                if (keyPressed.Key == ConsoleKey.R)
                {
                    //Wczytaj dane ponownie
                    if (DB.useDb)
                    {
                        DB.CallRecords.Database.DropCollection("CallRecords");
                        parserController.Parse();
                    }
                    else
                    {
                        parserController.Parse();
                        MessageController.ResetDataMessage(parserController.SavedDataRows.Count);
                    }
                }

                if (keyPressed.Key == ConsoleKey.W)
                {
                    //Pokaż informacje o danych
                    if (DB.useDb)
                    {
                        MessageController.ShowDataMessage(DB.CallRecords.Count(new BsonDocument()));
                    }
                    else
                    {
                        MessageController.ShowDataMessage(parserController.SavedDataRows.Count);
                    }
                }

                if (keyPressed.Key == ConsoleKey.Escape)
                {
                    //Wyjdź
                    Environment.Exit(0);
                }
            }
        }