예제 #1
0
        public FetchItemServiceResponse<Graph<UserDto>> FetchEmailsGraph(string connectionString, DateTime fromDate, DateTime toDate)
        {
            FetchItemServiceResponse<Graph<UserDto>> response = new FetchItemServiceResponse<Graph<UserDto>>();
            try
            {
                Graph<UserDto> graph = new Graph<UserDto>();

                using (IUnitOfWork uow = CreateUnitOfWork(connectionString))
                {
                    HashSet<ConversationEmails> conversationEmails = uow.ConvRepo.ExtractConversationsFromDatabase(fromDate, toDate);

                    HashSet<Edge<UserDto>> edges = uow.GraphRepo.ExtractEdgesFromConversation(conversationEmails);
                    graph.CreateGraph(edges);
                }

                response.Succeeded = true;
                response.Item = graph;
            }
            catch (Exception e)
            {
                response.Succeeded = false;
                response.Error = ($"Import of file failed with an error: {e.Message}");

                if (e.InnerException != null)
                {
                    response.Error = ($"Additional error: {e.InnerException.Message}");
                }
            }

            return response;
        }
예제 #2
0
        public FetchItemServiceResponse<int> FetchNodeIdByUserName(string name, string connectionString)
        {
            FetchItemServiceResponse<int> response = new FetchItemServiceResponse<int>();
            try
            {

                using (IUnitOfWork uow = CreateUnitOfWork(connectionString))
                {
                    int nodeId = uow.UserRepo.GetNodeIdByUserName(name);
                    response.Item = nodeId;
                }

                if (response.Item == 0)
                {
                    response.Succeeded = false;
                    response.Error = ("Node was not found.");
                }
            }
            catch (Exception e)
            {
                response.Succeeded = false;
                response.Error = ($"Import of file failed with an error: {e.Message}");

                if (e.InnerException != null)
                {
                    response.Error = ($"Additional error: {e.InnerException.Message}");
                }
            }

            return response;
        }
예제 #3
0
        public FetchItemServiceResponse<SSRMRolesDto> FetchSSRMRolesCounts(Graph<UserDto> graph)
        {
            FetchItemServiceResponse<SSRMRolesDto> response = new FetchItemServiceResponse<SSRMRolesDto>();
            try
            {
                if (graph.Nodes.All(x => x.Role == 0))
                {
                    throw new Exception("Detect SSRM roles first!");
                }
                int leadersCount = graph.Nodes.Count(x => x.Role == Role.Leader);
                int mediatorsCount = graph.Nodes.Count(x => x.Role == Role.Mediator);
                int outermostsCount = graph.Nodes.Count(x => x.Role == Role.Outermost);
                int outsidersCount = graph.Nodes.Count(x => x.Role == Role.Outsider);

                SSRMRolesDto ssrmRolesDto = new SSRMRolesDto()
                {
                    LeaderCount = leadersCount,
                    MediatorsCount = mediatorsCount,
                    OutsiderCount = outsidersCount,
                    OutermostCount = outermostsCount
                };

                response.Item = ssrmRolesDto;
                response.Succeeded = true;
                return response;
            }
            catch (Exception e)
            {
                response.Succeeded = false;
                throw new Exception(e.Message);
            }
        }
예제 #4
0
        public ActionResult FindRoles(GraphViewModel graphViewModel)
        {
            try
            {
                if (graphViewModel.Graph.Edges.Count != 0 && graphViewModel.Graph.GraphSet.Count == 0)
                {
                    foreach (Edge <UserDto> edge in graphViewModel.Graph.Edges)
                    {
                        graphViewModel.Graph.CreateGraphSet(edge);
                    }
                }

                FetchItemServiceResponse <Graph <UserDto> > response = _graphService.DetectRolesInGraph(graphViewModel.Graph);

                if (response.Succeeded)
                {
                    graphViewModel.Graph = response.Item;
                    FetchItemServiceResponse <SSRMRolesDto> ssrmRolesCounts = _graphService.FetchSSRMRolesCounts(graphViewModel.Graph);
                    graphViewModel.SsrmRolesDto = ssrmRolesCounts.Item;

                    List <NodeDto> nodes = graphViewModel.Graph.Nodes.Select(x => new NodeDto()
                    {
                        id    = x.Id,
                        label = x.NodeElement.Name,
                        group = (graphViewModel.GraphDto.nodes.First(y => y.id == x.Id).group),
                        title = $"Node degree: {x.Degree}",
                        size  = GetNodeSizeBasedOnRole(x),
                        shape = (graphViewModel.GraphDto.nodes.First(y => y.id == x.Id).shape)
                    }).ToList();
                    List <EdgeDto> edges = graphViewModel.Graph.Edges.Select(x => new EdgeDto()
                    {
                        from = x.Node1.Id, to = x.Node2.Id
                    }).ToList();

                    if (nodes.FirstOrDefault(x => x.id == graphViewModel.SelectedEgoId) != null)
                    {
                        nodes.First(x => x.id == graphViewModel.SelectedEgoId).size = 25;
                    }

                    foreach (Node <UserDto> node in graphViewModel.Graph.Nodes.Where(x => x.Role != 0))
                    {
                        nodes.First(x => x.id == node.Id).shape = GetNodeShapeBasedOnRole(node);
                    }
                    graphViewModel.Graph.SetCommunityNodes();
                    GraphDto graphDto = new GraphDto
                    {
                        nodes = nodes,
                        edges = edges
                    };
                    graphViewModel.RolesDetected = true;
                    graphViewModel.GraphDto      = graphDto;
                }
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(500, e.Message));
            }
            return(View("GraphView_partial", graphViewModel));
        }
예제 #5
0
        public FetchItemServiceResponse<Graph<UserDto>> DetectBrokerageInGraph(Graph<UserDto> graph)
        {
            FetchItemServiceResponse<Graph<UserDto>> response = new FetchItemServiceResponse<Graph<UserDto>>();
            if (graph.Communities.Count == 0)
            {
                throw new Exception("You have to find communities first!");
            }

            try
            {
                foreach (Node<UserDto> nodeB in graph.Nodes)
                {
                    HashSet<Node<UserDto>> adjacentNodes = graph.GetAdjacentNodes(nodeB);

                    nodeB.Brokerage = new Brokerage();
                    foreach (Node<UserDto> nodeA in adjacentNodes)
                    {
                        foreach (Node<UserDto> nodeC in adjacentNodes)
                        {
                            if (graph.ExistEdgeBetweenNodes(nodeA, nodeC) || nodeA.Id == nodeC.Id)
                            {
                                continue;
                            }

                            if (nodeA.CommunityId != nodeC.CommunityId && nodeA.CommunityId != nodeB.CommunityId && nodeC.CommunityId != nodeB.CommunityId)
                            {
                                nodeB.Brokerage.Liaison++;
                            }

                            if (nodeA.CommunityId == nodeC.CommunityId && nodeA.CommunityId != nodeB.CommunityId && nodeC.CommunityId != nodeB.CommunityId)
                            {
                                nodeB.Brokerage.Itinerant++;
                            }

                            if (nodeA.CommunityId == nodeB.CommunityId && nodeA.CommunityId == nodeC.CommunityId && nodeB.CommunityId == nodeC.CommunityId)
                            {
                                nodeB.Brokerage.Coordinator++;
                            }

                            if ((nodeA.CommunityId == nodeB.CommunityId && nodeA.CommunityId != nodeC.CommunityId && nodeB.CommunityId != nodeC.CommunityId)
                                || (nodeB.CommunityId == nodeC.CommunityId && nodeB.CommunityId != nodeA.CommunityId && nodeC.CommunityId != nodeA.CommunityId))
                            {
                                nodeB.Brokerage.Representative++;
                                nodeB.Brokerage.Gatepeeker++;
                            }
                        }
                    }
                }
                response.Item = graph;
                response.Succeeded = true;
                return response;
            }
            catch (Exception e)
            {
                response.Succeeded = false;
                throw new Exception(e.Message);
            }
        }
예제 #6
0
        public void FetchEmailGraphTest()
        {
            IGraphService service = new GraphService(UnitOfWorkFactory);

            const string connectionString = "GLEmailsDatabase";
            FetchItemServiceResponse <Graph <UserDto> > fetchEmailsGraph = service.FetchEmailsGraph(connectionString);

            Assert.IsNotEmpty(fetchEmailsGraph.Item.Edges);
        }
예제 #7
0
        public FetchItemServiceResponse<Graph<UserDto>> CreateEgoNetwork(Graph<UserDto> graph, int egoNetworkCenterId)
        {
            FetchItemServiceResponse<Graph<UserDto>> response = new FetchItemServiceResponse<Graph<UserDto>>();

            try
            {
                if (graph.EgoEdges.Count > 0)
                {
                    foreach (Edge<UserDto> egoEdge in graph.EgoEdges)
                    {
                        graph.Edges.RemoveWhere(x => x.Node1.Id == egoEdge.Node1.Id && x.Node2.Id == egoEdge.Node2.Id);
                    }
                }
                EgoNetwork egoNetwork = new EgoNetwork();

                HashSet<HashSet<Node<UserDto>>> subGraphs = egoNetwork.FindConectedSubgraphs(graph);
                
                Node<UserDto> egoNetworkCenter = graph.GetNodeById(egoNetworkCenterId);
                if (egoNetworkCenter == null)
                {
                    throw new Exception("Ego center node was not found.");
                }
                HashSet<Node<UserDto>> nodesWithMAximalDegreeInSubgraphsAximalDegreeInSubgraph = egoNetwork.GetNodesWithMaximalDegreeInSubgraphs(subGraphs, egoNetworkCenter);

                foreach (Node<UserDto> node in nodesWithMAximalDegreeInSubgraphsAximalDegreeInSubgraph)
                {
                    Edge<UserDto> newEdge = new Edge<UserDto>()
                    {
                        Node1 = egoNetworkCenter,
                        Node2 = node
                    };
                    graph.AddEdge(newEdge);
                    graph.EgoEdges.Add(newEdge);
                }

                graph.SetDegrees();

                double eiIndex = egoNetwork.GetEIIndex(graph, egoNetworkCenter);
                double effectiveSizeOfEgo = egoNetwork.GetEffectiveSizeOfEgo(graph, egoNetworkCenter);
                int connectedCommunities = egoNetwork.GetNumberOfConnectedCommunities(graph, egoNetworkCenter);

                graph.Nodes.First(x => x.Id == egoNetworkCenter.Id).EIIndex = eiIndex;
                graph.Nodes.First(x => x.Id == egoNetworkCenterId).EffectiveSize = effectiveSizeOfEgo;
                graph.Nodes.First(x => x.Id == egoNetworkCenterId).CommunitiesConnected = connectedCommunities;


                response.Succeeded = true;
                response.Item = graph;
            }
            catch (Exception e)
            {
                response.Succeeded = false;
                throw new Exception(e.Message);
            }
            return response;
        }
예제 #8
0
        public ActionResult GetSelectedValue(GraphViewModel model, string teamMemberId)
        {
            try
            {
                int    id = int.Parse(teamMemberId);
                string connectionString = GetConnectionStringBasedOnSelectedMember(teamMemberId);

                if (model.FromDate != null || model.ToDate != null)
                {
                    List <DateTime> startAndEndDateOfConversations = GetStartAndEndDateOfConversations(teamMemberId);
                    model.FromDate = startAndEndDateOfConversations.First().ToString("MM/dd/yyyy");
                    model.ToDate   = startAndEndDateOfConversations.Last().ToString("MM/dd/yyyy");
                }

                DateTime from = DateTime.ParseExact(model.FromDate, "MM/dd/yyyy", null);
                DateTime to   = DateTime.ParseExact(model.ToDate, "MM/dd/yyyy", null);

                FetchItemServiceResponse <Graph <UserDto> > responseGraph = _graphService.FetchEmailsGraph(connectionString, from, to);

                responseGraph.Item.SetDegrees();

                List <NodeDto> nodes = responseGraph.Item.Nodes.Select(x => new NodeDto()
                {
                    id    = x.Id,
                    label = x.NodeElement.Name,
                    title = $"Node degree: {x.Degree}",
                    size  = 10,
                    color = "#f5cbee"
                }).ToList();
                List <EdgeDto> edges = responseGraph.Item.Edges.Select(x => new EdgeDto()
                {
                    from = x.Node1.Id, to = x.Node2.Id
                }).ToList();

                GraphDto graphDto = new GraphDto
                {
                    nodes = nodes,
                    edges = edges
                };


                model.TeamMembers          = TeamMembers;
                model.SelectedTeamMemberId = id;
                model.Graph    = responseGraph.Item;
                model.GraphDto = graphDto;
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(500, e.Message));
            }

            return(View("GraphView_partial", model));
        }
예제 #9
0
        public FetchItemServiceResponse<Graph<UserDto>> DetectRolesInGraph(Graph<UserDto> graph)
        {
            FetchItemServiceResponse<Graph<UserDto>> response = new FetchItemServiceResponse<Graph<UserDto>>();

            try
            {
                if (graph.Communities.Count == 0)
                {
                    throw new Exception("You have to find communities first!");
                }

                GraphAlgorithm<UserDto> algorithms = new GraphAlgorithm<UserDto>(graph);
                HashSet<ShortestPathSet<UserDto>> shortestPaths = algorithms.GetAllShortestPathsInGraph(graph.Nodes);

                //setting closeness centrality
                algorithms.SetClosenessCentralityForEachNode(shortestPaths);

                //setting closeness centrality for community
                algorithms.SetClosenessCentralityForEachNodeInCommunity(shortestPaths);

                //community closeness centrality mean and standart deviation
                algorithms.SetMeanClosenessCentralityForEachCommunity();
                algorithms.SetStandartDeviationForClosenessCentralityForEachCommunity();

                //cPaths for nCBC measure
                HashSet<ShortestPathSet<UserDto>> cPaths = algorithms.CPaths(shortestPaths);

                //setting nCBC for each node
                algorithms.SetNCBCForEachNode(cPaths);

                //setting DSCount for each node
                algorithms.SetDSCountForEachNode(cPaths);

                GraphRoleDetection<UserDto> roleDetection = new GraphRoleDetection<UserDto>(graph, algorithms);
                roleDetection.ExtractOutsiders();
                roleDetection.ExtractLeaders();
                roleDetection.ExtractOutermosts();

                //sorting nodes by their mediacy score
                HashSet<Node<UserDto>> sortedNodes = algorithms.OrderNodesByMediacyScore();
                roleDetection.ExtractMediators(sortedNodes);

                response.Succeeded = true;
                response.Item = graph;
            }
            catch (Exception e)
            {
                response.Succeeded = false;
                throw new Exception(e.Message);
            }

            return response;
        }
예제 #10
0
        public ActionResult DrawNetworkStatistics(GraphViewModel graphViewModel)
        {
            try
            {
                DateTime fromDate         = DateTime.ParseExact(graphViewModel.FromDate, "MM/dd/yyyy", null);
                DateTime toDate           = DateTime.ParseExact(graphViewModel.ToDate, "MM/dd/yyyy", null);
                string   connectionString = graphViewModel.FileImported == false?GetConnectionStringBasedOnSelectedMember(graphViewModel.SelectedTeamMemberId.ToString()) : _importConnectionString;

                FetchItemServiceResponse <NetworkStatisticsDto> mostUsedEmailDomains = _graphService.FetchEmailNetworkStatistics(connectionString, fromDate, toDate);
                if (mostUsedEmailDomains.Succeeded)
                {
                    graphViewModel.NetworkStatisticsDto = mostUsedEmailDomains.Item;
                }
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(500, e.Message));
            }
            return(View("NetworkStatistics_partial", graphViewModel));
        }
예제 #11
0
        public FetchItemServiceResponse<NetworkStatisticsDto> FetchEmailNetworkStatistics(string connectionString, DateTime fromDate, DateTime toDate)
        {
            FetchItemServiceResponse<NetworkStatisticsDto> response = new FetchItemServiceResponse<NetworkStatisticsDto>();
            try
            {
                NetworkStatisticsDto emailNetworkStatistics;
                using (IUnitOfWork uow = CreateUnitOfWork(connectionString))
                {
                    emailNetworkStatistics = uow.GraphRepo.GetEmailNetworkStatistics(fromDate, toDate);
                }

                response.Item = emailNetworkStatistics;
                response.Succeeded = true;
                return response;
            }
            catch (Exception e)
            {
                response.Succeeded = false;
                throw new Exception(e.Message);
            }
        }
예제 #12
0
        public FetchItemServiceResponse<Node<UserDto>> FetchNodeWithBiggestDegree(string connectionString, Graph<UserDto> graph)
        {
            FetchItemServiceResponse<Node<UserDto>> response = new FetchItemServiceResponse<Node<UserDto>>();
            try
            {
                Node<UserDto> node = graph.Nodes.OrderByDescending(x => x.Degree).First();
                response.Item = node;


                if (response.Item == null)
                {
                    response.Succeeded = false;
                    throw new Exception("Node was not found.");
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            return response;
        }
예제 #13
0
        public ActionResult FindBrokerage(GraphViewModel graphViewModel)
        {
            try
            {
                if (graphViewModel.Graph.Edges.Count != 0 && graphViewModel.Graph.GraphSet.Count == 0)
                {
                    foreach (Edge <UserDto> edge in graphViewModel.Graph.Edges)
                    {
                        graphViewModel.Graph.CreateGraphSet(edge);
                    }
                }

                FetchItemServiceResponse <Graph <UserDto> > response = _graphService.DetectBrokerageInGraph(graphViewModel.Graph);

                if (response.Succeeded)
                {
                    graphViewModel.Graph = response.Item;
                    FetchListServiceResponse <BrokerageDto> topTenBrokersResponse = _graphService.FetchTopTenBrokers(graphViewModel.Graph, GetConnectionStringBasedOnSelectedMember(graphViewModel.SelectedTeamMemberId.ToString()));

                    if (topTenBrokersResponse.Succeeded)
                    {
                        graphViewModel.BrokerageDto      = topTenBrokersResponse.Items;
                        graphViewModel.BrokerageDetected = true;
                    }

                    List <NodeDto> nodes = graphViewModel.Graph.Nodes.Select(x => new NodeDto()
                    {
                        id    = x.Id,
                        label = x.NodeElement.Name,
                        title = $"Node degree: {x.Degree}",
                        size  = 20,
                        group = (graphViewModel.GraphDto.nodes.First(y => y.id == x.Id).group)
                    }).ToList();
                    List <EdgeDto> edges = graphViewModel.Graph.Edges.Select(x => new EdgeDto()
                    {
                        from = x.Node1.Id, to = x.Node2.Id
                    }).ToList();

                    HashSet <BrokerageDto> topTenBrokers = topTenBrokersResponse.Items;
                    foreach (NodeDto node in (nodes.Where(x => topTenBrokers.Select(y => y.UserId).Contains(x.id))))
                    {
                        node.shape = "diamond";
                    }

                    GraphDto graphDto = new GraphDto
                    {
                        nodes = nodes,
                        edges = edges
                    };
                    graphViewModel.Graph.SetCommunityNodes();
                    graphViewModel.RolesDetected = false;
                    graphViewModel.GraphDto      = graphDto;
                }
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(500, e.Message));
            }

            return(View("GraphView_partial", graphViewModel));
        }
예제 #14
0
        public ActionResult CreateEgoNetwork(GraphViewModel graphViewModel, int egoNetworkCenterId)
        {
            try
            {
                if (graphViewModel.Graph != null)
                {
                    if (graphViewModel.Graph.Edges.Count != 0)
                    {
                        foreach (Edge <UserDto> edge in graphViewModel.Graph.Edges)
                        {
                            graphViewModel.Graph.CreateGraphSet(edge);
                        }
                    }

                    FetchItemServiceResponse <Graph <UserDto> > graphResponse = _graphService.CreateEgoNetwork(graphViewModel.Graph, egoNetworkCenterId);
                    if (graphResponse.Succeeded)
                    {
                        graphViewModel.Graph = graphResponse.Item;

                        List <NodeDto> nodes = graphViewModel.Graph.Nodes.Select(x => new NodeDto()
                        {
                            id    = x.Id,
                            label = x.NodeElement.Name,
                            title = $"Node degree: {x.Degree}",
                            size  = GetNodeSizeBasedOnRole(x),
                            group = (graphViewModel.GraphDto.nodes.First(y => y.id == x.Id).group),
                            shape = (graphViewModel.GraphDto.nodes.First(y => y.id == x.Id).shape)
                        }).ToList();

                        List <EdgeDto> edges = graphViewModel.Graph.Edges.Select(x => new EdgeDto()
                        {
                            from = x.Node1.Id, to = x.Node2.Id
                        }).ToList();

                        GraphDto graphDto = new GraphDto
                        {
                            nodes = nodes,
                            edges = edges
                        };

                        graphDto.nodes.First(x => x.id == egoNetworkCenterId).title = $"Node degree: {graphViewModel.Graph.Nodes.First(x => x.Id == egoNetworkCenterId).Degree}"
                                                                                      + ", " + $"E-I Index: {graphViewModel.Graph.Nodes.First(x => x.Id == egoNetworkCenterId).EIIndex}"
                                                                                      + ", " + $"Effective size: {graphViewModel.Graph.Nodes.First(x => x.Id == egoNetworkCenterId).EffectiveSize}"
                                                                                      + ", " + $"Connected communities: {graphViewModel.Graph.Nodes.First(x => x.Id == egoNetworkCenterId).CommunitiesConnected}";
                        graphDto.nodes.First(x => x.id == egoNetworkCenterId).size = 25;
                        graphViewModel.SelectedEgoId        = egoNetworkCenterId;
                        graphViewModel.TeamMembers          = TeamMembers;
                        graphViewModel.SelectedTeamMemberId = graphViewModel.SelectedTeamMemberId;
                        graphViewModel.Graph    = graphViewModel.Graph;
                        graphViewModel.GraphDto = graphDto;

                        graphViewModel.GraphDto.nodes.First(x => x.id == egoNetworkCenterId).color = "#721549";
                        graphViewModel.GraphDto.nodes.First(x => x.id == egoNetworkCenterId).size  = 45;
                        graphViewModel.Graph.SetCommunityNodes();
                    }
                }
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(500, e.Message));
            }
            return(View("GraphView_partial", graphViewModel));
        }
예제 #15
0
        public ActionResult ApplyDateRange(string from, string to, string selectedTeamMemberId)
        {
            GraphViewModel model = new GraphViewModel();

            try
            {
                DateTime fromDate = DateTime.ParseExact(from, "MM/dd/yyyy", null);
                DateTime toDate   = DateTime.ParseExact(to, "MM/dd/yyyy", null);

                string connectionString;
                if (selectedTeamMemberId == null)
                {
                    connectionString   = _importConnectionString;
                    model.FileImported = true;
                }
                else
                {
                    connectionString = GetConnectionStringBasedOnSelectedMember(selectedTeamMemberId);
                }


                FetchItemServiceResponse <Graph <UserDto> > responseGraph = _graphService.FetchEmailsGraph(connectionString, fromDate, toDate);

                responseGraph.Item.SetDegrees();

                List <NodeDto> nodes = responseGraph.Item.Nodes.Select(x => new NodeDto()
                {
                    id    = x.Id,
                    label = x.NodeElement.Name,
                    title = $"Node degree: {x.Degree}",
                    size  = 10,
                    color = "#f5cbee"
                }).ToList();
                List <EdgeDto> edges = responseGraph.Item.Edges.Select(x => new EdgeDto()
                {
                    from = x.Node1.Id, to = x.Node2.Id
                }).ToList();

                GraphDto graphDto = new GraphDto
                {
                    nodes = nodes,
                    edges = edges
                };

                model.TeamMembers = TeamMembers;
                if (selectedTeamMemberId == null)
                {
                    model.SelectedTeamMemberId = null;
                }
                else
                {
                    model.SelectedTeamMemberId = int.Parse(selectedTeamMemberId);
                }
                model.Graph    = responseGraph.Item;
                model.GraphDto = graphDto;
                model.FromDate = fromDate.ToString("MM/dd/yyyy");
                model.ToDate   = toDate.ToString("MM/dd/yyyy");
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(500, e.Message));
            }

            return(View("GraphView_partial", model));
        }