Completed
Push — master ( 7f13d8...53cf08 )
by Shagiakhmetov
02:14
created

FileSource::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * @maintainer Timur Shagiakhmetov <[email protected]>
5
 */
6
7
namespace Badoo\LiveProfilerUI\DataProviders;
8
9
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SourceInterface;
10
use Badoo\LiveProfilerUI\Interfaces\DataPackerInterface;
11
12
class FileSource implements SourceInterface
13
{
14
    const SELECT_LIMIT = 1440;
15
16
    /** @var string  */
17
    protected $path;
18
    /** @var DataPackerInterface */
19
    protected $DataPacker;
20
21
    public function __construct(string $path, DataPackerInterface $DataPacker)
22
    {
23
        $this->path = $path;
24
        $this->DataPacker = $DataPacker;
25
    }
26
27
    public function getSnapshotsDataByDates(string $datetime_from, string $datetime_to): array
28
    {
29
        $snapshots = [];
30
        $apps = $this->getAppList();
31
        foreach ($apps as $app) {
32
            $label_dirs = scandir($this->path . '/' . $app, SCANDIR_SORT_NONE);
33
            foreach ($label_dirs as $label_dir) {
34
                if ($label_dir === '.' || $label_dir === '..') {
35
                    continue;
36
                }
37
                $files = scandir($this->path . '/' . $app . '/' . $label_dir, SCANDIR_SORT_NONE);
38
                foreach ($files as $file) {
39
                    if ($file === '.' || $file === '..') {
40
                        continue;
41
                    }
42
                    $timestamp = (int)str_replace('.json', '', $file);
43
                    if ($timestamp >= strtotime($datetime_from) && $timestamp <= strtotime($datetime_to)) {
44
                        $label = base64_decode($label_dir);
45
                        $date = date('Y-m-d', $timestamp);
46
                        $snapshots["$app|$label|$date"] = [
47
                            'app' => $app,
48
                            'label' => $label,
49
                            'date' => $date,
50
                        ];
51
                    }
52
                }
53
            }
54
        }
55
56
        return $snapshots;
57
    }
58
59
    public function getPerfData(string $app, string $label, string $date): array
60
    {
61
        $perf_data = [];
62
        $dir = $this->path . '/' . $app . '/' . base64_encode($label);
63
        if (is_dir($dir)) {
64
            $files = scandir($dir, SCANDIR_SORT_NONE);
65
            foreach ($files as $file) {
66
                if ($file === '.' || $file === '..') {
67
                    continue;
68
                }
69
                $timestamp = (int)str_replace('.json', '', $file);
70
                if ($timestamp >= strtotime($date . ' 00:00:00') && $timestamp <= strtotime($date . ' 23:59:59')) {
71
                    $perf_data[] = file_get_contents($dir . '/' . $file);
72
                    if (count($perf_data) >= self::SELECT_LIMIT + 1) {
73
                        break;
74
                    }
75
                }
76
            }
77
        }
78
79
        return $perf_data;
80
    }
81
82
    public function getLabelList(): array
83
    {
84
        $labels = [];
85
        $apps = $this->getAppList();
86
        foreach ($apps as $app) {
87
            $label_dirs = scandir($this->path . '/' . $app, SCANDIR_SORT_NONE);
88
            foreach ($label_dirs as $label_dir) {
89
                if ($label_dir !== '.' && $label_dir !== '..' && !isset($labels[$label_dir])) {
90
                    $label = base64_decode($label_dir);
91
                    $labels[$label_dir] = $label;
92
                }
93
            }
94
        }
95
        sort($labels);
96
97
        return $labels;
98
    }
99
100
    public function getAppList(): array
101
    {
102
        $apps = [];
103
        $dirs = scandir($this->path, SCANDIR_SORT_ASCENDING);
104
        foreach ($dirs as $dir) {
105
            if ($dir !== '.' && $dir !== '..') {
106
                $apps[] = $dir;
107
            }
108
        }
109
110
        return $apps;
111
    }
112
}
113