-
Notifications
You must be signed in to change notification settings - Fork 8
/
time_series_example.py
138 lines (122 loc) · 4.72 KB
/
time_series_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import logging
from pprint import pformat
import traceback
from datetime import datetime, timedelta
import intersight.api.telemetry_api
import intersight.model.telemetry_druid_data_source
import intersight.model.telemetry_druid_period_granularity
import intersight.model.telemetry_druid_query_context
import intersight.model.telemetry_druid_time_series_request
import credentials
from helpers import format_time
FORMAT = '%(asctime)-15s [%(levelname)s] [%(filename)s:%(lineno)s] %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
logger = logging.getLogger('openapi')
def get_time_interval(num_days=5):
current_time = datetime.now()
end_time = format_time(current_time)
start_time = format_time(current_time - timedelta(days=num_days))
interval_str = start_time + '/' + end_time
return interval_str
def get_time_series(api_client):
"""Query Druid time series"""
# Create an instance of the API telemetry service.
api_instance = intersight.api.telemetry_api.TelemetryApi(api_client)
logger.info("Query 'device_connector' time series")
req = intersight.model.telemetry_druid_time_series_request.TelemetryDruidTimeSeriesRequest(
query_type="timeseries",
data_source=intersight.model.telemetry_druid_data_source.TelemetryDruidDataSource(
type="table",
name="device_connector",
),
intervals=[
get_time_interval(),
],
granularity=intersight.model.telemetry_druid_period_granularity.TelemetryDruidPeriodGranularity(
type="period",
period="P1D",
),
context=intersight.model.telemetry_druid_query_context.TelemetryDruidQueryContext(
timeout=30,
query_id="device_connector-QueryIdentifier",
),
)
api_response = api_instance.query_telemetry_time_series(
telemetry_druid_time_series_request=req,
)
logger.info(pformat(api_response))
##########################
logger.info("Query 'ucs_ether_port_stat' time series")
req = intersight.model.telemetry_druid_time_series_request.TelemetryDruidTimeSeriesRequest(
aggregations=[
intersight.model.telemetry_druid_aggregator.TelemetryDruidAggregator(
field_name="sumBytesTx",
type="longSum",
name="traffic",
field_names=["sumBytesTx"]
),
],
query_type="timeseries",
data_source=intersight.model.telemetry_druid_data_source.TelemetryDruidDataSource(
type="table",
name="ucs_ether_port_stat",
),
intervals=[
get_time_interval(),
],
granularity=intersight.model.telemetry_druid_period_granularity.TelemetryDruidPeriodGranularity(
type="period",
period="P1D",
),
context=intersight.model.telemetry_druid_query_context.TelemetryDruidQueryContext(
timeout=30,
query_id="ucs_ether_port_stat-QueryIdentifier",
),
)
api_response = api_instance.query_telemetry_time_series(
telemetry_druid_time_series_request=req,
)
logger.info(pformat(api_response))
##########################
logger.info("Query 'PSU stat' time series")
req = intersight.model.telemetry_druid_time_series_request.TelemetryDruidTimeSeriesRequest(
aggregations=[
intersight.model.telemetry_druid_aggregator.TelemetryDruidAggregator(
field_name="sumEnergyConsumed",
type="doubleSum",
name="energyConsumed",
field_names=["sumEnergyConsumed"]
),
],
query_type="timeseries",
data_source=intersight.model.telemetry_druid_data_source.TelemetryDruidDataSource(
type="table",
name="psu_stat",
),
intervals=[
get_time_interval(),
],
granularity=intersight.model.telemetry_druid_period_granularity.TelemetryDruidPeriodGranularity(
type="period",
period="P1D",
),
context=intersight.model.telemetry_druid_query_context.TelemetryDruidQueryContext(
timeout=30,
query_id="psu_stat-QueryIdentifier",
),
)
api_response = api_instance.query_telemetry_time_series(
telemetry_druid_time_series_request=req,
)
logger.info(pformat(api_response))
def main():
# Configure API key settings for authentication
api_client = credentials.config_credentials()
try:
# Get example time series data
get_time_series(api_client)
except intersight.OpenApiException as e:
logger.error("Exception when calling API: %s\n" % e)
traceback.print_exc()
if __name__ == "__main__":
main()