RE: Ayuda con inquietud

From: Lazáro Rubén García Martínez <lgarciam(at)vnz(dot)uci(dot)cu>
To: Jorge Toro <jolthgs(at)gmail(dot)com>, Foro Postgres <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: RE: Ayuda con inquietud
Date: 2012-07-09 15:21:59
Message-ID: 294D3D02D5E18D42827B2ECFEADEB688472A44BB7C@mx-interno.vnz.uci.cu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Entonces podrias tener dos tablas positions, una que se llame hist_positions por ejemplo, y la otra positions, sobre positions almacenas la última ubicación de los gps y sobre hist_positions el historial de posiciones de los gps, para almacenar las posiciones de los gps utililizas una función, y para llevar el historial de los gps, puedes utilizar un trigger sobre positions. Acá te dejo el código de todo, solo falta la referencia de la clave foránea. Espero que te sirva.

PD: Pudieran existir otras soluciones.

--Crear la tabla positions
CREATE TABLE public.positions (
id SERIAL,
gps_id INTEGER,
posicion VARCHAR,
velocidad REAL,
altura REAL,
fecha TIMESTAMP WITHOUT TIME ZONE,
grados REAL,
satelites INTEGER,
CONSTRAINT positions_pkey PRIMARY KEY(id)
) WITHOUT OIDS;

--Crear la tabla hist_positions
CREATE TABLE public.hist_positions (
id SERIAL,
gps_id INTEGER,
posicion VARCHAR,
velocidad REAL,
altura REAL,
fecha TIMESTAMP WITHOUT TIME ZONE,
grados REAL,
satelites INTEGER,
CONSTRAINT hist_positions_pkey PRIMARY KEY(id)
) WITHOUT OIDS;

--Función para guardar en positions
CREATE OR REPLACE FUNCTION public.fun_guardar_posicion (
gps_id_ integer,
posicion_ varchar,
velocidad_ real,
altura_ real,
fecha_ timestamp,
grados_ real,
satelites_ integer
)
RETURNS integer AS
$body$
DECLARE
retorno INTEGER;
BEGIN
UPDATE positions
SET
posicion = posicion_,
velocidad = velocidad_,
altura = altura_,
fecha = fecha_,
grados = grados_,
satelites = satelites_
WHERE gps_id = gps_id_
RETURNING id INTO retorno;

IF (retorno IS NULL) THEN
INSERT INTO positions
(
gps_id,
posicion,
velocidad,
altura,
fecha,
grados,
satelites
)
VALUES (
gps_id_,
posicion_,
velocidad_,
altura_,
fecha_,
grados_,
satelites_
)RETURNING id INTO retorno;
END IF;

RETURN retorno;
END;
$body$
LANGUAGE 'plpgsql';

--Función ejecutada por el trigger para guardar en el historial de positions
CREATE OR REPLACE FUNCTION public.fun_trg_hist_positions (
)
RETURNS trigger AS
$body$
DECLARE
BEGIN
INSERT INTO hist_positions
(
gps_id,
posicion,
velocidad,
altura,
fecha,
grados,
satelites
)
VALUES (
NEW.gps_id,
NEW.posicion,
NEW.velocidad,
NEW.altura,
NEW.fecha,
NEW.grados,
NEW.satelites
);

RETURN NULL;
END;
$body$
LANGUAGE 'plpgsql';

--Trigger utilizado
CREATE TRIGGER trg_hist_positions AFTER INSERT OR UPDATE
ON public.positions FOR EACH ROW
EXECUTE PROCEDURE public.fun_trg_hist_positions();

Saludos a todos.
________________________________________
From: pgsql-es-ayuda-owner(at)postgresql(dot)org [pgsql-es-ayuda-owner(at)postgresql(dot)org] On Behalf Of Jorge Toro [jolthgs(at)gmail(dot)com]
Sent: Monday, July 09, 2012 9:43 AM
To: Foro Postgres
Subject: Re: [pgsql-es-ayuda] Ayuda con inquietud

Hola Lazáro, gracias por responder.

Sí gps.id<http://gps.id> es la referencia a una tabla "gps" que se usa para registrar cada uno de los GPS que se registran para ser aceptados por el servidor.
Esta tabla "gps" tiene datos como: id, name, type, active. de cada uno de los GPS. Y la tabla "position" se encarga de almacenar los reportes (históricos) hechos por cada GPS.

Y lo que quiero lograr es, tener a la mano siempre que consulte un dispositivo, toda la última información de dicho dispositivo que se encuentre en la tabla "position".

Pero no quiero realizar una consulta directa a la tabla "position" porque son alrededor de 1000 GPS reportando cada minuto y esta tabla "position" tenderá a ser demasiado grande con el pasar del tiempo.

Por tu ayuda muchas gracias,

Jorge Alonso Toro
Ing. Teleinformático.

http://jolthgs.wordpress.com/
www.devmicrosystem.com<http://www.devmicrosystem.com>
--------------------------------------------------------------
Powered By Debian.
Developer Bullix GNU/Linux.
--------------------------------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQBIWWH6q7mzdgTzI5ARAkX5AJ9TR6hL2ocLMOUDRfhts8DlVl+jpwCeNw5x
p4+4FNUHPDUx1lU9F8WSKCA=
=zRhQ
-----END PGP SIGNATURE-----
Este correo esta protegido bajo los términos de la Licencia Atribución-Compartir Obras Derivadas Igual a 2.5 Colombia de Creative Commons. Observé la licencia visitando este sitio http://creativecommons.org/licenses/by-sa/2.5/co/.

El 9 de julio de 2012 09:17, Lazáro Rubén García Martínez <lgarciam(at)vnz(dot)uci(dot)cu<mailto:lgarciam(at)vnz(dot)uci(dot)cu>> escribió:
La columna gps_id es única

________________________________
Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE ENCUENTRAN INJUSTAMENTE EN PRISIONES DE LOS EEUU!
http://www.antiterroristas.cu
http://justiciaparaloscinco.wordpress.com

Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE ENCUENTRAN INJUSTAMENTE EN PRISIONES DE LOS EEUU!
http://www.antiterroristas.cu
http://justiciaparaloscinco.wordpress.com

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Jorge Toro 2012-07-09 15:29:06 Re: Ayuda con inquietud
Previous Message Lazáro Rubén García Martínez 2012-07-09 14:17:10 RE: Ayuda con inquietud