Skip to content

Commit

Permalink
Line up annotations with chart points (elastic#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin committed Oct 13, 2021
1 parent efc4b0f commit e6fec4e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -40,7 +40,7 @@ You can see the revisions `5859c4d6e62606428c2e49a181c7f845cadabecb` and `c76058

#### Add an annotation

To add an annotation, use the admin tool. First find the correct race timestamp by issuing `night-rally-admin list races --environment=nightly`. You will need the race timestamp later. Below are examples for common cases:
To add an annotation, use the admin tool. First find the correct race timestamp by issuing `night-rally-admin list races --environment=nightly`. You will need to specify it as `--race-timestamp`. Below are examples for common cases:

* Add an annotation for all charts for a specific nightly benchmark race: `night-rally-admin add annotation --environment=nightly --race-timestamp=20170502T220213Z --message="Just a test annotation"`
* Add an annotation for all charts of one track for a specific nightly benchmark race: `night-rally-admin add annotation --environment=nightly --race-timestamp=20170502T220213Z --track=geonames --message="Just a test annotation for geonames"`
Expand Down
18 changes: 14 additions & 4 deletions night_rally/admin.py
Expand Up @@ -3,7 +3,7 @@
import argparse
from night_rally import client
import tabulate
from datetime import datetime
import datetime


def list_races(es, args):
Expand Down Expand Up @@ -155,9 +155,18 @@ def list_annotations(es, args):
print("No results")


def _at_midnight(race_timestamp):
TIMESTAMP_FMT = "%Y%m%dT%H%S%MZ"
date = datetime.datetime.strptime(race_timestamp, TIMESTAMP_FMT)
date = date.replace(hour=0, minute=0, second=0, tzinfo=datetime.timezone.utc)
return date.strftime(TIMESTAMP_FMT)


def add_annotation(es, args):
environment = args.environment
race_timestamp = args.race_timestamp
# To line up annotations with chart data points, use midnight of day N as this is
# what the chart use too.
race_timestamp = _at_midnight(args.race_timestamp)
track = args.track
chart_type = args.chart_type
chart_name = args.chart_name
Expand All @@ -172,14 +181,15 @@ def add_annotation(es, args):
cwd = os.path.dirname(os.path.realpath(__file__))
body = open(os.path.join(cwd, "resources", "annotation-mapping.json"), "rt").read()
es.indices.create(index="rally-annotations", body=body)
es.index(index="rally-annotations", doc_type="_doc", body={
resp = es.index(index="rally-annotations", doc_type="_doc", body={
"environment": environment,
"race-timestamp": race_timestamp,
"track": track,
"chart": chart_type,
"chart-name": chart_name,
"message": message
})
print(f"Successfully added annotation [{resp['_id']}].")


def delete_annotation(es, args):
Expand Down Expand Up @@ -258,7 +268,7 @@ def positive_number(v):
def valid_date(v):
pattern = "%Y%m%d"
try:
datetime.strptime(v, pattern)
datetime.datetime.strptime(v, pattern)
# don't convert, just check that the format is correct, we'll use the string value later on anyway
return v
except ValueError:
Expand Down
13 changes: 13 additions & 0 deletions tests/race_timestamp_test.py
@@ -0,0 +1,13 @@
import pytest
from night_rally import admin


@pytest.mark.parametrize(
"test_input, expected",
[
("20210928T200053Z", "20210928T000000Z"),
("20210928T000000Z", "20210928T000000Z"),
],
)
def test_at_midnight(test_input, expected):
assert admin._at_midnight(test_input) == expected

0 comments on commit e6fec4e

Please sign in to comment.