FileSource::getAppList()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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