ABTExperimentPayload.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseABTesting/Sources/Private/ABTExperimentPayload.h"
  15. static NSString *const kExperimentPayloadKeyExperimentID = @"experimentId";
  16. static NSString *const kExperimentPayloadKeyVariantID = @"variantId";
  17. // Start time can either be a date string or integer (milliseconds since 1970).
  18. static NSString *const kExperimentPayloadKeyExperimentStartTime = @"experimentStartTime";
  19. static NSString *const kExperimentPayloadKeyExperimentStartTimeMillis =
  20. @"experimentStartTimeMillis";
  21. static NSString *const kExperimentPayloadKeyTriggerEvent = @"triggerEvent";
  22. static NSString *const kExperimentPayloadKeyTriggerTimeoutMillis = @"triggerTimeoutMillis";
  23. static NSString *const kExperimentPayloadKeyTimeToLiveMillis = @"timeToLiveMillis";
  24. static NSString *const kExperimentPayloadKeySetEventToLog = @"setEventToLog";
  25. static NSString *const kExperimentPayloadKeyActivateEventToLog = @"activateEventToLog";
  26. static NSString *const kExperimentPayloadKeyClearEventToLog = @"clearEventToLog";
  27. static NSString *const kExperimentPayloadKeyTimeoutEventToLog = @"timeoutEventToLog";
  28. static NSString *const kExperimentPayloadKeyTTLExpiryEventToLog = @"ttlExpiryEventToLog";
  29. static NSString *const kExperimentPayloadKeyOverflowPolicy = @"overflowPolicy";
  30. static NSString *const kExperimentPayloadValueDiscardOldestOverflowPolicy = @"DISCARD_OLDEST";
  31. static NSString *const kExperimentPayloadValueIgnoreNewestOverflowPolicy = @"IGNORE_NEWEST";
  32. static NSString *const kExperimentPayloadKeyOngoingExperiments = @"ongoingExperiments";
  33. @implementation ABTExperimentLite
  34. - (instancetype)initWithExperimentId:(NSString *)experimentId {
  35. if (self = [super init]) {
  36. _experimentId = experimentId;
  37. }
  38. return self;
  39. }
  40. @end
  41. @implementation ABTExperimentPayload
  42. + (NSDateFormatter *)experimentStartTimeFormatter {
  43. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  44. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
  45. [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  46. // Locale needs to be hardcoded. See
  47. // https://developer.apple.com/library/ios/#qa/qa1480/_index.html for more details.
  48. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  49. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  50. return dateFormatter;
  51. }
  52. + (instancetype)parseFromData:(NSData *)data {
  53. NSError *error;
  54. NSDictionary *experimentDictionary =
  55. [NSJSONSerialization JSONObjectWithData:data
  56. options:NSJSONReadingAllowFragments
  57. error:&error];
  58. if (error != nil) {
  59. return nil;
  60. } else {
  61. return [[ABTExperimentPayload alloc] initWithDictionary:experimentDictionary];
  62. }
  63. }
  64. - (instancetype)initWithDictionary:(NSDictionary<NSString *, id> *)dictionary {
  65. if (self = [super init]) {
  66. _experimentId = dictionary[kExperimentPayloadKeyExperimentID];
  67. _variantId = dictionary[kExperimentPayloadKeyVariantID];
  68. _triggerEvent = dictionary[kExperimentPayloadKeyTriggerEvent];
  69. _setEventToLog = dictionary[kExperimentPayloadKeySetEventToLog];
  70. _activateEventToLog = dictionary[kExperimentPayloadKeyActivateEventToLog];
  71. _clearEventToLog = dictionary[kExperimentPayloadKeyClearEventToLog];
  72. _timeoutEventToLog = dictionary[kExperimentPayloadKeyTimeoutEventToLog];
  73. _ttlExpiryEventToLog = dictionary[kExperimentPayloadKeyTTLExpiryEventToLog];
  74. // Experiment start time can either be in the form of a date string or milliseconds since 1970.
  75. if (dictionary[kExperimentPayloadKeyExperimentStartTime]) {
  76. // Convert from date string.
  77. NSDate *experimentStartTime = [[[self class] experimentStartTimeFormatter]
  78. dateFromString:dictionary[kExperimentPayloadKeyExperimentStartTime]];
  79. _experimentStartTimeMillis =
  80. [@([experimentStartTime timeIntervalSince1970] * 1000) longLongValue];
  81. } else if (dictionary[kExperimentPayloadKeyExperimentStartTimeMillis]) {
  82. // Simply store milliseconds.
  83. _experimentStartTimeMillis =
  84. [dictionary[kExperimentPayloadKeyExperimentStartTimeMillis] longLongValue];
  85. ;
  86. }
  87. _triggerTimeoutMillis = [dictionary[kExperimentPayloadKeyTriggerTimeoutMillis] longLongValue];
  88. _timeToLiveMillis = [dictionary[kExperimentPayloadKeyTimeToLiveMillis] longLongValue];
  89. // Overflow policy can be an integer, or string e.g. "DISCARD_OLDEST" or "IGNORE_NEWEST".
  90. if ([dictionary[kExperimentPayloadKeyOverflowPolicy] isKindOfClass:[NSString class]]) {
  91. // If it's a string, pick against the preset string values.
  92. NSString *policy = dictionary[kExperimentPayloadKeyOverflowPolicy];
  93. if ([policy isEqualToString:kExperimentPayloadValueDiscardOldestOverflowPolicy]) {
  94. _overflowPolicy = ABTExperimentPayloadExperimentOverflowPolicyDiscardOldest;
  95. } else if ([policy isEqualToString:kExperimentPayloadValueIgnoreNewestOverflowPolicy]) {
  96. _overflowPolicy = ABTExperimentPayloadExperimentOverflowPolicyIgnoreNewest;
  97. } else {
  98. _overflowPolicy = ABTExperimentPayloadExperimentOverflowPolicyUnrecognizedValue;
  99. }
  100. } else {
  101. _overflowPolicy = [dictionary[kExperimentPayloadKeyOverflowPolicy] intValue];
  102. }
  103. NSMutableArray<ABTExperimentLite *> *ongoingExperiments = [[NSMutableArray alloc] init];
  104. NSArray<NSDictionary<NSString *, NSString *> *> *ongoingExperimentsArray =
  105. dictionary[kExperimentPayloadKeyOngoingExperiments];
  106. for (NSDictionary<NSString *, NSString *> *experimentDictionary in ongoingExperimentsArray) {
  107. NSString *experimentId = experimentDictionary[kExperimentPayloadKeyExperimentID];
  108. if (experimentId) {
  109. ABTExperimentLite *liteExperiment =
  110. [[ABTExperimentLite alloc] initWithExperimentId:experimentId];
  111. [ongoingExperiments addObject:liteExperiment];
  112. }
  113. }
  114. _ongoingExperiments = [ongoingExperiments copy];
  115. }
  116. return self;
  117. }
  118. - (void)clearTriggerEvent {
  119. _triggerEvent = nil;
  120. }
  121. - (BOOL)overflowPolicyIsValid {
  122. return self.overflowPolicy == ABTExperimentPayloadExperimentOverflowPolicyIgnoreNewest ||
  123. self.overflowPolicy == ABTExperimentPayloadExperimentOverflowPolicyDiscardOldest;
  124. }
  125. - (void)setOverflowPolicy:(ABTExperimentPayloadExperimentOverflowPolicy)overflowPolicy {
  126. _overflowPolicy = overflowPolicy;
  127. }
  128. @end