Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphql sorting #572

Merged
merged 3 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 13 additions & 99 deletions src/ctia/http/routes/common.clj
Original file line number Diff line number Diff line change
@@ -1,139 +1,53 @@
(ns ctia.http.routes.common
(:require [clojure.string :as str]
[clj-http.headers :refer [canonicalize]]
[ctia.schemas.sorting :as sorting]
[ring.util.http-status :refer [ok]]
[ring.util.http-response :as http-res]
[schema.core :as s]
[schema-tools.core :as st]
[ring.swagger.schema :refer [describe]]))


;; These are the fields that are sortable, per entity. We place them
;; here since they are used by more than one entity's routes. For
;; isntance, the indicator route needs to know how to sort sightings
;; for the `ctia/indicator/:ID/sighting` handler
;;
(def base-entity-sort-fields [:id :schema_version :revision
:timestamp :language :tlp])

(def sourcable-entity-sort-fields [:source :source_uri])

(def describable-entity-sort-fields [:title])

(def default-entity-sort-fields
(concat base-entity-sort-fields
sourcable-entity-sort-fields
describable-entity-sort-fields))


(def exploit-target-sort-fields
(apply s/enum (concat default-entity-sort-fields
describable-entity-sort-fields
sourcable-entity-sort-fields
[:valid_time.start_time
:valid_time.end_time])))
(apply s/enum sorting/exploit-target-sort-fields))

(def incident-sort-fields
(apply s/enum (concat default-entity-sort-fields
describable-entity-sort-fields
sourcable-entity-sort-fields
[:valid_time.start_time
:valid_time.end_time
:confidence
:status
:incident_time
:reporter
:coordinator
:victim
:security_compromise
:discovery_method
:contact
:intended_effect])))
(apply s/enum sorting/incident-sort-fields))

(def indicator-sort-fields
(apply s/enum (concat default-entity-sort-fields
[:indicator_type
:likely_impact
:confidence
:specification])))
(apply s/enum sorting/indicator-sort-fields))

(def relationship-sort-fields
(apply s/enum (concat default-entity-sort-fields
describable-entity-sort-fields
sourcable-entity-sort-fields
[:relationship_type
:source_ref
:target_ref])))
(apply s/enum sorting/relationship-sort-fields))

(def judgement-sort-fields
(apply s/enum (concat base-entity-sort-fields
sourcable-entity-sort-fields
[:disposition
:priority
:confidence
:severity
:valid_time.start_time
:valid_time.end_time
:reason
:observable.type
:observable.value])))
(apply s/enum sorting/judgement-sort-fields))

(def sighting-sort-fields
(apply s/enum (concat default-entity-sort-fields
[:observed_time.start_time
:observed_time.end_time
:confidence
:count
:sensor])))
(apply s/enum sorting/sighting-sort-fields))

(def ttp-sort-fields
(apply s/enum (concat default-entity-sort-fields
[:valid_time.start_time
:valid_time.end_time
:ttp_type])))
(apply s/enum sorting/ttp-sort-fields))

(def actor-sort-fields
(apply s/enum (concat default-entity-sort-fields
[:valid_time.start_time
:valid_time.end_time
:actor_type
:motivation
:sophistication
:intended_effect])))
(apply s/enum sorting/actor-sort-fields))

(def campaign-sort-fields
(apply s/enum (concat default-entity-sort-fields
[:valid_time.start_time
:valid_time.end_time
:campaign_type
:status
:activity
:confidence])))
(apply s/enum sorting/campaign-sort-fields))

(def coa-sort-fields
(apply s/enum (concat default-entity-sort-fields
[:valid_time.start_time
:valid_time.end_time
:stage
:coa_type
:impact
:cost
:efficacy
:structured_coa_type])))
(apply s/enum sorting/coa-sort-fields))

(def feedback-sort-fields
(apply s/enum (concat base-entity-sort-fields
sourcable-entity-sort-fields
[:entity_id
:feedback
:reason])))
(apply s/enum sorting/feedback-sort-fields))


;; Paging related values and code

(s/defschema PagingParams
"A schema defining the accepted paging and sorting related query parameters."
{(s/optional-key :sort_by) (describe (apply s/enum default-entity-sort-fields) "Sort results on a field")
{(s/optional-key :sort_by) (describe (apply s/enum sorting/default-entity-sort-fields) "Sort results on a field")
(s/optional-key :sort_order) (describe (s/enum :asc :desc) "Sort direction")
(s/optional-key :offset) (describe Long "Pagination Offset")
(s/optional-key :limit) (describe Long "Pagination Limit")})
Expand Down
30 changes: 18 additions & 12 deletions src/ctia/schemas/graphql.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
(:require [ctia.schemas.graphql
[common :as common]
[helpers :as g]
[indicator :refer [IndicatorType
IndicatorConnectionType]]
[judgement :refer [JudgementType
JudgementConnectionType]]
[indicator :as indicator
:refer [IndicatorType
IndicatorConnectionType]]
[judgement :as judgement
:refer [JudgementType
JudgementConnectionType]]
[observable :refer [ObservableType]]
[resolvers :as res]
[sighting :refer [SightingType
SightingConnectionType]]]
[sighting :as sighting
:refer [SightingType
SightingConnectionType]]]
[schema.core :as s]
[ctia.schemas.graphql.pagination :as p])
(:import graphql.Scalars))
Expand Down Expand Up @@ -38,15 +41,17 @@
:args search-by-id-args
:resolve (fn [_ args _] (res/indicator-by-id (:id args)))}
:indicators {:type IndicatorConnectionType
:args (into common/lucene-query-arguments
p/connection-arguments)
:args (merge common/lucene-query-arguments
indicator/indicator-order-arg
p/connection-arguments)
:resolve res/search-indicators}
:judgement {:type JudgementType
:args search-by-id-args
:resolve (fn [_ args _] (res/judgement-by-id (:id args)))}
:judgements {:type JudgementConnectionType
:args (into common/lucene-query-arguments
p/connection-arguments)
:args (merge common/lucene-query-arguments
judgement/judgement-order-arg
p/connection-arguments)
:resolve res/search-judgements}
:observable {:type ObservableType
:args {:type {:type (g/non-null Scalars/GraphQLString)}
Expand All @@ -56,8 +61,9 @@
:args search-by-id-args
:resolve (fn [_ args _] (res/sighting-by-id (:id args)))}
:sightings {:type SightingConnectionType
:args (into common/lucene-query-arguments
p/connection-arguments)
:args (merge common/lucene-query-arguments
sighting/sighting-order-arg
p/connection-arguments)
:resolve res/search-sightings}}))

(def schema (g/new-schema QueryType))
Expand Down
15 changes: 13 additions & 2 deletions src/ctia/schemas/graphql/feedback.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,32 @@
[helpers :as g]
[pagination :as pagination]
[resolvers :as resolvers]]
[ctim.schemas.feedback :as ctim-feedback]))
[ctim.schemas.feedback :as ctim-feedback]
[ctia.schemas.graphql.sorting :as sorting]
[ctia.schemas.sorting :as sort-fields]))

(def FeedbackType
(let [{:keys [fields name description]}
(f/->graphql ctim-feedback/Feedback)]
(g/new-object name description [] fields)))

(def feedback-order-arg
(sorting/order-by-arg
"FeedbackOrder"
"feedbacks"
(into {}
(map (juxt sorting/sorting-kw->enum-name name)
sort-fields/feedback-sort-fields))))

(def FeedbackConnectionType
(pagination/new-connection FeedbackType))

(def feedback-connection-field
{:feedbacks
{:type FeedbackConnectionType
:description "Related Feedbacks"
:args pagination/connection-arguments
:args (into pagination/connection-arguments
feedback-order-arg)
:resolve (fn [_ args src]
(resolvers/search-feedbacks-by-entity-id (:id src) args))}})

Loading