-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemProcessor.php
More file actions
132 lines (115 loc) · 4.56 KB
/
FileSystemProcessor.php
File metadata and controls
132 lines (115 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace oSoc\Smartflanders\Filesystem;
// TODO File system processor/reader/writer is bad design (lots of dependencies on each other). Redesign.
use \League\Flysystem\Adapter\Local;
use \League\Flysystem\Filesystem;
use oSoc\Smartflanders\Helpers\IGraphProcessor;
use pietercolpaert\hardf\TriGWriter;
Class FileSystemProcessor {
protected $out_fs;
protected $res_fs;
protected $stat_fs;
protected $second_interval;
protected $writer;
protected $graph_processor;
protected $static_data_filename;
protected $out_dirname;
protected $res_dirname;
const REFRESH_STATIC = false;
public function __construct($out_dirname, $res_dirname, $second_interval, IGraphProcessor $graph_processor)
{
$this->out_dirname = $out_dirname;
$this->res_dirname = $res_dirname;
$this->second_interval = $second_interval;
date_default_timezone_set("Europe/Brussels");
$out_adapter = new Local($out_dirname . "/" . $graph_processor->getName());
$this->out_fs = new Filesystem($out_adapter);
$res_adapter = new Local($res_dirname);
$this->res_fs = new Filesystem($res_adapter);
$stat_adapter = new Local($out_dirname . "/" . $graph_processor->getName() . "/statistical");
$this->stat_fs = new Filesystem($stat_adapter);
$this->graph_processor = $graph_processor;
$this->static_data_filename = $graph_processor->getName() . "_static_data.turtle";
if(!$this->res_fs->has($this->static_data_filename) || self::REFRESH_STATIC){
$graph = $graph_processor->getStaticGraph();
$this->writer = new TriGWriter();
$this->writer->addPrefixes($graph["prefixes"]);
$this->writer->addTriples($graph["triples"]);
$this->res_fs->put($this->static_data_filename, $this->writer->end());
}
}
public function getFilesForDay(\DateTime $day) {
$result = array();
$start = $day->format('U'); // To unix
$start = $start - ($start % 60*60*24); // Round down to day
$start = $this->getPreviousTimestampFromTimestamp($start); // Get valid timestamp
$end = $start + 60*60*24;
for ($i = $start; $i < $end; $i += $this->second_interval) {
if ($this->hasFile($i)) {
array_push($result, $i);
}
}
return $result;
}
public function getSecondInterval()
{
return $this->second_interval;
}
public function setSecondInterval($second_interval)
{
$this->second_interval = $second_interval;
}
// Get the last written page (closest to now)
public function getLastPage() {
return $this->getPreviousTimestampFromTimestamp(time());
}
// Get the oldest timestamp for which a file exists
public function getOldestTimestamp() {
$filename = $this->graph_processor->getName() . "_oldest_timestamp";
if ($this->res_fs->has($filename)) {
return $this->res_fs->read($filename);
}
return false;
}
// Round a timestamp to its respective file timestamp
protected function roundTimestamp($timestamp) {
//$timestamp -= $timestamp % $this->second_interval;
$timestamp -= $timestamp % 3600;
return $timestamp;
}
public function getPreviousTimestampFromTimestamp($timestamp) {
$oldest = $this->getOldestTimestamp();
if ($oldest) {
$timestamp = $this->roundTimestamp($timestamp);
while ($timestamp > $oldest) {
$filename = $this->roundTimestamp($timestamp);
if ($this->out_fs->has($filename)) {
return $timestamp;
}
$timestamp -= 60*60;
}
}
return false;
}
public function getNextTimestampForTimestamp($timestamp) {
$timestamp = $this->roundTimestamp($timestamp);
$now = time();
while($timestamp < $now) {
$timestamp += 60*60;
$filename = $this->roundTimestamp($timestamp);
if ($this->out_fs->has($filename)) {
return $timestamp;
}
}
return false;
}
public function hasFile($filename) {
return $this->out_fs->has($filename);
}
public function getFileReader() {
return new FileReader($this->out_dirname, $this->res_dirname, $this->second_interval, $this->graph_processor);
}
public function getFileWriter() {
return new FileWriter($this->out_dirname, $this->res_dirname, $this->second_interval, $this->graph_processor);
}
}