Tableau External Services API: Haskell Expressions as Calculations

Image for post
Image for post

Tableau External Services API?

Image for post
Image for post
I know TabPy, but what could be that “External API”?

Why Haskell?

fix (scanl (+) 0 . (1:))
primesT = sieve [2..]
where
sieve (p:xs) = p : sieve [x | x <- xs, rem x p > 0]

The Code

data EvalRequest = 
EvalRequest {
_script :: Text,
_data :: HashMap Text Value
} deriving (Show, Generic, ToJSON)

instance FromJSON EvalRequest
where parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = Prelude.drop 1 }

replaceArgsInScript (EvalRequest s d) = foldrWithKey replaceValuesScript s d
where getArrayText = T.toStrict . T.decodeUtf8 . encode
replaceValuesScript k v = replace k (getArrayText v)

command = replace_args . decode
where replace_args Nothing = "Json parse error"
replace_args (Just er) = unpack $ replaceArgsInScript er

executeCommand p = readProcess "mueval" ["-e", command p] []

infoResponse = "{\"description\": \"\", \"creation_time\": \"0\", \"state_path\": \"\", \"server_version\": \"0.8.7\", \"name\": \"haskell\", \"versions\": {\"v1\": {\"features\": {}}}}"

textAsJson t = setHeader "Content-Type" "application/json" >> text t

main = scotty 3000 $ do
get "/info" $
textAsJson infoResponse
post "/evaluate" $ do
jsonData <- body
cmdRes <- liftIO $ executeCommand jsonData
textAsJson $ T.pack cmdRes
POST /evaluate HTTP/1.1.

{"script":"sort _arg1",
"data" :{"_arg1":[1,2,3,4,5,6,8,7,9,10]}}
sort [1,2,3,4,5,6,8,7,9,10]

Getting up and ready our External Server

Image for post
Image for post
Image for post
Image for post
Set connection to localhost, port 3000
Image for post
Image for post
Image for post
Image for post
Fibonacci and Prime numbers in Tableau with Haskell

Haskell in Tableau

Tamas is co-founder and CTO of data services firm Starschema where he leads the Starschema technical team to deliver results for the most innovative enterprises

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store