Completed
Push — master ( cbb544...abb536 )
by Manuel
03:31
created

Statistics   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 94.44%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 8
c 5
b 1
f 2
lcom 0
cbo 2
dl 0
loc 92
ccs 34
cts 36
cp 0.9444
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A totalPokes() 0 6 1
A allPokes() 0 8 1
A totalDevices() 0 4 1
B networkDistribution() 0 28 2
A createSqliteRegexpFunction() 0 4 1
A addColors() 0 11 2
1
<?php
2
3
namespace PiFinder\Services;
4
5
use DB;
6
use PiFinder\Poke;
7
8
class Statistics
9
{
10
    /**
11
     * Returns the count of all pokes.
12
     *
13
     * @return mixed
14
     */
15 1
    public function totalPokes()
16
    {
17 1
        $base = 189771;
18
19 1
        return Poke::count() + $base;
20
    }
21
22
    /**
23
     * Returns the the count of all devices.
24
     *
25
     * @return mixed
26
     */
27 1
    public function totalDevices()
28
    {
29 1
        return Poke::distinct()->count('mac');
30
    }
31
32
    /**
33
     * Returns all pokes.
34
     *
35
     * @return mixed
36
     */
37 1
    public function allPokes()
38
    {
39 1
        return Poke::select(
40 1
            DB::raw('count(*) as pokes, date(created_at) as date')
41 1
        )
42 1
            ->groupBy('date')
43 1
            ->get();
44
    }
45
46
    /**
47
     * Returns a collection of all networks and it's count.
48
     *
49
     * @return mixed
50
     */
51 1
    public function networkDistribution()
52
    {
53 1
        $regex = '^172\.(1[6-9]|2[0-9]|3[01])\.';
54
55 1
        if((new Poke)->getConnection()->getConfig('driver') == 'sqlite') {
56 1
            $regex = "/$regex/";
57 1
            $this->createSqliteRegexpFunction();
58 1
        }
59
60 1
        $data = Poke::select(
61 1
            DB::raw("
62
                CASE
63
                    WHEN ip LIKE '192.168.%'
64
                        THEN '192.168.0.0/16'
65
                    WHEN (ip REGEXP '$regex')
66
                        THEN '172.16.0.0/12'
67
                    WHEN ip LIKE '10.%'
68
                        THEN '10.0.0.0/8'
69
                    ELSE 'Internet'
70
                END
71 1
		AS label, count(*) as value")
72 1
        )
73 1
            ->groupBy('label')
74 1
            ->orderBy('label')
75 1
            ->get();
76
77 1
        return $this->addColors($data);
78
    }
79
80
    /**
81
     * Creates the Regexp function for the SQLITE database.
82
     */
83 1
    protected function createSqliteRegexpFunction()
84
    {
85 1
        DB::connection()->getPdo()->sqliteCreateFunction('REGEXP', 'preg_match', 2);
86 1
    }
87
88 1
    private function addColors($data)
89
    {
90 1
        $colors = ['rgb(23,103,153)', 'rgb(47,135,176)', 'rgb(66,164,187)', 'rgb(91,192,196)'];
91 1
        $highlight_colors = ['#8BB3CC', '#97C3D7', '#ADDFE1', '#BBEAE3'];
92 1
        foreach ($data as $i => $network) {
93
            $network['color'] = $colors[$i];
94
            $network['highlight'] = $highlight_colors[$i];
95 1
        }
96
97 1
        return $data;
98
    }
99
}
100