Skip to content

Commit 3fa68ef

Browse files
committed
Fix query_text vs query_type bug
Also add weight to different queries
1 parent 419838e commit 3fa68ef

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

data_rows/QueryHistory.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ export default class QueryHistory extends DataRow {
1616
constructor(databaseName, date) {
1717
super();
1818
this.QUERY_ID = uuid4();
19-
this._setQueryText();
2019
this.DATABASE_NAME = databaseName;
21-
const queryType = helpers.randomFromArray(this._getQueryTypes());
22-
this.QUERY_TYPE = queryType.type;
20+
const queryInfo = helpers.randomFromArrayByWeight(
21+
this._getQueryInfo(),
22+
true
23+
);
24+
this.QUERY_TEXT = queryInfo.text;
25+
this.QUERY_TYPE = queryInfo.type;
2326
const user = helpers.randomFromArray(LoginHistory.getUsers());
2427
this.USER_NAME = user.name;
2528
[this.WAREHOUSE_NAME, this.WAREHOUSE_SIZE] = helpers.randomFromArray([
@@ -29,27 +32,47 @@ export default class QueryHistory extends DataRow {
2932
this._setExecutionStatus();
3033
this.START_TIME = date.toISOString();
3134
this.COMPILATION_TIME = Math.round(Math.random() * 1000);
32-
this._setExecutionTime(user.querySpeed, queryType.querySpeed);
35+
this._setExecutionTime(user.querySpeed, queryInfo.querySpeed);
3336
this.QUEUED_REPAIR_TIME = 0;
3437
this.QUEUED_OVERLOAD_TIME =
3538
Math.random() < 0.2 ? Math.round(Math.random() * 1000) : 0;
3639
this._setTransactionBlockedtime();
3740
}
3841

39-
_setQueryText() {
40-
const queries = ["SHOW USERS", "SHOW WAREHOUSES", "SELECT foo FROM bar"];
41-
this.QUERY_TEXT = helpers.randomFromArray(queries);
42-
}
43-
44-
_getQueryTypes() {
42+
_getQueryInfo() {
4543
return [
46-
{ type: "WITH", querySpeed: 1.2 },
47-
{ type: "REPLACE", querySpeed: 0.5 },
48-
{ type: "SHOW", querySpeed: 0.2 },
49-
{ type: "CREATE", querySpeed: 0.1 },
50-
{ type: "COPY", querySpeed: 1.5 },
51-
{ type: "SELECT", querySpeed: 1.9 },
52-
{ type: "UNKNOWN", querySpeed: 2.2 }
44+
{
45+
weight: 0.02,
46+
type: "UNKNOWN",
47+
querySpeed: 2.2,
48+
text: "What's all this then?"
49+
},
50+
{
51+
weight: 0.04,
52+
type: "CREATE",
53+
querySpeed: 0.1,
54+
text: "CREATE DATABASE"
55+
},
56+
{ weight: 0.06, type: "COPY", querySpeed: 1.5, text: "COPY TABLE" },
57+
{ weight: 0.08, type: "SHOW", querySpeed: 0.2, text: "SHOW USERS" },
58+
{
59+
weight: 0.1,
60+
type: "REPLACE",
61+
querySpeed: 0.5,
62+
text: "REPLACE INTO fooTable"
63+
},
64+
{
65+
weight: 0.2,
66+
type: "WITH",
67+
querySpeed: 1.2,
68+
text: "WITH fooTable (bar)"
69+
},
70+
{
71+
weight: 0.5,
72+
type: "SELECT",
73+
querySpeed: 1.9,
74+
text: "SELECT * FROM fooTable"
75+
}
5376
];
5477
}
5578

helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ export function randomFromArray(theArray) {
22
return theArray[Math.floor(Math.random() * theArray.length)];
33
}
44

5-
export function randomFromArrayByWeight(theArray) {
5+
export function randomFromArrayByWeight(theArray, all = false) {
66
let sumWeight = 0;
77
const r = Math.random();
88
for (let item of theArray) {
99
sumWeight += item.weight;
10-
if (r <= sumWeight) return item.name;
10+
if (r <= sumWeight) return all ? item : item.name;
1111
}
1212
}
1313

0 commit comments

Comments
 (0)