-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gather BIT metrics [implementation] #3122
Merged
Merged
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ad5fe37
Inflate 3116
originalsouth aa70bf7
Set default off
originalsouth 33139a6
Add yielded oois
originalsouth 79a94bf
Prevent bytes-strings
originalsouth 5d50bfe
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth 54cea93
Add bit metric "parser"
originalsouth 90fbda2
Manual merge of origin/main and address comments by @ammer92
originalsouth 030ec3d
Remove annotations from settings
originalsouth 059c461
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth 8216ab6
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth d5c3d1b
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth 2bc2ead
Merge branch 'main' into feature/3116
underdarknl 7ee2621
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth aa74603
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth 2912965
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth f70c5c3
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth 2430f64
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth 2ab00ca
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth 89c9806
Make analyze-bit-metric.py more user friendly
originalsouth 6dc35e5
Merge remote-tracking branch 'origin/main' into feature/3116
originalsouth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#!/usr/bin/env python | ||
|
||
import json | ||
import logging | ||
from datetime import datetime | ||
|
||
import click | ||
from xtdb_client import XTDBClient | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class BitMetric: | ||
def __init__(self, data): | ||
date_format = "%Y-%m-%dT%H:%M:%SZ" | ||
self.txTime: datetime = datetime.strptime(data["txTime"], date_format) | ||
self.txId: int = int(data["txId"]) | ||
self.validTime: datetime = datetime.strptime(data["validTime"], date_format) | ||
self.contentHash: str = data["contentHash"] | ||
underdarknl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.yld: dict[str, str] = json.loads(data["doc"]["yield"]) | ||
self.cfg: dict[str, str] = json.loads(data["doc"]["config"]) | ||
self.src: dict[str, str] = json.loads(data["doc"]["source"]) | ||
self.name: str = data["doc"]["bit"] | ||
self.pms: list[dict[str, str]] = json.loads(data["doc"]["parameters"]) | ||
self.elapsed: list[dict[str, str]] = json.loads(data["doc"]["elapsed"]) | ||
|
||
def empty(self): | ||
return len(self.yld) == 0 | ||
|
||
def __eq__(self, val): | ||
return all([getattr(self, op) == getattr(val, op) for op in ["src", "cfg", "yld", "name", "pms"]]) | ||
underdarknl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __hash__(self): | ||
return hash(str(hash(self.txTime)) + self.contentHash) | ||
|
||
|
||
@click.group( | ||
context_settings={ | ||
"help_option_names": ["-h", "--help"], | ||
"max_content_width": 120, | ||
"show_default": True, | ||
} | ||
) | ||
@click.option("-n", "--node", default="0", help="XTDB node") | ||
@click.option( | ||
"-u", | ||
"--url", | ||
default="http://localhost:3000", | ||
help="XTDB server base url", | ||
) | ||
@click.option( | ||
"-t", | ||
"--timeout", | ||
type=int, | ||
default=5000, | ||
help="XTDB request timeout (in ms)", | ||
) | ||
@click.option("-v", "--verbosity", count=True, help="Increase the verbosity level") | ||
@click.pass_context | ||
def cli(ctx: click.Context, url: str, node: str, timeout: int, verbosity: int): | ||
verbosities = [logging.WARN, logging.INFO, logging.DEBUG] | ||
try: | ||
if verbosity: | ||
logging.basicConfig(level=verbosities[verbosity - 1]) | ||
except IndexError: | ||
raise click.UsageError("Invalid verbosity level (use -v, -vv, or -vvv)") | ||
|
||
client = XTDBClient(url, node, timeout) | ||
logger.info("Instantiated XTDB client with endpoint %s for node %s", url, node) | ||
|
||
ctx.ensure_object(dict) | ||
ctx.obj["client"] = client | ||
ctx.obj["raw_bit_metrics"] = client.history("BIT_METRIC", False, True) | ||
ctx.obj["bit_metrics"] = list(map(lambda x: BitMetric(x), ctx.obj["raw_bit_metrics"])) | ||
|
||
|
||
@cli.command(help="Returns the raw bit metric") | ||
@click.pass_context | ||
def raw(ctx: click.Context): | ||
click.echo(ctx.obj["raw_bit_metrics"]) | ||
|
||
|
||
@cli.command(help="Returns the parsed metric") | ||
@click.pass_context | ||
def parse(ctx: click.Context): | ||
underdarknl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
metrics = { | ||
"total": len(ctx.obj["bit_metrics"]), | ||
"total_elapsed": sum([bm.elapsed for bm in ctx.obj["bit_metrics"]]), | ||
"empty": len([bm for bm in ctx.obj["bit_metrics"] if bm.empty()]), | ||
"empty_elapsed": sum([bm.elapsed for bm in ctx.obj["bit_metrics"] if bm.empty()]), | ||
"useful": len([bm for bm in ctx.obj["bit_metrics"] if not bm.empty()]), | ||
"useful_elapsed": sum([bm.elapsed for bm in ctx.obj["bit_metrics"] if not bm.empty()]), | ||
"futile_runs": { | ||
bm.name: ctx.obj["bit_metrics"].count(bm) - 1 | ||
for bm in ctx.obj["bit_metrics"] | ||
if ctx.obj["bit_metrics"].count(bm) > 1 | ||
}, | ||
} | ||
click.echo(json.dumps(metrics)) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the intention is, but usually feature flags are configurable. Perhaps this fits better in the
Settings
class and documented. This could work if it's only a private debug variable for development, although the location is bit misleading since it's placed next to the constants.The
bool
hint is unnecessary hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention is to have a developer flag to toggle gathering [bit] metrics (without exposing it to the public). Please let me know what the best way of defining this would be.