Temporary repo to track my changes on LTS functions app porting
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CDP
  7. {
  8. public class ContactsDocument
  9. {
  10. public string id
  11. {
  12. get
  13. {
  14. return GenPartitionKey(AppKey, UserId, Index);
  15. }
  16. }
  17. public string AppKey { get; set; }
  18. public string UserId { get; set; }
  19. public int Index { get; set; }
  20. public List<ContactRecord> Records { get; set; }
  21. public ContactsDocument()
  22. {
  23. Index = 0;
  24. Records = new List<ContactRecord>();
  25. }
  26. public static string GenRootPartitionKey(string id, DateTime dt)
  27. {
  28. return string.Format($"{id}-{dt.Year}-{dt.DayOfYear}");
  29. }
  30. public static string GenPartitionKey(string appKey, string userId, int index = 0)
  31. {
  32. return string.Format($"{appKey}-{userId}-{index}");
  33. }
  34. }
  35. public class ContactRecord
  36. {
  37. public string Name { get; set; }
  38. public string Email { get; set; }
  39. public string Phone { get; set; }
  40. public string Address { get; set; }
  41. public string Company { get; set; }
  42. public DateTime EventTime { get; set; }
  43. public ContactRecord()
  44. {
  45. EventTime = DateTime.UtcNow;
  46. }
  47. public ContactRecord(string name, string email, string phone, string address, string company)
  48. {
  49. Name = name;
  50. Email = email;
  51. Phone = phone;
  52. Address = address;
  53. Company = company;
  54. EventTime = DateTime.UtcNow;
  55. }
  56. public int CalculateRecordSize()
  57. {
  58. int size = 0;
  59. size += 16; // sizeof(DateTime); // EventTime
  60. size += Encoding.UTF8.GetByteCount(Name) == 0 ? 20 : Encoding.UTF8.GetByteCount(Name); // UserId
  61. size += Encoding.UTF8.GetByteCount(Email) == 0 ? 50 : Encoding.UTF8.GetByteCount(Email);
  62. size += Encoding.UTF8.GetByteCount(Phone) == 0 ? 15 : Encoding.UTF8.GetByteCount(Phone);
  63. size += Encoding.UTF8.GetByteCount(Address) == 0 ? 70 : Encoding.UTF8.GetByteCount(Address);
  64. size += Encoding.UTF8.GetByteCount(Company) == 0 ? 20 : Encoding.UTF8.GetByteCount(Company);
  65. size += sizeof(int);
  66. return size;
  67. }
  68. }
  69. public class MetadataDocumentContact
  70. {
  71. public string id { get; set; }
  72. public List<string> PartitionKeys { get; set; }
  73. public MetadataDocumentContact()
  74. {
  75. PartitionKeys = new List<string>();
  76. id = id + "-meta";
  77. }
  78. public string GetLatestKey(string appKey, string userId)
  79. {
  80. PartitionKeys.Sort();
  81. if (PartitionKeys.Count == 0)
  82. return GroupsDocument.GenPartitionKey(appKey, userId);
  83. return PartitionKeys.Last();
  84. }
  85. }
  86. }