Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import sys 

2import os 

3 

4sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../../../") 

5from sdc_etl_libs.api_helpers.apis.NewRelic.NewRelic import NewRelic 

6 

7 

8 

9def test_flatten_metrics_data(mocker): 

10 

11 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', return_value={"api_key": "this"}) 

12 dict_to_flatten = [ 

13 { 

14 "name": "EndUser/UserAgent/Mobile/Browser", 

15 "timeslices": [ 

16 { 

17 "from": "2015-10-01T01:00:00+00:00", 

18 "to": "2015-10-04T01:00:00+00:00", 

19 "values": { 

20 "average_fe_response_time": 0, 

21 "average_be_response_time": 0 

22 } 

23 } 

24 ] 

25 } 

26 ] 

27 

28 expected_result = [{'name': 'EndUser/UserAgent/Mobile/Browser', 

29 'from': '2015-10-01T01:00:00+00:00', 

30 'to': '2015-10-04T01:00:00+00:00', 

31 'average_fe_response_time': 0, 

32 'average_be_response_time': 0}] 

33 

34 

35 new_relic = NewRelic() 

36 

37 flattened_data = new_relic.flatten_metrics_data(dict_to_flatten) 

38 

39 assert flattened_data == expected_result 

40 

41def test_get_date_filter(mocker): 

42 

43 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', return_value={"api_key": "this"}) 

44 

45 date_to_check = '2019-11-06 09:50:56.172035' 

46 expected_result = 'from=2019-11-03T01:00:00+00:00&to=2019-11-06T01:00:00+00:00&period=3600' 

47 

48 new_relic = NewRelic() 

49 

50 url_date_filter = new_relic.get_date_filter(datetime_=date_to_check, days_=3, periods_=3600) 

51 

52 assert url_date_filter == expected_result 

53 

54def test_get_fields_filter(mocker): 

55 

56 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', return_value={"api_key": "this"}) 

57 names_to_check = ['EndUser/UserAgent/Mobile/Browser&names', 'EndUser/UserAgent/Tablet/Browser'] 

58 values_to_check = ['average_response_time', 'average_fe_response_time', 'average_be_response_time'] 

59 expected_result = 'names[]=EndUser/UserAgent/Mobile/Browser&names&names[]=EndUser/UserAgent/Tablet/Browser&values[]=average_response_time&values[]=average_fe_response_time&values[]=average_be_response_time' 

60 

61 new_relic = NewRelic() 

62 

63 url_fields_filter = new_relic.get_fields_filter(names_=names_to_check, values_=values_to_check) 

64 

65 assert url_fields_filter == expected_result 

66 

67 

68def test_get_metrics_data(mocker): 

69 mocker.patch('sdc_etl_libs.api_helpers.API.API.get_credentials', return_value={"api_key": "this"}) 

70 

71 mocker.patch('sdc_etl_libs.sdc_file_helpers.SDCFileHelpers.SDCFileHelpers.get_file_path', 

72 return_value=os.path.join( 

73 os.path.dirname(os.path.abspath(__file__)), 'newrelic-test-schema-1.json') 

74 ) 

75 

76 class ReqMock(): 

77 def __init__(self, status_, data_): 

78 self.status_code = status_ 

79 self.content = data_ 

80 

81 mocker.patch( 

82 'sdc_etl_libs.api_helpers.apis.NewRelic.NewRelic.NewRelic.process_metrics_endpoint', 

83 return_value=[{'name': 'EndUser/UserAgent/Tablet/Browser', 

84 'timeslices': [{'from': '2019-11-03T01:00:00+00:00', 

85 'to': '2019-11-03T02:00:00+00:00', 

86 'values': { 

87 'average_response_time': 13800, 

88 'average_fe_response_time': 12500, 

89 'average_be_response_time': 1320}}, 

90 {'from': '2019-11-03T02:00:00+00:00', 

91 'to': '2019-11-03T03:00:00+00:00', 

92 'values': { 

93 'average_response_time': 12300, 

94 'average_fe_response_time': 11100, 

95 'average_be_response_time': 1250}}] 

96 }] 

97 ) 

98 

99 new_relic = NewRelic() 

100 

101 df = new_relic.get_metrics_data("useragent-browser-performance", 

102 date_filter_=new_relic.get_date_filter(datetime_='2019-11-06 09:50:56.172035', days_=3), 

103 fields_filter_=new_relic.get_fields_filter(names_=["EndUser/UserAgent/Mobile/Browser&names", "EndUser/UserAgent/Tablet/Browser", "EndUser/UserAgent/Desktop/Browser"], 

104 values_=["average_response_time", "average_fe_response_time"]) 

105 ) 

106 assert len(df.df) == 2