Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit 320e88d

Browse files
committed
Stats view days order && Validate synced data
1 parent 2346635 commit 320e88d

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

core/Stats.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ private function backUntil(int $timestamp, array $f, int $forwardTo = StatsData:
160160
}
161161

162162
private function printDataset(array $data) : void {
163+
array_multisort( // sort multiple days (s.t. latest days are first)
164+
array_column( $data, 'begin' ), SORT_DESC,
165+
$data
166+
);
167+
163168
$noExternalDevice = true;
164169
$combi = array();
165170
foreach( $data as $d ){
@@ -196,7 +201,7 @@ private function printDataset(array $data) : void {
196201
'Name' => $d['name'],
197202
'Time' => $d['duration'],
198203
'Work Items' => str_pad($d['times'], 4, " ", STR_PAD_LEFT),
199-
'Days' => implode(', ', array_slice(array_reverse($d['days']), 0, self::DAYS_MAXCOUNT))
204+
'Days' => implode(', ', array_slice($d['days'], 0, self::DAYS_MAXCOUNT))
200205
. (count($d['days']) > self::DAYS_MAXCOUNT ? ', ...' : '' )
201206
),
202207
$noExternalDevice ? array() : array(

core/sync/DirectoryStatsAccess.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct(){
1313
$this->thisClientName = $c->getValue(['sync', 'directory', 'thisname']);
1414
}
1515

16-
public function listFiles() : array {
16+
protected function listFilesUnfiltered() : array {
1717
$files = array();
1818
foreach(array_diff(scandir($this->directory), ['.','..']) as $dir ){
1919
if( $dir !== $this->thisClientName && is_dir($this->directory . '/' . $dir) ){
@@ -39,7 +39,7 @@ function ($f) {
3939
return $files;
4040
}
4141

42-
public function getFile( string $file, string $device ) : array {
42+
protected function getFileUnfiltered( string $file, string $device ) : array {
4343
return json_decode(file_get_contents( $this->directory . '/' . $device . '/' . $file ), true);
4444
}
4545

core/sync/LocalStatsAccess.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class LocalStatsAccess extends StatsAccess {
44

5-
public function listFiles() : array {
5+
protected function listFilesUnfiltered() : array {
66
return array_map( function ($f) {
77
return array(
88
'timestamp' => strtotime(substr($f, 0, -5)),
@@ -16,7 +16,7 @@ public function listFiles() : array {
1616
);
1717
}
1818

19-
public function getFile( string $file, string $device ) : array {
19+
protected function getFileUnfiltered( string $file, string $device ) : array {
2020
return Config::getStorageReader(substr($file, 0, -5))->getArray();
2121
}
2222

core/sync/ServerStatsAccess.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ private function postToServer(string $endpoint, array $data = array() ) : array
4848
return array();
4949
}
5050

51-
public function listFiles() : array {
51+
protected function listFilesUnfiltered() : array {
5252
return $this->postToServer('list');
5353
}
5454

55-
public function getFile( string $file, string $device ) : array {
55+
protected function getFileUnfiltered( string $file, string $device ) : array {
5656
return $this->postToServer(
5757
'get',
5858
array(

core/sync/StatsAccess.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,65 @@ abstract class StatsAccess {
1212
*
1313
* Must not return stats of the client itself!
1414
*/
15-
abstract public function listFiles() : array;
15+
public function listFiles() : array {
16+
return $this->filterFileList($this->listFilesUnfiltered());
17+
}
18+
abstract protected function listFilesUnfiltered() : array;
1619

1720
/**
18-
* return array like JSON in 2020-02-12.json
21+
* return array like JSON in e.g. "2020-02-12.json"
1922
*/
20-
abstract public function getFile( string $file, string $device ) : array;
23+
public function getFile( string $file, string $device ) : array {
24+
return $this->filterFileArray($this->getFileUnfiltered($file, $device));
25+
}
26+
abstract protected function getFileUnfiltered( string $file, string $device ) : array;
2127

28+
/**
29+
* Copy initial files to the sync
30+
*/
2231
abstract public function initialSync() : bool;
2332

33+
/**
34+
* Set a array of daily task
35+
* (will use last tasks timestamp to calculate day)
36+
*/
2437
abstract public function setDayTasks(array $tasks) : void;
2538

39+
/**
40+
* Get a list of all local files to sync on init of a sync
41+
*/
2642
protected function filesToSyncInitially() : array {
2743
return array_values(array_filter(scandir( Config::getStorageDir() ), function ($f) {
2844
return preg_match(self::FILENAME_PREG, $f) === 1
2945
&& date('Y-m-d') !== substr($f, 0, -5);
3046
}));
3147
}
48+
49+
/**
50+
* Check format of a one days task array from server/ directory
51+
* @return the filtered $array, or array()
52+
*/
53+
private function filterFileArray(array $array) : array {
54+
return array_values(array_filter( $array, function ($a) {
55+
return isset($a['begin']) && isset($a['end']) && isset($a['name']) && isset($a['category'])
56+
&& is_int($a['begin']) && $a['begin'] > 0
57+
&& is_int($a['end']) && $a['end'] > 0
58+
&& InputParser::checkNameInput( $a['name'] )
59+
&& InputParser::checkCategoryInput( $a['category'] );
60+
}));
61+
}
62+
63+
/**
64+
* Check format of a file list array from server/ directory
65+
* @return the filtered $array, or array()
66+
*/
67+
private function filterFileList(array $array) : array {
68+
return array_values(array_filter( $array, function ($a) {
69+
return isset($a['timestamp']) && isset($a['file']) && isset($a['device'])
70+
&& is_int($a['timestamp']) && $a['timestamp'] > 0
71+
&& preg_match(self::FILENAME_PREG, $a['file']) === 1
72+
&& ($a['device'] === '' || InputParser::checkDeviceName( $a['device'] ));
73+
}));
74+
}
3275
}
3376
?>

0 commit comments

Comments
 (0)