<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using DependencySummary.Models;

namespace DependencySummary.Services
{
    public class SummaryService
    {
        public IEnumerable<ViewModels.SummaryPackage> GetSummary()
        {
            List<ViewModels.SummaryPackage> packages;

            using (var db = new PackageContext())
            {
                packages =
                    db.Packages.GroupBy(p => p.Name)
                        .Select(f => f.FirstOrDefault())
                        .Select(vm => new ViewModels.SummaryPackage
                        {
                            Name = vm.Name
                        }).ToList();

                packages.ForEach(package =>
                {
                    package.Versions.AddRange(
                        db.Packages.Where(v => v.Name == package.Name)
                            .GroupBy(g => new {g.Version, g.TargetFramework})
                            .Select(f => f.FirstOrDefault())
                            .Select(version => new ViewModels.SummaryVersion
                            {
                                Version = version.Version,
                                TargetFramework = version.TargetFramework
                            }));

                    package.Versions.ForEach(v =>
                    {
                        v.Components.AddRange(
                            db.Packages.Where(
                                p =>
                                    p.Name == package.Name && p.Version == v.Version &&
                                    p.TargetFramework == v.TargetFramework)
                                .Select(
                                    c =>
                                        new ViewModels.SummaryComponent
                                        {
                                            Id = c.ComponentId,
                                            Name = db.Components.FirstOrDefault(component => c.ComponentId == component.Id).Name
                                        })
                                .ToList()
                            );
                    });
                });
            }

            return packages;
        }
    }
}<commit_msg>Refactored SummaryService for performance improvement<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using DependencySummary.Models;

namespace DependencySummary.Services
{
    public class SummaryService
    {
        public IEnumerable<ViewModels.SummaryPackage> GetSummary()
        {
            List<ViewModels.SummaryPackage> packages;

            using (var db = new PackageContext())
            {
                var packageData = db.Packages.ToList();
                var componentData = db.Components.ToList();

                packages =
                    packageData.GroupBy(p => p.Name)
                        .Select(f => f.FirstOrDefault())
                        .Select(vm => new ViewModels.SummaryPackage
                        {
                            Name = vm.Name
                        }).ToList();

                packages.ForEach(package =>
                {
                    package.Versions.AddRange(
                        packageData.Where(v => v.Name == package.Name)
                            .GroupBy(g => new {g.Version, g.TargetFramework})
                            .Select(f => f.FirstOrDefault())
                            .Select(version => new ViewModels.SummaryVersion
                            {
                                Version = version.Version,
                                TargetFramework = version.TargetFramework
                            }));

                    package.Versions.ForEach(v =>
                    {
                        v.Components.AddRange(
                            packageData.Where(
                                p =>
                                    p.Name == package.Name && p.Version == v.Version &&
                                    p.TargetFramework == v.TargetFramework)
                                .Select(
                                    c =>
                                        new ViewModels.SummaryComponent
                                        {
                                            Id = c.ComponentId,
                                            Name = componentData.FirstOrDefault(component => c.ComponentId == component.Id)?.Name
                                        })
                                .ToList()
                            );
                    });
                });
            }

            return packages;
        }
    }
}
{'fraction_non_alphanumeric': 0.07900340715502556, 'fraction_numerical': 0.0, 'mean_word_length': 0.8051498847040738, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6191801', 'n_tokens_mistral': 948, 'n_tokens_neox': 889, 'n_words': 156}