2016-04-25 20:02:11 +00:00
/ *
* brozzler - webconsole / static / js / app . js - brozzler console angularjs code
*
* Copyright ( C ) 2014 - 2016 Internet Archive
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
2015-09-25 22:19:29 +00:00
"use strict" ;
var brozzlerConsoleApp = angular . module ( "brozzlerConsoleApp" , [
2015-10-09 20:31:07 +00:00
"ngRoute" ,
"brozzlerControllers" ,
2015-09-25 22:19:29 +00:00
] ) ;
2015-09-25 22:48:01 +00:00
brozzlerConsoleApp . config ( [ "$routeProvider" , "$locationProvider" ,
2015-10-09 20:31:07 +00:00
function ( $routeProvider , $locationProvider ) {
$routeProvider .
2015-10-27 19:00:44 +00:00
when ( "/workers" , {
templateUrl : "/static/partials/workers.html" ,
controller : "WorkersListController"
} ) .
2015-10-09 20:31:07 +00:00
when ( "/jobs/:id" , {
templateUrl : "/static/partials/job.html" ,
controller : "JobController"
} ) .
when ( "/sites/:id" , {
templateUrl : "/static/partials/site.html" ,
controller : "SiteController"
} ) .
when ( "/" , {
2015-11-12 02:57:27 +00:00
templateUrl : "/static/partials/home.html" ,
controller : "HomeController"
2015-10-09 20:31:07 +00:00
} ) .
otherwise ( {
template : '<div> <div class="page-header"> <h1>Not Found</h1> </div> <div class="row"> <div class="col-sm-12"> How the heck did you get here? </div> </div> </div> ' ,
} ) ;
2015-09-25 22:48:01 +00:00
2015-10-09 20:31:07 +00:00
$locationProvider . html5Mode ( {
enabled : true ,
requireBase : false ,
} ) ;
} ] ) ;
2015-09-28 22:05:43 +00:00
// copied from https://bitbucket.org/webarchive/ait5/src/master/archiveit/static/app/js/filters/ByteFormat.js
brozzlerConsoleApp . filter ( "byteformat" , function ( ) {
2015-10-09 20:31:07 +00:00
return function ( bytes , precision ) {
var bytes _f = parseFloat ( bytes ) ;
if ( bytes _f == 0 || isNaN ( bytes _f ) || ! isFinite ( bytes _f ) ) return "0" ;
if ( bytes _f < 1024 ) return bytes _f . toFixed ( 0 ) + " bytes" ;
if ( typeof precision === "undefined" ) precision = 1 ;
var units = [ "bytes" , "kB" , "MB" , "GB" , "TB" , "PB" ] ;
var number = Math . floor ( Math . log ( bytes _f ) / Math . log ( 1024 ) ) ;
var result = ( bytes _f / Math . pow ( 1024 , Math . floor ( number ) ) ) . toFixed ( precision ) + " " + units [ number ] ;
return result ;
}
2015-09-28 22:05:43 +00:00
} ) ;
2015-10-27 19:00:44 +00:00
var brozzlerControllers = angular . module ( "brozzlerControllers" , [ ] ) ;
2015-11-12 02:57:27 +00:00
brozzlerControllers . controller ( "HomeController" , [ "$scope" , "$http" ,
2015-10-27 19:00:44 +00:00
function ( $scope , $http ) {
2016-04-19 18:51:14 +00:00
$http . get ( "/api/config" ) . success ( function ( data ) {
$scope . config = data . config ;
} ) ;
$http . get ( "/api/jobs" ) . success ( function ( data ) {
$scope . jobs = data . jobs ;
} ) ;
$http . get ( "/api/services" ) . success ( function ( data ) {
$scope . services = data . services ;
} ) ;
2015-10-27 19:00:44 +00:00
} ] ) ;
brozzlerControllers . controller ( "WorkersListController" , [ "$scope" , "$http" ,
function ( $scope , $http ) {
2016-04-19 18:51:14 +00:00
$http . get ( "/api/config" ) . success ( function ( data ) {
$scope . config = data . config ;
} ) ;
2015-10-27 19:00:44 +00:00
$http . get ( "/api/workers" ) . success ( function ( data ) {
$scope . workers = data . workers ;
} ) ;
} ] ) ;
function statsSuccessCallback ( site , bucket ) {
return function ( data ) {
// console.log("site = ", site);
// console.log("/api/stats/" + bucket + " = ", data);
site . stats = data ;
}
}
function pageCountSuccessCallback ( site , job ) {
return function ( data ) {
// console.log("site = ", site);
// console.log("/api/sites/" + site.id + "/page_count = ", data);
site . page _count = data . count ;
2016-04-19 18:51:14 +00:00
if ( job ) {
job . page _count += data . count ;
}
2015-10-27 19:00:44 +00:00
}
}
function queuedCountSuccessCallback ( site , job ) {
return function ( data ) {
// console.log("site = ", site);
// console.log("/api/sites/" + site.id + "/queued_count = ", data);
site . queued _count = data . count ;
2016-04-19 18:51:14 +00:00
if ( job ) {
job . queued _count += data . count ;
}
2015-10-27 19:00:44 +00:00
}
}
function loadSiteStats ( $http , site , job ) {
$http . get ( "/api/sites/" + site . id + "/page_count" ) . success ( pageCountSuccessCallback ( site , job ) ) ;
$http . get ( "/api/sites/" + site . id + "/queued_count" ) . success ( queuedCountSuccessCallback ( site , job ) ) ;
2016-06-20 16:48:01 +00:00
// look at Warcprox-Meta to find stats bucket
for ( var j = 0 ; j < site . warcprox _meta . stats . buckets . length ; j ++ ) {
if ( site . warcprox _meta . stats . buckets [ j ] . indexOf ( "seed" ) >= 0 ) {
var bucket = site . warcprox _meta . stats . buckets [ j ] ;
2015-10-27 19:00:44 +00:00
// console.log("warcprox_meta.stats.buckets[" + j + "]=" + bucket);
$http . get ( "/api/stats/" + bucket ) . success ( statsSuccessCallback ( site , bucket ) ) ;
}
}
}
brozzlerControllers . controller ( "JobController" , [ "$scope" , "$routeParams" , "$http" ,
function ( $scope , $routeParams , $http ) {
2016-06-20 16:48:01 +00:00
// console.log('JobController');
2016-04-19 18:51:14 +00:00
$http . get ( "/api/config" ) . success ( function ( data ) {
$scope . config = data . config ;
} ) ;
2015-10-27 19:00:44 +00:00
$http . get ( "/api/jobs/" + $routeParams . id ) . success ( function ( data ) {
$scope . job = data ;
$scope . job . page _count = $scope . job . queued _count = 0 ;
// console.log("job=", $scope.job);
$http . get ( "/api/stats/" + $scope . job . conf . warcprox _meta . stats . buckets [ 0 ] ) . success ( function ( data ) {
$scope . job . stats = data ;
// console.log("job stats=", $scope.job.stats);
} ) ;
$http . get ( "/api/jobs/" + $routeParams . id + "/sites" ) . success ( function ( data ) {
$scope . sites = data . sites ;
// console.log("sites=", $scope.sites);
for ( var i = 0 ; i < $scope . sites . length ; i ++ ) {
2016-04-19 18:51:14 +00:00
loadSiteStats ( $http , $scope . sites [ i ] , $scope . job ) ;
2015-10-27 19:00:44 +00:00
}
} ) ;
} ) ;
} ] ) ;
brozzlerControllers . controller ( "SiteController" , [ "$scope" , "$routeParams" , "$http" , "$window" ,
function ( $scope , $routeParams , $http , $window ) {
var start = 0 ;
$scope . loading = false ;
$scope . pages = [ ] ;
$window . addEventListener ( "scroll" , function ( ) {
// console.log("window.scrollTop=" + window.scrollTop + " window.offsetHeight=" + window.offsetHeight + " window.scrollHeight=" + window.scrollHeight);
if ( $window . innerHeight + $window . scrollY + 50 >= window . document . documentElement . scrollHeight ) {
loadMorePages ( ) ;
}
} ) ;
var loadMorePages = function ( ) {
if ( $scope . loading )
return ;
$scope . loading = true ;
// console.log("load more! start=" + start);
$http . get ( "/api/site/" + $routeParams . id + "/pages?start=" + start + "&end=" + ( start + 90 ) ) . then ( function ( response ) {
$scope . pages = $scope . pages . concat ( response . data . pages ) ;
// console.log("pages = ", $scope.pages);
start += response . data . pages . length ;
$scope . loading = false ;
} , function ( reason ) {
$scope . loading = false ;
} ) ;
} ;
2016-04-19 18:51:14 +00:00
$http . get ( "/api/config" ) . success ( function ( data ) {
$scope . config = data . config ;
} ) ;
2015-10-27 19:00:44 +00:00
$http . get ( "/api/site/" + $routeParams . id ) . success ( function ( data ) {
$scope . site = data ;
loadSiteStats ( $http , $scope . site ) ;
// console.log("site = ", $scope.site);
} ) ;
loadMorePages ( ) ;
} ] ) ;