Created
January 26, 2019 18:22
-
-
Save pikoslav/d8d46f46b4481fe4775bfb296ad6209e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.IO; | |
using System.Linq; | |
using CsvHelper; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
enum CommandType { Get, Post, }; | |
static void Main(string[] args) | |
{ | |
var datum = DateTime.ParseExact(args[0], "yyyy-MM-dd", CultureInfo.InvariantCulture); | |
var datumString = datum.ToString("dd/MMM/yyyy", CultureInfo.InvariantCulture); | |
using (var reader = new StreamReader(@"D:\JAGR\access.log")) | |
using (var csv = new CsvReader(reader)) | |
{ | |
csv.Configuration.BadDataFound = null; | |
csv.Configuration.HasHeaderRecord = false; | |
csv.Configuration.Delimiter = " "; | |
var records = csv.GetRecords<dynamic>().Where(x => ((String)x.Field4).IndexOf(datumString) >= 0).ToArray(); | |
var data = records.Select(x => new | |
{ | |
Date = DateTime.ParseExact(new String(((String)x.Field4).Skip(1).Take(11).ToArray()), "dd/MMM/yyyy", CultureInfo.InvariantCulture), | |
Command = ((String)x.Field6).StartsWith("GET") ? CommandType.Get : CommandType.Post, | |
Agent = new String(((String)x.Field10).TakeWhile(c => c != ' ').ToArray()), | |
}) | |
.Where(x => x.Date == datum) | |
.ToArray(); | |
Console.WriteLine($"Number of GETs = {data.Where(x => x.Command == CommandType.Get).Count()}."); | |
Console.WriteLine($"Number of POSTs= {data.Where(x => x.Command == CommandType.Post).Count()}."); | |
Console.WriteLine($"Number of distinct UA = {data.Select(x => x.Agent).Distinct().Count()}."); | |
Console.ReadLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment