---
  output: html_document
---
  
```{r, echo=FALSE, results='asis'}

getInternalVal <- function(
  connectionHandler,
  resultSchema,
  myTableAppend,
  modelDesignId,
  databaseTableAppend,
  devDatabase,
  tempEmulationSchema = NULL
){
  
    sql <- "select per.performance_id, dm.cdm_source_abbreviation as validation_database
      from 
      (select performance_id, validation_database_id from 
      @result_schema.@my_table_appendperformances 
      where model_design_id = @model_design_id 
      and development_database_id = validation_database_id) per
      inner join @result_schema.@my_table_appenddatabase_details d
      on d.database_id = per.validation_database_id
      inner join @result_schema.@database_table_appenddatabase_meta_data dm
      on d.database_meta_data_id = dm.database_id
      where d.database_id = @dev_database
      ;
    "
  res <- connectionHandler$queryDb(
        sql = sql, 
    result_schema = resultSchema,
    my_table_append = myTableAppend,
    database_table_append = databaseTableAppend,
    model_design_id = modelDesignId,
    dev_database = devDatabase
  )
  
  if(is.null(res$performanceId)){
    return(NULL)
  }
  
  # get the performances for the performanceId
  result <- list()
  tableNames <- c(
    'attrition', 
    'prediction_distribution',
    'covariate_summary',
    'threshold_summary',
    'calibration_summary',
    'evaluation_statistics',
    'demographic_summary'
    )
  length(result) <- length(tableNames)+1
  names(result) <- c('database',tableNames)
  result[['database']] <- res$validationDatabase
  
  for(tabName in tableNames){
  
  sql <- "select * from 
    @result_schema.@my_table_append@table_name 
    where performance_id = @performance_id 
    "
  
  result[[tabName]] <- connectionHandler$queryDb(
        sql = sql, 
    result_schema = resultSchema,
    table_name = tabName,
    my_table_append = myTableAppend,
    performance_id = res$performanceId
  )
  }
  
  return(result)
}

result <- getInternalVal(
  connectionHandler = params$connectionHandler,
  resultSchema = params$resultSchema,
  myTableAppend = params$myTableAppend,
  databaseTableAppend = params$databaseTableAppend,
  modelDesignId = params$modelDesignIds[i],
  devDatabase = devDatabase
  )
```

### Internal Validation

The internal validation is the performance of the models when validated using held-out data from the database used to develop the model.

```{r, child = if(!is.null(result)) "plp-results-template.Rmd"}
```


