Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/SOFe/Capital/AccountQueryMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function getLabelTable() : string {
return "capital_acc_label";
}

public function getTimestampColumn() : string {
return "capital_acc.touch";
}

public function getExpr() : string {
return $this->expr;
}
Expand Down
15 changes: 12 additions & 3 deletions src/SOFe/Capital/Analytics/Top/DatabaseUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,21 @@ public function compute(string $runId, QueryArgs $query) : Generator {
$metricExpr .= " INNER JOIN $labelTable AS t{$i} USING (id)";
}

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

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

if ($query->interval !== null) {
$tsColumn = $query->metric->getTimestampColumn();
$timeDiffExpr = match ($this->db->dialect) {
SqlDialect::SQLITE => "JULIANDAY(CURRENT_TIMESTAMP) - JULIANDAY($tsColumn)",
SqlDialect::MYSQL => "TIMESTAMPDIFF(SECOND, $tsColumn, CURRENT_TIMESTAMP)",
};
$metricExpr .= " AND $timeDiffExpr < :interval";
$vars["interval"] = new GenericVariable("interval", GenericVariable::TYPE_INT, null);
$args["interval"] = $query->interval;
}

$i = 0;
foreach ($query->labelSelector->getEntries() as $name => $value) {
$metricExpr .= " AND t{$i}.name = :name{$i}";
Expand Down
2 changes: 1 addition & 1 deletion src/SOFe/Capital/Analytics/Top/Mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
final class Mod implements Singleton, FromContext {
use SingletonArgs, SingletonTrait;

public const API_VERSION = "0.1.0";
public const API_VERSION = "0.2.0";

public static function fromSingletonArgs(Analytics\Config $config, MainClass $plugin, AwaitStd $std, DatabaseUtils $dbu) : self {
Info::registerByReflection("capital.analytics.top", PaginationInfo::class);
Expand Down
16 changes: 15 additions & 1 deletion src/SOFe/Capital/Analytics/Top/QueryArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SOFe\Capital\Analytics\Top;

use pocketmine\utils\Binary;
use SOFe\Capital\AccountLabels;
use SOFe\Capital\AccountQueryMetric;
use SOFe\Capital\Config\Parser;
Expand Down Expand Up @@ -31,7 +32,7 @@ final class QueryArgs {
private ?string $hash = null;

/**
* @param LabelSelector $labelSelector The labels that filter the cacounts/transactions.
* @param LabelSelector $labelSelector The labels that filter the accounts/transactions.
* @param string $groupingLabel The label to group by.
* @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.
* @param self::ORDERING_* $ordering Whether to sort ascendingly or descendingly.
Expand All @@ -43,6 +44,7 @@ public function __construct(
public string $groupingLabel,
public array $displayLabels,
public string $ordering,
public ?int $interval,
public QueryMetric $metric,
) {
}
Expand All @@ -64,6 +66,12 @@ public function hash() : string {
$bytes .= $this->groupingLabel;
$bytes .= "\0";

if ($this->interval !== null) {
$bytes .= "\1" . Binary::writeLong($this->interval);
} else {
$bytes .= "\0";
}

$bytes .= get_class($this->metric);
$bytes .= "\0";

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

return new self(
labelSelector: $schema->getSelector(),
groupingLabel: $groupingLabel,
displayLabels: $displayLabels,
ordering: $ordering,
interval: $interval,
metric: $metric,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/SOFe/Capital/QueryMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public function getMainTable() : string;

public function getLabelTable() : string;

public function getTimestampColumn() : string;

public function getExpr() : string;

public function usesIdOnly() : bool;
Expand Down
4 changes: 4 additions & 0 deletions src/SOFe/Capital/TransactionQueryMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function getLabelTable() : string {
return "capital_tran_label";
}

public function getTimestampColumn() : string {
return "capital_acc.touch";
}

public function getExpr() : string {
return $this->expr;
}
Expand Down
4 changes: 4 additions & 0 deletions suitetest/cases/config-regen-currency/expect-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ analytics:
Whether to sort results ascendingly or descendingly.
Use "asc" for ascending sort and "desc" for descending sort.
ordering: desc
'#interval': |-
Number of hours of data to consider.
Only accounts used in this interval or transactions that took place in this interval are considered.
interval: 24
'#metric': |-
The statistic used to combine multiple values.

Expand Down
4 changes: 4 additions & 0 deletions suitetest/cases/mysql/expect-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ analytics:
Whether to sort results ascendingly or descendingly.
Use "asc" for ascending sort and "desc" for descending sort.
ordering: desc
'#interval': |-
Number of hours of data to consider.
Only accounts used in this interval or transactions that took place in this interval are considered.
interval: 24
'#metric': |-
The statistic used to combine multiple values.

Expand Down
4 changes: 4 additions & 0 deletions suitetest/cases/sqlite/expect-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ analytics:
Whether to sort results ascendingly or descendingly.
Use "asc" for ascending sort and "desc" for descending sort.
ordering: desc
'#interval': |-
Number of hours of data to consider.
Only accounts used in this interval or transactions that took place in this interval are considered.
interval: 24
'#metric': |-
The statistic used to combine multiple values.

Expand Down