Doesn't create statistics after processing files.



  • Get out of here. https://ru.stackoverflow.com/questions/473770/%D0%A0%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8F-%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D1%8B-%D1%81-%D0%BF%D0%BE%D1%82%D0%BE%D0%BA%D0%B0%D0%BC%D0%B8

    The structure of the file log is that

    1. user adres trafik data User1 Yandex 110 26.06
      User2 Yandex 600 23.07 User3 Google 700 12.08
      User1 Yahoo 800 28.08 User3 Google 100 13.09
      User2 Yandex 120 14.09 User1 Google 140 27.09
      User3 Yahoo 100 23.10 User2 Google 150 16.11
      User1 Yandex 160 17.11 User3 Yahoo 110 24.11
      User2 Google 700 25.11 User1 Yandex 900 18.12

    The code corrects the exemptions don't go out, makes the files, but there's nothing in them.

    He's been walking around, he's got zero. He saw 10 directory files, so the variable filelist was assigned 10, but then the whole null was gone.

    Maybe all the reading results go where they don't want to go, or I don't do that to pass the files. I know there's a mistake in the file parsing and attribution, but I'm trying to figure it out.

    I hope your advice and clues.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Text;
    using System.IO;
    using System.Diagnostics.Eventing.Reader;
    using System.Threading.Tasks;
    

    namespace Лабораторная_2
    {
    class countFiles
    {
    string[] filelist = Directory.GetFiles(@"D:\logfiles", "*.txt");
    public string user
    { get; set; }
    public string adres
    { get; set; }
    public int trafik
    { get; set; }
    public string data
    { get; set; }

         public countFiles(string userr="", string adress="", int traffic=0)
        {
            user = userr;
            adres = adress;
            trafik = traffic;
        }
    
      public void parsingfiles(string lline)
        {
            List<countFiles> list = new List<countFiles>(); //здесь будет результат
            for (int i = 1; i < filelist.Length; i++)
            {
                if (filelist[i] == "")
                    continue;
                string[] lineParts = filelist[i].Split(' ');
                list.Add(new countFiles(lineParts[0], lineParts[1], int.Parse(lineParts[2])));
            }
        }
    
        public void ReadFile(string filename)
        {
            using (StreamReader sr = new StreamReader(filename))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    parsingfiles(line);
                }
            }
        }
    

    }

    class StatLog
    {
       public Dictionary<String, UInt64> userstat;
       public Dictionary<String, UInt64> adrestat;
       public Dictionary<int, UInt64> trafikstat;
    
       public StatLog()//конструктор 
       {
           userstat = new Dictionary<String, UInt64>();
           adrestat = new Dictionary<String, UInt64>();
           trafikstat = new Dictionary<int, UInt64>();
       }
    
     public StatLog createStat(StatLog info)//
     {
         UInt64 value;
         foreach (var item in info.userstat)
            {              
                userstat[item.Key] = (userstat.TryGetValue(item.Key, out value) ? value : 0) + item.Value;
            }
            foreach (var item in info.adrestat)
            {
                adrestat[item.Key.Trim()] = (adrestat.TryGetValue(item.Key.Trim(), out value) ? value : 0) + item.Value;
            }
            foreach (var item in info.trafikstat)
            {
                trafikstat[item.Key] = (trafikstat.TryGetValue(item.Key, out value) ? value : 0) + item.Value;
            }
            return this;
     }
    }         
    



  • Like, there are two files.

    log1.txt

    user    address   traffic  date
    User2   Yandex    600      23.07
    User1   Yahoo     800      28.08
    User2   Yandex    120      14.09
    User3   Yahoo     100      23.10
    

    log2.txt

    user    address   traffic  date
    User2   Yandex    600      23.07
    User1   Yandex    120      14.09
    User3   Yahoo     100      23.10
    

    The steaming method is used for these files https://ru.stackoverflow.com/a/485601/196972 ♪
    For the collection of statistics, add the next class

    class TrafficStat {
        public class Item {
            public string Key;
            public int Traffic;
            public DateTime From;
            public DateTime To;
            public string ToCsv(bool header=false) {
                var s = ", ";
                return header 
                ? String.Concat("Key", s, "Traffic", s, "From", s, "To")
                : String.Concat(
                    this.Key, s,
                    this.Traffic, s,
                    this.From.ToString("d.MM"), s,
                    this.To.ToString("d.MM"));
            }
        }
        Dictionary<string, Item> tbl = new Dictionary<string, Item>();
        public void Add(string key, Line line) {
            Item itm;
            if (tbl.TryGetValue(key, out itm) == false) {
                itm = new Item { Key = key, From = line.Date, To = line.Date };
                tbl.Add(key, itm);
            }
            itm.From = new DateTime(Math.Min(itm.From.Ticks, line.Date.Ticks));
            itm.To = new DateTime(Math.Max(itm.To.Ticks, line.Date.Ticks));
            itm.Traffic += line.Traffic;
        }
        public IEnumerable<Item> Items { get { return tbl.Values; } }
    }
    

    var root = @"C:\Temp\logs\";
    var userstat = new TrafficStat();
    foreach (var line in ParseFiles(root, "log*.txt", 1)) 
       userstat.Add(line.User, line);
    

    Console.WriteLine(si.ToCsv(true));
    foreach (var si in userstat.Items) Console.WriteLine(si.ToCsv());

    // записать статистику в файл
    File.WriteAllLines(root + "userstat.txt",
    userstat.Items.Select(si => si.ToCsv()));

    Outcome

    Key,    Traffic,  From,   To            -- From и To - это мин. и макс. даты
    User2, 1320, 23.07, 14.09
    User1, 920, 28.08, 14.09
    User3, 200, 23.10, 23.10


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2