We have multiple ways to calculate the folder size in Alfresco:
1) Write a javascript to calculate folder size and deploy it in Data Dictionary > Scripts; and run it as a rule after selecting 'Execute script' action.
2) Write a java webscript to calculate folder size, but this approach will require a deployment as the controller with business logic will be written in java
3) Execute some db queries against the alfresco database and fetch alfresco site wise folder size.
Let us see each approach one by one:
1) Javascript approach:
var path="Sites/sitename/documentLibrary";
var space = companyhome.childByNamePath(path);
var children = space.children;
var docCount = 0;
var docSize = 0;
var duplicates=new Array();
var noOfUsers = 0;
var uniqueUsers = new Array();
traverse(children);
logger.log("Folder : "+ space.name);
logger.log("Total Document Count :"+ docCount);
logger.log("Total Document Size :"+ formatBytes(docSize,2));
logger.log("Duplicates users count :"+ duplicates.length);
uniqueUsers = duplicates.filter(function(item, i, ar){ return ar.indexOf(item) === i; });
logger.log("Unique users count :"+ uniqueUsers.length);
logger.log("Unique Users :" + uniqueUsers);
function traverse(nodes)
{
for each(var node in nodes)
{
if (node.isContainer)
{
traverse(node.children);
}
else
{
docCount = docCount + 1;
docSize = docSize + node.size;
duplicates.push(node.properties["cm:modifier"]);
}
}
}
function formatBytes(bytes,decimals) {
if(bytes == 0) return '0 Byte';
var k = 1000; // or 1024 for binary
var dm = decimals + 1 || 3;
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
2) Java approach:
https://www.dedunu.info/2015/03/alfresco-calculate-folder-size-using.html
https://github.com/zhihailiu/alfresco-folder-size/blob/master/repo-amp/src/main/java/com/acme/nodesize/NodeSizeWebScript.java
3) DB approach:
-- Step 1 create Tables---
create table Test_Size (node_id bigint(20), level int);
create table Doc_Size (site_name varchar(50), node_id bigint(20), doc_size bigint(20))
-- Step 2 -- Get Node id of parent site name node ref - uuid)
select id from alf_node where uuid ='2f9c9739-ae2e-4e57-ad8c-94fddb933199';
-- Step 3 - insert first level record of site node id
insert into Test_Size values (6636272,1);
-- Step -4 (Repeat for each level - till you get 0 rows inserted...
-- change the level every repeation..
insert into Test_Size
select child_node_id, 2 from alf_child_assoc where parent_node_id in (select node_id from Test_Size where level = 1) ;
then
insert into Test_Size
select child_node_id, 3 from alf_child_assoc where parent_node_id in (select node_id from Test_Size where level = 2) ;
then
insert into Test_Size
select child_node_id, 4 from alf_child_assoc where parent_node_id in (select node_id from Test_Size where level = 3) ;
and so on till you get 0 rows affected
select count(1) from Test_Size;
-- Step 5 -- get the content size for retrieved all the nodes of the specific site
insert into Doc_Size
select 'app_name', n.id, round(u.content_size/1024/1024,2) from alf_node as n, alf_node_properties as p , alf_content_data as d, alf_content_url AS u
where n.id=p.node_id and n.id in (select node_id from Test_Size)
and p.long_value=d.id and d.content_url_id=u.id;
[
Optional (without rounding):
select 'app_name', n.id, u.content_size/1024/1024 from alf_node as n, alf_node_properties as p , alf_content_data as d, alf_content_url AS u
where n.id=p.node_id and n.id in (select node_id from Test_Size)
and p.long_value=d.id and d.content_url_id=u.id;
]
select sum(doc_size) from Doc_Size;
-- Step 6 -- Delete the Temporary table Test_Size
delete from Test_Size;
-- Repeat Step 1 to 6 again for different site...
1) Write a javascript to calculate folder size and deploy it in Data Dictionary > Scripts; and run it as a rule after selecting 'Execute script' action.
2) Write a java webscript to calculate folder size, but this approach will require a deployment as the controller with business logic will be written in java
3) Execute some db queries against the alfresco database and fetch alfresco site wise folder size.
Let us see each approach one by one:
1) Javascript approach:
var path="Sites/sitename/documentLibrary";
var space = companyhome.childByNamePath(path);
var children = space.children;
var docCount = 0;
var docSize = 0;
var duplicates=new Array();
var noOfUsers = 0;
var uniqueUsers = new Array();
traverse(children);
logger.log("Folder : "+ space.name);
logger.log("Total Document Count :"+ docCount);
logger.log("Total Document Size :"+ formatBytes(docSize,2));
logger.log("Duplicates users count :"+ duplicates.length);
uniqueUsers = duplicates.filter(function(item, i, ar){ return ar.indexOf(item) === i; });
logger.log("Unique users count :"+ uniqueUsers.length);
logger.log("Unique Users :" + uniqueUsers);
function traverse(nodes)
{
for each(var node in nodes)
{
if (node.isContainer)
{
traverse(node.children);
}
else
{
docCount = docCount + 1;
docSize = docSize + node.size;
duplicates.push(node.properties["cm:modifier"]);
}
}
}
function formatBytes(bytes,decimals) {
if(bytes == 0) return '0 Byte';
var k = 1000; // or 1024 for binary
var dm = decimals + 1 || 3;
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
https://www.dedunu.info/2015/03/alfresco-calculate-folder-size-using.html
https://github.com/zhihailiu/alfresco-folder-size/blob/master/repo-amp/src/main/java/com/acme/nodesize/NodeSizeWebScript.java
3) DB approach:
-- Step 1 create Tables---
create table Test_Size (node_id bigint(20), level int);
create table Doc_Size (site_name varchar(50), node_id bigint(20), doc_size bigint(20))
-- Step 2 -- Get Node id of parent site name node ref - uuid)
select id from alf_node where uuid ='2f9c9739-ae2e-4e57-ad8c-94fddb933199';
-- Step 3 - insert first level record of site node id
insert into Test_Size values (6636272,1);
-- Step -4 (Repeat for each level - till you get 0 rows inserted...
-- change the level every repeation..
insert into Test_Size
select child_node_id, 2 from alf_child_assoc where parent_node_id in (select node_id from Test_Size where level = 1) ;
then
insert into Test_Size
select child_node_id, 3 from alf_child_assoc where parent_node_id in (select node_id from Test_Size where level = 2) ;
then
insert into Test_Size
select child_node_id, 4 from alf_child_assoc where parent_node_id in (select node_id from Test_Size where level = 3) ;
and so on till you get 0 rows affected
select count(1) from Test_Size;
-- Step 5 -- get the content size for retrieved all the nodes of the specific site
insert into Doc_Size
select 'app_name', n.id, round(u.content_size/1024/1024,2) from alf_node as n, alf_node_properties as p , alf_content_data as d, alf_content_url AS u
where n.id=p.node_id and n.id in (select node_id from Test_Size)
and p.long_value=d.id and d.content_url_id=u.id;
[
Optional (without rounding):
select 'app_name', n.id, u.content_size/1024/1024 from alf_node as n, alf_node_properties as p , alf_content_data as d, alf_content_url AS u
where n.id=p.node_id and n.id in (select node_id from Test_Size)
and p.long_value=d.id and d.content_url_id=u.id;
]
select sum(doc_size) from Doc_Size;
-- Step 6 -- Delete the Temporary table Test_Size
delete from Test_Size;
-- Repeat Step 1 to 6 again for different site...