From cb7752750930b14b5bf5ceb91a98fe8556e2da6c Mon Sep 17 00:00:00 2001 From: matthew Date: Sat, 8 Oct 2022 03:16:31 +0400 Subject: [PATCH] add state ton sites --- searching-front/app/core/commonTypes.ts | 3 + .../core/components/Footer/styles.module.css | 1 + .../app/core/components/Header/Header.tsx | 2 +- .../core/components/SearchForm/SearchForm.tsx | 2 - .../app/core/hooks/useCurrentTheme.ts | 6 +- .../app/core/pages/Search/Search.tsx | 24 +-- .../app/core/pages/State/State.tsx | 141 ++++++++++++++++++ searching-front/app/core/pages/State/index.ts | 1 + .../app/core/pages/State/styles.module.css | 67 +++++++++ searching-front/app/i18n/en.ts | 4 + searching-front/app/i18n/ru.ts | 3 + .../queries/getSearchRequests.ts | 2 +- .../stateSites/queries/getActualSitesState.ts | 11 ++ .../queries/getHistoryOfSitesState.ts | 6 + searching-front/package-lock.json | 58 +++++++ searching-front/package.json | 6 +- searching-front/pages/state.tsx | 58 +++++++ searching-front/services/influxTest.ts | 17 +++ .../services/modules/influxdb/helpers.ts | 77 +++++----- .../services/modules/influxdb/index.ts | 44 ++++-- .../services/modules/influxdb/types.ts | 11 +- 21 files changed, 471 insertions(+), 73 deletions(-) create mode 100644 searching-front/app/core/commonTypes.ts create mode 100644 searching-front/app/core/pages/State/State.tsx create mode 100644 searching-front/app/core/pages/State/index.ts create mode 100644 searching-front/app/core/pages/State/styles.module.css create mode 100644 searching-front/app/stateSites/queries/getActualSitesState.ts create mode 100644 searching-front/app/stateSites/queries/getHistoryOfSitesState.ts create mode 100644 searching-front/pages/state.tsx create mode 100644 searching-front/services/influxTest.ts diff --git a/searching-front/app/core/commonTypes.ts b/searching-front/app/core/commonTypes.ts new file mode 100644 index 0000000..1bb46aa --- /dev/null +++ b/searching-front/app/core/commonTypes.ts @@ -0,0 +1,3 @@ +export interface StaticPageProps{ + props: T +} \ No newline at end of file diff --git a/searching-front/app/core/components/Footer/styles.module.css b/searching-front/app/core/components/Footer/styles.module.css index e10e1bb..2df9fa4 100644 --- a/searching-front/app/core/components/Footer/styles.module.css +++ b/searching-front/app/core/components/Footer/styles.module.css @@ -1,4 +1,5 @@ .root { + margin-top: 30px; box-sizing: border-box; display: flex; justify-content: center; diff --git a/searching-front/app/core/components/Header/Header.tsx b/searching-front/app/core/components/Header/Header.tsx index 92df0a2..4c68a72 100644 --- a/searching-front/app/core/components/Header/Header.tsx +++ b/searching-front/app/core/components/Header/Header.tsx @@ -7,7 +7,7 @@ import s from "./styles.module.css" const Header = () => { const { route } = useRouter() - const shouldShowSearchForm = route === Routes.SearchPage().pathname + const shouldShowSearchForm = route !== Routes.Home().pathname const router = useRouter() const toMain = async () => { await router.push("/") diff --git a/searching-front/app/core/components/SearchForm/SearchForm.tsx b/searching-front/app/core/components/SearchForm/SearchForm.tsx index 4e133ac..6f27ee1 100644 --- a/searching-front/app/core/components/SearchForm/SearchForm.tsx +++ b/searching-front/app/core/components/SearchForm/SearchForm.tsx @@ -115,8 +115,6 @@ const SearchForm = () => { setInputFocused(true) }, []) - - debugger return ( diff --git a/searching-front/app/core/hooks/useCurrentTheme.ts b/searching-front/app/core/hooks/useCurrentTheme.ts index 6d7e96b..a1527a8 100644 --- a/searching-front/app/core/hooks/useCurrentTheme.ts +++ b/searching-front/app/core/hooks/useCurrentTheme.ts @@ -6,5 +6,9 @@ const COOKIE_NAME = "theme" export const useCurrentTheme = () => { const { cookies } = useContext(ServerSidePropsContext) - return jsCookies.get(COOKIE_NAME) || cookies[COOKIE_NAME] || "light" + if (cookies) { + return jsCookies.get(COOKIE_NAME) || cookies[COOKIE_NAME] || "light" + } else { + return "light" + } } diff --git a/searching-front/app/core/pages/Search/Search.tsx b/searching-front/app/core/pages/Search/Search.tsx index 3cb0061..df6e132 100644 --- a/searching-front/app/core/pages/Search/Search.tsx +++ b/searching-front/app/core/pages/Search/Search.tsx @@ -11,9 +11,9 @@ import s from "./styles.module.css" const Search = () => { const router = useRouter() - const {t} = useTranslation() + const { t } = useTranslation() const [page, setPage] = useState(0) - const text = router.query.query as string; + const text = router.query.query as string const [res] = useQuery( getSearchResult, { text, page }, @@ -33,17 +33,23 @@ const Search = () => { ))}
- + {res.pagesCount > 1 && ( + + )}
) } else if (res && !res.hits.length) { - return
{t("searchPage.notFound")}«{text}»
- + return ( +
+ {t("searchPage.notFound")} + «{text}» +
+ ) } return "loading" } diff --git a/searching-front/app/core/pages/State/State.tsx b/searching-front/app/core/pages/State/State.tsx new file mode 100644 index 0000000..6e9e15b --- /dev/null +++ b/searching-front/app/core/pages/State/State.tsx @@ -0,0 +1,141 @@ +import { Doughnut, Line } from "react-chartjs-2" +import { format } from "date-fns" +import { useQuery } from "@blitzjs/rpc" +import { useState } from "react" +import { useTranslation } from "react-i18next" +import { ChartData } from "chart.js/auto" + +import getActualSitesState from "app/stateSites/queries/getActualSitesState" +import getHistoryOfSitesState from "app/stateSites/queries/getHistoryOfSitesState" +import { InfluxPeriod } from "services/modules/influxdb/types" +import { cn } from "app/core/helpers/common" + +import s from "./styles.module.css" + +interface HistoryOfStateItem { + value: number + time: string +} + +interface HistoryOfState { + all: HistoryOfStateItem[] + available: HistoryOfStateItem[] +} + +export interface StatePageProps { + actualState: Awaited> + historyOfState: HistoryOfState +} + +const availableSitesColor = "#08c" +const allSitesColor = "hsl(200 15% 81% / 1)" + +const getDohnutData = (data: number[]) => ({ + labels: ["Available", "All"], + datasets: [ + { + data: data, + backgroundColor: [availableSitesColor, allSitesColor], + borderColor: [availableSitesColor, "rgba(0,0,0,0)"], + borderWidth: 1, + }, + ], +}) + +const liteOptions = { + responsive: true, + plugins: { + legend: { + display: false, + }, + tooltip: { + intersect: false, + }, + }, + + scales: { + x: { + display: false, + }, + }, +} +export const getGraphData = ( + data: HistoryOfStateItem[], + label: string, + color: string +): ChartData<"line", number[], string> => ({ + labels: data.map((item) => format(new Date(item.time), "dd.MM.yy hh:mm")), + datasets: [ + { + label, + data: data.map((item) => item.value), + backgroundColor: color, + borderColor: color, + tension: 0.3, + }, + ], +}) + +interface HistoryItemProps { + data: ChartData<"line", number[], string> + title: string +} + +const HistoryItem = ({ data, title }: HistoryItemProps) => { + return ( +
+
{title}
+ +
+ ) +} + +const State = ({ actualState, historyOfState: historyOfStatePreloaded }: StatePageProps) => { + const [historyPeriod, setHistoryPeriod] = useState(InfluxPeriod.D) + const { t } = useTranslation() + const [historyOfState] = useQuery(getHistoryOfSitesState, historyPeriod, { + suspense: false, + keepPreviousData: true, + initialData: historyOfStatePreloaded, + }) + console.log(historyPeriod, historyOfState) + return ( +
+
{t("state.title")}
+
+ +
+
+ {actualState.availableDomainsCount}{" "} + {t("state.siteOutOf")} {actualState.allDomainsCount} + {t("state.areNowAvailable")} +
+ {historyOfState && ( +
+
+ {Object.values(InfluxPeriod).map((i) => ( + + ))} +
+ + +
+ )} +
+ ) +} + +export default State diff --git a/searching-front/app/core/pages/State/index.ts b/searching-front/app/core/pages/State/index.ts new file mode 100644 index 0000000..eb89e89 --- /dev/null +++ b/searching-front/app/core/pages/State/index.ts @@ -0,0 +1 @@ +export { default } from "./State" diff --git a/searching-front/app/core/pages/State/styles.module.css b/searching-front/app/core/pages/State/styles.module.css new file mode 100644 index 0000000..4c59fcd --- /dev/null +++ b/searching-front/app/core/pages/State/styles.module.css @@ -0,0 +1,67 @@ +.root { + /* margin-right: var(--logoWrapperWidth); */ +} + +.actualStateWrapper { + font-size: 20px; + display: flex; + justify-content: center; + align-items: center; +} +.availableCount { + font-size: 100px; + margin-right: 10px; + color: var(--button_primary); + font-weight: bold; +} +.allCount { + margin: 0px 10px; + font-size: 30px; + color: var(--text_secondary); + font-weight: bold; +} +.areNowAvailable { +} +.doughnutAvailable { + width: 200px; + margin: auto; +} + +.title { + font-size: 40px; + text-align: center; + margin: auto; + margin-bottom: 20px; + /* font-weight: 500; */ +} + +.historyStateWrapper { + display: flex; + justify-content: space-around; + flex-wrap: wrap; + max-width: 1000px; + margin: auto; +} +.historyStateItem { + width: 400px; +} + +.historyStatePeriodsWrapper { + width: 100%; + display: flex; + justify-content: center; + margin-bottom: 10px; +} + +.historyStatePeriodsItem { + border-radius: 5px; + margin: 5px; + border: none; + background: rgba(0, 0, 0, 0.04); + cursor: pointer; +} + +.historyStatePeriodsItem.active { + background: var(--button_primary); + color: white; +} diff --git a/searching-front/app/i18n/en.ts b/searching-front/app/i18n/en.ts index e5d24fe..b6398aa 100644 --- a/searching-front/app/i18n/en.ts +++ b/searching-front/app/i18n/en.ts @@ -7,6 +7,10 @@ const en = { "footer.linkContacts": "Contact Us", "footer.linkAnnouncments": "Announcments", "footer.linkFeedback": "Feedback", + + "state.title": "State Of Ton Sites", + "state.siteOutOf": " sites out of ", + "state.areNowAvailable": " are now available!", }, } diff --git a/searching-front/app/i18n/ru.ts b/searching-front/app/i18n/ru.ts index f2c144a..88a6b55 100644 --- a/searching-front/app/i18n/ru.ts +++ b/searching-front/app/i18n/ru.ts @@ -7,6 +7,9 @@ const ru = { "footer.linkContacts": "Связаться с нами", "footer.linkAnnouncments": "Новости", "footer.linkFeedback": "Оставить обратную связь", + + "state.siteOutOf": " сайтов из ", + "state.areNowAvailable": " сейчас доступны!", }, } diff --git a/searching-front/app/search-requests/queries/getSearchRequests.ts b/searching-front/app/search-requests/queries/getSearchRequests.ts index a80bd42..5e6d15f 100644 --- a/searching-front/app/search-requests/queries/getSearchRequests.ts +++ b/searching-front/app/search-requests/queries/getSearchRequests.ts @@ -5,7 +5,7 @@ import { z } from "zod"; const GetSearchRequest = z.object({ // This accepts type of undefined, but is required at runtime - text: z.string() + text: z.string().optional() }); export default resolver.pipe( diff --git a/searching-front/app/stateSites/queries/getActualSitesState.ts b/searching-front/app/stateSites/queries/getActualSitesState.ts new file mode 100644 index 0000000..a602bcd --- /dev/null +++ b/searching-front/app/stateSites/queries/getActualSitesState.ts @@ -0,0 +1,11 @@ +import db from "db" + +export default async function getActualSitesState() { + const allDomainsCount = await db.nftDomain.count() + const availableDomainsCount = await db.nftDomain.count({ where: { available: true } }) + + return { + allDomainsCount, + availableDomainsCount, + } +} diff --git a/searching-front/app/stateSites/queries/getHistoryOfSitesState.ts b/searching-front/app/stateSites/queries/getHistoryOfSitesState.ts new file mode 100644 index 0000000..67b06b4 --- /dev/null +++ b/searching-front/app/stateSites/queries/getHistoryOfSitesState.ts @@ -0,0 +1,6 @@ +import influxdb from "services/modules/influxdb" +import { InfluxPeriod } from "services/modules/influxdb/types" + +export default async function getActualSitesState(period: InfluxPeriod = InfluxPeriod.W) { + return await influxdb.getHistoryOfState(period) +} diff --git a/searching-front/package-lock.json b/searching-front/package-lock.json index 4d0ce51..bf01a79 100644 --- a/searching-front/package-lock.json +++ b/searching-front/package-lock.json @@ -20,18 +20,22 @@ "@types/textversionjs": "1.1.1", "axios": "0.27.2", "blitz": "2.0.0-beta.3", + "chart.js": "3.9.1", "cheerio": "1.0.0-rc.12", "classnames": "2.3.1", + "date-fns": "2.29.3", "dotenv": "16.0.1", "framer-motion": "7.2.1", "html-to-text": "8.2.1", "i": "0.3.7", "i18next": "21.9.1", + "install": "0.13.0", "jsdom": "20.0.0", "next": "12.2.5", "node-fetch": "3.2.10", "npm": "8.18.0", "react": "18.2.0", + "react-chartjs-2": "4.3.1", "react-dom": "18.2.0", "react-hook-form": "7.34.2", "react-i18next": "11.18.6", @@ -4426,6 +4430,11 @@ "is-regex": "^1.0.3" } }, + "node_modules/chart.js": { + "version": "3.9.1", + "resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.9.1.tgz", + "integrity": "sha512-Ro2JbLmvg83gXF5F4sniaQ+lTbSv18E+TIf2cOeiH1Iqd2PGFOtem+DUufMZsCJwFE7ywPOpfXFBwRTGq7dh6w==" + }, "node_modules/cheerio": { "version": "1.0.0-rc.12", "resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.12.tgz", @@ -5261,6 +5270,18 @@ "node": ">=12" } }, + "node_modules/date-fns": { + "version": "2.29.3", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", + "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==", + "engines": { + "node": ">=0.11" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/date-fns" + } + }, "node_modules/debug": { "version": "4.3.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", @@ -8223,6 +8244,14 @@ "css-in-js-utils": "^2.0.0" } }, + "node_modules/install": { + "version": "0.13.0", + "resolved": "https://registry.npmjs.org/install/-/install-0.13.0.tgz", + "integrity": "sha512-zDml/jzr2PKU9I8J/xyZBQn8rPCAY//UOYNmR01XwNwyfhEWObo2SWfSl1+0tm1u6PhxLwDnfsT/6jB7OUxqFA==", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/internal-slot": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.3.tgz", @@ -13728,6 +13757,15 @@ "react-dom": ">= 16.14" } }, + "node_modules/react-chartjs-2": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/react-chartjs-2/-/react-chartjs-2-4.3.1.tgz", + "integrity": "sha512-5i3mjP6tU7QSn0jvb8I4hudTzHJqS8l00ORJnVwI2sYu0ihpj83Lv2YzfxunfxTZkscKvZu2F2w9LkwNBhj6xA==", + "peerDependencies": { + "chart.js": "^3.5.0", + "react": "^16.8.0 || ^17.0.0 || ^18.0.0" + } + }, "node_modules/react-dnd": { "version": "14.0.5", "resolved": "https://registry.npmjs.org/react-dnd/-/react-dnd-14.0.5.tgz", @@ -19794,6 +19832,11 @@ "is-regex": "^1.0.3" } }, + "chart.js": { + "version": "3.9.1", + "resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.9.1.tgz", + "integrity": "sha512-Ro2JbLmvg83gXF5F4sniaQ+lTbSv18E+TIf2cOeiH1Iqd2PGFOtem+DUufMZsCJwFE7ywPOpfXFBwRTGq7dh6w==" + }, "cheerio": { "version": "1.0.0-rc.12", "resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.12.tgz", @@ -20434,6 +20477,11 @@ "whatwg-url": "^11.0.0" } }, + "date-fns": { + "version": "2.29.3", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", + "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==" + }, "debug": { "version": "4.3.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", @@ -22515,6 +22563,11 @@ "css-in-js-utils": "^2.0.0" } }, + "install": { + "version": "0.13.0", + "resolved": "https://registry.npmjs.org/install/-/install-0.13.0.tgz", + "integrity": "sha512-zDml/jzr2PKU9I8J/xyZBQn8rPCAY//UOYNmR01XwNwyfhEWObo2SWfSl1+0tm1u6PhxLwDnfsT/6jB7OUxqFA==" + }, "internal-slot": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.3.tgz", @@ -26419,6 +26472,11 @@ "react-window": "^1.8.6" } }, + "react-chartjs-2": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/react-chartjs-2/-/react-chartjs-2-4.3.1.tgz", + "integrity": "sha512-5i3mjP6tU7QSn0jvb8I4hudTzHJqS8l00ORJnVwI2sYu0ihpj83Lv2YzfxunfxTZkscKvZu2F2w9LkwNBhj6xA==" + }, "react-dnd": { "version": "14.0.5", "resolved": "https://registry.npmjs.org/react-dnd/-/react-dnd-14.0.5.tgz", diff --git a/searching-front/package.json b/searching-front/package.json index 043009f..d176860 100644 --- a/searching-front/package.json +++ b/searching-front/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "scripts": { "watcher": "ts-node-esm ./services/main.ts", - "influx": "ts-node-esm ./services/influx.ts", + "influx": "ts-node-esm ./services/influxTest.ts", "parser": "ts-node-esm ./services/parser.ts", "dev": "blitz dev", "build": "blitz build", @@ -38,18 +38,22 @@ "@types/textversionjs": "1.1.1", "axios": "0.27.2", "blitz": "2.0.0-beta.3", + "chart.js": "3.9.1", "cheerio": "1.0.0-rc.12", "classnames": "2.3.1", + "date-fns": "2.29.3", "dotenv": "16.0.1", "framer-motion": "7.2.1", "html-to-text": "8.2.1", "i": "0.3.7", "i18next": "21.9.1", + "install": "0.13.0", "jsdom": "20.0.0", "next": "12.2.5", "node-fetch": "3.2.10", "npm": "8.18.0", "react": "18.2.0", + "react-chartjs-2": "4.3.1", "react-dom": "18.2.0", "react-hook-form": "7.34.2", "react-i18next": "11.18.6", diff --git a/searching-front/pages/state.tsx b/searching-front/pages/state.tsx new file mode 100644 index 0000000..8ee23a4 --- /dev/null +++ b/searching-front/pages/state.tsx @@ -0,0 +1,58 @@ +import { Suspense } from "react" + +import Layout from "app/core/layouts/Layout" +import getActualSitesState from "app/stateSites/queries/getActualSitesState" +import getHistoryOfSitesState from "app/stateSites/queries/getHistoryOfSitesState" +import { BlitzPage } from "@blitzjs/next" +import State from "app/core/pages/State" +import { gSP } from "app/blitz-server" + +import { ErrorBoundary } from "@blitzjs/next" + +import { + ServerSidePropsContext, +} from "app/core/contextProviders/serverSidePropsProvider" +import { StatePageProps } from "app/core/pages/State/State" +import { StaticPageProps } from "app/core/commonTypes" + +function ErrorFallback({ error, resetErrorBoundary }) { + return ( +
+

Something went wrong:

+
{error.message}
+ +
+ ) +} + +const StatePage: BlitzPage = (props) => { + return ( + { + // reset the state of your app so the error doesn't happen again + }} + > + + + + + + + + + ) +} + +export const getStaticProps = gSP(async ({ params, ctx }): StaticPageProps => { + const actualState = await getActualSitesState(); + const historyOfState = await getHistoryOfSitesState(); + return { + props: { + actualState, + historyOfState + }, + } +}) + +export default StatePage diff --git a/searching-front/services/influxTest.ts b/searching-front/services/influxTest.ts new file mode 100644 index 0000000..c6f8939 --- /dev/null +++ b/searching-front/services/influxTest.ts @@ -0,0 +1,17 @@ +import path from "path" +import dotenv from "dotenv" +dotenv.config({ path: path.resolve(__dirname, "../.env.local") }) + +import { Ctx } from "blitz" +import db from "db" + + +import { InfluxPeriod } from "./modules/influxdb/types" +import influxdb from "./modules/influxdb" + +export default async function run(period:InfluxPeriod = InfluxPeriod.W) { + const res = await influxdb.getHistoryOfState(period); + console.log(res) + +} +run() \ No newline at end of file diff --git a/searching-front/services/modules/influxdb/helpers.ts b/searching-front/services/modules/influxdb/helpers.ts index 671d520..f8e7500 100644 --- a/searching-front/services/modules/influxdb/helpers.ts +++ b/searching-front/services/modules/influxdb/helpers.ts @@ -1,46 +1,51 @@ -import { fluxDuration } from "@influxdata/influxdb-client"; -import { influxBucket, influxHost, influxPointName } from "./constants"; -import { InfluxField, InfluxPeriod } from "./types"; +import { fluxDuration } from "@influxdata/influxdb-client" +import { influxBucket, influxHost, influxPointName } from "./constants" +import { InfluxField, InfluxPeriod } from "./types" -export const influxQuery = (field: InfluxField, fetchPeriod: InfluxPeriod) =>{ - let start; - let period; +export const influxQuery = (field: InfluxField, fetchPeriod: InfluxPeriod) => { + let start + let period - switch(fetchPeriod){ - case InfluxPeriod.H: - start ='-1h' - period = '6m' - case InfluxPeriod.D: - start ='-1d' - period = '144m' - case InfluxPeriod.W: - start ='-1w' - period = '1008m' - case InfluxPeriod.M: - start ='-1mo' - period = '3d' - case InfluxPeriod.Y: - start ='-1y' - period = '36d' - case InfluxPeriod.tenminute: - start ='10m' - period = '1m' - } + switch (fetchPeriod) { + case InfluxPeriod.H: + start = "-1h" + period = "6m" + break + case InfluxPeriod.D: + start = "-1d" + period = "144m" + break + // case InfluxPeriod.W: + // start = "-1w" + // period = "1008m" + // break + // case InfluxPeriod.M: + // start = "-1mo" + // period = "3d" + // break + // case InfluxPeriod.Y: + // start = "-1y" + // period = "36d" + // break + // case InfluxPeriod.tenminute: + // start ='-10m' + // period = '1m' + } - const influxPeriod = fluxDuration(period); - const influxStart = fluxDuration(start); - return `from(bucket: "${influxBucket}") + const query = `from(bucket: "${influxBucket}") |> range(start: ${start}, stop: now()) |> filter(fn: (r) => r["host"] == "${influxHost}") |> filter(fn: (r) => r["_field"] == "${field}") |> filter(fn: (r) => r["_measurement"] == "${influxPointName}") |> aggregateWindow(every: ${period}, fn: mean, createEmpty: false) |> yield(name: "mean")` -} -export const processInfluxResult = (res:unknown[]) => { - return res.map(i=>({ - value: i._value, - time: i._time - })) -} \ No newline at end of file + return query +} + +export const processInfluxResult = (res: unknown[]) => { + return res.map((i) => ({ + value: i._value, + time: i._time, + })) +} diff --git a/searching-front/services/modules/influxdb/index.ts b/searching-front/services/modules/influxdb/index.ts index d6139ce..1f720d1 100644 --- a/searching-front/services/modules/influxdb/index.ts +++ b/searching-front/services/modules/influxdb/index.ts @@ -1,17 +1,17 @@ import { fluxDuration, InfluxDB } from "@influxdata/influxdb-client" import { Point } from "@influxdata/influxdb-client" import { influxBucket, influxHost, influxOrg, influxPointName, influxToken } from "./constants" -import { influxQuery, processInfluxResult } from "./helpers"; +import { influxQuery, processInfluxResult } from "./helpers" import { InfluxField, InfluxPeriod } from "./types" interface WriteSitesCounteParams { - all: number; - available: number; + all: number + available: number } interface QueryParams { - field: InfluxField; - period: InfluxPeriod; + field: InfluxField + period: InfluxPeriod } class InfluxDb { @@ -24,28 +24,40 @@ class InfluxDb { writeApi.useDefaultTags({ host: influxHost }) return writeApi } - writeSitesCount({all,available}:WriteSitesCounteParams){ + writeSitesCount({ all, available }: WriteSitesCounteParams) { const writeApi = this.getWriteApi() - const pointAll = new Point(influxPointName).intField(InfluxField.ALL_SITES,all); - const pointAvailable = new Point(influxPointName).intField(InfluxField.AVAILABLE_SITES,available); + const pointAll = new Point(influxPointName).intField(InfluxField.ALL_SITES, all) + const pointAvailable = new Point(influxPointName).intField( + InfluxField.AVAILABLE_SITES, + available + ) writeApi.writePoint(pointAll) writeApi.writePoint(pointAvailable) writeApi.close() } - async query({field,period}:QueryParams){ + async query({ field, period }: QueryParams) { const queryApi = this.client.getQueryApi(influxOrg) - const query = influxQuery(field, period); + const query = influxQuery(field, period) const result = await queryApi.collectRows(query) - console.log(processInfluxResult(result)); + return processInfluxResult(result); } - getAllSiteCounts(period:InfluxPeriod){ - - this.query({ - field:InfluxField.ALL_SITES, - period + + async getHistoryOfState(period: InfluxPeriod) { + const all = await this.query({ + field: InfluxField.ALL_SITES, + period, + }) + + const available = await this.query({ + field: InfluxField.AVAILABLE_SITES, + period, }) + return { + all, + available, + } } } diff --git a/searching-front/services/modules/influxdb/types.ts b/searching-front/services/modules/influxdb/types.ts index 2230c4c..9cb7aa1 100644 --- a/searching-front/services/modules/influxdb/types.ts +++ b/searching-front/services/modules/influxdb/types.ts @@ -1,10 +1,9 @@ export enum InfluxPeriod { - H='H', - D='D', - W='W', - M='M', - Y='Y', - tenminute='tenminute', + H='Hour', + D='Day', + // W='Week', + // M='Month', + // Y='Year', } export enum InfluxField {