Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. forked from tokono.ma/diffuse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328module Brain.Sources.Processing.Steps exposing ( takeFirstStep , takePrepareStep , takeTagsStep , takeTreeStep )
{-| Processing.
## How it works
This describes the process for a single source.
1. Prepare the source for processing. -> For example, a source could get an access token first. 2. Get a file tree/list from the source -> This can happen in multiple steps as with Amazon S3. A command is issued for each step of this process. 3. Get the tags (ie. metadata) for each file that we found. -> This also happens in multiple steps, so that we can flush every x tracks while processing. A command is issued for each step of this process.
-}
import Alienimport Brain.Ports as Portsimport Brain.Sources.Processing.Common exposing (..)import Brain.Sources.Processing.Types exposing (..)import Brain.Types as Brain exposing (..)import List.Extra as Listimport Setimport Sources exposing (Source)import Sources.Encodingimport Sources.Processing exposing (..)import Sources.Services as Servicesimport Task.Extra exposing (do)import Timeimport Tracks exposing (Track)
-- SETTINGS
{-| How much tags do we want to processbefore we send them back to Elm.
eg. After we got the tags for 20 tracks, we store these and continue with the rest.
-}tagsBatchSize : InttagsBatchSize = 20
-- 1st STEP
takeFirstStep : String -> Time.Posix -> Source -> Cmd Brain.MsgtakeFirstStep origin currentTime source = let initialContext = { filePaths = [] , origin = origin , preparationMarker = TheBeginning , source = source , treeMarker = TheBeginning } in prepare initialContext currentTime
-- 2nd STEP
takePrepareStep : Context -> String -> Time.Posix -> Cmd Brain.MsgtakePrepareStep context response currentTime = context |> handlePreparationResponse response currentTime |> intoPreparationCommands currentTime
-- 3rd STEP
takeTreeStep : Context -> String -> List Track -> Time.Posix -> Cmd Brain.MsgtakeTreeStep context response associatedTracks currentTime = context |> handleTreeResponse response |> intoTreeCommand associatedTracks currentTime
-- 4th STEP
takeTagsStep : Time.Posix -> ContextForTags -> Source -> Maybe (Cmd Brain.Msg)takeTagsStep currentTime tagsCtx source = let ( filesToProcess, nextFiles ) = List.splitAt tagsBatchSize tagsCtx.nextFilePaths
newTagsCtx = { amount = tagsCtx.amount , nextFilePaths = nextFiles , receivedFilePaths = filesToProcess , receivedTags = [] , sourceId = source.id , urlsForTags = makeTrackUrls currentTime source filesToProcess } in if List.isEmpty filesToProcess then Nothing
else Just (getTags newTagsCtx)
------------------------------------------- ㊙️------------------------------------------- PREPARE
prepare : Context -> Time.Posix -> Cmd Brain.Msgprepare context currentTime = let maybePreparationCommand = Services.prepare context.source.service context.origin context.source.data context.preparationMarker (ProcessingMsg << PrepareStep context) in case maybePreparationCommand of Just cmd -> cmd
Nothing -> -- Some services don't need to prepare for processing. -- 🚀 makeTree context currentTime
handlePreparationResponse : String -> Time.Posix -> Context -> ContexthandlePreparationResponse response currentTime context = let answer = Services.parsePreparationResponse context.source.service response currentTime context.source.data context.preparationMarker
source = context.source in { context | preparationMarker = answer.marker , source = { source | data = answer.sourceData } }
intoPreparationCommands : Time.Posix -> Context -> Cmd Brain.MsgintoPreparationCommands currentTime context = case context.preparationMarker of TheBeginning -> Cmd.none
-- Still preparing, -- carry on. -- InProgress _ -> prepare context currentTime
-- The preparation is completed, -- continue to the next step. -- TheEnd -> let updatedSource = context.source in Cmd.batch [ -- Make a file tree, the next step. -- 🚀 makeTree context currentTime
-- Update source data. , updatedSource |> Sources.Encoding.encode |> Alien.broadcast Alien.UpdateSourceData |> Ports.toUI ]
-- TREE
makeTree : Context -> Time.Posix -> Cmd Brain.MsgmakeTree context currentTime = Services.makeTree context.source.service context.source.data context.treeMarker currentTime (ProcessingMsg << TreeStep context)
handleTreeResponse : String -> Context -> ContexthandleTreeResponse response context = let parsingFunc = Services.parseTreeResponse context.source.service
parsedResponse = parsingFunc response context.treeMarker in { context | filePaths = context.filePaths ++ parsedResponse.filePaths , treeMarker = parsedResponse.marker }
intoTreeCommand : List Track -> Time.Posix -> Context -> Cmd Brain.MsgintoTreeCommand associatedTracks currentTime context = case context.treeMarker of TheBeginning -> Cmd.none
-- Still building the tree, -- carry on. -- InProgress _ -> makeTree context currentTime
-- The tree's been build, -- continue to the next step. -- TheEnd -> let filteredFiles = Services.postProcessTree context.source.service context.filePaths
postContext = { context | filePaths = filteredFiles }
pathsSourceOfTruth = postContext.filePaths
pathsCurrent = List.map .path associatedTracks
( pathsAdded, pathsRemoved ) = separate pathsCurrent pathsSourceOfTruth in Cmd.batch [ -- Get tags from tracks, the next step. -- 🚀 postContext |> (\ctx -> { ctx | filePaths = pathsAdded }) |> contextToTagsContext |> TagsStep |> ProcessingMsg |> do
-- Remove tracks , if not (List.isEmpty pathsRemoved) then pathsRemoved |> TreeStepRemoveTracks context.source.id |> ProcessingMsg |> do
else Cmd.none ]
separate : List String -> List String -> ( List String, List String )separate current srcOfTruth = let setCurrent = Set.fromList current
setSrcOfTruth = Set.fromList srcOfTruth in ( -- Added -------- Set.diff setSrcOfTruth setCurrent |> Set.toList , -- Removed ---------- Set.diff setCurrent setSrcOfTruth |> Set.toList )
-- TAGS
getTags : ContextForTags -> Cmd Brain.MsggetTags = Ports.requestTags
makeTrackUrls : Time.Posix -> Source -> List String -> List TagUrlsmakeTrackUrls currentTime source filePaths = let maker = Services.makeTrackUrl source.service
mapFn = \path -> { getUrl = maker currentTime source.id source.data Get path , headUrl = maker currentTime source.id source.data Head path } in List.map mapFn filePaths