Skip to content

Commit

Permalink
Fix: Trello limit constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMulligan committed Mar 6, 2020
1 parent 847f534 commit c1997c1
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 25 deletions.
1 change: 1 addition & 0 deletions Constants/SolutionConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static class TrelloListIndexMap
public const string kTrelloUserSecret = "861101898508498761cd2952b258926b568326994f44ef23fd85ca3685667525";
public const string kTrelloUserToken = "9a4b5cc8411e7380cf8fc392341f118e2f32869ac50a134fb9bd81f43fb0fe62";
public const string kTrelloBoardId = "5cdf08913ae8753993cfdd9c";
public const int kTrelloLimitMax = 1000;

public const string kLabelSeperator = ":";

Expand Down
13 changes: 13 additions & 0 deletions Models/TrelloActionDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ public class TrelloActionDto
public TrelloActionDataDto data { get; set; }


public DateTime DateFromId
{
get
{
var timestamp = int.Parse(this.id.Substring(0, 8), System.Globalization.NumberStyles.HexNumber);

var dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(timestamp);

return dateTimeOffset.UtcDateTime;
}
}


public TrelloActionDto()
{
data = new TrelloActionDataDto();
Expand Down
14 changes: 14 additions & 0 deletions Models/TrelloCardDto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace esdc_sa_appdev_reporting_api.Models
Expand All @@ -13,6 +14,19 @@ public class TrelloCardDto
public List<string> idMembers { get; set; }


public DateTime DateFromId
{
get
{
var timestamp = int.Parse(this.id.Substring(0, 8), System.Globalization.NumberStyles.HexNumber);

var dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(timestamp);

return dateTimeOffset.UtcDateTime;
}
}


public TrelloCardDto()
{
idLabels = new List<string>();
Expand Down
148 changes: 123 additions & 25 deletions Services/TrelloService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public async Task<List<GeneralReportResult>> GetGeneralReportResults()
var trelloLists = await this.GetTrelloLists();
var trelloLabels = await this.GetTrelloLabels();
var trelloMembers = await this.GetTrelloMembers();
var trelloCards = await this.GetTrelloCards();
var trelloCardMoveActions = await this.GetTrelloCardMoveActions();
var trelloCardCreatedActions = await this.GetTrelloCardCreateActions();
var trelloCards = await this.GetAllTrelloCards();
var trelloCardMoveActions = await this.GetAllTrelloCardMoveActions();
var trelloCardCreatedActions = await this.GetAllTrelloCardCreateActions();

// Pre-sort
trelloCardMoveActions
Expand Down Expand Up @@ -264,7 +264,7 @@ public async Task<List<TrelloListDto>> GetTrelloLists()
Console.WriteLine($"Error in GetTrelloLists: {httpRequestException.Message}");
}

return null;
return new List<TrelloListDto>();
}
}

Expand Down Expand Up @@ -293,7 +293,7 @@ public async Task<List<TrelloLabelsDto>> GetTrelloLabels()
Console.WriteLine($"Error in GetTrelloLabels: {httpRequestException.Message}");
}

return null;
return new List<TrelloLabelsDto>();
}
}

Expand Down Expand Up @@ -321,20 +321,55 @@ public async Task<List<TrelloMemberDto>> GetTrelloMembers()
Console.WriteLine($"Error in GetTrelloMembers: {httpRequestException.Message}");
}

return null;
return new List<TrelloMemberDto>();
}
}


public async Task<List<TrelloCardDto>> GetTrelloCards()
public async Task<List<TrelloCardDto>> GetAllTrelloCards()
{
var trelloCards = new List<TrelloCardDto>();
var beforeDate = DateTime.Now;
var isBreak = false;

while (isBreak == false)
{
var results = await this.GetTrelloCards(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();
}

trelloCards.AddRange(results);
}

return trelloCards;
}


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

var response = await http.GetAsync(url);

Expand All @@ -349,26 +384,58 @@ public async Task<List<TrelloCardDto>> GetTrelloCards()
Console.WriteLine($"Error in GetTrelloCards: {httpRequestException.Message}");
}

return null;
return new List<TrelloCardDto>();
}
}


public async Task<List<TrelloActionDto>> GetTrelloCardCreateActions()
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;

while (isBreak == false)
{
var results = await this.GetTrelloCardMoveActions(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);
}

// Only keep the records that where list transfers.
return trelloActions.Where(x => (x.IsListTransfer() == true)).ToList();
}


public async Task<List<TrelloActionDto>> GetTrelloCardMoveActions(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
//+ "&before=2019-07-01"
+ "&filter=updateCard"
//+ "&since=2019-06-01"
+ "&filter=createCard"
+ "&limit=1000";
+ "&before=" + beforeDate.ToString(CoreConstants.Formats.DtmZuluIso)
+ "&limit=" + SolutionConstants.kTrelloLimitMax.ToString();

var response = await http.GetAsync(url);

Expand All @@ -383,44 +450,75 @@ public async Task<List<TrelloActionDto>> GetTrelloCardCreateActions()
Console.WriteLine($"Error in GetTrelloCardMoveActions: {httpRequestException.Message}");
}

return null;
return new List<TrelloActionDto>();
}
}


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

while (isBreak == false)
{
var results = await this.GetTrelloCardCreateActions(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>> GetTrelloCardMoveActions()
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
{
var url = "https://api.trello.com/1/boards/" + SolutionConstants.kTrelloBoardId + "/actions/"
+ "?key=" + SolutionConstants.kTrelloAppKey
+ "&token=" + SolutionConstants.kTrelloUserToken
//+ "&before=2019-07-01"
//+ "&since=2019-06-01"
+ "&filter=updateCard"
+ "&limit=1000";
+ "&filter=createCard"
+ "&before=" + beforeDate.ToString(CoreConstants.Formats.DtmZuluIso)
+ "&limit=" + SolutionConstants.kTrelloLimitMax.ToString();

var response = await http.GetAsync(url);

response.EnsureSuccessStatusCode();

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

var list = JsonConvert.DeserializeObject<List<TrelloActionDto>>(jsonResult);

return list.Where(x => (x.IsListTransfer() == true)).ToList();
return JsonConvert.DeserializeObject<List<TrelloActionDto>>(jsonResult);
}
catch (HttpRequestException httpRequestException)
{
Console.WriteLine($"Error in GetTrelloCardMoveActions: {httpRequestException.Message}");
}

return null;
return new List<TrelloActionDto>();
}
}


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

Expand Down

0 comments on commit c1997c1

Please sign in to comment.