1

At the moment I am working at a project to analyze the performance of different actions. For that I created an ASP.NET MVC Project with an MDF Database. I read the Log Files (.csv Files), parse them and safe them into the datbase. The csv files look like:

And i get this in the console of the browser:

Array[6]
0: {durat: 148, err: false, time: "2019-06-03T02:00:06.86"}
1: {durat: 151, err: false, time: "2019-06-03T02:01:06.393"}
2: {durat: 139, err: false, time: "2019-06-03T02:13:06.63"}
3: {durat: 173, err: false, time: "2019-06-03T03:00:06.86"}
4: {durat: 151, err: false, time: "2019-06-03T03:01:06.393"}
5: {durat: 139, err: false, time: "2019-06-03T04:13:06.63"}

What do I have to do to group this output by hours? Like:

Array[3]
0: {durat: 148, err: false, time: "2019-06-03T02:00:06.86"}
1: {durat: 151, err: false, time: "2019-06-03T02:01:06.393"}
2: {durat: 139, err: false, time: "2019-06-03T02:13:06.63"}

Array[2]

0: {durat: 173, err: false, time: "2019-06-03T03:00:06.86"}
1: {durat: 151, err: false, time: "2019-06-03T03:01:06.393"}

Array[1]

0: {durat: 139, err: false, time: "2019-06-03T04:13:06.63"}

Database Structure:

public partial class ChartData
{
    public int Id { get; set; }
    public DateTime Timestamp { get; set; }
    public string Function { get; set; }
    public int Duration { get; set; }
    public bool IsError { get; set; }
}`

With this method I get the all datas from the selected function from the database:

public List<ChartDataDTO> GetDataForChart(string function)
    {
        return db.ChartDatas
            .Where(x => x.Function == function)
            .Select(x => new ChartDataDTO
            {
                durat = x.Duration,
                err = x.IsError,
                time = x.Timestamp
            })
            .ToList();
    }

In JS I use this method like:

$.getJSON(`/Home/GetDataForChart?function=${selectedFunction}`)

Csv File Structure:

date,functionName,DurationInMs,3,False

Flo Hab
  • 20
  • 7

1 Answers1

3

You can do this very easily using LINQ. Just group by date and hour ans split the list into multiple sublists. Here's an example:

List<ChartData> allItems = GetDataForChart("somefunctionIguess");

List<List<ChartData>> sublists = new List<List<ChartData>>();

sublists = 
allItems.GroupBy( x => new {
     x.Timestamp.Date,
     x.Timestamp.Hour
})
.Select(x=> x.ToList()).ToList();

Or maby even something like that:

db.ChartDatas
        .Where(x => x.Function == function)
        .Select(x => new ChartDataDTO
        {
            durat = x.Duration,
            err = x.IsError,
            time = x.Timestamp
        })
        .ToList()
    .GroupBy( x => new {
     x.Timestamp.Date,
     x.Timestamp.Hour
})
.Select(x=> x.ToList()).ToList()

But you might have to make further changes since you get multiple lists instead of one...

colosso
  • 2,525
  • 3
  • 25
  • 49