/
Factors.lhs
140 lines (120 loc) · 5.32 KB
/
Factors.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Copyright (C) 2011 Associated Universities, Inc. Washington DC, USA.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Correspondence concerning GBT software should be addressed as follows:
GBT Operations
National Radio Astronomy Observatory
P. O. Box 2
Green Bank, WV 24944-0002 USA
> module Server.Factors (
> getFactorsHandler
> , getFactors
> ) where
> import Control.Monad.Trans (liftIO)
> import Control.Monad.State.Lazy (StateT)
> import Database.HDBC.PostgreSQL (Connection)
> import Network.Protocol.Http
> import Network.Salvia.Handlers.PathRouter (hParameters)
> import Network.Salvia.Handlers.Error (hError)
> import Network.Salvia.Handlers.MethodRouter (hMethodRouter)
> import Network.Salvia.Httpd
> import qualified Data.ByteString.Lazy.Char8 as L
> import Server.Json
> import Network.Salvia.Handlers.Redirect (hRedirect)
> import Text.Printf
> import Maybe
> import Antioch.DateTime
> import Antioch.DSSData (getProjects, getSession)
> import Antioch.HardwareSchedule (getReceiverSchedule)
> import Antioch.Score
> import Antioch.Settings (proxyListenerPort)
> import Antioch.Filters
> import Antioch.Types
> import Antioch.Utilities (rad2deg, rad2hrs)
> import Antioch.RunScores (runFactors)
> import Control.OldException
> import Data.Either
> getFactorsHandler :: Connection -> Handler()
> getFactorsHandler cnn = hMethodRouter [
> (GET, getFactors cnn)
> -- , (POST, getFactors)
> ] $ hError NotFound
> getFactors :: Connection -> StateT Context IO ()
> getFactors cnn = do
> params <- hParameters
> -- Interpret options: id, start, tz, duration
> let id = read . fromJust . fromJust . lookup "id" $ params
> let startStr = fromJust . fromJust . lookup "start" $ params
> -- timezone
> let timezone = fromJust . fromJust . lookup "tz" $ params
> -- start time
> let startStr = fromJust . fromJust . lookup "start" $ params
> -- start at ...
> dt <- liftIO $ httpTzToDt startStr timezone
> -- for duration.
> let dur = read . fromJust . fromJust . lookup "duration" $ params
> -- compute all factors with the given inputs
> result <- liftIO $ try $ runFactors' id dt dur
> liftIO $ print ("runFactors': ", result)
> -- send the information back, checking for errors
> case result of
> Left e -> jsonError e
> Right x -> jsonSuccess dt dur x
> where
> wittyMsg = "Unexpected error encounted in service factors: "
> jsonError e = jsonHandler $ makeObj [("error", showJSON (wittyMsg ++ (show e)))]
> jsonSuccess dt dur x = jsonHandler . makeObj $ objs dt dur x
> objs dt dur (s, scoresNfactors) = [("ra", showJSON . floatStr . rad2hrs . ra $ s)
> , ("dec", showJSON . floatStr . rad2deg . dec $ s)
> , ("freq", showJSON . floatStr . frequency $ s)
> , ("xi", showJSON . floatStr . xi $ s)
> , ("type", showJSON . isSchedulableType dt dur $ s)
> , ("time", showJSON . hasTimeSchedulable dt undefined $ s)
> , ("not_complete", showJSON . isNotComplete dt undefined $ s)
> , ("enabled", showJSON . enabled $ s)
> , ("authorized", showJSON . authorized $ s)
> , ("observers", showJSON . hasObservers dt undefined $ s)
> , ("factors", factorsListToJSValue scoresNfactors)]
> runFactors' id dt dur = do
> projs <- getProjects
> runFactors id dt dur projs False
> floatStr :: Float -> String
> floatStr f = printf "%.2f" f
> floatFactor :: Maybe Score -> String
> floatFactor f = if isJust f then printf "%.7f" $ fromJust f else "0.0000"
> data JFactor = JFactor {
> fName :: String
> , fScore :: Maybe Score
> } deriving Show
> defaultJFactor = JFactor {
> fName = ""
> , fScore = Nothing
> }
> toJFactor :: Factor -> JFactor
> toJFactor f = defaultJFactor {
> fName = fst f
> , fScore = snd f
> }
> instance JSON JFactor where
> readJSON = jsonToJFactor
> showJSON = jFactorToJson
> jsonToJFactor _ = undefined
> factorsToJSValue :: [Factor] -> JSValue
> factorsToJSValue = JSArray . map showJSON
> factorsListToJSValue :: [[Factor]] -> JSValue
> factorsListToJSValue = JSArray . map factorsToJSValue
> jFactorToJson :: JFactor -> JSValue
> jFactorToJson jfactor = makeObj $
> [
> ("name", showJSON . fName $ jfactor)
> , ("score", showJSON . floatFactor $ fScore jfactor)
> ]