forked from grihey/atsd-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_data.py
48 lines (42 loc) · 1.62 KB
/
copy_data.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
from atsd_client import connect, connect_url
from atsd_client.models import SeriesFilter, EntityFilter, DateFilter, SeriesQuery, Sample
from atsd_client.services import SeriesService
'''
Load data for one year and insert the data with multiple year shift.
'''
# Connect to ATSD server
#connection = connect('/path/to/connection.properties')
connection = connect_url('https://atsd_hostname:8443', 'username', 'password')
# Initialize services
svc = SeriesService(connection)
# specify years to increase
year_count = 5
# specify metric and entity names
metric_name = 'sml.power-consumed'
entity_name = '*'
# specify date filter
start_date = '2018-01-01T00:00:00Z'
end_date = '2019-01-01T00:00:01Z'
# prepare series_query and execute it
sf = SeriesFilter(metric=metric_name)
ef = EntityFilter(entity=entity_name)
df = DateFilter(start_date=start_date, end_date=end_date)
query = SeriesQuery(series_filter=sf, entity_filter=ef, date_filter=df)
series_list = svc.query(query)
for series in series_list:
# update timestamps
updated_data = []
for sample in series.data:
current_date = sample.get_date()
for i in range(1, year_count + 1):
try:
# increment year
new_date = current_date.replace(year=current_date.year + i)
updated_data.append(Sample(value=sample.v, time=new_date, version=sample.version))
except ValueError:
# Uncomment to add nonexistent dates to output
print('%s, year shift is %s' % (current_date, i))
continue
series.data = updated_data
series.aggregate = None
svc.insert(series)