0

Suppose I have a directory with log files. log file My program has to create 3 text files with some statistics. date statistic domain statistic user statistic

Each log file has to process separately in parallel. But I don't understand how to create threads for functions with many arguments. Main functions are void WriteInFileUserStat(string filename,string txt, Dictionary dict), void WriteInFileDomainStat(string filename, string txt, Dictionary dict), void WriteInFileDateStat(string filename, string txt, Dictionary dict).

    class Program
{        
    static void Main(string[] args)
    {
        string dirName = @"C:\MY DISK\Study\7th semester\Основы параллельной обработки данных\log";
        string[] filelist = Directory.GetFiles(@"C:\MY DISK\Study\7th semester\Основы параллельной обработки данных\log", "*.txt");
        string[] fileliststat = Directory.GetFiles(@"C:\MY DISK\Study\7th semester\Основы параллельной обработки данных\log\statistic", "*.txt");
        string userlog = @"C:\MY DISK\Study\7th semester\Основы параллельной обработки данных\log\statistic\userlog.txt";
        string domainlog = @"C:\MY DISK\Study\7th semester\Основы параллельной обработки данных\log\statistic\domainlog.txt";
        string datelog = @"C:\MY DISK\Study\7th semester\Основы параллельной обработки данных\log\statistic\datelog.txt";

        Dictionary<string, string> userDict = new Dictionary<string, string>();
        Dictionary<string, string> domainDict = new Dictionary<string, string>();
        Dictionary<string, string> dateDict = new Dictionary<string, string>();

        //Thread userStatThread = new Thread(new ParameterizedThreadStart(WriteInFileUserStat));
        //userStatThread.Start(s, userlog, userDict);
        if (Directory.Exists(dirName))
        {
            Console.WriteLine("Файлы с логами прокси-сервера:");
            string[] files = Directory.GetFiles(dirName);
            foreach (string s in files)
            {
                string fname = s;
                Console.WriteLine(s);
            }
        }
        Console.WriteLine("Статистика по пользователям: ");
        foreach(string s in filelist)
        {
            WriteInFileUserStat(s, userlog, userDict);
        }
        foreach (KeyValuePair<string, string> keyValue in userDict)
        {
            Console.WriteLine(keyValue.Key + " - " + keyValue.Value);
        }
        Console.WriteLine("Статистика по доменам: ");
        foreach (string s in filelist)
        {
            WriteInFileDomainStat(s, domainlog, domainDict);
        }
        foreach (KeyValuePair<string, string> keyValue in domainDict)
        {
            Console.WriteLine(keyValue.Key + " - " + keyValue.Value);
        }
        Console.WriteLine("Статистика по датам: ");
        foreach (string s in filelist)
        {
            WriteInFileDateStat(s, datelog, dateDict);
        }
        foreach (KeyValuePair<string, string> keyValue in dateDict)
        {
            Console.WriteLine(keyValue.Key + " - " + keyValue.Value);
        }
        Console.ReadLine();
    }

    /*Парсинг файла*/
   public static void FilesParsing(string line)
    {
        string userName;
        string domainName;
        string traffic;
        string date;
        string[] filelist = Directory.GetFiles(@"C:\MY DISK\Study\7th semester\Основы параллельной обработки данных\log", "*.txt");
        foreach (string file_to_read in filelist)
        {

        }
        string[] parts = line.Split('\t');
        userName = parts[0];
        domainName = parts[1];
        traffic = parts[2];
        date = parts[3];

        Console.WriteLine($"Username = {userName} , Domain name = {domainName}, Traffic = {traffic}, Date = {date}");
    }

    /*Чтение из файла*/
    static public void ReadingFile(string filename)
    {
        using (StreamReader sr = new StreamReader(filename))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                FilesParsing(line);
            }
        }
    }
    /*Запись в файл статистики по пользователю*/
    static public void WriteInFileUserStat(string filename,string txt, Dictionary<string, string> dict)
    {
        string user;
        string traffic;
        int trafficInt = 0;
        int tempValue = 0; 
        using (StreamWriter swr = new StreamWriter(txt))
        {
            using (StreamReader sr = new StreamReader(filename))
            {
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    string[] parts = line.Split('\t');
                    user = parts[0];
                    traffic = parts[2];
                    if (dict.ContainsKey(user))
                    {
                        tempValue = Convert.ToInt32(dict[user]) + Convert.ToInt32(traffic);
                        dict.Remove(user);
                        trafficInt = Convert.ToInt32(traffic);                                    
                        dict.Add(user, Convert.ToString(tempValue));

                    }
                    else dict.Add(user, traffic);

                    //swr.WriteLine("{0}\t{1}", user, traffic);
                }
            }
            foreach (KeyValuePair<string, string> KeyValue in dict)
            {
                swr.WriteLine("{0} - {1}", KeyValue.Key, KeyValue.Value);
            }
            swr.Close();
        }
    }

    /*Запись в файл статистики по доменам*/
    static public void WriteInFileDomainStat(string filename, string txt, Dictionary<string, string> dict)
    {
        string domain;
        string traffic;
        int trafficInt = 0;
        int tempValue = 0;
        using (StreamWriter swr = new StreamWriter(txt))
        {
            using (StreamReader sr = new StreamReader(filename))
            {
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    string[] parts = line.Split('\t');
                    domain = parts[1];
                    traffic = parts[2];
                    if (dict.ContainsKey(domain))
                    {
                        tempValue = Convert.ToInt32(dict[domain]) + Convert.ToInt32(traffic);
                        dict.Remove(domain);
                        trafficInt = Convert.ToInt32(traffic);
                        dict.Add(domain, Convert.ToString(tempValue));
                    }
                    else dict.Add(domain, traffic);

                    //swr.WriteLine("{0}\t{1}", domain, traffic);
                }
            }
            foreach (KeyValuePair<string, string> KeyValue in dict)
            {
                swr.WriteLine("{0} - {1}", KeyValue.Key, KeyValue.Value);
            }
            swr.Close();
        }
    }

    /*Запись в файл статистики по дате*/
    static public void WriteInFileDateStat(string filename, string txt, Dictionary<string, string> dict)
    {
        string date;
        string traffic;
        int trafficInt = 0;
        int tempValue = 0;
        using (StreamWriter swr = new StreamWriter(txt))
        {
            using (StreamReader sr = new StreamReader(filename))
            {
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    string[] parts = line.Split('\t');
                    date = parts[3];
                    traffic = parts[2];
                    if (dict.ContainsKey(date))
                    {
                        tempValue = Convert.ToInt32(dict[date]) + Convert.ToInt32(traffic);
                        dict.Remove(date);
                        trafficInt = Convert.ToInt32(traffic);
                        dict.Add(date, Convert.ToString(tempValue));
                    }
                    else dict.Add(date, traffic);

                    //swr.WriteLine("{0}\t{1}", date, traffic);
                }
            }
            foreach (KeyValuePair<string, string> KeyValue in dict)
            {
                swr.WriteLine("{0} - {1}", KeyValue.Key, KeyValue.Value);
            }
            swr.Close();
        }
    }

}
Lika Barken
  • 45
  • 1
  • 8

1 Answers1

-1

If you got many arguments but only want 1 type to write in, make a single class, struct or tupple to take them all. Use that as the type for the argument. That is the way Events Arguments go.

Christopher
  • 9,634
  • 2
  • 17
  • 31