1 {-# LANGUAGE OverloadedStrings, TemplateHaskell, RecordWildCards, DataKinds #-}
    2 module Databrary.Model.Metric
    3   ( module Databrary.Model.Metric.Types
    4   , allMetrics
    5   , participantMetricId
    6   , participantMetricInfo
    7   , participantMetricDescription
    8   , participantMetricBirthdate
    9   , participantMetricGender
   10   , participantMetricRace
   11   , participantMetricEthnicity
   12   , participantMetricGestationalAge
   13   , participantMetricPregnancyTerm
   14   , participantMetricBirthWeight
   15   , participantMetricDisability
   16   , participantMetricLanguage
   17   , participantMetricCountry
   18   , participantMetricState
   19   , participantMetricSetting
   20   , getMetric
   21   , getMetric'
   22   , lookupParticipantMetricBySymbolicName
   23   , participantMetrics
   24   , validateParticipantId
   25   , validateParticipantInfo
   26   , validateParticipantDescription
   27   , validateParticipantGender
   28   , validateParticipantCountry
   29   , validateParticipantRace
   30   , validateParticipantEthnicity
   31   , validateParticipantPregnancyTerm
   32   , validateParticipantState
   33   , validateParticipantSetting
   34   , validateParticipantDisability
   35   , validateParticipantGestationalAge
   36   , validateParticipantBirthWeight
   37   , validateParticipantBirthdate
   38   , validateParticipantLanguage
   39   , metricLong
   40   , birthdateMetric
   41   , metricJSON
   42   ) where
   43 
   44 import Control.Applicative (empty, pure)
   45 import qualified Data.ByteString as BS
   46 import qualified Data.ByteString.Char8 as BSC
   47 import qualified Data.ByteString
   48 import qualified Data.IntMap.Strict as IntMap
   49 import Data.List (find)
   50 import Data.Maybe (fromJust)
   51 import Data.Monoid ((<>))
   52 import qualified Data.Text
   53 import Data.Text (Text)
   54 import qualified Data.Time as Time
   55 import qualified Text.Read as TR
   56 
   57 import Databrary.Ops
   58 import qualified Databrary.JSON as JSON
   59 import Databrary.Model.Id
   60 import Databrary.Model.Category
   61 import Databrary.Model.Metric.Types
   62 import qualified Databrary.Model.Release.Types
   63 
   64 {- formerly used when loading from db
   65 makeMetric :: Id Metric -> Id Category -> T.Text -> Maybe Release -> MeasureType -> Maybe [Maybe MeasureDatum] -> Maybe MeasureDatum -> Maybe T.Text -> Maybe Bool -> Metric
   66 makeMetric i c n r t o = Metric i (getCategory' c) n r t (maybe [] (map (fromMaybe (error "NULL measure.option"))) o)
   67 
   68 metricRow :: Selector -- Metric
   69 metricRow = selectColumns 'makeMetric "metric" ["id", "category", "name", "release", "type", "options", "assumed", "description", "required"]
   70 -}
   71 
   72 -- TODO: db coherence
   73 allMetrics :: [Metric]
   74 allMetrics = 
   75     [Metric
   76        (Id 1)
   77        (Category
   78           (Id 1)
   79           (Data.Text.pack "participant")
   80           (Just
   81              (Data.Text.pack
   82                 "An individual human subject whose data are used or represented")))
   83        (Data.Text.pack "ID")
   84        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
   85        MeasureTypeText
   86        []
   87        Nothing
   88        (Just
   89           (Data.Text.pack
   90              "A unique, anonymized, primary identifier, such as participant ID"))
   91        (Just True),
   92      Metric
   93        (Id 2)
   94        (Category
   95           (Id 1)
   96           (Data.Text.pack "participant")
   97           (Just
   98              (Data.Text.pack
   99                 "An individual human subject whose data are used or represented")))
  100        (Data.Text.pack "info")
  101        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  102        MeasureTypeText
  103        []
  104        Nothing
  105        (Just (Data.Text.pack "Other information or alternate identifier"))
  106        Nothing,
  107      Metric
  108        (Id 3)
  109        (Category
  110           (Id 1)
  111           (Data.Text.pack "participant")
  112           (Just
  113              (Data.Text.pack
  114                 "An individual human subject whose data are used or represented")))
  115        (Data.Text.pack "description")
  116        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  117        MeasureTypeText
  118        []
  119        Nothing
  120        (Just (Data.Text.pack "A longer explanation or description"))
  121        Nothing,
  122      Metric
  123        (Id 4)
  124        (Category
  125           (Id 1)
  126           (Data.Text.pack "participant")
  127           (Just
  128              (Data.Text.pack
  129                 "An individual human subject whose data are used or represented")))
  130        (Data.Text.pack "birthdate")
  131        Nothing
  132        MeasureTypeDate
  133        []
  134        Nothing
  135        (Just
  136           (Data.Text.pack
  137              "Date of birth (used with session date to calculate age; you can also use the group category to designate age groups)"))
  138        (Just False),
  139      Metric
  140        (Id 5)
  141        (Category
  142           (Id 1)
  143           (Data.Text.pack "participant")
  144           (Just
  145              (Data.Text.pack
  146                 "An individual human subject whose data are used or represented")))
  147        (Data.Text.pack "gender")
  148        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  149        MeasureTypeText
  150        [Data.ByteString.pack [70, 101, 109, 97, 108, 101],
  151         Data.ByteString.pack [77, 97, 108, 101]]
  152        Nothing
  153        (Just
  154           (Data.Text.pack
  155              "\"Male\", \"Female\", or any other relevant gender"))
  156        (Just False),
  157      Metric
  158        (Id 6)
  159        (Category
  160           (Id 1)
  161           (Data.Text.pack "participant")
  162           (Just
  163              (Data.Text.pack
  164                 "An individual human subject whose data are used or represented")))
  165        (Data.Text.pack "race")
  166        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  167        MeasureTypeText
  168        [Data.ByteString.pack
  169           [65, 109, 101, 114, 105, 99, 97, 110, 32, 73, 110, 100, 105, 97,
  170            110, 32, 111, 114, 32, 65, 108, 97, 115, 107, 97, 32, 78, 97, 116,
  171            105, 118, 101],
  172         Data.ByteString.pack [65, 115, 105, 97, 110],
  173         Data.ByteString.pack
  174           [78, 97, 116, 105, 118, 101, 32, 72, 97, 119, 97, 105, 105, 97,
  175            110, 32, 111, 114, 32, 79, 116, 104, 101, 114, 32, 80, 97, 99, 105,
  176            102, 105, 99, 32, 73, 115, 108, 97, 110, 100, 101, 114],
  177         Data.ByteString.pack
  178           [66, 108, 97, 99, 107, 32, 111, 114, 32, 65, 102, 114, 105, 99, 97,
  179            110, 32, 65, 109, 101, 114, 105, 99, 97, 110],
  180         Data.ByteString.pack [87, 104, 105, 116, 101],
  181         Data.ByteString.pack
  182           [77, 111, 114, 101, 32, 116, 104, 97, 110, 32, 111, 110, 101],
  183         Data.ByteString.pack
  184           [85, 110, 107, 110, 111, 119, 110, 32, 111, 114, 32, 110, 111, 116,
  185            32, 114, 101, 112, 111, 114, 116, 101, 100]]
  186        Nothing
  187        (Just
  188           (Data.Text.pack
  189              "As classified by NIH, or user-defined classification"))
  190        (Just False),
  191      Metric
  192        (Id 7)
  193        (Category
  194           (Id 1)
  195           (Data.Text.pack "participant")
  196           (Just
  197              (Data.Text.pack
  198                 "An individual human subject whose data are used or represented")))
  199        (Data.Text.pack "ethnicity")
  200        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  201        MeasureTypeText
  202        [Data.ByteString.pack
  203           [78, 111, 116, 32, 72, 105, 115, 112, 97, 110, 105, 99, 32, 111,
  204            114, 32, 76, 97, 116, 105, 110, 111],
  205         Data.ByteString.pack
  206           [72, 105, 115, 112, 97, 110, 105, 99, 32, 111, 114, 32, 76, 97,
  207            116, 105, 110, 111],
  208         Data.ByteString.pack
  209           [85, 110, 107, 110, 111, 119, 110, 32, 111, 114, 32, 110, 111, 116,
  210            32, 114, 101, 112, 111, 114, 116, 101, 100]]
  211        Nothing
  212        (Just
  213           (Data.Text.pack
  214              "As classified by NIH (Hispanic/Non-Hispanic), or user-defined classification"))
  215        (Just False),
  216      Metric
  217        (Id 8)
  218        (Category
  219           (Id 1)
  220           (Data.Text.pack "participant")
  221           (Just
  222              (Data.Text.pack
  223                 "An individual human subject whose data are used or represented")))
  224        (Data.Text.pack "gestational age")
  225        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  226        MeasureTypeNumeric
  227        []
  228        Nothing
  229        (Just
  230           (Data.Text.pack
  231              "Pregnancy age in weeks between last menstrual period and birth (or pre-natal observation)"))
  232        Nothing,
  233      Metric
  234        (Id 9)
  235        (Category
  236           (Id 1)
  237           (Data.Text.pack "participant")
  238           (Just
  239              (Data.Text.pack
  240                 "An individual human subject whose data are used or represented")))
  241        (Data.Text.pack "pregnancy term")
  242        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  243        MeasureTypeText
  244        [Data.ByteString.pack [70, 117, 108, 108, 32, 116, 101, 114, 109],
  245         Data.ByteString.pack [80, 114, 101, 116, 101, 114, 109]]
  246        (Just
  247           (Data.ByteString.pack [70, 117, 108, 108, 32, 116, 101, 114, 109]))
  248        (Just
  249           (Data.Text.pack
  250              "\"Full term\", \"Preterm\", or other gestational term"))
  251        Nothing,
  252      Metric
  253        (Id 10)
  254        (Category
  255           (Id 1)
  256           (Data.Text.pack "participant")
  257           (Just
  258              (Data.Text.pack
  259                 "An individual human subject whose data are used or represented")))
  260        (Data.Text.pack "birth weight")
  261        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  262        MeasureTypeNumeric
  263        []
  264        Nothing
  265        (Just (Data.Text.pack "Weight at birth (in grams, e.g., 3250)"))
  266        Nothing,
  267      Metric
  268        (Id 11)
  269        (Category
  270           (Id 1)
  271           (Data.Text.pack "participant")
  272           (Just
  273              (Data.Text.pack
  274                 "An individual human subject whose data are used or represented")))
  275        (Data.Text.pack "disability")
  276        Nothing
  277        MeasureTypeText
  278        []
  279        (Just (Data.ByteString.pack [116, 121, 112, 105, 99, 97, 108]))
  280        (Just
  281           (Data.Text.pack
  282              "Any developmental, physical, or mental disability or disabilities"))
  283        (Just False),
  284      Metric
  285        (Id 12)
  286        (Category
  287           (Id 1)
  288           (Data.Text.pack "participant")
  289           (Just
  290              (Data.Text.pack
  291                 "An individual human subject whose data are used or represented")))
  292        (Data.Text.pack "language")
  293        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  294        MeasureTypeText
  295        []
  296        (Just (Data.ByteString.pack [69, 110, 103, 108, 105, 115, 104]))
  297        (Just
  298           (Data.Text.pack
  299              "Primary language(s) spoken by and to participant"))
  300        (Just False),
  301      Metric
  302        (Id 13)
  303        (Category
  304           (Id 1)
  305           (Data.Text.pack "participant")
  306           (Just
  307              (Data.Text.pack
  308                 "An individual human subject whose data are used or represented")))
  309        (Data.Text.pack "country")
  310        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  311        MeasureTypeText
  312        []
  313        (Just (Data.ByteString.pack [85, 83]))
  314        (Just (Data.Text.pack "Country where participant was born"))
  315        Nothing,
  316      Metric
  317        (Id 14)
  318        (Category
  319           (Id 1)
  320           (Data.Text.pack "participant")
  321           (Just
  322              (Data.Text.pack
  323                 "An individual human subject whose data are used or represented")))
  324        (Data.Text.pack "state")
  325        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  326        MeasureTypeText
  327        [Data.ByteString.pack [65, 76], Data.ByteString.pack [65, 75],
  328         Data.ByteString.pack [65, 90], Data.ByteString.pack [65, 82],
  329         Data.ByteString.pack [67, 65], Data.ByteString.pack [67, 79],
  330         Data.ByteString.pack [67, 84], Data.ByteString.pack [68, 69],
  331         Data.ByteString.pack [68, 67], Data.ByteString.pack [70, 76],
  332         Data.ByteString.pack [71, 65], Data.ByteString.pack [72, 73],
  333         Data.ByteString.pack [73, 68], Data.ByteString.pack [73, 76],
  334         Data.ByteString.pack [73, 78], Data.ByteString.pack [73, 65],
  335         Data.ByteString.pack [75, 83], Data.ByteString.pack [75, 89],
  336         Data.ByteString.pack [76, 65], Data.ByteString.pack [77, 69],
  337         Data.ByteString.pack [77, 84], Data.ByteString.pack [78, 69],
  338         Data.ByteString.pack [78, 86], Data.ByteString.pack [78, 72],
  339         Data.ByteString.pack [78, 74], Data.ByteString.pack [78, 77],
  340         Data.ByteString.pack [78, 89], Data.ByteString.pack [78, 67],
  341         Data.ByteString.pack [78, 68], Data.ByteString.pack [79, 72],
  342         Data.ByteString.pack [79, 75], Data.ByteString.pack [79, 82],
  343         Data.ByteString.pack [77, 68], Data.ByteString.pack [77, 65],
  344         Data.ByteString.pack [77, 73], Data.ByteString.pack [77, 78],
  345         Data.ByteString.pack [77, 83], Data.ByteString.pack [77, 79],
  346         Data.ByteString.pack [80, 65], Data.ByteString.pack [82, 73],
  347         Data.ByteString.pack [83, 67], Data.ByteString.pack [83, 68],
  348         Data.ByteString.pack [84, 78], Data.ByteString.pack [84, 88],
  349         Data.ByteString.pack [85, 84], Data.ByteString.pack [86, 84],
  350         Data.ByteString.pack [86, 65], Data.ByteString.pack [87, 65],
  351         Data.ByteString.pack [87, 86], Data.ByteString.pack [87, 73],
  352         Data.ByteString.pack [87, 89]]
  353        Nothing
  354        (Just
  355           (Data.Text.pack "State/territory where participant was born"))
  356        Nothing,
  357      Metric
  358        (Id 15)
  359        (Category
  360           (Id 1)
  361           (Data.Text.pack "participant")
  362           (Just
  363              (Data.Text.pack
  364                 "An individual human subject whose data are used or represented")))
  365        (Data.Text.pack "setting")
  366        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  367        MeasureTypeText
  368        [Data.ByteString.pack [76, 97, 98],
  369         Data.ByteString.pack [72, 111, 109, 101],
  370         Data.ByteString.pack [67, 108, 97, 115, 115, 114, 111, 111, 109],
  371         Data.ByteString.pack [79, 117, 116, 100, 111, 111, 114],
  372         Data.ByteString.pack [67, 108, 105, 110, 105, 99]]
  373        Nothing
  374        (Just
  375           (Data.Text.pack
  376              "The physical context of the participant (please do not use for new data: see the context category instead)"))
  377        Nothing,
  378      Metric
  379        (Id 16)
  380        (Category
  381           (Id 2)
  382           (Data.Text.pack "pilot")
  383           (Just
  384              (Data.Text.pack
  385                 "Indicates that the methods used were not finalized or were non-standard")))
  386        (Data.Text.pack "pilot")
  387        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  388        MeasureTypeVoid
  389        []
  390        Nothing
  391        Nothing
  392        (Just False),
  393      Metric
  394        (Id 17)
  395        (Category
  396           (Id 2)
  397           (Data.Text.pack "pilot")
  398           (Just
  399              (Data.Text.pack
  400                 "Indicates that the methods used were not finalized or were non-standard")))
  401        (Data.Text.pack "name")
  402        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  403        MeasureTypeText
  404        []
  405        Nothing
  406        (Just
  407           (Data.Text.pack
  408              "A label or identifier referring to the pilot method"))
  409        Nothing,
  410      Metric
  411        (Id 18)
  412        (Category
  413           (Id 2)
  414           (Data.Text.pack "pilot")
  415           (Just
  416              (Data.Text.pack
  417                 "Indicates that the methods used were not finalized or were non-standard")))
  418        (Data.Text.pack "description")
  419        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  420        MeasureTypeText
  421        []
  422        Nothing
  423        (Just
  424           (Data.Text.pack
  425              "A longer explanation or description of the pilot method"))
  426        Nothing,
  427      Metric
  428        (Id 19)
  429        (Category
  430           (Id 3)
  431           (Data.Text.pack "exclusion")
  432           (Just (Data.Text.pack "Indicates that data were not usable")))
  433        (Data.Text.pack "excluded")
  434        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  435        MeasureTypeVoid
  436        []
  437        Nothing
  438        Nothing
  439        Nothing,
  440      Metric
  441        (Id 20)
  442        (Category
  443           (Id 3)
  444           (Data.Text.pack "exclusion")
  445           (Just (Data.Text.pack "Indicates that data were not usable")))
  446        (Data.Text.pack "name")
  447        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  448        MeasureTypeText
  449        []
  450        Nothing
  451        (Just
  452           (Data.Text.pack
  453              "A label or identifier referring to the exclusion criterion"))
  454        Nothing,
  455      Metric
  456        (Id 21)
  457        (Category
  458           (Id 3)
  459           (Data.Text.pack "exclusion")
  460           (Just (Data.Text.pack "Indicates that data were not usable")))
  461        (Data.Text.pack "reason")
  462        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  463        MeasureTypeText
  464        [Data.ByteString.pack
  465           [68, 105, 100, 32, 110, 111, 116, 32, 109, 101, 101, 116, 32, 105,
  466            110, 99, 108, 117, 115, 105, 111, 110, 32, 99, 114, 105, 116, 101,
  467            114, 105, 97],
  468         Data.ByteString.pack
  469           [80, 114, 111, 99, 101, 100, 117, 114, 97, 108, 47, 101, 120, 112,
  470            101, 114, 105, 109, 101, 110, 116, 101, 114, 32, 101, 114, 114,
  471            111, 114],
  472         Data.ByteString.pack
  473           [87, 105, 116, 104, 100, 114, 101, 119, 47, 102, 117, 115, 115,
  474            121, 47, 116, 105, 114, 101, 100],
  475         Data.ByteString.pack [79, 117, 116, 108, 105, 101, 114]]
  476        Nothing
  477        (Just (Data.Text.pack "The reason for excluding these data"))
  478        (Just False),
  479      Metric
  480        (Id 22)
  481        (Category
  482           (Id 3)
  483           (Data.Text.pack "exclusion")
  484           (Just (Data.Text.pack "Indicates that data were not usable")))
  485        (Data.Text.pack "description")
  486        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  487        MeasureTypeText
  488        []
  489        Nothing
  490        (Just
  491           (Data.Text.pack
  492              "A longer explanation or description of the reason for excluding data"))
  493        Nothing,
  494      Metric
  495        (Id 23)
  496        (Category
  497           (Id 4)
  498           (Data.Text.pack "condition")
  499           (Just
  500              (Data.Text.pack
  501                 "An experimenter-determined manipulation (within or between sessions)")))
  502        (Data.Text.pack "name")
  503        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  504        MeasureTypeText
  505        []
  506        Nothing
  507        (Just (Data.Text.pack "A label or identifier for the condition"))
  508        (Just True),
  509      Metric
  510        (Id 24)
  511        (Category
  512           (Id 4)
  513           (Data.Text.pack "condition")
  514           (Just
  515              (Data.Text.pack
  516                 "An experimenter-determined manipulation (within or between sessions)")))
  517        (Data.Text.pack "description")
  518        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  519        MeasureTypeText
  520        []
  521        Nothing
  522        (Just
  523           (Data.Text.pack
  524              "A longer explanation or description of the condition"))
  525        Nothing,
  526      Metric
  527        (Id 25)
  528        (Category
  529           (Id 4)
  530           (Data.Text.pack "condition")
  531           (Just
  532              (Data.Text.pack
  533                 "An experimenter-determined manipulation (within or between sessions)")))
  534        (Data.Text.pack "info")
  535        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  536        MeasureTypeText
  537        []
  538        Nothing
  539        (Just (Data.Text.pack "Other information or alternate identifier"))
  540        Nothing,
  541      Metric
  542        (Id 26)
  543        (Category
  544           (Id 5)
  545           (Data.Text.pack "group")
  546           (Just
  547              (Data.Text.pack
  548                 "A grouping determined by an aspect of the data (participant ability, age, grade level, experience, longitudinal visit, measurements used/available)")))
  549        (Data.Text.pack "name")
  550        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  551        MeasureTypeText
  552        []
  553        Nothing
  554        (Just (Data.Text.pack "A label or identifier for the grouping"))
  555        (Just True),
  556      Metric
  557        (Id 27)
  558        (Category
  559           (Id 5)
  560           (Data.Text.pack "group")
  561           (Just
  562              (Data.Text.pack
  563                 "A grouping determined by an aspect of the data (participant ability, age, grade level, experience, longitudinal visit, measurements used/available)")))
  564        (Data.Text.pack "description")
  565        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  566        MeasureTypeText
  567        []
  568        Nothing
  569        (Just
  570           (Data.Text.pack
  571              "A longer explanation or description of the grouping"))
  572        Nothing,
  573      Metric
  574        (Id 28)
  575        (Category
  576           (Id 5)
  577           (Data.Text.pack "group")
  578           (Just
  579              (Data.Text.pack
  580                 "A grouping determined by an aspect of the data (participant ability, age, grade level, experience, longitudinal visit, measurements used/available)")))
  581        (Data.Text.pack "info")
  582        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  583        MeasureTypeText
  584        []
  585        Nothing
  586        (Just (Data.Text.pack "Other information or alternate identifier"))
  587        Nothing,
  588      Metric
  589        (Id 29)
  590        (Category
  591           (Id 6)
  592           (Data.Text.pack "task")
  593           (Just
  594              (Data.Text.pack
  595                 "A particular task, activity, or phase of the session or study")))
  596        (Data.Text.pack "name")
  597        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  598        MeasureTypeText
  599        []
  600        Nothing
  601        (Just (Data.Text.pack "A label or identifier for the task"))
  602        (Just True),
  603      Metric
  604        (Id 30)
  605        (Category
  606           (Id 6)
  607           (Data.Text.pack "task")
  608           (Just
  609              (Data.Text.pack
  610                 "A particular task, activity, or phase of the session or study")))
  611        (Data.Text.pack "description")
  612        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  613        MeasureTypeText
  614        []
  615        Nothing
  616        (Just
  617           (Data.Text.pack "A longer explanation or description of the task"))
  618        (Just False),
  619      Metric
  620        (Id 31)
  621        (Category
  622           (Id 6)
  623           (Data.Text.pack "task")
  624           (Just
  625              (Data.Text.pack
  626                 "A particular task, activity, or phase of the session or study")))
  627        (Data.Text.pack "info")
  628        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  629        MeasureTypeText
  630        []
  631        Nothing
  632        (Just (Data.Text.pack "Other information or alternate identifier"))
  633        Nothing,
  634      Metric
  635        (Id 32)
  636        (Category
  637           (Id 7)
  638           (Data.Text.pack "context")
  639           (Just
  640              (Data.Text.pack
  641                 "A particular setting or other aspect of where/when/how data were collected")))
  642        (Data.Text.pack "name")
  643        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  644        MeasureTypeText
  645        []
  646        Nothing
  647        (Just (Data.Text.pack "A label or identifier for the context"))
  648        Nothing,
  649      Metric
  650        (Id 33)
  651        (Category
  652           (Id 7)
  653           (Data.Text.pack "context")
  654           (Just
  655              (Data.Text.pack
  656                 "A particular setting or other aspect of where/when/how data were collected")))
  657        (Data.Text.pack "setting")
  658        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  659        MeasureTypeText
  660        [Data.ByteString.pack [76, 97, 98],
  661         Data.ByteString.pack [72, 111, 109, 101],
  662         Data.ByteString.pack [67, 108, 97, 115, 115, 114, 111, 111, 109],
  663         Data.ByteString.pack [79, 117, 116, 100, 111, 111, 114],
  664         Data.ByteString.pack [67, 108, 105, 110, 105, 99]]
  665        Nothing
  666        (Just (Data.Text.pack "The physical context"))
  667        (Just True),
  668      Metric
  669        (Id 34)
  670        (Category
  671           (Id 7)
  672           (Data.Text.pack "context")
  673           (Just
  674              (Data.Text.pack
  675                 "A particular setting or other aspect of where/when/how data were collected")))
  676        (Data.Text.pack "language")
  677        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  678        MeasureTypeText
  679        []
  680        (Just (Data.ByteString.pack [69, 110, 103, 108, 105, 115, 104]))
  681        (Just (Data.Text.pack "Language used in this context"))
  682        (Just False),
  683      Metric
  684        (Id 35)
  685        (Category
  686           (Id 7)
  687           (Data.Text.pack "context")
  688           (Just
  689              (Data.Text.pack
  690                 "A particular setting or other aspect of where/when/how data were collected")))
  691        (Data.Text.pack "country")
  692        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  693        MeasureTypeText
  694        []
  695        (Just (Data.ByteString.pack [85, 83]))
  696        (Just (Data.Text.pack "Country of data collection"))
  697        (Just False),
  698      Metric
  699        (Id 36)
  700        (Category
  701           (Id 7)
  702           (Data.Text.pack "context")
  703           (Just
  704              (Data.Text.pack
  705                 "A particular setting or other aspect of where/when/how data were collected")))
  706        (Data.Text.pack "state")
  707        (Just Databrary.Model.Release.Types.ReleasePUBLIC)
  708        MeasureTypeText
  709        [Data.ByteString.pack [65, 76], Data.ByteString.pack [65, 75],
  710         Data.ByteString.pack [65, 90], Data.ByteString.pack [65, 82],
  711         Data.ByteString.pack [67, 65], Data.ByteString.pack [67, 79],
  712         Data.ByteString.pack [67, 84], Data.ByteString.pack [68, 69],
  713         Data.ByteString.pack [68, 67], Data.ByteString.pack [70, 76],
  714         Data.ByteString.pack [71, 65], Data.ByteString.pack [72, 73],
  715         Data.ByteString.pack [73, 68], Data.ByteString.pack [73, 76],
  716         Data.ByteString.pack [73, 78], Data.ByteString.pack [73, 65],
  717         Data.ByteString.pack [75, 83], Data.ByteString.pack [75, 89],
  718         Data.ByteString.pack [76, 65], Data.ByteString.pack [77, 69],
  719         Data.ByteString.pack [77, 84], Data.ByteString.pack [78, 69],
  720         Data.ByteString.pack [78, 86], Data.ByteString.pack [78, 72],
  721         Data.ByteString.pack [78, 74], Data.ByteString.pack [78, 77],
  722         Data.ByteString.pack [78, 89], Data.ByteString.pack [78, 67],
  723         Data.ByteString.pack [78, 68], Data.ByteString.pack [79, 72],
  724         Data.ByteString.pack [79, 75], Data.ByteString.pack [79, 82],
  725         Data.ByteString.pack [77, 68], Data.ByteString.pack [77, 65],
  726         Data.ByteString.pack [77, 73], Data.ByteString.pack [77, 78],
  727         Data.ByteString.pack [77, 83], Data.ByteString.pack [77, 79],
  728         Data.ByteString.pack [80, 65], Data.ByteString.pack [82, 73],
  729         Data.ByteString.pack [83, 67], Data.ByteString.pack [83, 68],
  730         Data.ByteString.pack [84, 78], Data.ByteString.pack [84, 88],
  731         Data.ByteString.pack [85, 84], Data.ByteString.pack [86, 84],
  732         Data.ByteString.pack [86, 65], Data.ByteString.pack [87, 65],
  733         Data.ByteString.pack [87, 86], Data.ByteString.pack [87, 73],
  734         Data.ByteString.pack [87, 89]]
  735        Nothing
  736        (Just (Data.Text.pack "State/territory of data collection"))
  737        (Just False)]
  738 
  739 participantMetricId :: Metric
  740 participantMetricId = getMetric' (Id 1)
  741   
  742 participantMetricInfo :: Metric
  743 participantMetricInfo = getMetric' (Id 2)
  744 
  745 participantMetricDescription :: Metric
  746 participantMetricDescription = getMetric' (Id 3)
  747 
  748 participantMetricBirthdate :: Metric
  749 participantMetricBirthdate = getMetric' (Id 4)
  750 
  751 participantMetricGender :: Metric
  752 participantMetricGender = getMetric' (Id 5)
  753 
  754 participantMetricRace :: Metric
  755 participantMetricRace = getMetric' (Id 6)
  756 
  757 participantMetricEthnicity :: Metric
  758 participantMetricEthnicity = getMetric' (Id 7)
  759 
  760 participantMetricGestationalAge :: Metric
  761 participantMetricGestationalAge = getMetric' (Id 8)
  762 
  763 participantMetricPregnancyTerm :: Metric
  764 participantMetricPregnancyTerm = getMetric' (Id 9)
  765 
  766 participantMetricBirthWeight :: Metric
  767 participantMetricBirthWeight = getMetric' (Id 10)
  768 
  769 participantMetricDisability :: Metric
  770 participantMetricDisability = getMetric' (Id 11)
  771 
  772 participantMetricLanguage :: Metric
  773 participantMetricLanguage = getMetric' (Id 12)
  774 
  775 participantMetricCountry :: Metric
  776 participantMetricCountry = getMetric' (Id 13)
  777 
  778 participantMetricState :: Metric
  779 participantMetricState = getMetric' (Id 14)
  780 
  781 participantMetricSetting :: Metric
  782 participantMetricSetting = getMetric' (Id 15)
  783 
  784 metricsById :: IntMap.IntMap Metric
  785 metricsById = IntMap.fromAscList $ map (\a -> (fromIntegral $ unId $ metricId a, a)) allMetrics
  786 
  787 getMetric :: Id Metric -> Maybe Metric
  788 getMetric (Id i) = IntMap.lookup (fromIntegral i) metricsById
  789 
  790 getMetric' :: Id Metric -> Metric
  791 getMetric' (Id i) = metricsById IntMap.! fromIntegral i
  792 
  793 lookupParticipantMetricBySymbolicName :: Text -> Maybe Metric
  794 lookupParticipantMetricBySymbolicName symbolicName =
  795     find (\m -> (Data.Text.filter (/= ' ') . Data.Text.toLower . metricName) m == symbolicName) participantMetrics
  796 
  797 participantMetrics :: [Metric]
  798 participantMetrics = filter ((== participantCategory) . metricCategory) allMetrics
  799 
  800 validateParticipantId :: BS.ByteString -> Maybe (Maybe BS.ByteString) -- TODO: not optional
  801 validateParticipantId val = do
  802     _ <- validateNotEmpty val
  803     pure (Just val)
  804 
  805 validateParticipantInfo :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  806 validateParticipantInfo val = pure (valToMaybe val)
  807 
  808 validateParticipantDescription :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  809 validateParticipantDescription val = pure (valToMaybe val)
  810 
  811 validateParticipantDisability :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  812 validateParticipantDisability val = pure (valToMaybe val)
  813 
  814 validateParticipantGender :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  815 validateParticipantGender val = do
  816     _ <- validateInOptions val participantMetricGender
  817     pure (valToMaybe val)
  818 
  819 validateParticipantCountry :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  820 validateParticipantCountry val = pure (valToMaybe val)
  821 
  822 validateParticipantRace :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  823 validateParticipantRace val = do
  824     _ <- validateInOptions val participantMetricRace
  825     pure (valToMaybe val)
  826 
  827 validateParticipantEthnicity :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  828 validateParticipantEthnicity val = do
  829     _ <- validateInOptions val participantMetricEthnicity
  830     pure (valToMaybe val)
  831 
  832 validateParticipantPregnancyTerm :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  833 validateParticipantPregnancyTerm val = do
  834     _ <- validateInOptions val participantMetricPregnancyTerm
  835     pure (valToMaybe val)
  836 
  837 validateParticipantState :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  838 validateParticipantState val = do
  839     _ <- validateInOptions val participantMetricState
  840     pure (valToMaybe val)
  841 
  842 validateParticipantSetting :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  843 validateParticipantSetting val = do
  844     _ <- validateInOptions val participantMetricSetting
  845     pure (valToMaybe val)
  846 
  847 validateParticipantGestationalAge :: BS.ByteString -> Maybe (Maybe Double)
  848 validateParticipantGestationalAge val =
  849     if val == ""
  850     then pure Nothing
  851     else do
  852         age <- (TR.readMaybe (BSC.unpack val) :: Maybe Double)
  853         pure (Just age)
  854 
  855 validateParticipantBirthWeight :: BS.ByteString -> Maybe (Maybe Double)
  856 validateParticipantBirthWeight val =
  857     if val == ""
  858     then pure Nothing
  859     else do
  860         weight <- (TR.readMaybe (BSC.unpack val) :: Maybe Double)
  861         pure (Just weight)
  862 
  863 validateParticipantBirthdate :: BS.ByteString -> Maybe (Maybe Time.Day)
  864 validateParticipantBirthdate val = do
  865     if val == ""
  866     then pure Nothing
  867     else do
  868         time <- Time.parseTimeM True Time.defaultTimeLocale "%F" (BSC.unpack val) -- :: Maybe Time.Day
  869         pure (Just time)
  870 
  871 validateParticipantLanguage :: BS.ByteString -> Maybe (Maybe BS.ByteString)
  872 validateParticipantLanguage val = do
  873     pure (valToMaybe val)
  874 
  875 validateInOptions :: BS.ByteString -> Metric -> Maybe BS.ByteString
  876 validateInOptions val metric =
  877     if val == ""
  878     then Just ""
  879     else find (== val) (metricOptions metric)
  880 
  881 validateNotEmpty :: BS.ByteString -> Maybe BS.ByteString
  882 validateNotEmpty val =
  883     if BS.length val > 0 then Just val else Nothing
  884 
  885 valToMaybe :: BS.ByteString -> Maybe BS.ByteString
  886 valToMaybe "" = Nothing
  887 valToMaybe v = Just v
  888 
  889 -- this is a hack, should be in database
  890 metricLong :: Metric -> Bool
  891 metricLong = ("description" ==) . metricName
  892 
  893 birthdateMetric :: Metric--T MeasureTypeDate
  894 birthdateMetric = fromJust $ {- castMetric =<< -} find (("birthdate" ==) . metricName) allMetrics
  895 
  896 metricJSON :: JSON.ToObject o => Metric -> JSON.Record (Id Metric) o
  897 metricJSON m@Metric{..} = JSON.Record metricId $
  898      "category" JSON..= categoryId metricCategory
  899   <> "name" JSON..= metricName
  900   <> "release" `JSON.kvObjectOrEmpty` metricRelease
  901   <> "type" JSON..= show metricType
  902   <> "options" `JSON.kvObjectOrEmpty` (if null metricOptions then empty else pure metricOptions)
  903   <> "assumed" `JSON.kvObjectOrEmpty` metricAssumed
  904   <> "long" `JSON.kvObjectOrEmpty` (True `useWhen` (metricLong m))
  905   <> "description" `JSON.kvObjectOrEmpty` metricDescription
  906   <> "required" `JSON.kvObjectOrEmpty` metricRequired
  907 
  908 {- schema synchronization:
  909 20160201-nih_race
  910 20160202-context_language
  911 20160303-pregnancy_term
  912 -}