1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
| using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Threading;
using System.Text;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace LabWork2
{
public class Line
{
public string User;
public string Address;
public int Traffic;
public DateTime Date;
public string ToCsv(bool header = false)
{
var s = ", ";
return header
? String.Concat("User", s, "Address", s, "Traffic", s, "Date")
: String.Concat(
this.User, s,
this.Address, s,
this.Traffic, s,
this.Date.ToString("d.MM"));
}
public static IEnumerable<Line> Parse(string logDir, string pattern = "log?.txt", int skip = 0)
{
string result = Path.GetFileName(pattern);
foreach (var file in Directory.EnumerateFiles(logDir, result))
{
//foreach (var file in Directory.GetFiles(logDir,result))
// while()
while (File.Exists(pattern))
{
foreach (var line in ParseFile(logDir, file, 1))
yield return line;
}
}
}
public static IEnumerable<Line> ParseFile(string logDir,string file, int skip = 0)
{
return Parse(logDir,file, skip);
}
}
class Stat
{
public class Item
{
public string Key;
public string Address;
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"));
}
public string ToCsv1(bool header = false)
{
var s = ", ";
return header
? String.Concat("Adress", s, "Traffic", s)
: String.Concat(
this.Address, s,
this.Traffic, s);
}
public string ToCsv2(bool header = false)
{
var s = ", ";
return header
? String.Concat( "Traffic", s, "From", s, "To")
: String.Concat(
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; } }
}
class Program
{
static Queue<String> m_workFiles = new Queue<String>();//файлы на обработку
static System.Collections.Generic.List<Stat> m_threadResult; //результат выполнения потока
static bool m_iscomplete = false;//флаг завершения ввода
static readonly object m_locker = new object();//мьютекс для регулирования доступа к очереди файлов на обработку
public void processDirectory(String logDir)//извлечь все файлы очередь для обработки
{
if (!Directory.Exists(logDir))
{
return;
}
lock (m_locker)//захватить мьютекс
{
foreach (var x in Directory.EnumerateFiles(logDir))
{
m_workFiles.Enqueue(x);
}
}
m_iscomplete = true;//установить флаг завершения
}
static void Main(string[] args)
{
var cq = new ConcurrentQueue<Stat>();
var logDir = @"D:\logfiles";
Program pr = new Program();
pr.processDirectory(logDir);
var userstat = new Stat();
Line ln = new Line();
Stat st = new Stat();
foreach (var line in Line.ParseFile(logDir,"log*.txt", 1))
userstat.Add(line.User, line);
//Console.WriteLine(ln.ToCsv(true));
// параллельно ...
Directory.EnumerateFiles(logDir, "log*.txt").AsParallel().ForAll(file =>
{
Console.WriteLine("@" + Environment.CurrentManagedThreadId + "\t" + file);
//var userstat = new Stat();
// собрать статистику из файла
foreach (var line in Line.ParseFile(logDir,file, 1))
userstat.Add(line.User, line);
// сохранить статистику для дальнейшего агрегирования
cq.Enqueue(userstat);
});
Console.WriteLine(cq.Count);
foreach (var si in userstat.Items) Console.WriteLine(si.ToCsv());
Console.WriteLine("Запись файлов-отчётов");
File.WriteAllLines(logDir + "userstat.txt",userstat.Items.Select(si => si.ToCsv()));
File.WriteAllLines(logDir + "adrestat.txt",userstat.Items.Select(si => si.ToCsv1()));
File.WriteAllLines(logDir + "datastat.txt",userstat.Items.Select(si => si.ToCsv2()));
Console.WriteLine("Файлы записаны");
}
}
} |