F#代码——Run分析结果统计

由于每次到项目统计的时候,都需要统计一下Run的分析情况,手动来做会非常麻烦,由此想写一个小程序统计一下分析情况,下面是代码:

// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.

#r @"C:\Program Files()\Reference Assemblies\Microsoft\FSharp\3.0\Runtime\v4.0\Type Providers\FSharp.Data.TypeProviders.dll"
#r @"C:\Program Files()\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.Linq.dll"
#r @"C:\Program Files()\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.ServiceModel.dll"
#r @"C:\Program Files()\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.dll"


open System.Timers 
open Microsoft.FSharp.Data.TypeProviders
open System.Data

type T = Microsoft.FSharp.Data.TypeProviders.SqlDataConnection<ConnectionString = "Data Source=mdsql3;Initial Catalog=OrcasTS;Integrated Security=True">


let defaultstartTime = new System.DateTime(2011,12,1,0,0,0)
let defaultendTime = new System.DateTime(2012,3,1,0,0,0)

let sqlConnection = new System.Data.SqlClient.SqlConnection("Data Source=mdsql3;Initial Catalog=OrcasTS;Integrated Security=True")
    
let queryStr = 
        "
        select *
        from dbo.Results
        where dbo.Results.RunID in
                (
                    select dbo.run.RunID
                    from dbo.run
                    where dbo.run.OwnerID = 1170
                          and dbo.run.StartTime >= '2011-12-1 0:0:0'
                          and dbo.run.EndTime < '2012-3-1 0:0:0'
                  
                )
        "

let sqlAdapter = new System.Data.SqlClient.SqlDataAdapter(queryStr, sqlConnection)

let dataSet = new DataSet()
sqlAdapter.Fill(dataSet) |> ignore
let count = dataSet.Tables.Item(0).Rows.Count
let rows = dataSet.Tables.Item(0).Rows
let resultSeq = seq{ for i in 0 .. (count - 1) -> rows.[i]}
let fsrunsSeq = resultSeq |> Seq.map( fun item -> (item.[0].ToString(), item.[1].ToString())) |> Seq.filter( fun i -> snd i = "1610322") |> Seq.map(fun x -> fst x) |> Seq.distinct |> Seq.sort
fsrunsSeq |> Seq.length
type RunsByOwnerName( alias : string) =
    let client = T.GetDataContext()

    [<DefaultValue>]
    val mutable startTime : System.DateTime 

    [<DefaultValue>]
    val mutable endTime : System.DateTime

    //query the runs in database by the specific rules
    member this.QueryRun() =
        let runQuery =
            query{
                for run in client.Run do                
                where(run.StartTime >= this.startTime
                      && run.EndTime < this.endTime
                      && run.Owners.OwnerName = alias 
                      && (run.RunStateID = 8 || run.RunStateID = 6)
                      && run.Product.TestSourcesLocation <> ""
                      && run.Results.Count > 0 )
                select run
            }
        
        //return the Seq which reduce the duplicated items 
        runQuery
            |> Seq.distinct
            //|> Seq.sort
        
type AnalyzedFsRunsByAlias(alias : string, ? startTime : System.DateTime, ? endTime : System.DateTime) =
    
    let client = T.GetDataContext()

    let defaultstartTime = new System.DateTime(2011,12,1,0,0,0)
    let defaultendTime = new System.DateTime(2012,4,1,0,0,0)

    let startTime = defaultArg startTime defaultstartTime
    let endTime = defaultArg endTime defaultendTime
    
    member this.AllFsRunsInAllRuns ()= 
        //"v-lindfe",v-naz,matteot,v-sezha, v-shuzhu
        let runsByOwener = new RunsByOwnerName(alias)

        runsByOwener.startTime <- startTime
        runsByOwener.endTime <- endTime

        //get the runs of the owner which specified by the alias
        let runs = runsByOwener.QueryRun()

        //the number of runs
        let totalRunsNum = 
            runs
                |> Seq.length
        
        //get the Fsharp runs kicked off by the owner which specified by the alias
        let fsRuns = 
            //filter the runs whose testcase query contain one of fsharp testcase. i.e: 1610322, also can specify another typical Fsharp testcase
            runs 
                |> Seq.filter( fun x ->     
                                        x.Results
                                        |> Seq.exists( fun y -> y.TestCaseID = 1610322))
            
        let fsRunsID = 
            fsRuns
                |> Seq.map( fun x -> x.RunID )
                |> Seq.distinct
                |> Seq.sort 
                   
        let fsRunsNum =
            fsRunsID 
                |> Seq.length
        //return Fsharp runs number and total runs, Fsharp runs' ID
        fsRunsNum,totalRunsNum,fsRunsID

    member this.FsAnalyzedRunsNumAndID() =
        //get fsharp runs number and ID in all runs
        let fsRunandTotalRunandfsRunID = this.AllFsRunsInAllRuns()

        //Function to get analyzed or not information about  Fsharp runs , argument runIDSeq is the Fsharp runs ID Seq
        let fsRunsIDAndAnalyzedInfo(runIDSeq : int seq) =
            runIDSeq
                |> Seq.map( fun x ->
                                let resultInEachRun =
                                    query{
                                        for result in client.Results do
                                        where(result.RunID = x)
                                        select result
                                    }
                                let tcIDandAnalyzedOrNot =
                                    resultInEachRun 
                                        |> Seq.map( fun y -> (y.TestCaseID, y.Analyzed))
                                x, tcIDandAnalyzedOrNot     
                                 )
        let (_,_,srcIDSeq) = fsRunandTotalRunandfsRunID
        let infoForEachFsRun = fsRunsIDAndAnalyzedInfo(srcIDSeq)
        let finalRuns =
            infoForEachFsRun
                |> Seq.filter(fun x -> 
                                    let tcSrc  = snd x
                                    let failedTc = 
                                        tcSrc |> Seq.filter(fun y -> 
                                                                    let (tcID,analyzed) = y
                                                                    analyzed = false)
                                    let tcOnwerSrc =
                                        failedTc
                                            |> Seq.exists(fun z -> 
                                                                let owners =
                                                                    query{
                                                                        for i in client.Testcases do
                                                                        where(i.TestCaseID = (fst z))
                                                                        select i.Owners
                                                                        }
                                                                //check if contain our Fsharp failed testcases that weren't analyzed.
                                                                owners |> Seq.exists(fun u -> 
                                                                                        u.OwnerName = "MatteoT"
                                                                                        || u.OwnerName = "JackHu"
                                                                                        || u.OwnerName = "v-anxia")                            
                                                           )
                                    //exculed the runs which contain the fsharp failed testcases that weren't analyzed 
                                    not tcOnwerSrc
                            )
                |> Seq.map (fun k -> fst k)

        let count = finalRuns |> Seq.length
        count, finalRuns   
//"v-lindfe",v-naz,matteot,v-sezha, v-shuzhu,v-amzh v-alhua v-yazhou

let runs = new AnalyzedFsRunsByAlias("v-alhua")
let num = runs.FsAnalyzedRunsNumAndID()

做个笔记,留个纪念也是好的:)

posted @ 2012-11-12 12:33  ZackZhou  阅读(342)  评论(0编辑  收藏  举报