:- ensure_loaded(common).
cvs_id('$Id: harvest_watch.pl,v 1.3 2004/10/08 18:29:39 sandro Exp $').
/*

     OBSOLETE / BROKEN.

  
  Watch the status of the harvest!

  We use a bunch of dynamic predicates, updated by various places, to
  keep track of the progress.  Sorry if the names don't seem terribly
  obvious.   The connections are rather spaghetti-like.  :-(
  
  comment on thrown host-in-use, host-still-warm, etc?

  REALLY SHOULD BE an instance of Harvester, with all this data
  available as RDF.  Really.
  
*/

:- ensure_loaded(library(debug)).

:- multi_dynamic(ret_watchable/1).    % 
:- multi_dynamic(ret_activated/3).    % (Ret, TimeActivated, Thread)
:- multi_dynamic(ret_dns/2).          % (Ret, TimeDone)
:- multi_dynamic(ret_connected/2).    % (Ret, TimeDone)
:- multi_dynamic(ret_length_so_far/2).
:- multi_dynamic(ret_content_length/2).  % only asserted if the server told us!
:- multi_dynamic(ret_length_so_far_at/3).   % for rate calculations
:- multi_dynamic(ret_transfer_complete/1).
:- multi_dynamic(ret_format/2).
:- multi_dynamic(ret_parsed/2).
:- multi_dynamic(ret_error/2).
:- multi_dynamic(ret_triples_indexed/2).


ret_triples_parsed(Ret, Parsed) :-
	% see parse.pl
	total_retrieval_counter(Ret, Counter),
	flag(Counter, Parsed, Parsed).


%  all active transfers, all active or recently-completed transfer, all queued,
%  all precache-requested, ...?
%
%  For now -- all done since this server started!

web_server_path_view(harvest_watch, harvest_watch_view).

harvest_watch_view(_Q, H) :-
	H = [ h2('Current Harvest Status'),
	      table(border='2',
		    tr(th('Request'),
		       th('Status'),
		       th('DNS'),
		       th('Connect'),
		       th('Transfer'),
		       th('Format'),
		       th('Triples Parsed'),
		       th('Indexed'),
		       th('Address (URI)')
		       ),
		    +forall(ret_watchable(Ret),
			    tr(+harvest_watch_ret(Ret)))
		   )
	    ].

harvest_watch_ret(Ret, H) :-
	%(   rdfpage(Ret, retr:host, literal(Host))
	%->  true
	%;    throw(ret_without_host(Ret))
	%),
	(   rdfpage(Ret, retr:initialResource, Address)
	->  true
	;   throw(ret_without_address(Ret))
	),
	!,
	H=[ td(+word([], Ret)),
	    td(+harvest_status(Ret)),
	    td(+ifelse(ret_dns(Ret, _), 'done', ' ')),
	    td(+ifelse(ret_connected(Ret, _), 'done', ' ')),
	    td(+ifelse(ret_length_so_far(Ret, Bytes), [ int(Bytes), ' bytes'], ' '),
	       +if(ret_transfer_complete(Ret), ' (done)')),
	    td(+ifelse(ret_format(Ret, Format), Format, ' ')),
	    td(+ifelse(ret_triples_parsed(Ret, Triples), int(Triples), ' ')),
	    td(+ifelse(ret_index_done_fraction(Ret, Frac), [{Pct is Frac * 100}, float(Pct, 1), '%'], ' ')),
	    td(a(href=Address, Address))
	  ].

harvest_status(Ret, H) :-
	harvest_queue_priority_position(Ret, Pri, Pos),
	H = [ 'Queued ', int(Pri), '.', int(Pos) ].
harvest_status(Ret, H) :-
	ret_error(Ret, Error),
	(   harvest_error_message(Error, M)
	->  H = M
	;   print_message(error, Error),
	    H = 'Unknown Error'
	).
harvest_status(Ret, H) :-
	ret_transfer_complete(Ret),
	H = 'Transfer complete'.
harvest_status(Ret, H) :-
	ret_activated(Ret, Time, Thread),
	H = ['Retrieval started ', +reltime(Time), ', thread ', Thread].
harvest_status(_Ret, 'confused').
	
harvest_error_message(not_found, 'Not Found (Error 404)').
harvest_error_message(forbidden, 'Forbidden (Error 403)').
harvest_error_message(authorise, 'Authorization Required').

harvest_queue_priority_position(Ret, Pri, Pos) :-
	harvest_queue(Pri, Ret),
	gensym(hqpp, Flag),
	flag(Flag, _, 1),
	harvest_queue(Pri, Maybe),
	flag(Flag, Pos, Pos+1),
	Maybe = Ret.   % fail back to harvest_queue until last

ret_format(_Ret, not_impl).


   	