-- Tracking and aggregating object views...
--
-- Copyright (C) 2003 Jeff Davis
-- @author Jeff Davis <davis@xarg.net>
-- @creation-date 1/12/2003
--
-- @cvs-id $Id: views-create.sql,v 1.1 2004/03/28 21:17:14 jeffd Exp $
--
-- This is free software distributed under the terms of the GNU Public
-- License.  Full text of the license is available from the GNU Project:
-- http://www.fsf.org/copyleft/gpl.html

create table cop_object_views (
        object_id       integer
                        constraint cop_object_views_object_id_fk
                        references acs_objects(object_id) on delete cascade
                        constraint cop_object_views_object_id_nn
                        not null,
        viewer_id       integer
                        constraint cop_object_views_owner_id_fk
                        references parties(party_id) on delete cascade
                        constraint cop_object_views_viewer_id_nn
                        not null,
        views           integer default 1,
        last_viewed     timestamptz default now(),
        constraint cop_object_views_pk 
        primary key (object_id, viewer_id)
);

create unique index cop_object_views_viewer_idx on cop_object_views(viewer_id, object_id);

comment on table cop_object_views is '
        a simple count of how many times an object is viewed.
';

create table cop_object_view_agg (
        object_id       integer
                        constraint cop_object_views_object_id_fk
                        references acs_objects(object_id) on delete cascade
                        constraint cop_object_views_object_id_nn
                        not null 
                        constraint cop_object_view_agg_pk 
                        primary key,
        views           integer default 1,
        unique_views    integer default 1,
        last_viewed     timestamptz default now()
);

comment on table cop_object_views is '
        a simple count of how many times an object is viewed, multiple visits 
        trigger maintained by updates on cop_object_views.
';









