Skip to content

Commit

Permalink
Rename Recommendation Strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsaeedi committed Feb 2, 2020
1 parent 7e31026 commit 9a479ae
Show file tree
Hide file tree
Showing 34 changed files with 291 additions and 137 deletions.
175 changes: 161 additions & 14 deletions src/RelationalGit.Calculation/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ public void AnalyzeSimulations(long actualSimulationId, long[] recommenderSimula

CalculateFaRReduction(actualSimulationId, recommenderSimulationIds, analyzeResultPath);
CalculateExpertiseLoss(actualSimulationId, recommenderSimulationIds, analyzeResultPath);
CalculateIoW(actualSimulationId, recommenderSimulationIds, 10, analyzeResultPath);
//CalculateHoardings(actualSimulationId, recommenderSimulationIds, 10, analyzeResultPath);
CalculateWorkload(actualSimulationId, recommenderSimulationIds, 10, analyzeResultPath);
}

private static void CalculateIoW(long actualId, long[] simulationsIds, int topReviewers, string path)
public void CalculateWorkload(long actualId, long[] simulationsIds, int topReviewers, string path)
{

if (!Directory.Exists(path))
Directory.CreateDirectory(path);

var result = new List<SimulationResult>();

using (var dbContext = GetDbContext())
Expand All @@ -48,10 +51,17 @@ private static void CalculateIoW(long actualId, long[] simulationsIds, int topRe
actualWorkload[period.Id][actualSelectedReviewer.NormalizedReviewerName]++;
}

var lastPeriod = actualWorkload.Max(q => q.Key);
actualWorkload.Remove(lastPeriod);

foreach (var simulationId in simulationsIds)
{
var lossSimulation = dbContext.LossSimulations.Single(q => q.Id == simulationId);
var simulatedSelectedReviewers = dbContext.RecommendedPullRequestReviewers.Where(q => q.LossSimulationId == simulationId).ToArray();

var simulatedSelectedReviewers = dbContext.RecommendedPullRequestReviewers.Where(q => q.LossSimulationId == simulationId)
.Select(q=>new { q.NormalizedReviewerName, q.PullRequestNumber })
.AsNoTracking()
.ToArray();

var simulatedWorkload = new Dictionary<long, Dictionary<string, int>>();
foreach (var simulatedSelectedReviewer in simulatedSelectedReviewers)
Expand Down Expand Up @@ -93,7 +103,7 @@ private static void CalculateIoW(long actualId, long[] simulationsIds, int topRe
}
}

Write(result, Path.Combine(path, "iow.csv"));
Write(result, Path.Combine(path, "workload.csv"));
}

private static void CalculateWorkloadRaw(long[] simulationsIds, int topReviewers, string path)
Expand Down Expand Up @@ -150,16 +160,21 @@ private static void CalculateWorkloadRaw(long[] simulationsIds, int topReviewers
Write(result, Path.Combine(path, "workload_raw.csv"));
}

private static void CalculateExpertiseLoss(long actualId, long[] simulationsIds, string path)
public void CalculateExpertiseLoss(long actualId, long[] simulationsIds, string path)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);

var result = new List<SimulationResult>();

using (var dbContext = GetDbContext())
{
var periods = dbContext.Periods.ToArray();
var pullRequests = dbContext.PullRequests.ToDictionary(q => q.Number);

var actualRecommendationResults = dbContext.PullRequestRecommendationResults.Where(q => q.LossSimulationId == actualId && q.ActualReviewersLength > 0).ToArray();
var actualRecommendationResults = dbContext.PullRequestRecommendationResults.Where(q => q.LossSimulationId == actualId && q.ActualReviewersLength > 0)
.Select(q => new { q.Expertise,q.PullRequestNumber})
.ToArray();

var actualExpertise = new Dictionary<long, List<double>>();
foreach (var actualRecommendationResult in actualRecommendationResults)
Expand All @@ -173,11 +188,17 @@ private static void CalculateExpertiseLoss(long actualId, long[] simulationsIds,
actualExpertise[period.Id].Add(actualRecommendationResult.Expertise);
}


var lastPeriod = actualExpertise.Max(q => q.Key);
actualExpertise.Remove(lastPeriod);

foreach (var simulationId in simulationsIds)
{
var simulatedExpertise = new Dictionary<long, List<double>>();
var lossSimulation = dbContext.LossSimulations.Single(q => q.Id == simulationId);
var simulatedRecommendationResults = dbContext.PullRequestRecommendationResults.Where(q => q.LossSimulationId == simulationId && q.ActualReviewersLength > 0).ToArray();
var simulatedRecommendationResults = dbContext.PullRequestRecommendationResults.Where(q => q.LossSimulationId == simulationId && q.ActualReviewersLength > 0)
.Select(q => new { q.Expertise,q.PullRequestNumber})
.ToArray();

foreach (var simulatedRecommendationResult in simulatedRecommendationResults)
{
Expand Down Expand Up @@ -353,8 +374,11 @@ private static void CalculateExpertiseRaw(long[] simulationsIds, string path)
Write(result, Path.Combine(path, "expertise_raw.csv"));
}

private static void CalculateFaRReduction(long actualId, long[] simulationsIds, string path)
public void CalculateFaRReduction(long actualId, long[] simulationsIds, string path)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);

var result = new List<SimulationResult>();

using (var dbContext = GetDbContext())
Expand All @@ -364,6 +388,10 @@ private static void CalculateFaRReduction(long actualId, long[] simulationsIds,
Select(q => new { Count = q.Count(), PeriodId = q.Key })
.ToArray();

var lastPeriod = actualFaR.Max(q => q.PeriodId);
actualFaR = actualFaR.Where(q => q.PeriodId != lastPeriod)
.ToArray();

foreach (var simulationId in simulationsIds)
{
var lossSimulation = dbContext.LossSimulations.Single(q => q.Id == simulationId);
Expand Down Expand Up @@ -399,6 +427,71 @@ private static void CalculateFaRReduction(long actualId, long[] simulationsIds,

}

public void CalculateFaRReductionBetweenRealityAndNoReviews(long actualId, long noReviewId, string path)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);

var result = new List<SimulationResult>();

using (var dbContext = GetDbContext())
{
var actualFaR = dbContext.FileKnowledgeables.Where(q => q.TotalKnowledgeables < 2 && q.LossSimulationId == actualId).
GroupBy(q => q.PeriodId).
Select(q => new { Count = q.Count(), PeriodId = q.Key })
.ToArray();

var actualProportations = dbContext.FileKnowledgeables.Where(q => q.LossSimulationId == actualId)
.GroupBy(q => q.PeriodId)
.ToArray()
.Select(q => new { Count = actualFaR.Single(f => f.PeriodId == q.Key).Count / (double)q.Count(), PeriodId = q.Key })
.ToArray();


var lastPeriod = actualFaR.Max(q => q.PeriodId);
actualFaR = actualFaR.Where(q => q.PeriodId != lastPeriod).ToArray();

var realityLossSimulation = dbContext.LossSimulations.Single(q => q.Id == actualId);


var actualResult = new SimulationResult()
{
LossSimulation = realityLossSimulation
};

foreach (var simulatedFaRPeriod in actualProportations)
{
actualResult.Results.Add((simulatedFaRPeriod.PeriodId, simulatedFaRPeriod.Count));
}

var noreviewFaR = dbContext.FileKnowledgeables.Where(q => q.TotalKnowledgeables < 2 && q.LossSimulationId == noReviewId).
GroupBy(q => q.PeriodId).
Select(q => new { Count = q.Count(), PeriodId = q.Key })
.ToArray();

var noReviewProportations = dbContext.FileKnowledgeables.Where(q => q.LossSimulationId == noReviewId)
.GroupBy(q => q.PeriodId)
.ToArray()
.Select(q => new { Count = noreviewFaR.Single(f => f.PeriodId == q.Key).Count / (double)q.Count(), PeriodId = q.Key })
.ToArray();

var noReviewsLossSimulation = dbContext.LossSimulations.Single(q => q.Id == noReviewId);

var noReviewsResult = new SimulationResult()
{
LossSimulation = noReviewsLossSimulation
};

foreach (var simulatedFaRPeriod in noReviewProportations)
{
noReviewsResult.Results.Add((simulatedFaRPeriod.PeriodId, simulatedFaRPeriod.Count));
}

Write(new[] { actualResult, noReviewsResult }, Path.Combine(path, "far_between_reality_noreviews.csv"));

}
}

private static void CalculateFaRRaw(long[] simulationsIds, string path)
{
var result = new List<SimulationResult>();
Expand Down Expand Up @@ -475,18 +568,21 @@ private static void CalculateTotalFaRRaw(long[] simulationsIds, string path)

}

private static void Write(List<SimulationResult> simulationResults, string path)
private static void Write(IEnumerable<SimulationResult> simulationResults, string path)
{
using (var dt = new DataTable())
{
dt.Columns.Add("PeriodId", typeof(int));
dt.Columns.Add("PeriodId", typeof(string));

foreach (var simulationResult in simulationResults)
{
dt.Columns.Add(simulationResult.LossSimulation.KnowledgeShareStrategyType + "-" + simulationResult.LossSimulation.Id, typeof(double));
}

var rows = simulationResults[0].Results.Select(q => q.PeriodId).OrderBy(q=>q).Select(q =>
var rows = simulationResults.ElementAt(0).Results
.Select(q => q.PeriodId)
.OrderBy(q=>q)
.Select(q =>
{
var row = dt.NewRow();
row[0] = q;
Expand All @@ -496,13 +592,46 @@ private static void Write(List<SimulationResult> simulationResults, string path)

for (int j = 0; j < rows.Length - 1; j++)
{
for (int i = 0; i < simulationResults.Count; i++)
for (int i = 0; i < simulationResults.Count(); i++)
{
rows[j][i + 1] = simulationResults[i].Results.SingleOrDefault(q => q.PeriodId == (int)rows[j][0]).Value;
rows[j][i + 1] = simulationResults.ElementAt(i).Results.SingleOrDefault(q => q.PeriodId == int.Parse(rows[j][0].ToString())).Value;
}
dt.Rows.Add(rows[j]);
}

for (int j = dt.Rows.Count - 1; j>=0; j--)
{
var isRowConstant = IsRowConstant(dt.Rows[j]);

if (isRowConstant)
{
dt.Rows.RemoveAt(j);
}
}

var rowMedian = dt.NewRow();
rowMedian[0] = "Median";
var medians = new List<double>();

for (int columnIndex = 1; columnIndex < dt.Columns.Count; columnIndex++)
{
var values = new List<double>();

for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
values.Add((double) dt.Rows[rowIndex][columnIndex]);
}

medians.Add(values.Median());
rowMedian[columnIndex] = values.Median();
}

var rowAverage = dt.NewRow();
rowAverage[0] = "Average";
rowAverage[1] = medians.Average();
dt.Rows.Add(rowMedian);
dt.Rows.Add(rowAverage);

using (var writer = new StreamWriter(path))
using (var csv = new CsvWriter(writer))
{
Expand All @@ -524,6 +653,24 @@ private static void Write(List<SimulationResult> simulationResults, string path)
}
}

private static bool IsRowConstant(DataRow row)
{
if (row.Table.Columns.Count <= 2)
return false;

var lastCheckedValue = row[1];

for (int i = 2; i < row.Table.Columns.Count; i++)
{
if(lastCheckedValue.ToString() != row[i].ToString())
{
return false;
}
}

return true;
}

private static GitRepositoryDbContext GetDbContext()
{
return new GitRepositoryDbContext(autoDetectChangesEnabled: false);
Expand Down
56 changes: 39 additions & 17 deletions src/RelationalGit.Calculation/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using CsvHelper;
using MathNet.Numerics.Statistics;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System;
using System.Linq;
using RelationalGit.Data;

Expand All @@ -14,20 +8,48 @@ class Program
{
static void Main(string[] args)
{
var actualId = 38;
var simulationsIds = new int[] { 39,40,41,42,43,44,45};
var path = @"Results\kubernetes_accuracy";
var actualId = 47;

if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (var dbContext = GetDbContext())
{
var simulationsIds = dbContext.LossSimulations.Where(q => q.KnowledgeShareStrategyType == "persist-spreading" || q.KnowledgeShareStrategyType== "TurnoverRec")
.Where(q=>q.EndDateTime>DateTime.MinValue && q.ChangePast)
.Select(q => q.Id).ToArray();

//CalculateWorkloadRaw(simulationsIds,10,path);
//CalculateFaRRaw(simulationsIds, path);
//CalculateTotalFaRRaw(simulationsIds, path);
//CalculateExpertiseRaw(simulationsIds, path);
var path = @"Results\rust-TurnoverRec";

}
var analyzer = new Analyzer();
analyzer.AnalyzeSimulations(actualId, simulationsIds, path);
}

using (var dbContext = GetDbContext())
{
var simulationsIds = dbContext.LossSimulations.Where(q => q.KnowledgeShareStrategyType == "bird" || q.KnowledgeShareStrategyType == "cHRev")
.Where(q => q.EndDateTime > DateTime.MinValue && q.ChangePast)
.Select(q => q.Id).ToArray();

var path = @"Results\roslyn-chrev";

var analyzer = new Analyzer();
analyzer.AnalyzeSimulations(actualId, simulationsIds, path);
}

using (var dbContext = GetDbContext())
{
var simulationsIds = dbContext.LossSimulations.Where(q => q.KnowledgeShareStrategyType == "sophia" || q.KnowledgeShareStrategyType == "Sofia")
.Where(q => q.EndDateTime > DateTime.MinValue && q.ChangePast)
.Select(q => q.Id).ToArray();

var path = @"Results\roslyn-sophia";

var analyzer = new Analyzer();
analyzer.AnalyzeSimulations(actualId, simulationsIds, path);
}
}

private static GitRepositoryDbContext GetDbContext()
{
return new GitRepositoryDbContext(autoDetectChangesEnabled: false);
}
}
}
6 changes: 5 additions & 1 deletion src/RelationalGit.CommandLine/CommandLine/InputOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ internal class InputOption
[Option("change-past")]
public bool? ChangePast { get; set; }

[Option("actual-simulation")]
[Option("reality-simulation")]
public long ActualSimulationId { get; set; }

[Option("no-reviews-simulation")]
public long? NoReviewsSimulationId { get; set; }

[Option("recommender-simulation")]
public IEnumerable<long> RecommenderSimulationId { get; set; }

Expand Down Expand Up @@ -171,6 +174,7 @@ internal InputOption Override(InputOption fileConfigurationOption)
overridedInputOption.ChangePast = Override(ChangePast, fileConfigurationOption.ChangePast);
overridedInputOption.AnalyzeResultPath = Override(AnalyzeResultPath, fileConfigurationOption.AnalyzeResultPath);
overridedInputOption.ActualSimulationId = Override(ActualSimulationId, fileConfigurationOption.ActualSimulationId);
overridedInputOption.NoReviewsSimulationId = Override(NoReviewsSimulationId, fileConfigurationOption.NoReviewsSimulationId);
overridedInputOption.RecommenderSimulationId = Override(RecommenderSimulationId, fileConfigurationOption.RecommenderSimulationId);

return overridedInputOption;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using Diacritics.Extensions;
using F23.StringSimilarity;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using RelationalGit.Data;

Expand Down
Loading

0 comments on commit 9a479ae

Please sign in to comment.