Skip to content

Commit

Permalink
Fix up endTimestamp to be Monarch compliant (google#879)
Browse files Browse the repository at this point in the history
Fix up endTimestamp field to be Monarch compliant.
  • Loading branch information
pmarkowsky authored Aug 17, 2022
1 parent ec203e8 commit fd23a5c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions Source/santametricservice/Formats/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ santa_unit_test(
deps = [
":SNTMetricFormatTestHelper",
":SNTMetricMonarchJSONFormat",
"@OCMock",
],
)

Expand Down
20 changes: 13 additions & 7 deletions Source/santametricservice/Formats/SNTMetricMonarchJSONFormat.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ - (void)encodeValueAndStreamKindFor:(NSString *)metricName
}
}

- (NSArray<NSDictionary *> *)encodeDataForMetric:(NSDictionary *)metric {
- (NSArray<NSDictionary *> *)encodeDataForMetric:(NSDictionary *)metric
withEndTimestamp:(NSDate *)endTimestamp {
NSMutableArray<NSDictionary *> *monarchMetricData = [[NSMutableArray alloc] init];

for (NSString *fieldName in metric[@"fields"]) {
Expand All @@ -112,8 +113,8 @@ - (void)encodeValueAndStreamKindFor:(NSString *)metricName
}

monarchDataEntry[kStartTimestamp] = [self->_dateFormatter stringFromDate:entry[@"created"]];
monarchDataEntry[kEndTimestamp] =
[self->_dateFormatter stringFromDate:entry[@"last_updated"]];
// Monarch wants all the end timestamp to be updated, even if the value does not change.
monarchDataEntry[kEndTimestamp] = [self->_dateFormatter stringFromDate:endTimestamp];

if (!metric[@"type"]) {
LOGE(@"metric type is nil");
Expand Down Expand Up @@ -157,10 +158,13 @@ - (void)encodeValueAndStreamKindFor:(NSString *)metricName
}

/**
* formatMetric translates the SNTMetricSet metric entries into those consumable by Monarch.
* formatMetric translates the SNTMetricSet metric entries into those consumable
* by Monarch.
**/

- (NSDictionary *)formatMetric:(NSString *)name withMetric:(NSDictionary *)metric {
- (NSDictionary *)formatMetric:(NSString *)name
withValue:(NSDictionary *)metric
andEndtimestamp:(NSDate *)endTimestamp {
NSMutableDictionary *monarchMetric = [[NSMutableDictionary alloc] init];

monarchMetric[kMetricName] = name;
Expand All @@ -175,7 +179,7 @@ - (NSDictionary *)formatMetric:(NSString *)name withMetric:(NSDictionary *)metri
}

[self encodeValueAndStreamKindFor:name withMetric:metric into:monarchMetric];
monarchMetric[@"data"] = [self encodeDataForMetric:metric];
monarchMetric[@"data"] = [self encodeDataForMetric:metric withEndTimestamp:endTimestamp];

return monarchMetric;
}
Expand All @@ -185,10 +189,12 @@ - (NSDictionary *)formatMetric:(NSString *)name withMetric:(NSDictionary *)metri
**/
- (NSDictionary *)normalize:(NSDictionary *)metrics {
NSMutableArray<NSDictionary *> *monarchMetrics = [[NSMutableArray alloc] init];
NSDate *endTimestamp = [NSDate date];

for (NSString *metricName in metrics[@"metrics"]) {
[monarchMetrics addObject:[self formatMetric:metricName
withMetric:metrics[@"metrics"][metricName]]];
withValue:metrics[@"metrics"][metricName]
andEndtimestamp:endTimestamp]];
}

NSMutableArray<NSDictionary *> *rootLabels = [[NSMutableArray alloc] init];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import <XCTest/XCTest.h>

#import <Foundation/Foundation.h>
#import <OCMock/OCMock.h>
#import "Source/santametricservice/Formats/SNTMetricFormatTestHelper.h"
#import "Source/santametricservice/Formats/SNTMetricMonarchJSONFormat.h"

Expand All @@ -9,6 +11,10 @@ @interface SNTMetricMonarchJSONFormatTest : XCTestCase
@implementation SNTMetricMonarchJSONFormatTest

- (void)testMetricsConversionToJSON {
id classMock = OCMClassMock([NSDate class]);
OCMStub([classMock date])
.andReturn([NSDate dateWithTimeIntervalSince1970:1631826490]); // 2021-09-16 21:08:10Z

NSDictionary *validMetricsDict = [SNTMetricFormatTestHelper createValidMetricsDictionary];
SNTMetricMonarchJSONFormat *formatter = [[SNTMetricMonarchJSONFormat alloc] init];
NSError *err = nil;
Expand Down
18 changes: 9 additions & 9 deletions Source/santametricservice/Formats/testdata/json/monarch.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"data" : [
{
"int64Value" : 1,
"endTimestamp" : "2021-09-16T21:07:34.826Z",
"endTimestamp" : "2021-09-16T21:08:10.000Z",
"field" : [
{
"name" : "rule_type",
Expand All @@ -37,7 +37,7 @@
},
{
"int64Value" : 3,
"endTimestamp" : "2021-09-16T21:07:34.826Z",
"endTimestamp" : "2021-09-16T21:08:10.000Z",
"field" : [
{
"name" : "rule_type",
Expand All @@ -54,7 +54,7 @@
"data" : [
{
"int64Value" : 123456789,
"endTimestamp" : "2021-09-16T21:07:34.826Z",
"endTimestamp" : "2021-09-16T21:08:10.000Z",
"startTimestamp" : "2021-09-16T21:07:34.826Z"
}
],
Expand All @@ -73,7 +73,7 @@
"data" : [
{
"int64Value" : 1,
"endTimestamp" : "2021-09-16T21:07:34.826Z",
"endTimestamp" : "2021-09-16T21:08:10.000Z",
"field" : [
{
"name" : "rule_type",
Expand All @@ -84,7 +84,7 @@
},
{
"int64Value" : 2,
"endTimestamp" : "2021-09-16T21:07:34.826Z",
"endTimestamp" : "2021-09-16T21:08:10.000Z",
"field" : [
{
"name" : "rule_type",
Expand All @@ -104,7 +104,7 @@
"data" : [
{
"boolValue" : true,
"endTimestamp" : "2021-09-16T21:07:34.826Z",
"endTimestamp" : "2021-09-16T21:08:10.000Z",
"startTimestamp" : "2021-09-16T21:07:34.826Z"
}
]
Expand All @@ -116,7 +116,7 @@
"data" : [
{
"int64Value" : 1250999830800,
"endTimestamp" : "2021-09-16T21:07:34.826Z",
"endTimestamp" : "2021-09-16T21:08:10.000Z",
"startTimestamp" : "2021-09-16T21:07:34.826Z"
}
]
Expand All @@ -127,7 +127,7 @@
"data" : [
{
"int64Value" : 987654321,
"endTimestamp" : "2021-09-16T21:07:34.826Z",
"endTimestamp" : "2021-09-16T21:08:10.000Z",
"startTimestamp" : "2021-09-16T21:07:34.826Z"
}
],
Expand All @@ -141,7 +141,7 @@
"data" : [
{
"stringValue" : "20210809.0.1",
"endTimestamp" : "2021-09-16T21:07:34.826Z",
"endTimestamp" : "2021-09-16T21:08:10.000Z",
"startTimestamp" : "2021-09-16T21:07:34.826Z"
}
]
Expand Down
4 changes: 4 additions & 0 deletions Source/santametricservice/SNTMetricServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ - (void)testWritingJSONOverHTTP {
}

- (void)testWritingMonarchJSONToAFile {
id classMock = OCMClassMock([NSDate class]);
OCMStub([classMock date])
.andReturn([NSDate dateWithTimeIntervalSince1970:1631826490]); // 2021-09-16 21:08:10Z

OCMStub([self.mockConfigurator exportMetrics]).andReturn(YES);
OCMStub([self.mockConfigurator metricFormat]).andReturn(SNTMetricFormatTypeMonarchJSON);
OCMStub([self.mockConfigurator metricURL]).andReturn(self.jsonURL);
Expand Down

0 comments on commit fd23a5c

Please sign in to comment.