Skip to content

Commit 2fa507c

Browse files
committed
Added interval to Top\QueryArgs
Closes #27
1 parent 9f50679 commit 2fa507c

File tree

9 files changed

+50
-5
lines changed

9 files changed

+50
-5
lines changed

src/SOFe/Capital/AccountQueryMetric.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function getLabelTable() : string {
4343
return "capital_acc_label";
4444
}
4545

46+
public function getTimestampColumn() : string {
47+
return "capital_acc.touch";
48+
}
49+
4650
public function getExpr() : string {
4751
return $this->expr;
4852
}

src/SOFe/Capital/Analytics/Top/DatabaseUtils.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,21 @@ public function compute(string $runId, QueryArgs $query) : Generator {
226226
$metricExpr .= " INNER JOIN $labelTable AS t{$i} USING (id)";
227227
}
228228

229-
if (!$query->metric->usesIdOnly()) {
230-
$metricExpr .= " INNER JOIN $mainTable USING (id)";
231-
}
229+
$metricExpr .= " INNER JOIN $mainTable USING (id)";
232230

233231
$metricExpr .= " WHERE grouping_label.name = :groupingLabel AND grouping_label.value = capital_analytics_top_cache.group_value";
234232

233+
if ($query->interval !== null) {
234+
$tsColumn = $query->metric->getTimestampColumn();
235+
$timeDiffExpr = match ($this->db->dialect) {
236+
SqlDialect::SQLITE => "JULIANDAY(CURRENT_TIMESTAMP) - JULIANDAY($tsColumn)",
237+
SqlDialect::MYSQL => "TIMESTAMPDIFF(SECOND, $tsColumn, CURRENT_TIMESTAMP)",
238+
};
239+
$metricExpr .= " AND $timeDiffExpr < :interval";
240+
$vars["interval"] = new GenericVariable("interval", GenericVariable::TYPE_INT, null);
241+
$args["interval"] = $query->interval;
242+
}
243+
235244
$i = 0;
236245
foreach ($query->labelSelector->getEntries() as $name => $value) {
237246
$metricExpr .= " AND t{$i}.name = :name{$i}";

src/SOFe/Capital/Analytics/Top/Mod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
final class Mod implements Singleton, FromContext {
2222
use SingletonArgs, SingletonTrait;
2323

24-
public const API_VERSION = "0.1.0";
24+
public const API_VERSION = "0.2.0";
2525

2626
public static function fromSingletonArgs(Analytics\Config $config, MainClass $plugin, AwaitStd $std, DatabaseUtils $dbu) : self {
2727
Info::registerByReflection("capital.analytics.top", PaginationInfo::class);

src/SOFe/Capital/Analytics/Top/QueryArgs.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SOFe\Capital\Analytics\Top;
66

7+
use pocketmine\utils\Binary;
78
use SOFe\Capital\AccountLabels;
89
use SOFe\Capital\AccountQueryMetric;
910
use SOFe\Capital\Config\Parser;
@@ -31,7 +32,7 @@ final class QueryArgs {
3132
private ?string $hash = null;
3233

3334
/**
34-
* @param LabelSelector $labelSelector The labels that filter the cacounts/transactions.
35+
* @param LabelSelector $labelSelector The labels that filter the accounts/transactions.
3536
* @param string $groupingLabel The label to group by.
3637
* @param array<string, string> $displayLabels The labels to display in the top list. Keys are InfoAPI info names and values are the labels to display.
3738
* @param self::ORDERING_* $ordering Whether to sort ascendingly or descendingly.
@@ -43,6 +44,7 @@ public function __construct(
4344
public string $groupingLabel,
4445
public array $displayLabels,
4546
public string $ordering,
47+
public ?int $interval,
4648
public QueryMetric $metric,
4749
) {
4850
}
@@ -64,6 +66,12 @@ public function hash() : string {
6466
$bytes .= $this->groupingLabel;
6567
$bytes .= "\0";
6668

69+
if ($this->interval !== null) {
70+
$bytes .= "\1" . Binary::writeLong($this->interval);
71+
} else {
72+
$bytes .= "\0";
73+
}
74+
6775
$bytes .= get_class($this->metric);
6876
$bytes .= "\0";
6977

@@ -89,13 +97,19 @@ public static function parse(Parser $config, Schema $schema) : self {
8997
self::ORDERING_DESC => self::ORDERING_DESC,
9098
default => $config->setValue("ordering", self::ORDERING_DESC, "Invalid ordering"),
9199
};
100+
$interval = $config->expectNullableNumber("interval", 24, <<<'EOT'
101+
Number of hours of data to consider.
102+
Only accounts used in this interval or transactions that took place in this interval are considered.
103+
EOT);
104+
$interval = $interval !== null ? (int) ($interval * 3600) : null;
92105
$metric = AccountQueryMetric::parseConfig($config, "metric");
93106

94107
return new self(
95108
labelSelector: $schema->getSelector(),
96109
groupingLabel: $groupingLabel,
97110
displayLabels: $displayLabels,
98111
ordering: $ordering,
112+
interval: $interval,
99113
metric: $metric,
100114
);
101115
}

src/SOFe/Capital/QueryMetric.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public function getMainTable() : string;
99

1010
public function getLabelTable() : string;
1111

12+
public function getTimestampColumn() : string;
13+
1214
public function getExpr() : string;
1315

1416
public function usesIdOnly() : bool;

src/SOFe/Capital/TransactionQueryMetric.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public function getLabelTable() : string {
5151
return "capital_tran_label";
5252
}
5353

54+
public function getTimestampColumn() : string {
55+
return "capital_acc.touch";
56+
}
57+
5458
public function getExpr() : string {
5559
return $this->expr;
5660
}

suitetest/cases/config-regen-currency/expect-config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ analytics:
125125
Whether to sort results ascendingly or descendingly.
126126
Use "asc" for ascending sort and "desc" for descending sort.
127127
ordering: desc
128+
'#interval': |-
129+
Number of hours of data to consider.
130+
Only accounts used in this interval or transactions that took place in this interval are considered.
131+
interval: 24
128132
'#metric': |-
129133
The statistic used to combine multiple values.
130134

suitetest/cases/mysql/expect-config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ analytics:
116116
Whether to sort results ascendingly or descendingly.
117117
Use "asc" for ascending sort and "desc" for descending sort.
118118
ordering: desc
119+
'#interval': |-
120+
Number of hours of data to consider.
121+
Only accounts used in this interval or transactions that took place in this interval are considered.
122+
interval: 24
119123
'#metric': |-
120124
The statistic used to combine multiple values.
121125

suitetest/cases/sqlite/expect-config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ analytics:
116116
Whether to sort results ascendingly or descendingly.
117117
Use "asc" for ascending sort and "desc" for descending sort.
118118
ordering: desc
119+
'#interval': |-
120+
Number of hours of data to consider.
121+
Only accounts used in this interval or transactions that took place in this interval are considered.
122+
interval: 24
119123
'#metric': |-
120124
The statistic used to combine multiple values.
121125

0 commit comments

Comments
 (0)