ABTExperimentPayload.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #import "FirebaseABTesting/Sources/Public/FirebaseABTesting/FIRExperimentController.h"
  16. static NSString *const kExperimentPayloadKeyExperimentID = @"experimentId";
  17. static NSString *const kExperimentPayloadKeyVariantID = @"variantId";
  18. // Start time can either be a date string or integer (milliseconds since 1970).
  19. static NSString *const kExperimentPayloadKeyExperimentStartTime = @"experimentStartTime";
  20. static NSString *const kExperimentPayloadKeyExperimentStartTimeMillis =
  21. @"experimentStartTimeMillis";
  22. static NSString *const kExperimentPayloadKeyTriggerEvent = @"triggerEvent";
  23. static NSString *const kExperimentPayloadKeyTriggerTimeoutMillis = @"triggerTimeoutMillis";
  24. static NSString *const kExperimentPayloadKeyTimeToLiveMillis = @"timeToLiveMillis";
  25. static NSString *const kExperimentPayloadKeySetEventToLog = @"setEventToLog";
  26. static NSString *const kExperimentPayloadKeyActivateEventToLog = @"activateEventToLog";
  27. static NSString *const kExperimentPayloadKeyClearEventToLog = @"clearEventToLog";
  28. static NSString *const kExperimentPayloadKeyTimeoutEventToLog = @"timeoutEventToLog";
  29. static NSString *const kExperimentPayloadKeyTTLExpiryEventToLog = @"ttlExpiryEventToLog";
  30. static NSString *const kExperimentPayloadKeyOverflowPolicy = @"overflowPolicy";
  31. static NSString *const kExperimentPayloadValueDiscardOldestOverflowPolicy = @"DISCARD_OLDEST";
  32. static NSString *const kExperimentPayloadValueIgnoreNewestOverflowPolicy = @"IGNORE_NEWEST";
  33. static NSString *const kExperimentPayloadKeyOngoingExperiments = @"ongoingExperiments";
  34. @implementation ABTExperimentLite
  35. - (instancetype)initWithExperimentId:(NSString *)experimentId {
  36. if (self = [super init]) {
  37. _experimentId = experimentId;
  38. }
  39. return self;
  40. }
  41. @end
  42. @implementation ABTExperimentPayload
  43. + (NSDateFormatter *)experimentStartTimeFormatter {
  44. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  45. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
  46. [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  47. // Locale needs to be hardcoded. See
  48. // https://developer.apple.com/library/ios/#qa/qa1480/_index.html for more details.
  49. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  50. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  51. return dateFormatter;
  52. }
  53. + (nullable instancetype)parseFromData:(NSData *)data {
  54. NSError *error;
  55. NSDictionary *experimentDictionary =
  56. [NSJSONSerialization JSONObjectWithData:data
  57. options:NSJSONReadingAllowFragments
  58. error:&error];
  59. if (error != nil) {
  60. return nil;
  61. } else {
  62. return [[ABTExperimentPayload alloc] initWithDictionary:experimentDictionary];
  63. }
  64. }
  65. - (instancetype)initWithDictionary:(NSDictionary<NSString *, id> *)dictionary {
  66. if (self = [super init]) {
  67. _experimentId = dictionary[kExperimentPayloadKeyExperimentID];
  68. _variantId = dictionary[kExperimentPayloadKeyVariantID];
  69. _triggerEvent = dictionary[kExperimentPayloadKeyTriggerEvent];
  70. _setEventToLog = dictionary[kExperimentPayloadKeySetEventToLog];
  71. _activateEventToLog = dictionary[kExperimentPayloadKeyActivateEventToLog];
  72. _clearEventToLog = dictionary[kExperimentPayloadKeyClearEventToLog];
  73. _timeoutEventToLog = dictionary[kExperimentPayloadKeyTimeoutEventToLog];
  74. _ttlExpiryEventToLog = dictionary[kExperimentPayloadKeyTTLExpiryEventToLog];
  75. // Experiment start time can either be in the form of a date string or milliseconds since 1970.
  76. if (dictionary[kExperimentPayloadKeyExperimentStartTime]) {
  77. // Convert from date string.
  78. NSDate *experimentStartTime = [[[self class] experimentStartTimeFormatter]
  79. dateFromString:dictionary[kExperimentPayloadKeyExperimentStartTime]];
  80. _experimentStartTimeMillis =
  81. [@([experimentStartTime timeIntervalSince1970] * 1000) longLongValue];
  82. } else if (dictionary[kExperimentPayloadKeyExperimentStartTimeMillis]) {
  83. // Simply store milliseconds.
  84. _experimentStartTimeMillis =
  85. [dictionary[kExperimentPayloadKeyExperimentStartTimeMillis] longLongValue];
  86. ;
  87. }
  88. _triggerTimeoutMillis = [dictionary[kExperimentPayloadKeyTriggerTimeoutMillis] longLongValue];
  89. _timeToLiveMillis = [dictionary[kExperimentPayloadKeyTimeToLiveMillis] longLongValue];
  90. // Overflow policy can be an integer, or string e.g. "DISCARD_OLDEST" or "IGNORE_NEWEST".
  91. if ([dictionary[kExperimentPayloadKeyOverflowPolicy] isKindOfClass:[NSString class]]) {
  92. // If it's a string, pick against the preset string values.
  93. NSString *policy = dictionary[kExperimentPayloadKeyOverflowPolicy];
  94. if ([policy isEqualToString:kExperimentPayloadValueDiscardOldestOverflowPolicy]) {
  95. _overflowPolicy = ABTExperimentPayloadExperimentOverflowPolicyDiscardOldest;
  96. } else if ([policy isEqualToString:kExperimentPayloadValueIgnoreNewestOverflowPolicy]) {
  97. _overflowPolicy = ABTExperimentPayloadExperimentOverflowPolicyIgnoreNewest;
  98. } else {
  99. _overflowPolicy = ABTExperimentPayloadExperimentOverflowPolicyUnrecognizedValue;
  100. }
  101. } else {
  102. _overflowPolicy = [dictionary[kExperimentPayloadKeyOverflowPolicy] intValue];
  103. }
  104. NSMutableArray<ABTExperimentLite *> *ongoingExperiments = [[NSMutableArray alloc] init];
  105. NSArray<NSDictionary<NSString *, NSString *> *> *ongoingExperimentsArray =
  106. dictionary[kExperimentPayloadKeyOngoingExperiments];
  107. for (NSDictionary<NSString *, NSString *> *experimentDictionary in ongoingExperimentsArray) {
  108. NSString *experimentId = experimentDictionary[kExperimentPayloadKeyExperimentID];
  109. if (experimentId) {
  110. ABTExperimentLite *liteExperiment =
  111. [[ABTExperimentLite alloc] initWithExperimentId:experimentId];
  112. [ongoingExperiments addObject:liteExperiment];
  113. }
  114. }
  115. _ongoingExperiments = [ongoingExperiments copy];
  116. }
  117. return self;
  118. }
  119. - (void)clearTriggerEvent {
  120. _triggerEvent = nil;
  121. }
  122. - (BOOL)overflowPolicyIsValid {
  123. return self.overflowPolicy == ABTExperimentPayloadExperimentOverflowPolicyIgnoreNewest ||
  124. self.overflowPolicy == ABTExperimentPayloadExperimentOverflowPolicyDiscardOldest;
  125. }
  126. - (void)setOverflowPolicy:(ABTExperimentPayloadExperimentOverflowPolicy)overflowPolicy {
  127. _overflowPolicy = overflowPolicy;
  128. }
  129. @end