It's all about the answers!

Ask a question

RTC build result log size


SUNIL KUURAM (6431923) | asked Feb 25 '15, 1:19 p.m.
I am trying to get RTC build result size via API. I have the following so far. Is it possible to get the size of each contribution using IBuildResultContribution?

                     IBuildResult buildResult = (IBuildResult) teamRepository.itemManager().fetchPartialItem(ibr, IItemManager.REFRESH,
                                IBuildResultContribution[] ibrc = buildClient.getBuildResultContributions(ibr,  IBuildResultContribution.LOG_EXTENDED_CONTRIBUTION_ID, monitor);

Thanks for your help.

Cheers
Sunil

Accepted answer


permanent link
sam detweiler (12.5k6195201) | answered Feb 25 '15, 3:29 p.m.
edited Feb 25 '15, 3:36 p.m.
a while back I needed to find the size of all the contributions of all the build results in a project..

here is the code that does that

package com.sd.tools;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ibm.team.build.client.ITeamBuildClient;
import com.ibm.team.build.common.model.IBuildEngine;
import com.ibm.team.build.common.model.IBuildResult;
import com.ibm.team.build.common.model.IBuildResultContribution;
import com.ibm.team.build.common.model.query.IBaseBuildEngineQueryModel.IBuildEngineQueryModel;
import com.ibm.team.build.common.model.query.IBaseBuildResultQueryModel.IBuildResultQueryModel;
import com.ibm.team.process.client.IProcessClientService;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.process.common.IProjectAreaHandle;
import com.ibm.team.repository.client.IContentManager;
import com.ibm.team.repository.client.IItemManager;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.client.ITeamRepository.*;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.*;
import com.ibm.team.repository.common.IContent;
import com.ibm.team.repository.common.IFetchResult;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.repository.common.query.IItemQuery;
import com.ibm.team.repository.common.query.IItemQueryPage;
import com.ibm.team.repository.common.query.ast.IPredicate;
import com.ibm.team.repository.common.service.IQueryService;
import com.ibm.team.repository.common.util.ObfuscationHelper;

public class DumpContributions
{
    private static boolean debug = false;
    private static int master_connection_timeout=24*60*60;
    private static ITeamRepository productionServer = null;
    private  static IProcessClientService prodprocessClient = null;   
    public final static String fContributionTypeId = IBuildResultContribution.LOG_EXTENDED_CONTRIBUTION_ID;

    private static class LoginHandler implements ILoginHandler, ILoginInfo
    {

        private String fUserId;
        private String fPassword;

        private LoginHandler(String userId, String password)
        {
            fUserId = userId;
            fPassword = password;
        }


        public String getUserId()
        {
            return fUserId;
        }


        public String getPassword()
        {
            return fPassword;
        }


        public ILoginInfo challenge(ITeamRepository repository)
        {
            return this;
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String productionURI = args[0];
        String produserId = args[1];
        String prodpassword = args[2];
        String prodprojectAreaName = args[3];

        try
        {
            IProjectArea iprja_prod = null;

            TeamPlatform.startup();
   
            // TODO Auto-generated method stub
            productionServer = TeamPlatform.getTeamRepositoryService().getTeamRepository(productionURI);
   
            if (debug) System.out.println("logging in\n");
            try
            {
                // decrypt the user password, if encrypted
                prodpassword = ObfuscationHelper.decryptString(prodpassword);
            }
            catch(Exception ex)
            {
                // nothing to do, variable not overlayed on error
            }
            productionServer.registerLoginHandler(new LoginHandler(produserId, prodpassword));
   
            productionServer.setConnectionTimeout(master_connection_timeout);       

            productionServer.login(null);
            prodprocessClient = (IProcessClientService) productionServer
                    .getClientLibrary(IProcessClientService.class);
            URI produri = URI.create(prodprojectAreaName.replaceAll(" ", "%20"));
            iprja_prod = (IProjectArea) prodprocessClient.findProcessArea(produri, null, null);   
            ITeamBuildClient buildClient = (ITeamBuildClient) productionServer.getClientLibrary(ITeamBuildClient.class);
            for(IBuildResult br:  getBuildResultsforProjectArea(iprja_prod))
            {
                System.out.println("Build result for build = "+br.getLabel());
                IBuildResultContribution[] ibrcl =buildClient.getBuildResultContributions(br, fContributionTypeId, null);
                System.out.println(" there are "+ibrcl.length+" contributions to display");
                for(IBuildResultContribution ibrc : ibrcl)
                {
                    System.out.println("entry");
                    String r=getContent(ibrc.getExtendedContributionData(), productionServer.contentManager());
                    System.out.println("content size= "+r.length());
                    //System.out.println("content ="+getContent(ibrc.getExtendedContributionData(), productionServer.contentManager()));
                }
            }                   

        }
        catch (TeamRepositoryException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }         
        TeamPlatform.shutdown();

    }
    private static String getContent(IContent ic, IContentManager icm)
    {
        // make an output stream for the content manager
        OutputStream os = new ByteArrayOutputStream();
        // get the content of the content object.
        try
        {
            if (ic != null)
            {
                icm.retrieveContent(ic, os, null);
                return os.toString();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }   
    private static ArrayList<IBuildResult> getBuildResultsforProjectArea(IProjectAreaHandle iprja)
    {
        Boolean allProperties = true;
        // allocate space for results
        ArrayList<IBuildResult> allbe = new ArrayList<IBuildResult>();
        ITeamRepository repo= (ITeamRepository)iprja.getOrigin();
        // get the data interface
        ITeamBuildClient buildClient = (ITeamBuildClient) repo.getClientLibrary(ITeamBuildClient.class);

        try
        {
            IBuildResultQueryModel queryModel = IBuildResultQueryModel.ROOT;
            // create a query for build engines
            IItemQuery query = IItemQuery.FACTORY.newInstance(queryModel);
            // objects of build engine type
            IPredicate isBuildResultType = queryModel._isTypeOf(IBuildResult.ITEM_TYPE);
            // the query filter
            //IPredicate filter = queryModel._in(new IItemHandleInputArg[] { query.newItemHandleArg() });
            query.filter(isBuildResultType);
            // no parameters required
            IItemQueryPage queryPage = buildClient.queryItems(query,
            // com.ibm.team.repository.common.service.IQueryService.EMPTY_PARAMETERS,
                    new Object[0], IQueryService.ITEM_QUERY_MAX_PAGE_SIZE, null);

            // if there are buildengines defined

            if (queryPage.getResultSize() != 0)
            {

                while (queryPage != null)
                {

                    IFetchResult fetchResult = null;
                    if (allProperties == true)
                    {
                        fetchResult = repo.itemManager().fetchCompleteItemsPermissionAware(queryPage.getItemHandles(),
                                IItemManager.DEFAULT, null);
                    }
                    else
                    {
                        // print out a page of IDs of the retrieved build
                        // engines
                        final String[] all_properties = new String[]
                        { IBuildEngine.PROPERTY_ACTIVE, IBuildEngine.PROPERTY_BUILD_ENGINE_ACTIVITY,
                                IBuildEngine.PROPERTY_DESCRIPTION, IBuildEngine.PROPERTY_ENGINE_CONTACT_INTERVAL,
                                IBuildEngine.PROPERTY_ID, IBuildEngine.PROPERTY_PROCESS_AREA,
                                IBuildEngine.PROPERTY_SUPPORTED_BUILD_DEFINITIONS,
                                IBuildEngine.PROPERTY_SUPPORTS_CANCELLATION, IBuildEngine.PROPERTY_USE_TEAM_SCHEDULER,
                                IBuildEngine.PROPERTY_PROPERTIES, IBuildEngine.PROPERTY_CONFIGURATION_ELEMENTS };
                       
                        fetchResult = repo.itemManager().fetchPartialItemsPermissionAware(queryPage.getItemHandles(),
                                IItemManager.DEFAULT, Arrays.asList(all_properties), null);
                    }

                    // add these to the list
                    allbe.addAll(fetchResult.getRetrievedItems());

                    if (queryPage.hasNext())
                    {
                        queryPage = (IItemQueryPage) buildClient.fetchPage(queryPage.getToken(),
                                queryPage.getNextStartPosition(), queryPage.getSize(), null);
                    }
                    else
                    {
                        queryPage = null;
                    }

                }
            }
        }
        catch (Exception ex)
        {
            System.out.println("build exception=" + ex.getMessage());
        }
        return allbe;
    }

}

SUNIL KUURAM selected this answer as the correct answer

Comments
SUNIL KUURAM commented Feb 25 '15, 3:58 p.m.

Many thanks for this. I just want to gather the sizes of the build contributions without reading the content. The build result UI shows  the size of each contribution. I think it should be possible to get the contribution size. Appreciate any ideas.


Cheers
Sunil


sam detweiler commented Feb 25 '15, 4:27 p.m.

Mine does not show the content.. that line is commented out

                   //System.out.println("content ="+getContent(ibrc.getExtendedContributionData(), productionServer.contentManager()));


SUNIL KUURAM commented Feb 25 '15, 4:58 p.m. | edited Feb 28 '15, 10:16 a.m.

Well, the code seems to retrieve the stream to get the content size. I want to avoid reading the whole stream to calculate the size. I am trying to basically run though all build results and assess their size. Is there an easy way to get the conent size?


Thanks

   OutputStream os = new ByteArrayOutputStream(); 
        // get the content of the content object. 
        try 
        { 
            if (ic != null) 
            { 
                icm.retrieveContent(ic, os, null); 
                return os.toString(); 
            } 


sam detweiler commented Feb 25 '15, 5:07 p.m.

you can try ibrc.getExtendedContributionData().getRawLength(), or ibrc.getExtendedContributionData().getEstimatedConvertedLength()


SUNIL KUURAM commented Feb 25 '15, 6:14 p.m.

 That worked. Thanks a lot!

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.