2019-08-01 15:47:48 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import csv
|
|
|
|
import os
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2019-09-02 15:44:54 +03:00
|
|
|
import numpy as np
|
|
|
|
|
2019-08-01 15:47:48 +03:00
|
|
|
from bugbug import bugzilla
|
|
|
|
from bugbug.models import get_model_class
|
|
|
|
|
|
|
|
|
2019-08-02 00:01:50 +03:00
|
|
|
def generate_sheet(model_name, token, days, threshold):
|
2019-08-01 15:47:48 +03:00
|
|
|
model_file_name = f"{model_name}model"
|
|
|
|
|
|
|
|
assert os.path.exists(
|
|
|
|
model_file_name
|
|
|
|
), f"{model_file_name} does not exist. Train the model with trainer.py first."
|
|
|
|
|
|
|
|
model_class = get_model_class(model_name)
|
|
|
|
model = model_class.load(model_file_name)
|
|
|
|
|
|
|
|
today = datetime.utcnow()
|
2019-08-02 00:01:50 +03:00
|
|
|
start_date = today - timedelta(days)
|
2019-08-01 15:47:48 +03:00
|
|
|
bugzilla.set_token(token)
|
2019-08-02 00:01:50 +03:00
|
|
|
bug_ids = bugzilla.get_ids_between(start_date, today)
|
2019-08-01 15:47:48 +03:00
|
|
|
bugs = bugzilla.get(bug_ids)
|
|
|
|
|
|
|
|
print(f"Classifying {len(bugs)} bugs...")
|
|
|
|
|
|
|
|
rows = [["Bug", f"{model_name}(model)", model_name, "Title"]]
|
|
|
|
|
|
|
|
for bug in bugs.values():
|
|
|
|
p = model.classify(bug, probabilities=True)
|
2019-09-02 15:44:54 +03:00
|
|
|
probability = p[0]
|
|
|
|
if len(probability) > 2:
|
|
|
|
index = np.argmax(probability)
|
|
|
|
prediction = model.class_names[index]
|
|
|
|
else:
|
|
|
|
prediction = "y" if probability[1] >= threshold else "n"
|
|
|
|
|
2019-08-01 15:47:48 +03:00
|
|
|
rows.append(
|
|
|
|
[
|
|
|
|
f'https://bugzilla.mozilla.org/show_bug.cgi?id={bug["id"]}',
|
2019-09-02 15:44:54 +03:00
|
|
|
prediction,
|
2019-08-01 15:47:48 +03:00
|
|
|
"",
|
|
|
|
bug["summary"],
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
os.makedirs("sheets", exist_ok=True)
|
|
|
|
with open(
|
|
|
|
os.path.join(
|
|
|
|
"sheets",
|
|
|
|
f'{model_name}-{datetime.utcnow().strftime("%Y-%m-%d")}-labels.csv',
|
|
|
|
),
|
|
|
|
"w",
|
|
|
|
) as f:
|
|
|
|
writer = csv.writer(f)
|
|
|
|
writer.writerows(rows)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-08-02 00:01:50 +03:00
|
|
|
description = "Perform evaluation on bugs from specified days back on the specified model and generate a csv file "
|
2019-08-01 15:47:48 +03:00
|
|
|
parser = argparse.ArgumentParser(description=description)
|
|
|
|
|
|
|
|
parser.add_argument("model", help="Which model to generate a csv for.")
|
|
|
|
parser.add_argument("token", help="Bugzilla token")
|
2019-08-02 00:01:50 +03:00
|
|
|
parser.add_argument(
|
|
|
|
"days",
|
|
|
|
type=int,
|
|
|
|
default=7,
|
|
|
|
help="No. of days back from which bugs will be evaluated",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"threshold", type=float, default=0.7, help="Confidence threshold for the model"
|
|
|
|
)
|
2019-08-01 15:47:48 +03:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-08-02 00:01:50 +03:00
|
|
|
generate_sheet(args.model, args.token, args.days, args.threshold)
|