Skip to content

Commit

Permalink
Fix: start and completed dates for copyCard and createCard (in Done).
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMulligan committed Mar 9, 2020
1 parent c1997c1 commit a34a50d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Models/TrelloCardDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public DateTime DateFromId
{
get
{
// https://help.trello.com/article/759-getting-the-time-a-card-or-board-was-created

var timestamp = int.Parse(this.id.Substring(0, 8), System.Globalization.NumberStyles.HexNumber);

var dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(timestamp);
Expand Down
113 changes: 105 additions & 8 deletions Services/TrelloService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@ the tallied number of cards per list in order of Backlog to Done.
public async Task<List<GeneralReportResult>> GetGeneralReportResults()
{
// 1. Gather all cards
// 2. Compute date started and completed
// 2.a) Cards that were transferred from Backlog or Committed
// 2.b) Cards that were created directly in either In Progress, On Hold or Done.
// 3. Compute number of days on hold
// 2. Compute a card's start date
// 3. Compute a card's completed date

var results = new List<GeneralReportResult>();

Expand All @@ -147,6 +145,7 @@ public async Task<List<GeneralReportResult>> GetGeneralReportResults()
var trelloCards = await this.GetAllTrelloCards();
var trelloCardMoveActions = await this.GetAllTrelloCardMoveActions();
var trelloCardCreatedActions = await this.GetAllTrelloCardCreateActions();
var trelloCardCopyActions = await this.GetAllTrelloCardCopyActions();

// Pre-sort
trelloCardMoveActions
Expand Down Expand Up @@ -191,11 +190,12 @@ public async Task<List<GeneralReportResult>> GetGeneralReportResults()
);
}

// Step 2
// Card in backlog and committed aren't considered started.
if ((result.CardCurrentListTitle != SolutionConstants.TrelloLists.Backlog) &&
(result.CardCurrentListTitle != SolutionConstants.TrelloLists.Committed))
{
// 2.a)
// 2.a) The start date is taken from the first action that moved the card in the 'In Progress' list.
var action = trelloCardMoveActions
.Where
(
Expand All @@ -206,7 +206,8 @@ public async Task<List<GeneralReportResult>> GetGeneralReportResults()
.OrderBy(x => x.date)
.FirstOrDefault();

// 2.b)
// 2.b) If the card wasn't moved into the 'In Progress' list, but was directly created in a list,
// then the start date is taken from its create action.
if (action == null)
{
action = trelloCardCreatedActions
Expand All @@ -215,12 +216,24 @@ public async Task<List<GeneralReportResult>> GetGeneralReportResults()
.FirstOrDefault();
}

// 2.c) If the card wasn't created in the 'In Progress' list, but was directly copied in a list,
// then the start date is taken from its copy action.
if (action == null)
{
action = trelloCardCopyActions
.Where(x => x.data.card.id == card.id)
.OrderBy(x => x.date)
.FirstOrDefault();
}

result.CardDateStarted = action?.date;
}

// Step 3
// Only cards in done are considered completed.
if (result.CardCurrentListTitle == SolutionConstants.TrelloLists.Done)
{
// 3.a) The card completed date is taken from the last action that placed the card in the 'Done' list.
var action = trelloCardMoveActions
.Where
(
Expand All @@ -231,6 +244,26 @@ public async Task<List<GeneralReportResult>> GetGeneralReportResults()
.OrderByDescending(x => x.date)
.FirstOrDefault();

// 3.b) If the card wasn't moved into the 'Done' list, but was directly created in it,
// then the start date is taken from its create action.
if (action == null)
{
action = trelloCardCreatedActions
.Where(x => x.data.card.id == card.id)
.OrderBy(x => x.date)
.FirstOrDefault();
}

// 3.c) If the card wasn't created in the 'Done' list, but was directly copied in it,
// then the start date is taken from its copy action.
if (action == null)
{
action = trelloCardCopyActions
.Where(x => x.data.card.id == card.id)
.OrderBy(x => x.date)
.FirstOrDefault();
}

result.CardDateCompleted = action?.date;
}

Expand Down Expand Up @@ -391,6 +424,8 @@ public async Task<List<TrelloCardDto>> GetTrelloCards(DateTime beforeDate)

public async Task<List<TrelloActionDto>> GetAllTrelloCardMoveActions()
{
// Reference: https://stackoverflow.com/questions/51777063/how-can-i-get-all-actions-for-a-board-using-trellos-rest-api

var trelloActions = new List<TrelloActionDto>();
var beforeDate = DateTime.Now;
var isBreak = false;
Expand Down Expand Up @@ -490,8 +525,6 @@ public async Task<List<TrelloActionDto>> GetAllTrelloCardCreateActions()

public async Task<List<TrelloActionDto>> GetTrelloCardCreateActions(DateTime beforeDate)
{
// Reference: https://stackoverflow.com/questions/51777063/how-can-i-get-all-actions-for-a-board-using-trellos-rest-api

using (var http = new HttpClient())
{
try
Expand Down Expand Up @@ -519,6 +552,70 @@ public async Task<List<TrelloActionDto>> GetTrelloCardCreateActions(DateTime bef
return new List<TrelloActionDto>();
}
}


public async Task<List<TrelloActionDto>> GetAllTrelloCardCopyActions()
{
var trelloActions = new List<TrelloActionDto>();
var beforeDate = DateTime.Now;
var isBreak = false;

while (isBreak == false)
{
var results = await this.GetTrelloCardCopyActions(beforeDate);

if (results.Count < SolutionConstants.kTrelloLimitMax)
{
isBreak = true;
}
else
{
// Grab the earliest date of the results.
var dateMin = results.Min(x => x.DateFromId);

// Because the earliest timestamp record may not necessarily be the last record within a given day, the target date is the last date + 1.
beforeDate = dateMin.AddDays(1).Date;

// Only keep the results of the last known full day.
results = results.Where(x => x.DateFromId >= beforeDate).ToList();
}

trelloActions.AddRange(results);
}

return trelloActions;
}


public async Task<List<TrelloActionDto>> GetTrelloCardCopyActions(DateTime beforeDate)
{
using (var http = new HttpClient())
{
try
{
var url = "https://api.trello.com/1/boards/" + SolutionConstants.kTrelloBoardId + "/actions/"
+ "?key=" + SolutionConstants.kTrelloAppKey
+ "&token=" + SolutionConstants.kTrelloUserToken
+ "&filter=copyCard"
+ "&before=" + beforeDate.ToString(CoreConstants.Formats.DtmZuluIso)
+ "&limit=" + SolutionConstants.kTrelloLimitMax.ToString();

var response = await http.GetAsync(url);

response.EnsureSuccessStatusCode();

var jsonResult = await response.Content.ReadAsStringAsync();

return JsonConvert.DeserializeObject<List<TrelloActionDto>>(jsonResult);
}
catch (HttpRequestException httpRequestException)
{
Console.WriteLine($"Error in GetTrelloCardCopyActions: {httpRequestException.Message}");
}

return new List<TrelloActionDto>();
}
}

#region --- Private -------------------------------------------------------

Expand Down

0 comments on commit a34a50d

Please sign in to comment.