Ejemplo n.º 1
0
        public void Seed(SankeyContext context, string contentRootPath, ILogger <SankeyContext> logger)
        {
            if (false == context.Geos.Any())
            {
                context.Geos.AddRange(GetGeosFromFile(contentRootPath, logger));
                context.SaveChanges();
            }

            if (false == context.Nodes.Any())
            {
                context.Nodes.AddRange(GetNodesFromFile(contentRootPath, logger));
                context.SaveChanges();
            }

            if (false == context.Tables.Any())
            {
                context.Tables.AddRange(GetTablesFromFile(contentRootPath, logger));
                context.SaveChanges();
            }

            if (false == context.Flows.Any())
            {
                var setupPath = Path.Combine(contentRootPath, "Setup");
                var flowPaths = Directory.GetFiles(setupPath).Where(p => p.Contains("Flow"));

                foreach (var path in flowPaths)
                {
                    context.Flows.AddRange(GetFlowsFromPath(path, logger, context));
                    context.SaveChanges();
                }
            }
        }
Ejemplo n.º 2
0
        private Flow CreateFlow(string row, ILogger <SankeyContext> logger, SankeyContext context)
        {
            string[] colums = row.Split(";");

            try
            {
                var sourceName = colums[0].Trim();
                var source     = GetOrAddNode(sourceName, logger, context);

                var targetName = colums[1].Trim();
                var target     = GetOrAddNode(targetName, logger, context);

                var geoName = colums[3].Trim();
                var geo     = context.Geos.Where(p => p.NameEn == geoName).SingleOrDefault();

                if (null == geo)
                {
                    geo = new Geo
                    {
                        NameEn = geoName,
                        NameFr = geoName
                    };

                    logger.LogInformation($"Missing geo {geoName}");
                }

                var tableTag = colums[5].Trim().ToLower();
                var table    = context.Tables.Where(p => p.Tag == tableTag).SingleOrDefault();

                if (null == table)
                {
                    table = new Table
                    {
                        NameEn = tableTag,
                        NameFr = tableTag,
                        NoteEn = "Notes",
                        NoteFr = "Notes",
                        Tag    = tableTag
                    };

                    logger.LogInformation($"Missing table {tableTag}");
                }

                return(new Flow
                {
                    Source = source,
                    Target = target,
                    Value = Convert.ToInt32(colums[2]),
                    Year = Convert.ToInt32(colums[4]),
                    Geo = geo,
                    Table = table
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message} on flow {row}.");
            }

            return(null);
        }
Ejemplo n.º 3
0
        private Node GetOrAddNode(string nodeName, ILogger <SankeyContext> logger, SankeyContext context)
        {
            Node node;

            if (false == nodeCache.TryGetValue(nodeName, out node))
            {
                node = context.Nodes.Where(p => p.NameEn == nodeName).SingleOrDefault();

                if (null == node)
                {
                    node = new Node
                    {
                        NameEn = nodeName,
                        NameFr = nodeName
                    };

                    logger.LogInformation($"Missing node {nodeName}");
                }

                nodeCache.TryAdd(nodeName, node);
            }

            return(node);
        }
Ejemplo n.º 4
0
        private IEnumerable <Flow> GetTopLevelFlowsFromFile(string contentRootPath, ILogger <SankeyContext> logger, SankeyContext context)
        {
            string path = Path.Combine(contentRootPath, "Setup", "TopLevel.Flows.csv");

            logger.LogInformation("Flows loaded from file.");

            return(File.ReadAllLines(path)
                   .Skip(1) // skip header row
                   .Select(x => CreateFlow(x, logger, context))
                   .Where(x => x != null));
        }
Ejemplo n.º 5
0
        private IEnumerable <Flow> GetFlowsFromPath(string contentPath, ILogger <SankeyContext> logger, SankeyContext context)
        {
            logger.LogInformation($"Flows loaded from {contentPath}.");

            return(File.ReadAllLines(contentPath)
                   .Skip(1) // skip header row
                   .Select(x => CreateFlow(x, logger, context))
                   .Where(x => x != null));
        }