Browse Source

display list of running devices, simplify loading logic

Mark deVilliers 2 years ago
parent
commit
feadd276d7
5 changed files with 635 additions and 217 deletions
  1. 504 177
      ui/node.js
  2. 9 7
      ui/search.js
  3. 12 1
      ui/src/Decoders.elm
  4. 109 31
      ui/src/Node.elm
  5. 1 1
      ui/src/Search.elm

File diff suppressed because it is too large
+ 504 - 177
ui/node.js


+ 9 - 7
ui/search.js

@@ -10457,16 +10457,17 @@ var _user$project$Decoders$Entitlement = F4(
10457 10457
 	function (a, b, c, d) {
10458 10458
 		return {subject: a, level: b, uid: c, status: d};
10459 10459
 	});
10460
-var _user$project$Decoders$MetadataItem = F3(
10461
-	function (a, b, c) {
10462
-		return {subject: a, description: b, name: c};
10460
+var _user$project$Decoders$MetadataItem = F4(
10461
+	function (a, b, c, d) {
10462
+		return {subject: a, description: b, name: c, path: d};
10463 10463
 	});
10464
-var _user$project$Decoders$decodeMetadataItem = A4(
10465
-	_elm_lang$core$Json_Decode$map3,
10464
+var _user$project$Decoders$decodeMetadataItem = A5(
10465
+	_elm_lang$core$Json_Decode$map4,
10466 10466
 	_user$project$Decoders$MetadataItem,
10467 10467
 	A2(_elm_lang$core$Json_Decode$field, 'subject', _elm_lang$core$Json_Decode$string),
10468 10468
 	A2(_elm_lang$core$Json_Decode$field, 'description', _elm_lang$core$Json_Decode$string),
10469
-	A2(_elm_lang$core$Json_Decode$field, 'name', _elm_lang$core$Json_Decode$string));
10469
+	A2(_elm_lang$core$Json_Decode$field, 'name', _elm_lang$core$Json_Decode$string),
10470
+	A2(_elm_lang$core$Json_Decode$field, 'path', _elm_lang$core$Json_Decode$string));
10470 10471
 var _user$project$Decoders$decodeMetadata = _elm_lang$core$Json_Decode$list(_user$project$Decoders$decodeMetadataItem);
10471 10472
 var _user$project$Decoders$Device = F4(
10472 10473
 	function (a, b, c, d) {
@@ -10479,6 +10480,7 @@ var _user$project$Decoders$decodeDevice = A5(
10479 10480
 	A2(_elm_lang$core$Json_Decode$field, 'description', _elm_lang$core$Json_Decode$string),
10480 10481
 	A2(_elm_lang$core$Json_Decode$field, 'uid', _elm_lang$core$Json_Decode$string),
10481 10482
 	A2(_elm_lang$core$Json_Decode$field, 'name', _elm_lang$core$Json_Decode$string));
10483
+var _user$project$Decoders$decodeDevices = _elm_lang$core$Json_Decode$list(_user$project$Decoders$decodeDevice);
10482 10484
 var _user$project$Decoders$Requesting = {ctor: 'Requesting'};
10483 10485
 var _user$project$Decoders$RequestAccess = {ctor: 'RequestAccess'};
10484 10486
 var _user$project$Decoders$Unknown = {ctor: 'Unknown'};
@@ -11086,7 +11088,7 @@ var _user$project$Search$view = function (model) {
11086 11088
 									},
11087 11089
 									{
11088 11090
 										ctor: '::',
11089
-										_0: _elm_lang$html$Html$text('refresh'),
11091
+										_0: _elm_lang$html$Html$text('new search'),
11090 11092
 										_1: {ctor: '[]'}
11091 11093
 									}),
11092 11094
 								_1: {ctor: '[]'}

+ 12 - 1
ui/src/Decoders.elm

@@ -160,15 +160,17 @@ type alias MetadataItem =
160 160
     { subject : String
161 161
     , description : String
162 162
     , name : String
163
+    , path : String
163 164
     }
164 165
 
165 166
 
166 167
 decodeMetadataItem : Decoder MetadataItem
167 168
 decodeMetadataItem =
168
-    Json.Decode.map3 MetadataItem
169
+    Json.Decode.map4 MetadataItem
169 170
         (Json.Decode.field "subject" Json.Decode.string)
170 171
         (Json.Decode.field "description" Json.Decode.string)
171 172
         (Json.Decode.field "name" Json.Decode.string)
173
+        (Json.Decode.field "path" Json.Decode.string)
172 174
 
173 175
 
174 176
 type alias Metadata =
@@ -195,3 +197,12 @@ decodeDevice =
195 197
         (Json.Decode.field "description" Json.Decode.string)
196 198
         (Json.Decode.field "uid" Json.Decode.string)
197 199
         (Json.Decode.field "name" Json.Decode.string)
200
+
201
+
202
+type alias Devices =
203
+    List Device
204
+
205
+
206
+decodeDevices : Decoder Devices
207
+decodeDevices =
208
+    Json.Decode.list decodeDevice

+ 109 - 31
ui/src/Node.elm

@@ -28,6 +28,7 @@ type alias Model =
28 28
     { accepted : Maybe Decoders.Entitlements
29 29
     , requested : Maybe Decoders.Entitlements
30 30
     , metadata : Maybe Decoders.Metadata
31
+    , devices : Maybe Decoders.Devices
31 32
     , tabState : Tab.State
32 33
     }
33 34
 
@@ -37,18 +38,20 @@ initialModel =
37 38
     { accepted = Nothing
38 39
     , requested = Nothing
39 40
     , metadata = Nothing
41
+    , devices = Nothing
40 42
     , tabState = Tab.initialState
41 43
     }
42 44
 
43 45
 
44 46
 init : ( Model, Cmd Msg )
45 47
 init =
46
-    ( initialModel, getMetadata )
48
+    ( initialModel, Cmd.batch [ getRequestedEntitlements, getAcceptedEntitlements, getMetadata, getDevices ] )
47 49
 
48 50
 
49 51
 type Msg
50 52
     = NoOp
51
-    | Refresh
53
+    | RefreshEntitlements
54
+    | RefreshDevices
52 55
     | GetMetadataCompleted (Result Http.Error Decoders.Metadata)
53 56
     | GetAcceptedEntitlementsCompleted (Result Http.Error Decoders.Entitlements)
54 57
     | GetRequestedEntitlementsCompleted (Result Http.Error Decoders.Entitlements)
@@ -60,6 +63,7 @@ type Msg
60 63
     | AmendEntitlementCompleted (Result Http.Error Decoders.Entitlement)
61 64
     | AddDevice String
62 65
     | AddDeviceCompleted (Result Http.Error Decoders.Device)
66
+    | GetDevicesCompleted (Result Http.Error Decoders.Devices)
63 67
     | TabMsg Tab.State
64 68
 
65 69
 
@@ -69,11 +73,14 @@ update msg model =
69 73
         NoOp ->
70 74
             ( model, Cmd.none )
71 75
 
72
-        Refresh ->
73
-            ( model, getMetadata )
76
+        RefreshEntitlements ->
77
+            ( model, Cmd.batch [ getRequestedEntitlements, getAcceptedEntitlements, getMetadata ] )
78
+
79
+        RefreshDevices ->
80
+            ( model, getDevices )
74 81
 
75 82
         GetAcceptedEntitlementsCompleted (Ok items) ->
76
-            ( { model | accepted = Just items }, getRequestedEntitlements )
83
+            ( { model | accepted = Just items }, Cmd.none )
77 84
 
78 85
         GetAcceptedEntitlementsCompleted (Err httpError) ->
79 86
             Debug.crash (toString httpError)
@@ -85,7 +92,7 @@ update msg model =
85 92
             Debug.crash (toString httpError)
86 93
 
87 94
         GetMetadataCompleted (Ok items) ->
88
-            ( { model | metadata = Just items }, getAcceptedEntitlements )
95
+            ( { model | metadata = Just items }, Cmd.none )
89 96
 
90 97
         GetMetadataCompleted (Err httpError) ->
91 98
             Debug.crash (toString httpError)
@@ -94,7 +101,7 @@ update msg model =
94 101
             ( model, acceptEntitlement ent )
95 102
 
96 103
         AcceptEntitlementCompleted (Ok ent) ->
97
-            ( model, getAcceptedEntitlements )
104
+            ( model, Cmd.batch [ getRequestedEntitlements, getAcceptedEntitlements ] )
98 105
 
99 106
         AcceptEntitlementCompleted (Err httpError) ->
100 107
             Debug.crash (toString httpError)
@@ -103,7 +110,7 @@ update msg model =
103 110
             ( model, declineEntitlement ent )
104 111
 
105 112
         DeclineEntitlementCompleted (Ok ent) ->
106
-            ( model, getAcceptedEntitlements )
113
+            ( model, Cmd.batch [ getRequestedEntitlements, getAcceptedEntitlements ] )
107 114
 
108 115
         DeclineEntitlementCompleted (Err httpError) ->
109 116
             Debug.crash (toString httpError)
@@ -112,7 +119,7 @@ update msg model =
112 119
             ( model, amendEntitlement ent )
113 120
 
114 121
         AmendEntitlementCompleted (Ok ent) ->
115
-            ( model, getAcceptedEntitlements )
122
+            ( model, Cmd.batch [ getRequestedEntitlements, getAcceptedEntitlements ] )
116 123
 
117 124
         AmendEntitlementCompleted (Err httpError) ->
118 125
             Debug.crash (toString httpError)
@@ -124,11 +131,17 @@ update msg model =
124 131
             ( model, addDevice deviceType )
125 132
 
126 133
         AddDeviceCompleted (Ok ent) ->
127
-            ( model, getAcceptedEntitlements )
134
+            ( model, getDevices )
128 135
 
129 136
         AddDeviceCompleted (Err httpError) ->
130 137
             Debug.crash (toString httpError)
131 138
 
139
+        GetDevicesCompleted (Ok d) ->
140
+            ( { model | devices = Just d }, Cmd.none )
141
+
142
+        GetDevicesCompleted (Err httpError) ->
143
+            Debug.crash (toString httpError)
144
+
132 145
 
133 146
 
134 147
 --RPC
@@ -232,6 +245,15 @@ addDevice deviceType =
232 245
         Http.send AddDeviceCompleted request
233 246
 
234 247
 
248
+getDevices : Cmd Msg
249
+getDevices =
250
+    let
251
+        request =
252
+            Http.get (nodeURL ++ "/devices") Decoders.decodeDevices
253
+    in
254
+        Http.send GetDevicesCompleted request
255
+
256
+
235 257
 
236 258
 -- SUBSCRIPTIONS
237 259
 
@@ -270,10 +292,12 @@ deviceTab : Model -> Tab.Pane Msg
270 292
 deviceTab model =
271 293
     Tab.pane [ Html.Attributes.class "mt-3" ]
272 294
         [ h4 [] [ text "Devices" ]
273
-        , p [] [ text "This is the page where you can add, list and remove devices." ]
295
+        , p [] [ text "This is the page where you can configure your devices." ]
296
+        , div [] [ h5 [] [ text "Configured devices" ] ]
297
+        , button [ onClick RefreshDevices ] [ text "Refresh" ]
298
+        , div [] [ devicesTable model ]
299
+        , div [] [ h5 [] [ text "New device" ] ]
274 300
         , Card.group devicesGallery
275
-
276
-        --|> Card.view
277 301
         ]
278 302
 
279 303
 
@@ -284,8 +308,8 @@ devicesGallery =
284 308
             [ img [ src "/static/elm-bootstrap.svg", width 100 ] []
285 309
             ]
286 310
         |> Card.block []
287
-            [ Card.titleH5 [] [ text "Sine Fake Device" ]
288
-            , Card.text [] [ text "Fake device to generate the perfect sine curve." ]
311
+            [ Card.titleH5 [] [ text (deviceDescription "fake-sine") ]
312
+            , Card.text [] [ text (deviceDescription "fake-sine") ]
289 313
             , Card.custom <|
290 314
                 Button.button [ Button.primary, Button.attrs [ onClick (AddDevice "fake-sine") ] ] [ text "Add device" ]
291 315
             ]
@@ -294,20 +318,76 @@ devicesGallery =
294 318
             [ img [ src "/static/elm-bootstrap.svg", width 100 ] []
295 319
             ]
296 320
         |> Card.block []
297
-            [ Card.titleH5 [] [ text "Temp Humidity Fake Device" ]
298
-            , Card.text [] [ text "Fake device to generate temperature and humidity values." ]
321
+            [ Card.titleH5 [] [ text (deviceType "fake-temp-humidity") ]
322
+            , Card.text [] [ text (deviceDescription "fake-temp-humidity") ]
299 323
             , Card.custom <|
300 324
                 Button.button [ Button.primary, Button.attrs [ onClick (AddDevice "fake-temp-humidity") ] ] [ text "Add device" ]
301 325
             ]
302 326
     ]
303 327
 
304 328
 
329
+devicesTable : Model -> Html Msg
330
+devicesTable model =
331
+    case model.devices of
332
+        Nothing ->
333
+            text "No devices running"
334
+
335
+        Just d ->
336
+            Table.simpleTable
337
+                ( Table.simpleThead
338
+                    [ Table.th [] [ text "Device" ]
339
+                    , Table.th [] [ text "Type" ]
340
+                    , Table.th [] [ text "Description" ]
341
+                    ]
342
+                , Table.tbody [] <| drawDevices d
343
+                )
344
+
345
+
346
+drawDevices : Decoders.Devices -> List (Table.Row msg)
347
+drawDevices d =
348
+    List.map
349
+        (\device ->
350
+            Table.tr []
351
+                [ Table.td [] [ text device.name ]
352
+                , Table.td [] [ text (deviceType device.typez) ]
353
+                , Table.td [] [ text device.description ]
354
+                ]
355
+        )
356
+        d
357
+
358
+
359
+deviceDescription : String -> String
360
+deviceDescription s =
361
+    case s of
362
+        "fake-sine" ->
363
+            "Fake device to generate the perfect sine curve."
364
+
365
+        "fake-temp-humidity" ->
366
+            "Fake device to generate temperature and humidity values."
367
+
368
+        _ ->
369
+            "UNKNOWN DEVICE"
370
+
371
+
372
+deviceType : String -> String
373
+deviceType s =
374
+    case s of
375
+        "fake-sine" ->
376
+            "Sine Fake Device."
377
+
378
+        "fake-temp-humidity" ->
379
+            "Temp Humidity Fake Device "
380
+
381
+        _ ->
382
+            "UNKNOWN DEVICE"
383
+
384
+
305 385
 entitlementsTab : Model -> Tab.Pane Msg
306 386
 entitlementsTab model =
307 387
     Tab.pane [ Html.Attributes.class "mt-3" ]
308 388
         [ h4 [] [ text "Entitlements" ]
309 389
         , p [] [ text "This page is where you can edit, view, accept and reject entitlements to your data." ]
310
-        , button [ onClick Refresh ] [ text "Refresh" ]
390
+        , button [ onClick RefreshEntitlements ] [ text "Refresh" ]
311 391
         , div [] []
312 392
         , entitlementsTable model
313 393
         ]
@@ -330,7 +410,7 @@ drawMetadata model =
330 410
     case model.metadata of
331 411
         Nothing ->
332 412
             -- there must be a better way than this!
333
-            List.map (\m -> Table.tr [] [ Table.td [] [ text ("no data exists.") ] ]) [ 1 ]
413
+            List.map (\m -> Table.tr [] [ Table.td [] [ text ("") ] ]) [ 1 ]
334 414
 
335 415
         Just e ->
336 416
             List.map (\m -> drawMetadataItem m model) e
@@ -345,7 +425,7 @@ drawMetadataItem m model =
345 425
         requested =
346 426
             findEntitlement m.subject model.requested
347 427
     in
348
-        Table.tr [] [ Table.td [] [ text m.name, div [] [ text m.description ] ], Table.td [] [ drawAccepted (accepted) ], Table.td [] [ drawRequested (requested) ] ]
428
+        Table.tr [] [ Table.td [] [ text m.name, div [] [ text m.path ], div [] [ text m.description ] ], Table.td [] [ drawAccepted (accepted) ], Table.td [] [ drawRequested (requested) ] ]
349 429
 
350 430
 
351 431
 drawEntitlementSelector : Decoders.MetadataItem -> Model -> Html Msg
@@ -374,30 +454,29 @@ drawAccessLevelSelector : Decoders.Entitlement -> Html Msg
374 454
 drawAccessLevelSelector ent =
375 455
     case ent.level of
376 456
         Decoders.OwnerOnly ->
377
-            div [] [ a [ onClick (AmendEntitlement { ent | level = Decoders.CanDiscover }), href "#" ] [ text ("make data searchable") ] ]
457
+            div [] [ a [ onClick (AmendEntitlement { ent | level = Decoders.CanDiscover }), href "#" ] [ text ("make data discoverable") ] ]
378 458
 
379 459
         Decoders.CanDiscover ->
380 460
             div []
381
-                [ a [ onClick (AmendEntitlement { ent | level = Decoders.OwnerOnly }), href "#" ] [ text ("stop making available for search") ]
382
-                , text (" ")
383
-                , a [ onClick (AmendEntitlement { ent | level = Decoders.CanAccess }), href "#" ] [ text ("make data accessible") ]
461
+                [ div [] [ a [ onClick (AmendEntitlement { ent | level = Decoders.OwnerOnly }), href "#" ] [ text ("stop making data discoverable") ] ]
462
+                , div [] [ a [ onClick (AmendEntitlement { ent | level = Decoders.CanAccess }), href "#" ] [ text ("make data accessible") ] ]
384 463
                 ]
385 464
 
386 465
         Decoders.CanAccess ->
387
-            div [] [ a [ onClick (AmendEntitlement { ent | level = Decoders.CanDiscover }), href "#" ] [ text ("remove access") ] ]
466
+            div [] [ a [ onClick (AmendEntitlement { ent | level = Decoders.CanDiscover }), href "#" ] [ text ("stop making data accessible") ] ]
388 467
 
389 468
 
390 469
 drawAccessLevel : Decoders.AccessLevel -> Html Msg
391 470
 drawAccessLevel level =
392 471
     case level of
393 472
         Decoders.OwnerOnly ->
394
-            text ("No one can see the data")
473
+            text ("No one can discover or access the data")
395 474
 
396 475
         Decoders.CanDiscover ->
397
-            text ("Anyone can discover the data")
476
+            text ("Anyone can discover the data, no one can access the data")
398 477
 
399 478
         Decoders.CanAccess ->
400
-            text ("Anyone can access the data")
479
+            text ("Anyone can discover the data, anyone can access the data")
401 480
 
402 481
 
403 482
 drawRequested : Maybe Decoders.Entitlement -> Html Msg
@@ -410,9 +489,8 @@ drawRequested ent =
410 489
             div []
411 490
                 [ drawAccessLevel (e.level)
412 491
                 , div []
413
-                    [ a [ onClick (AcceptEntitlement e), href "#" ] [ text ("accept") ]
414
-                    , text (" ")
415
-                    , a [ onClick (DeclineEntitlement e), href "#" ] [ text ("decline") ]
492
+                    [ div [] [ a [ onClick (AcceptEntitlement e), href "#" ] [ text ("accept") ] ]
493
+                    , div [] [ a [ onClick (DeclineEntitlement e), href "#" ] [ text ("decline") ] ]
416 494
                     ]
417 495
                 ]
418 496
 

+ 1 - 1
ui/src/Search.elm

@@ -228,7 +228,7 @@ view model =
228 228
             div []
229 229
                 [ div [] [ text "Metadata" ]
230 230
                 , drawFiltered model.filter d
231
-                , div [] [ button [ onClick RefreshMetadata ] [ text "refresh" ] ]
231
+                , div [] [ button [ onClick RefreshMetadata ] [ text "new search" ] ]
232 232
                 ]
233 233
 
234 234