<commit_before>﻿// Copyright and license at: https://github.com/MatthewMWR/MeasureTraceAutomation/blob/master/LICENSE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MeasureTrace.TraceModel;
using Microsoft.Data.Entity;

namespace MeasureTraceAutomation
{
    public static class MeasurementStoreExtension
    {
        public static string GetDiscoveredAtPath(this MeasurementStore store, MeasuredTrace measuredTrace)
        {
            var dbtrace = store.Traces.FirstOrDefault(t => t == measuredTrace);
            if (dbtrace == null) return string.Empty;
            return
                dbtrace.TraceAttributes.Where(
                    ta =>
                        string.Equals(ta.Name, MeasurementStore.NameOfProcessingStateAttribute,
                            StringComparison.OrdinalIgnoreCase))
                    .Where(ta => ta.WholeNumberValue == (int) ProcessingState.Discovered)
                    .OrderBy(ta => ta.DateTimeValue)
                    .Select(ta => Path.Combine(ta.StringValue, ta.Trace.PackageFileName))
                    .Last();
        }

        public static void HydrateTraceMeasurements(this MeasurementStore store, MeasuredTrace measuredTrace)
        {
            foreach (
                var mm in
                    store.Model.GetEntityTypes()
                        .Where(et => et.ClrType.GetInterfaces().Contains(typeof (IMeasurement)))
                        .Select(mtype => NewDynamicSet(store, (IMeasurement) Activator.CreateInstance(mtype.ClrType)))
                        .SelectMany(set => set.Where(m => m.Trace == measuredTrace)))
            {
                measuredTrace.AddMeasurement(mm);
            }
        }

        public static void HydrateTraceMeasurements<TMeasurement>(this MeasurementStore store,
            MeasuredTrace measuredTrace) where TMeasurement : class, IMeasurement
        {
            foreach (var mm in store.Set<TMeasurement>().Where(m => m.Trace == measuredTrace))
            {
                measuredTrace.AddMeasurement(mm);
            }
        }

        public static DbSet<TEntity> NewDynamicSet<TEntity>(this MeasurementStore store, TEntity entity)
            where TEntity : class, IMeasurement
        {
            return store.Set<TEntity>();
        }

        public static IEnumerable<ProcessingRecord> GetLatestProcessingRecordForTraceByState(
            this MeasurementStore store, ProcessingState processingState)
        {
            return store.ProcessingRecords.GroupBy(pr => pr.MeasuredTrace.PackageFileName)
                .Where(gpr => gpr.AsEnumerable().Latest().ProcessingState == processingState)
                .Select(gpr => gpr.Latest())
                .Include(pr => pr.MeasuredTrace);
        }

        public static ProcessingRecord Latest(this IEnumerable<ProcessingRecord> records)
        {
            return records.OrderBy(r => r.StateChangeTime).Last();
        }
    }
}<commit_msg>Added GetTraceWithRecordsFromSparseTrace for linq-free callers like powershell<commit_after>﻿// Copyright and license at: https://github.com/MatthewMWR/MeasureTraceAutomation/blob/master/LICENSE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MeasureTrace.TraceModel;
using Microsoft.Data.Entity;

namespace MeasureTraceAutomation
{
    public static class MeasurementStoreExtension
    {
        public static string GetDiscoveredAtPath(this MeasurementStore store, MeasuredTrace measuredTrace)
        {
            var dbtrace = store.Traces.FirstOrDefault(t => t == measuredTrace);
            if (dbtrace == null) return string.Empty;
            return
                dbtrace.TraceAttributes.Where(
                    ta =>
                        string.Equals(ta.Name, MeasurementStore.NameOfProcessingStateAttribute,
                            StringComparison.OrdinalIgnoreCase))
                    .Where(ta => ta.WholeNumberValue == (int) ProcessingState.Discovered)
                    .OrderBy(ta => ta.DateTimeValue)
                    .Select(ta => Path.Combine(ta.StringValue, ta.Trace.PackageFileName))
                    .Last();
        }

        public static void HydrateTraceMeasurements(this MeasurementStore store, MeasuredTrace measuredTrace)
        {
            foreach (
                var mm in
                    store.Model.GetEntityTypes()
                        .Where(et => et.ClrType.GetInterfaces().Contains(typeof (IMeasurement)))
                        .Select(mtype => NewDynamicSet(store, (IMeasurement) Activator.CreateInstance(mtype.ClrType)))
                        .SelectMany(set => set.Where(m => m.Trace == measuredTrace)))
            {
                measuredTrace.AddMeasurement(mm);
            }
        }

        public static void HydrateTraceMeasurements<TMeasurement>(this MeasurementStore store,
            MeasuredTrace measuredTrace) where TMeasurement : class, IMeasurement
        {
            foreach (var mm in store.Set<TMeasurement>().Where(m => m.Trace == measuredTrace))
            {
                measuredTrace.AddMeasurement(mm);
            }
        }

        public static MeasuredTrace GetTraceWithRecordsFromSparseTrace(this MeasurementStore store, MeasuredTrace measuredTrace)
        {
            return
                store.Traces.Include(t => t.ProcessingRecords)
                    .Where(t => t.IsSameDataPackatge(measuredTrace))
                    .SingleOrDefault();
        }

        public static DbSet<TEntity> NewDynamicSet<TEntity>(this MeasurementStore store, TEntity entity)
            where TEntity : class, IMeasurement
        {
            return store.Set<TEntity>();
        }

        public static IEnumerable<ProcessingRecord> GetLatestProcessingRecordForTraceByState(
            this MeasurementStore store, ProcessingState processingState)
        {
            return store.ProcessingRecords.GroupBy(pr => pr.MeasuredTrace.PackageFileName)
                .Where(gpr => gpr.AsEnumerable().Latest().ProcessingState == processingState)
                .Select(gpr => gpr.Latest())
                .Include(pr => pr.MeasuredTrace);
        }

        public static ProcessingRecord Latest(this IEnumerable<ProcessingRecord> records)
        {
            return records.OrderBy(r => r.StateChangeTime).Last();
        }
    }
}
{'fraction_non_alphanumeric': 0.08722204823050422, 'fraction_numerical': 0.0, 'mean_word_length': 2.1324178518881807, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5523421', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1531, 'n_words': 310}