<commit_before>﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace VigilantCupcake.Models {

    public class FragmentBrowserModel : FilterableTreeModel {

        public FragmentBrowserModel(string path) {
            Root.Nodes.Add(CreateDirectoryNode(new DirectoryInfo(path)));
        }

        public IEnumerable<FragmentNode> FragmentNodes {
            get {
                return GetAllNodesRecursively(Root).Select(x => x as FragmentNode);
            }
        }

        public IEnumerable<Fragment> Fragments {
            get {
                return FragmentNodes.Where(x => x != null && x.Fragment != null).Select(y => y.Fragment);
            }
        }

        public void SaveAll() {
            if (Fragments.Any(f => f.DownloadPending)) {
                new TaskFactory().StartNew(() => {
                    while (Fragments.Any(f => f.DownloadPending)) {
                        System.Threading.Thread.Sleep(1000);
                    }
                    DoSaveAll();
                });
            } else {
                DoSaveAll();
            }
        }

        private FragmentNode CreateDirectoryNode(DirectoryInfo directoryInfo) {
            var directoryNode = new FragmentNode() { Text = directoryInfo.Name };
            if (!directoryInfo.Exists) return directoryNode;
            foreach (var directory in directoryInfo.GetDirectories())
                directoryNode.Nodes.Add(CreateDirectoryNode(directory));
            foreach (var file in directoryInfo.GetFiles()) {
                var name = Path.GetFileNameWithoutExtension(file.Name);
                var treeNode = new FragmentNode() { Text = name };
                var fragment = new Fragment() {<commit_msg>Fix issue with parents not being set correctly<commit_after>﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace VigilantCupcake.Models {

    public class FragmentBrowserModel : FilterableTreeModel {

        public FragmentBrowserModel(string path) {
            Root.Nodes.Add(CreateDirectoryNode(new DirectoryInfo(path), Root));
        }

        public IEnumerable<FragmentNode> FragmentNodes {
            get {
                return GetAllNodesRecursively(Root).Select(x => x as FragmentNode);
            }
        }

        public IEnumerable<Fragment> Fragments {
            get {
                return FragmentNodes.Where(x => x != null && x.Fragment != null).Select(y => y.Fragment);
            }
        }

        public void SaveAll() {
            if (Fragments.Any(f => f.DownloadPending)) {
                new TaskFactory().StartNew(() => {
                    while (Fragments.Any(f => f.DownloadPending)) {
                        System.Threading.Thread.Sleep(1000);
                    }
                    DoSaveAll();
                });
            } else {
                DoSaveAll();
            }
        }

        private FragmentNode CreateDirectoryNode(DirectoryInfo directoryInfo, FragmentNode parent) {
            var directoryNode = new FragmentNode() { Text = directoryInfo.Name, Parent = parent};
            if (!directoryInfo.Exists) return directoryNode;
            foreach (var directory in directoryInfo.GetDirectories())
                directoryNode.Nodes.Add(CreateDirectoryNode(directory, directoryNode));
            foreach (var file in directoryInfo.GetFiles()) {
                var name = Path.GetFileNameWithoutExtension(file.Name);
                var treeNode = new FragmentNode() { Text = name, Parent = directoryNode };
                var fragment = new Fragment() {
{'fraction_non_alphanumeric': 0.09506946336148188, 'fraction_numerical': 0.002179242713157178, 'mean_word_length': 1.9166004765687052, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6806955', 'n_tokens_mistral': 919, 'n_tokens_neox': 857, 'n_words': 199}