//------------------------------------------------------------------------- public void AddTag(BuildTag tag) { if (HasTag(tag) == false) { Tags.Add(tag); } }
//------------------------------------------------------------------------- // Returns stats provider for the specified tag, use null for all stats. public IBuildStatsProvider GetStats(string tag = null) { if (tag == null) { tag = "All"; } BuildTag tagOb = BuildTag.GetTag(tag, false); if (tagOb == null) { return(null); } if (Stats.ContainsKey(tagOb)) { return(Stats[tagOb]); } return(null); }
//------------------------------------------------------------------------- // Returns a tag if it already exists, otherwise creates it first. public static BuildTag GetTag( string text, bool createTagIfNotFound = true) { foreach (string tagText in Tags.Keys) { if (text.ToLower() == tagText.ToLower()) { return(Tags[tagText]); } } if (createTagIfNotFound == false) { return(null); } BuildTag tag = new BuildTag(text); Tags.Add(text, tag); return(tag); }
//------------------------------------------------------------------------- public void AddBuild( DateTime start, DateTime?end, string[] tags) { // Create new stats object. BuildStats stats = new BuildStats(start, end); // Include these stats under the 'all' tag. stats.AddTag(AllBuilds); // Add the tags to the stats object. foreach (string t in tags) { // 'All' is reserved. if (t.ToLower() == "all") { throw new Exception("The tag 'all' is reserved for internal use."); } // Add the tag to the stats. BuildTag tag = BuildTag.GetTag(t); stats.AddTag(tag); } // Add build to the relevant tags' build collection. foreach (BuildTag tag in stats.GetTags()) { if (Stats.ContainsKey(tag) == false) { Stats.Add(tag, new BuildStatsCollection()); } Stats[tag].AddStats(stats); } }
//------------------------------------------------------------------------- public bool HasTag(BuildTag tag) { return(Tags.Contains(tag)); }
//------------------------------------------------------------------------- private static void OutputStats(BuildTag tag) { Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine(); if (tag != null) { Console.WriteLine( "Tag: {0}" + Environment.NewLine, tag.Text); } Console.WriteLine( "{0,-20} | {1,-9} | {2,-5} | {3,-5} | {4,-5} | {5,-5} | {6,-5} | {7,-5} |", "Project", "Completed", "Tot h", "Tot m", "Avg m", "Avg s", "Max m", "Max s"); Console.WriteLine("----------------------------------------------------------------------------------"); double totTotalBuildTime = 0.0; double totAvgBuildTime = 0.0; double totMaxBuildTime = 0.0; foreach (Project prj in Project.GetProjects()) { IBuildStatsProvider stats = prj.GetStats(tag?.Text); if (stats == null) { continue; } Console.WriteLine( "{0,-20} | {1,4}/{2,-4} | {3,5} | {4,5} | {5,5} | {6,5} | {7,5} | {8,5} |", prj.Name, stats.CompletedBuildsCount, stats.TotalBuildsCount, (int)stats.TotalBuildTime.TotalHours, (int)stats.TotalBuildTime.TotalMinutes, (int)stats.AverageBuildTime.TotalMinutes, (int)stats.AverageBuildTime.TotalSeconds, (int)stats.MaxBuildTime.TotalMinutes, (int)stats.MaxBuildTime.TotalSeconds); totTotalBuildTime += stats.TotalBuildTime.TotalMinutes; totAvgBuildTime += stats.AverageBuildTime.TotalMinutes; totMaxBuildTime += stats.MaxBuildTime.TotalMinutes; } Console.WriteLine("----------------------------------------------------------------------------------"); Console.WriteLine( "{0,-20} | {1,4} {2,-4} | {3,5} | {4,5} | {5,5} | {6,5} | {7,5} | {8,5} |", "", "", "", "", (int)totTotalBuildTime, (int)totAvgBuildTime, "", (int)totMaxBuildTime, ""); Console.WriteLine(Environment.NewLine); }
//------------------------------------------------------------------------- private void ProcessFilesAndOutputSummary( string[] files, bool significantBuildsOnly) { foreach (string f in files) { try { // Parse the log file. BuildLogFile log = new BuildLogFile(f); string[] tags = log.GetTags(); IReadOnlyCollection <BuildLogFile.LogEntry> entries = log.GetEntries(); // Extract the project name. string projectName = tags[1]; // Add log entries to collection. foreach (BuildLogFile.LogEntry entry in entries) { if (LogEntriesByProject.ContainsKey(projectName) == false) { LogEntriesByProject.Add(projectName, new List <BuildLogFile.LogEntry>()); } LogEntriesByProject[projectName].Add(entry); } } catch (Exception) { // TODO } } // Sort the entries. foreach (List <BuildLogFile.LogEntry> entries in LogEntriesByProject.Values) { entries.Sort(); } // Create stats. foreach (string projectName in LogEntriesByProject.Keys) { Project project = Project.GetProject(projectName); List <BuildLogFile.LogEntry> entries = LogEntriesByProject[projectName]; for (int i = 0; i < entries.Count - 1; i++) { if (entries[i].EntryType == BuildLogFile.LogEntry.LogEntryType.BUILD_STARTED && entries[i + 1].EntryType == BuildLogFile.LogEntry.LogEntryType.BUILD_ENDED) { if (significantBuildsOnly == false || (entries[i + 1].Timestamp - entries[i].Timestamp).TotalSeconds > 5) { project.AddBuild( entries[i].Timestamp, entries[i + 1].Timestamp, entries[i].Tags.ToArray()); } } else if (entries[i].EntryType == BuildLogFile.LogEntry.LogEntryType.BUILD_STARTED) { if (significantBuildsOnly == false) { project.AddBuild( entries[i].Timestamp, null, entries[i].Tags.ToArray()); } } } } foreach (Project prj in Project.GetProjects()) { IBuildStatsProvider stats = prj.GetStats("All"); Stats.Add(stats); } // Summary. foreach (BuildTag tag in BuildTag.GetTags()) { OutputStats(tag); } }