Skip to content

Commit

Permalink
Close #202
Browse files Browse the repository at this point in the history
  • Loading branch information
manoelcampos committed Jul 18, 2020
1 parent 1a00c02 commit d5f0e04
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 2 additions & 8 deletions backend/controllers/localizacao.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@ exports.getUltimaLocalizacao = async (req, res) => {
exports.getUltimaLocalizacaoTodos = async (req, res) => {
const client = await db.connect();
try {
const seconds = 60 * 5;
const sql =
`select latitude, longitude
from localizacao_dispositivo l where l.id =
(select max(l2.id) from localizacao_dispositivo as l2
where l2.uid = l.uid and
extract(epoch from (current_timestamp - l2.data_hora_ultima_atualizacao)) <= $1)`;
const resultado = await client.query(sql, [seconds]);
const sql = 'select latitude, longitude from vwUltimaLocalizacaoTodos';
const resultado = await client.query(sql);

res.status(200).send(resultado.rows);
} catch (error) {
Expand Down
15 changes: 15 additions & 0 deletions database/postgres-estrutura.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ create table localizacao_dispositivo (

comment on column localizacao_dispositivo.latitude is 'Latitude em Graus Decimais';
comment on column localizacao_dispositivo.longitude is 'Longitude em Graus Decimais';


create view vwUltimosDispositivosAtivos as
select uid, max(id) as id_localizacao from localizacao_dispositivo l
where extract(epoch from (current_timestamp - data_hora_ultima_atualizacao)) <= 300
group by uid;

comment on view vwUltimosDispositivosAtivos is 'Obtém os dispositivos que estiveram ativos nos últimos 5 minutos';

create view vwUltimaLocalizacaoTodos as
select latitude, longitude
from localizacao_dispositivo l
inner join vwUltimosDispositivosAtivos v on l.uid = v.uid and l.id = v.id_localizacao;

comment on view vwUltimaLocalizacaoTodos is 'Obtém a localização dos dispositivos que estiveram ativos nos últimos 5 minutos';

0 comments on commit d5f0e04

Please sign in to comment.