Blame | Last modification | View Log | RSS feed
<%@LANGUAGE="VBSCRIPT"%><%'=====================================================' release_stats_rest.asp'' REST API(ish) interfaceo to extract Release Stats from Release Manager' This page will return JSON output'' Parameters' rtag_id=nnn - Release Tag' ignore=n - 0 ignore none, else ignore COTS, SDK (default=1)' group=n - 0 no group, 1 group (default 1)' summary=n - 0 All data, 1 just summary (default 0)' json=n - 0 CSV, 1JSON (default CSV)'' The output is pure JSON' There are two sections' info : {} - Internal data about the process' data : {} - Build and Test results'=====================================================%><%Option explicit' Essential to get UTF through all the hoops. ie: VÄSTTRAFIK (VTK)Response.ContentType = "text/html"Response.AddHeader "Content-Type", "text/html;charset=UTF-8"Response.CodePage = 65001Response.CharSet = "UTF-8"%><!--#include file="common/conf.asp"--><!--#include file="common/globals.asp"--><!--#include file="common/qstr.asp"--><!--#include file="common/common_subs.asp"--><SCRIPT LANGUAGE="VBScript" RUNAT=SERVER SRC="class/classaspJSON.vbs"></SCRIPT><%'------------ Variable Definition -------------Dim result : result = -1Dim SqlQryDim rsQryDim parRtagIdDim parIgnoreDim parGroupDim parSummaryDim parJson' Init the output JSON class' Default data will be added at the endDim oJSON : Set oJSON = New aspJSONDim infoSet : Set infoSet = newDataSet("info", oJSON.data)'' Global Data'Dim projNameDim relNameDim sDateTimeDim dataSet' Constants' Names of the Fields'Const fDate = "Date"Const fRtagId = "RtagId"Const fPname ="Project Name"Const fRname = "Release Name"Const fGroup = "Group"Const fTPkgs = "Total Packages"Const fTTested = "Packages with Tests"Const fTTests = "Total Unit Tests"Const fTLoc = "Total Lines of Code"Const fpkg = "Package"Const fTests = "Tests"Const fLoc = "LoC"'' Get parametersparRtagId = RequestDefault("rtag_id", 0)parIgnore = RequestDefault("ignore", 1)parGroup = RequestDefault("group", 1)parSummary = RequestDefault("summary",0)parJson = RequestDefault("json",0)'' Attempt to be helpful'If parRtagId = 0 ThenResponse.Write "<html>" & vbCrLfResponse.Write "<body>" & vbCrLfResponse.Write "<br>release_stats_rest.asp is designed to be used by automation scripts" & vbCrLfResponse.Write "<br>It will accept the following parameters" & vbCrLfResponse.Write "<br>rtag_id=nnn - Specify Release to Process (mandatory)" & vbCrLfResponse.Write "<br>ignore=0|1 - Ignore COTS and TOOL packages. Ignore packages provided by an SDK (default ignore)" & vbCrLfResponse.Write "<br>group=0|1 - Output All or by Base View (default by Base View)" & vbCrLfResponse.Write "<br>summary=0|1 - Display summary only (default ALL)" & vbCrLfResponse.Write "<br>json=0|1 - Display CSV or JSON (default json)" & vbCrLfResponse.Write "" & vbCrLfResponse.Write "</body>" & vbCrLfResponse.Write "</html>" & vbCrLfResponse.EndEnd If'' Perform the bulk of the work within a sub' Done so that errors propergate up' Using the On Error Resume Next, any error within the routine' will cause the routine to exit'On Error Resume NextCall DoWork'' SQL error detection and reportingIf objEH.LastOraFailed TheninfoSet("error") = 1result = -2infoSet("emsgSummary") = objEH.MessageSummaryinfoSet("emsgDetails") = objEH.MessageDetailsinfoSet("SqlQry") = SqlQry'' Detect program errorsElseIf Err.number <> 0 Thenresult = -3infoSet("error") = 2infoSet("errnum") = Err.numberinfoSet("errtxt") = Err.descriptioninfoSet("errsrc") = Err.sourceEnd IfinfoSet("result") = result'Return the objectIf parJson = 0 ThenCall DumpAsCsvElseResponse.Write oJSON.JSONoutput()End IfSet oJSON = NothingCall Destroy_All_Objects' --- End of Mainline'------------------------------------------------------------------------------'' Create a new collection and add it to an existing colelction' sname - Name of the new collection (Key Name)' pset - Parent collection. The collection to add it to' Returns a Collection ( Scripting Dictionary)Function newDataSet(sname, pset)Set newDataSet = oJSON.Collection()Set pset(sname) = newDataSetEnd Function'--------------------------------------------------------------------------------'' Create or re-use structure to handle a given BaseView'Function getGroupHeader( dataSet, sBaseView)If NOT dataSet.Exists(sBaseView) ThenDim vsetSet vset = newDataSet(sBaseView,dataSet )vset(fDate) = sDateTimevset(fRtagId) = parRtagIdvset(fPname) = projNamevset(fRname) = relNamevset(fGroup) = sBaseViewvset(fTPkgs) = 0vset(fTLoc) = 0vset(fTTested) = 0vset(fTTests) = 0End IfSet getGroupHeader = dataSet.Item(sBaseView)End Function'--------------------------------------------------------------------------------'' Create or re-use structure to store detailed data for given basename'Function getGroupData( dVset)If NOT dVset.Exists("DATA") ThenSet getGroupData = newDataSet("DATA",dVset )End IfSet getGroupData = dVset.Item("DATA")End Function'------------------------------------------------------------------------------'' Get Project Name and Release name for the rtag being processed'Sub getProjectReleaseNameSqlQry = "select p.PROJ_NAME, rt.RTAG_NAME from RELEASE_TAGS rt, PROJECTS p WHERE rt.PROJ_ID = p.PROJ_ID and rt.RTAG_ID = :RTAG_ID"On Error Resume NextobjEH.ErrorRedirect = FALSEobjEH.TryORA ( OraSession )OraDatabase.Parameters.Add "RTAG_ID", parRtagId, ORAPARM_INPUT, ORATYPE_NUMBERSet rsQry = OraDatabase.DbCreateDynaset( SqlQry, ORADYN_DEFAULT )OraDatabase.Parameters.Remove "RTAG_ID"objEH.CatchORA ( OraSession )' Process one RowIf objEH.Finally ThenOn Error goto 0projName = rsQry("PROJ_NAME")relName = rsQry("RTAG_NAME")End IfEnd Sub'------------------------------------------------------------------------------'' Perform the body of the query' Done in a Sub so that errors can be better captured and reportedSub DoWorkIf parRtagId <= 0 Thenresult = -2infoSet("error") = 1infoSet("emsgSummary") = "Invalid rtag_id"Exit SubEnd If' Setup common data' Calc timestamp for data' Get Project and Release Name' Use globals to pass dataDim date : date = NowsDateTime = Year(date) & Right(String(2, "0") & Month(date), 2) & Right(String(2, "0") & Day(date), 2)Call getProjectReleaseName' Create a collection to contain the returned data'Set dataSet = newDataSet("data", oJSON.data)On Error Resume NextobjEH.ErrorRedirect = FALSEobjEH.TryORA ( OraSession )OraDatabase.Parameters.Add "RTAG_ID", parRtagId, ORAPARM_INPUT, ORATYPE_NUMBERSqlQry = GetQuery("release_stats.sql")Set rsQry = OraDatabase.DbCreateDynaset( SqlQry, ORADYN_DEFAULT )OraDatabase.Parameters.Remove "RTAG_ID"objEH.CatchORA ( OraSession )' Process each row and return required fields to the userIf objEH.Finally ThenOn Error goto 0Dim includeThisDo While (NOT rsQry.BOF) AND (NOT rsQry.EOF)includeThis = TRUEDim pvId, buildId, buildTime, lastBuild, codeLines, isDeployable, viewName, isSdk, tcount, pVersion, pName, pExtpvId = rsQry("pv_id")tcount = NiceInt(rsQry("test_count"), 0)buildId = rsQry("build_id")buildTime = rsQry("build_time")If isNull(buildTime) Then buildTime = ""lastBuild = rsQry("lastBuild")If isNull(lastBuild) Then lastBuild = ""codeLines = NiceCLng(rsQry("code_lines"),0)If isNull(codeLines) Then codeLines = ""isDeployable = rsQry("is_deployable")If isNull(isDeployable) Then isDeployable = "N"viewName = rsQry("view_name")isSdk = rsQry("isSdk")pVersion = rsQry("pkg_version")pName = rsQry("pkg_name")pExt = rsQry("v_ext")' Ignore some packagesIf parIgnore > 0 ThenIf isSdk = "Y" ThenincludeThis = FALSEEnd IfDim oRE, bMatchSet oRE = New RegExpoRE.IgnoreCase = TrueoRE.Pattern = ".cots$|.tool$"bMatch = oRE.Test(pVersion)If bMatch ThenincludeThis = FALSEEnd IfEnd If' Process active elementsIf includeThis ThenIf parGroup = 0 ThenviewName = "ALL"End If' Things for oJSON object' obj(field) = data (Will create a hash entry)' newObj = oJSON.AddToCollection( obj ) (Will create an array)Dim dGroup : Set dGroup = getGroupHeader (dataSet, viewName)If parSummary = 0 ThenDim dData : Set dData = getGroupData(dGroup)Dim dEl : Set dEl = oJSON.AddToCollection( dData )dEl(fpkg) = pNamedEl(fTests) = tcountdEl(fLoc) = codeLinesEnd IfdGroup(fTPkgs) = dGroup(fTPkgs) + 1dGroup(fTLoc) = dGroup(fTLoc) + codeLinesIf tcount > 0 ThendGroup(fTTested) = dGroup(fTTested) + 1dGroup(fTTests) = dGroup(fTTests) + tcountEnd IfEnd IfrsQry.MoveNextLoopEnd IfrsQry.CloseSet rsQry = Nothingresult = 0End SubSub csvAdd( sName, sData, byRef sHdr, byRef sDtr)sHdr = sHdr & "," & sNamesDtr = sDtr & "," & sDataEnd Sub'--------------------------------------------------------------------------------'' Dump the data in CSV formatSub DumpAsCsvOn Error goto 0dim gKeys : gKeys = dataSet.KeysDim ii,jj,kkDim mHdrDim sFullDatafor ii=0 to dataSet.Count-1' Process one Base ViewDim hdr, dtrhdr = ""dtr = ""Dim dGroup : Set dGroup = dataSet.Item(gKeys(ii))Dim hList: hList = Array(fDate, fRtagId, fPname, fRname, fGroup, fTPkgs, fTTested, fTTests,fTLoc)For jj=0 to Ubound(hList)Call csvAdd(hList(jj), dGroup(hList(jj)), hdr, dtr)NextIf dGroup.Exists("DATA")ThenDim dData : Set dData = dGroup("DATA")dim dKeys : dKeys = dData.KeysFor kk=0 to Ubound(dKeys)Dim dEl : Set dEl = dData(dKeys(kk))Dim eList: eList = Array(fpkg, fTests, fLoc )For jj=0 to Ubound(eList)Call csvAdd(eList(jj), dEl(eList(jj)), hdr, dtr)NextNextEnd Ifhdr = Mid(hdr, 2, Len(hdr)-1)dtr = Mid(dtr, 2, Len(dtr)-1)If Len(hdr) > Len(mHdr) ThenmHdr = hdrEnd IfsFullData = sFullData & dtr & vbCrLfnextResponse.Write mHdr & vbCrLfResponse.Write sFullData & vbCrLfEnd Sub%>