This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.php
More file actions
88 lines (73 loc) · 1.97 KB
/
main.php
File metadata and controls
88 lines (73 loc) · 1.97 KB
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
<?
namespace app;
use \MongoDate;
class Main extends \Enchinga\Controller {
private static $codes = array(
200 => 'Ok',
201 => 'Created',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
406 => 'Not Acceptable',
409 => 'Conflict',
418 => 'I\'m a Teapot'
);
public function index()
{
$data['version'] = \enchinga\VERSION;
$this->view('main', $data);
}
public function fetch($tipo, $cuantos)
{
$code = 200;
$cuantos = $cuantos? : 'ultimo';
if ( !in_array($cuantos, array('ultimo', 'todos', 'dump')) ) {
self::response(array(
'error'=>"No me enseño el PSM a darte $cuantos para $tipo",
"ayuda" => "Tal vez debas votar por el PSM para Rey del Eje central"
), 418);
exit;
}
$uno = $cuantos=='ultimo';
$fields = $find = array($tipo, 'fecha');
$q = $this->db->resultados->set($fields);
if ( $uno ) {
$data = $this->db->resultados->order('fecha', -1)->limit(1)->find();
$result = array(
'timestamp' => current($data)->fecha->sec
)+current($data)->$tipo;
} else {
$end = $this->get('end')? : time();
$start = $this->get('start')? : time()-60*10;
$where = $cuantos == 'dump'? array() : array(
'fecha' => array(
'$gte' => new MongoDate($start),
'$lte' => new MongoDate($end)
)
);
$data = $q->find($where);
if ($data){
$res = array();
foreach ( $data as $resultado ) {
$res[] = array('timestamp'=>$resultado->fecha->sec)+$resultado->$tipo;
}
$result = array(
'total' => count($data),
'resultados' => array_values($res)
);
} else {
$code = 404;
$result = array('error' => 'No tengo resultados para estas fechas', 'start'=>$start, 'end'=>$end);
}
}
self::response($result, $code);
}
public function response($resultSet, $code=200)
{
$message = self::$codes[$code];
header("HTTP/1.1 $code $message");
#header("Content-type: application/json");
echo json_encode($resultSet);
}
}