Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit ff81ede

Browse files
committedMay 17, 2018
Add backfill-pushlog command
Add a command that can select and send pings for pushlog items using pushid ranges.
1 parent fd9983d commit ff81ede

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
 

‎README.md

+16
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ Use `--help` for full command usage info.
6464

6565
Use `--debug` for full command debug output.
6666

67+
```console
68+
$ PYTHONPATH=. bin/backfill-pushlog REPO_URL STARTING_PUSHID ENDING_PUSHID
69+
```
70+
71+
Read the Mercurial repository pushlog at REPO_URL, fetch all pushes from
72+
STARTING_PUSHID to ENDING_PUSHID, then calculate and publish their
73+
telemetry. This can be used to back-fill pushes missed by service gaps.
74+
75+
Use `--help` for full command usage info.
76+
77+
Use `--debug` for full command debug output.
78+
79+
Use `--no-send` to gather all the data and build a payload, but do not
80+
send any real pings.
81+
82+
6783
----
6884

6985
## Development

‎bin/backfill-pushlog

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
"""
6+
A utility script to process ping data by pushlog entry.
7+
8+
For help, run:
9+
10+
$ bin/backfill-pushlog --help
11+
12+
"""
13+
14+
from committelemetry.tool import backfill_pushlog
15+
16+
backfill_pushlog()

‎committelemetry/pushlog.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
"""
5+
Functions related to processing mercurial pushlog messsages.
6+
7+
See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#writing-agents-that-consume-pushlog-data
8+
"""
9+
import logging
10+
11+
import requests
12+
13+
from committelemetry.telemetry import payload_for_changeset, send_ping
14+
15+
log = logging.getLogger(__name__)
16+
17+
18+
def pushes_for_range(repo_url, starting_push_id, ending_push_id):
19+
"""Fetch a dict of pushes by ID from a repo pushlog.
20+
21+
Args:
22+
repo_url: The full URL of the repo whose pushlog we want to process.
23+
starting_push_id: Integer. Process all pushes greater than this push id.
24+
ending_push_id: Integer. Process all pushes less than and including
25+
this push id.
26+
27+
Returns:
28+
A dict of {'pushid': {pushdata}}. See
29+
https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#version-2.
30+
"""
31+
# See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#version-2
32+
params = dict(startID=starting_push_id, endID=ending_push_id, version=2)
33+
response = requests.get(f'{repo_url}/json-pushes/', params=params)
34+
response.raise_for_status()
35+
pushlog = response.json()
36+
return pushlog['pushes']
37+
38+
39+
def send_pings_by_pushid(repo_url, starting_push_id, ending_push_id, no_send):
40+
"""Fetch repo pushes by pushid and send pings for them.
41+
42+
Args:
43+
repo_url: The full URL of the repo whose pushlog we want to process.
44+
starting_push_id: Integer. Process all pushes greater than this push id.
45+
ending_push_id: Integer. Process all pushes less than and including
46+
this push id.
47+
no_send: Boolean: don't send any ping data, just print a message.
48+
"""
49+
if no_send:
50+
log.info('transmission of ping data has been disabled')
51+
52+
for pushid, pushdata in pushes_for_range(repo_url, starting_push_id, ending_push_id).items():
53+
log.info(f'processing pushid {pushid}')
54+
55+
# See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#version-2
56+
changesets = pushdata['changesets']
57+
log.info(f'got {len(changesets)} changesets for pushid {pushid}')
58+
59+
for changeset in changesets:
60+
log.info(f'processing changeset {changeset}')
61+
ping = payload_for_changeset(changeset, repo_url)
62+
63+
if no_send:
64+
log.info(f'ping data (not sent): {ping}')
65+
continue
66+
67+
# Pings need a unique ID so they can be de-duplicated by the ingestion
68+
# service. We can use the changeset ID for the unique key.
69+
send_ping(changeset, ping)

‎committelemetry/tool.py

+33
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import click
99

1010
from committelemetry.pulse import run_pulse_listener
11+
from committelemetry.pushlog import send_pings_by_pushid
1112
from .telemetry import NoSuchChangeset, payload_for_changeset
1213

1314

@@ -82,3 +83,35 @@ def process_queue_messages(debug, user, password, timeout, no_send):
8283
logging.basicConfig(stream=sys.stdout, level=log_level)
8384

8485
run_pulse_listener(user, password, timeout, no_send)
86+
87+
88+
@click.command()
89+
@click.option(
90+
'--debug',
91+
envvar='DEBUG',
92+
is_flag=True,
93+
help='Print debugging messages about the script\'s progress.'
94+
)
95+
@click.option(
96+
'--no-send',
97+
is_flag=True,
98+
help='For testing. Do not send ping data or drain any queues.'
99+
)
100+
@click.argument('repo_url')
101+
@click.argument('starting_push_id')
102+
@click.argument('ending_push_id')
103+
def backfill_pushlog(debug, no_send, repo_url, starting_push_id, ending_push_id):
104+
"""Process repo pushes by pushlog ID."""
105+
if debug:
106+
log_level = logging.DEBUG
107+
else:
108+
log_level = logging.INFO
109+
110+
logging.basicConfig(stream=sys.stdout, level=log_level)
111+
112+
print(f'Checking repo {repo_url}')
113+
print(f'Fetching pushes {starting_push_id} to {ending_push_id}')
114+
115+
send_pings_by_pushid(repo_url, starting_push_id, ending_push_id, no_send)
116+
117+
print('Done.')

0 commit comments

Comments
 (0)
Failed to load comments.