File: TimeSeriesTrait.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (101 lines) | stat: -rw-r--r-- 2,966 bytes parent folder | download | duplicates (3)
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
<?php

namespace Basho\Tests;

use Basho\Riak\TimeSeries\Cell;

/**
 * Helps with reusability for timeseries commands
 *
 * @author Christopher Mancini <cmancini at basho d0t com>
 */

trait TimeSeriesTrait
{
    protected static $table = "WeatherByRegion";
    protected static $tableBlob = "GeoCheckin_Wide_1_5";
    protected static $key = [];
    protected static $now;

    protected static function tableDefinition($table_name = "")
    {
        $table = "
            CREATE TABLE %s (
                region varchar not null,
                state varchar not null,
                time timestamp not null,
                weather varchar not null,
                temperature double,
                uv_index sint64,
                observed boolean not null,
                PRIMARY KEY((region, state, quantum(time, 15, 'm')), region, state, time)
            )";

        return sprintf($table, $table_name ? $table_name : static::$table);
    }

    protected static function populateKey()
    {
        static::$now = new \DateTime("@1443816900");

        static::$key = [
            (new Cell("region"))->setValue("South Atlantic"),
            (new Cell("state"))->setValue("South Carolina"),
            (new Cell("time"))->setTimestampValue(static::$now->getTimestamp()),
        ];
    }

    public static function generateRows()
    {
        $row = static::generateRow();
        $rows = [
            $row,
            [
                $row[0],
                $row[1],
                (new Cell("time"))->setTimestampValue(static::oneHourAgo()),
                (new Cell("weather"))->setValue("windy"),
                (new Cell("temperature"))->setDoubleValue(19.8),
                (new Cell("uv_index"))->setIntValue(10),
                (new Cell("observed"))->setBooleanValue(true),
            ],
            [
                $row[0],
                $row[1],
                (new Cell("time"))->setTimestampValue(static::twoHoursAgo()),
                (new Cell("weather"))->setValue("cloudy"),
                (new Cell("temperature"))->setDoubleValue(19.1),
                (new Cell("uv_index"))->setIntValue(15),
                (new Cell("observed"))->setBooleanValue(false),
            ],
        ];

        return $rows;
    }

    public static function generateRow()
    {
        $row = static::$key;
        $row[] = (new Cell("weather"))->setValue("hot");
        $row[] = (new Cell("temperature"))->setDoubleValue(23.5);
        $row[] = (new Cell("uv_index"))->setIntValue(10);
        $row[] = (new Cell("observed"))->setBooleanValue(true);

        return $row;
    }

    public static function oneHourAgo()
    {
        return static::$now->getTimestamp() - 60 * 60 * 1;
    }

    public static function twoHoursAgo()
    {
        return static::$now->getTimestamp() - 60 * 60 * 2;
    }

    public static function threeHoursAgo()
    {
        return static::$now->getTimestamp() - 60 * 60 * 3;
    }
}