From d6d90579bc8a49d575426ff4a0890042af203291 Mon Sep 17 00:00:00 2001 From: Jesse White Date: Thu, 2 May 2024 12:01:42 -0400 Subject: [PATCH] graphql client --- .gitignore | 1 + Cargo.lock | 145 +++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 2 + gql/queries.graphql | 26 ++++++++ src/gql.rs | 22 +++++++ src/lib.rs | 4 +- 6 files changed, 191 insertions(+), 9 deletions(-) create mode 100644 gql/queries.graphql create mode 100644 src/gql.rs diff --git a/.gitignore b/.gitignore index 337241a..f31f42e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /node_modules .env +.idea diff --git a/Cargo.lock b/Cargo.lock index f1cf76f..ade10f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ascii" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" + [[package]] name = "async-trait" version = "0.1.80" @@ -25,7 +31,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.60", ] [[package]] @@ -55,6 +61,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.6.0" @@ -84,6 +96,25 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "combine" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3da6baa321ec19e1cc41d31bf599f00c783d0517095cdaf0332e3fe8d20680" +dependencies = [ + "ascii", + "byteorder", + "either", + "memchr", + "unreachable", +] + +[[package]] +name = "either" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" + [[package]] name = "fnv" version = "1.0.7" @@ -128,7 +159,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.60", ] [[package]] @@ -166,6 +197,70 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "graphql-introspection-query" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f2a4732cf5140bd6c082434494f785a19cfb566ab07d1382c3671f5812fed6d" +dependencies = [ + "serde", +] + +[[package]] +name = "graphql-parser" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ebc8013b4426d5b81a4364c419a95ed0b404af2b82e2457de52d9348f0e474" +dependencies = [ + "combine", + "thiserror", +] + +[[package]] +name = "graphql_client" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a50cfdc7f34b7f01909d55c2dcb71d4c13cbcbb4a1605d6c8bd760d654c1144b" +dependencies = [ + "graphql_query_derive", + "serde", + "serde_json", +] + +[[package]] +name = "graphql_client_codegen" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e27ed0c2cf0c0cc52c6bcf3b45c907f433015e580879d14005386251842fb0a" +dependencies = [ + "graphql-introspection-query", + "graphql-parser", + "heck", + "lazy_static", + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn 1.0.109", +] + +[[package]] +name = "graphql_query_derive" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83febfa838f898cfa73dfaa7a8eb69ff3409021ac06ee94cfb3d622f6eeb1a97" +dependencies = [ + "graphql_client_codegen", + "proc-macro2", + "syn 1.0.109", +] + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "http" version = "1.1.0" @@ -212,6 +307,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.154" @@ -292,7 +393,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.60", ] [[package]] @@ -376,7 +477,7 @@ checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.60", ] [[package]] @@ -411,6 +512,17 @@ dependencies = [ "autocfg", ] +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.60" @@ -439,7 +551,7 @@ checksum = "d1cd413b5d558b4c5bf3680e324a6fa5014e7b7c067a51e69dbdf47eb7148b66" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.60", ] [[package]] @@ -488,6 +600,15 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +dependencies = [ + "void", +] + [[package]] name = "url" version = "2.5.0" @@ -499,6 +620,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + [[package]] name = "wasm-bindgen" version = "0.2.92" @@ -520,7 +647,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.60", "wasm-bindgen-shared", ] @@ -554,7 +681,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.60", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -643,7 +770,7 @@ dependencies = [ "async-trait", "proc-macro2", "quote", - "syn", + "syn 2.0.60", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-macro-support", @@ -654,6 +781,8 @@ dependencies = [ name = "worker-rust" version = "0.1.0" dependencies = [ + "graphql_client", + "serde", "worker", ] diff --git a/Cargo.toml b/Cargo.toml index 75177de..73cf7c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] +graphql_client = "0.14.0" +serde = "1.0.200" worker = "0.2.0" [profile.release] diff --git a/gql/queries.graphql b/gql/queries.graphql new file mode 100644 index 0000000..643a314 --- /dev/null +++ b/gql/queries.graphql @@ -0,0 +1,26 @@ +query GetWorkersAnalytics($accountTag: string, $datetimeStart: string, $datetimeEnd: string, $scriptName: string) { + viewer { + accounts(filter: {accountTag: $accountTag}) { + workersInvocationsAdaptive(limit: 100, filter: { + scriptName: $scriptName, + datetime_geq: $datetimeStart, + datetime_leq: $datetimeEnd + }) { + sum { + subrequests + requests + errors + } + quantiles { + cpuTimeP50 + cpuTimeP99 + } + dimensions{ + datetime + scriptName + status + } + } + } + } + } diff --git a/src/gql.rs b/src/gql.rs new file mode 100644 index 0000000..9ded8d0 --- /dev/null +++ b/src/gql.rs @@ -0,0 +1,22 @@ +use graphql_client::GraphQLQuery; + +// The paths are relative to the directory where your `Cargo.toml` is located. +// Both json and the GraphQL schema language are supported as sources for the schema +#[derive(GraphQLQuery)] +#[graphql( +schema_path = "gql/schema.graphql", +query_path = "gql/queries.graphql", +)] +pub struct GetWorkersAnalytics; + +#[allow(non_camel_case_types)] +type float32 = f32; + +#[allow(non_camel_case_types)] +type string = String; + +#[allow(non_camel_case_types)] +type Time = String; + +#[allow(non_camel_case_types)] +type uint64 = u64; diff --git a/src/lib.rs b/src/lib.rs index 3272019..6d42b3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ +mod gql; + use worker::*; #[event(fetch)] -async fn main(req: Request, env: Env, ctx: Context) -> Result { +async fn main(_req: Request, _env: Env, _ctx: Context) -> Result { Response::ok("Hello, World!") }