Completed
Push — 2.4 ( f4bb1c...1f4492 )
by James
132:27 queued 67:29
created

Console/Commands/DemoMetricPointSeederCommand.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of Cachet.
5
 *
6
 * (c) Alt Three Services Limited
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CachetHQ\Cachet\Console\Commands;
13
14
use CachetHQ\Cachet\Models\MetricPoint;
15
use Illuminate\Console\Command;
16
use Illuminate\Console\ConfirmableTrait;
17
use Symfony\Component\Console\Input\InputOption;
18
19
/**
20
 * This is the demo seeder of metric points command.
21
 *
22
 * @author James Brooks <[email protected]>
23
 */
24
class DemoMetricPointSeederCommand extends Command
25
{
26
    use ConfirmableTrait;
27
28
    /**
29
     * The console command name.
30
     *
31
     * @var string
32
     */
33
    protected $name = 'cachet:metrics';
34
35
    /**
36
     * The console command description.
37
     *
38
     * @var string
39
     */
40
    protected $description = 'Seeds the demo Cachet metrics with points';
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return void
46
     */
47
    public function handle()
48
    {
49
        if (!$this->confirmToProceed()) {
50
            return;
51
        }
52
53
        $this->seedMetricPoints();
54
55
        $this->info('Demo metric seeded with demo data successfully!');
56
    }
57
58
    /**
59
     * Seed the metric points table.
60
     *
61
     * @return void
62
     */
63
    protected function seedMetricPoints()
64
    {
65
        MetricPoint::truncate();
66
67
        $points = [];
68
69
        // Generate 24 hours of metric points
70
        for ($i = 0; $i <= 23; $i++) {
71
            for ($j = 0; $j <= 59; $j++) {
72
                $this->info("{$i}:{$j}");
73
74
                $pointTime = date("Y-m-d {$i}:{$j}:00");
75
76
                $points[] = [
77
                    'metric_id'  => 1,
78
                    'value'      => random_int(1, 10),
79
                    'created_at' => $pointTime,
80
                    'updated_at' => $pointTime,
81
                ];
82
            }
83
        }
84
85
        foreach (array_chunk($points, 100) as $chunk) {
86
            MetricPoint::insert($chunk);
0 ignored issues
show
The method insert() does not exist on CachetHQ\Cachet\Models\MetricPoint. Did you maybe mean insertAndSetId()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
87
        }
88
    }
89
90
    /**
91
     * Get the console command options.
92
     *
93
     * @return array
94
     */
95 102
    protected function getOptions()
96
    {
97
        return [
98 102
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
99
        ];
100
    }
101
}
102