Пример #1
0
        static void Main(string[] args)
        {
            // Read and validate configurations
            ColleagueTrackerConfig config = ColleagueTrackerConfig.GetConfig();

            if (config.Teams == null || config.Teams.Count == 0)
            {
                Console.WriteLine("No configurations found. Create <ColleagueTracker> section in ColleagueTracker.exe.config and try again.");
                return;
            }

            ADServices.LdapPath             = config.LdapPath;
            ADServices.AlternativeLdapPath  = config.AlternativeLdapPath != null && config.AlternativeLdapPath.Length > 0 ? config.AlternativeLdapPath : null;
            ADServices.AlternativeLdapPath2 = config.AlternativeLdapPath2 != null && config.AlternativeLdapPath2.Length > 0 ? config.AlternativeLdapPath2 : null;

            Stopwatch sw = new Stopwatch(); sw.Start();

            // Process each of the configurations
            List <UserInfo> teamMembers = new List <UserInfo>();

            foreach (TeamConfigurationElement item in config.Teams)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine();
                Console.Write("Team '{0}' ", item.Name);
                Console.ForegroundColor = ConsoleColor.Gray;

                // get all direct reports of the different managers
                string[] managers = item.Managers.Split(',');
                foreach (string manager in managers)
                {
                    teamMembers.AddRange(ADServices.GetDirectReports(manager));
                }

                teamMembers = teamMembers.OrderBy(m => m.Manager).ToList();

                Console.WriteLine();
                Console.WriteLine("Team '{0}' has {1} members.", item.Name, teamMembers.Count);

                // now check the deltas
                string userFilename   = string.Format("users_{0}.json", item.Name);
                string deltasFilename = string.Format("deltas_{0}.json", item.Name);

                int changes = UserStore.StoreAndCompare(userFilename, deltasFilename, teamMembers);

                Console.WriteLine("Number of changes: {0}", changes);
                teamMembers.Clear();
            }

            sw.Stop();
            Console.WriteLine("\nDone. Elapsed time: {0} seconds. Press any key.", sw.ElapsedMilliseconds / 1000);
            Console.ReadLine();
        }
Пример #2
0
        public static int StoreAndCompare(string storefilename, string deltafilename, List <UserInfo> currentUsers)
        {
            int deltas = 0;

            // read current records
            List <string> previousUsers = new List <string>();

            if (File.Exists(storefilename))
            {
                StreamReader usersReader = new StreamReader(storefilename);
                previousUsers = JsonConvert.DeserializeObject <List <string> >(usersReader.ReadToEnd());
                usersReader.Close();
            }

            List <string> deltaUsers = new List <string>();

            if (File.Exists(deltafilename))
            {
                StreamReader deltasReader = new StreamReader(deltafilename);
                deltaUsers = JsonConvert.DeserializeObject <List <string> >(deltasReader.ReadToEnd());
                deltasReader.Close();
            }

            // compare

            // find new hires
            foreach (UserInfo currentUser in currentUsers)
            {
                if (!previousUsers.Contains(currentUser.Login))
                {
                    Console.Write("- NEW: ");

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write(currentUser.Name);
                    Console.ForegroundColor = ConsoleColor.Gray;

                    Console.Write(" is a ");

                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write(currentUser.JobTitle);
                    Console.ForegroundColor = ConsoleColor.Gray;

                    Console.Write(" managed by ");

                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.Write(ADServices.GetUserInfo(currentUser.Manager).Name);
                    Console.ForegroundColor = ConsoleColor.Gray;

                    Console.Write(" in ");

                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine(currentUser.Department);
                    Console.ForegroundColor = ConsoleColor.Gray;

                    deltaUsers.Add(string.Format("joined, {0}, {1}", currentUser.Login, DateTime.Now.ToShortDateString()));
                    deltas++;
                }
            }

            // find those who left the company or moved to another team
            foreach (string user in previousUsers)
            {
                if (!currentUsers.Any(cu => cu.Login == user))
                {
                    UserInfo uinfo = ADServices.GetUserInfo(user);
                    if (uinfo == null)
                    {
                        Console.WriteLine("- LEFT COMPANY: " + user);
                    }
                    else
                    {
                        Console.Write("- MOVED: ");

                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(uinfo.Name);
                        Console.ForegroundColor = ConsoleColor.Gray;

                        Console.Write(" is a ");

                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write(uinfo.JobTitle);
                        Console.ForegroundColor = ConsoleColor.Gray;

                        Console.Write(" managed by ");

                        Console.ForegroundColor = ConsoleColor.Magenta;
                        if (uinfo.Manager.Contains(",") && uinfo.Manager.Contains("="))
                        {
                            Console.Write(uinfo.Manager.Split(',')[0].Split('=')[1]);
                        }
                        else
                        {
                            Console.Write(uinfo.Manager);
                        }

                        Console.ForegroundColor = ConsoleColor.Gray;

                        Console.Write(" in ");

                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        Console.WriteLine(uinfo.Department);
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }

                    deltaUsers.Add(string.Format("left, {0}, {1}", user, DateTime.Now.ToShortDateString()));
                    deltas++;
                }
            }

            // write updates
            StreamWriter usersWriter = new StreamWriter(storefilename);

            usersWriter.Write(JsonConvert.SerializeObject(currentUsers.Select(cu => cu.Login).ToList()));
            usersWriter.Close();

            StreamWriter deltaWriter = new StreamWriter(deltafilename);

            deltaWriter.Write(JsonConvert.SerializeObject(deltaUsers));
            deltaWriter.Close();

            return(deltas);
        }