Skip to content

Commit d734982

Browse files
authored
Merge pull request github#12286 from github/tausbn/ql-add-a-bunch-of-convenience-predicates
QL: Add predicates for timestamps and locations
2 parents 748387a + 6a32a3a commit d734982

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

ql/ql/src/codeql_ql/StructuredLogs.qll

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
private import ql
22
private import codeql_ql.ast.internal.TreeSitter
33

4+
/** Gets a timestamp corresponding to the number of seconds since the date Semmle was founded. */
5+
bindingset[d, h, m, s, ms]
6+
private float getTimestamp(date d, int h, int m, int s, int ms) {
7+
result = (("2006-12-28".toDate().daysTo(d) * 24 + h) * 60 + m) * 60 + s + ms / 1000.0
8+
}
9+
10+
bindingset[str]
11+
private float stringToTimestamp(string str) {
12+
exists(string r, date d, int h, int m, int s, int ms |
13+
r = "(\\d{4}-\\d{2}-\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})\\.(\\d{3})Z"
14+
|
15+
d = str.regexpCapture(r, 1).toDate() and
16+
h = str.regexpCapture(r, 2).toInt() and
17+
m = str.regexpCapture(r, 3).toInt() and
18+
s = str.regexpCapture(r, 4).toInt() and
19+
ms = str.regexpCapture(r, 5).toInt() and
20+
result = getTimestamp(d, h, m, s, ms)
21+
)
22+
}
23+
24+
bindingset[s]
25+
private Predicate getPredicateFromPosition(string s) {
26+
exists(string r, string filepath, int startline | r = "(.*):(\\d+),(\\d+)-(\\d+),(\\d+)" |
27+
filepath = s.regexpCapture(r, 1) and
28+
startline = s.regexpCapture(r, 2).toInt() and
29+
result.hasLocationInfo(filepath, startline, _, _, _)
30+
)
31+
}
32+
433
class Object extends JSON::Object {
534
JSON::Value getValue(string key) {
635
exists(JSON::Pair p | p = this.getChild(_) |
@@ -24,6 +53,8 @@ class Object extends JSON::Object {
2453
int getEventId() { result = this.getNumber("event_id") }
2554

2655
string getTime() { result = this.getString("time") }
56+
57+
float getTimestamp() { result = stringToTimestamp(this.getTime()) }
2758
}
2859

2960
class Array extends JSON::Array {
@@ -227,6 +258,8 @@ module KindPredicatesLog {
227258
)
228259
}
229260

261+
float getCompletionTime() { result = stringToTimestamp(this.getCompletionTimeString()) }
262+
230263
float getResultSize() { result = this.getFloat("resultSize") }
231264
}
232265

@@ -278,6 +311,10 @@ module KindPredicatesLog {
278311
Depencencies getDependencies() { result = this.getObject("dependencies") }
279312

280313
PipeLineRun getPipelineRun() { result.getArray() = this.getArray("pipelineRuns") }
314+
315+
string getPosition() { result = this.getString("position") }
316+
317+
Predicate getPredicate() { result = getPredicateFromPosition(this.getPosition()) }
281318
}
282319

283320
class ComputeRecursive extends SummaryEvent {

0 commit comments

Comments
 (0)