diff --git a/src/constants/api.ts b/src/constants/api.ts index 1b03bc3e..b89a13a6 100644 --- a/src/constants/api.ts +++ b/src/constants/api.ts @@ -37,6 +37,6 @@ export const API = { YOUTUBE: '/embed/youtube', }, BORIS: { - GET_GIT_LOG: 'http://vault48.org/stats/git.json', + GET_BACKEND_STATS: '/stats', }, }; diff --git a/src/redux/boris/api.ts b/src/redux/boris/api.ts index 5d2888fb..3a8bd25f 100644 --- a/src/redux/boris/api.ts +++ b/src/redux/boris/api.ts @@ -1,3 +1,13 @@ import git from '~/stats/git.json'; +import { API } from '~/constants/api'; +import { api, resultMiddleware, errorMiddleware } from '~/utils/api'; +import { IBorisState, IStatBackend } from './reducer'; +import { IResultWithStatus } from '../types'; -export const getBorisGitStats = (): Promise => Promise.resolve(git); +export const getBorisGitStats = (): Promise => Promise.resolve(git); + +export const getBorisBackendStats = (): Promise> => + api + .get(API.BORIS.GET_BACKEND_STATS) + .then(resultMiddleware) + .catch(errorMiddleware); diff --git a/src/redux/boris/reducer.ts b/src/redux/boris/reducer.ts index 85b306e1..03bea94c 100644 --- a/src/redux/boris/reducer.ts +++ b/src/redux/boris/reducer.ts @@ -7,9 +7,31 @@ export type IStatGitRow = { timestamp: string; }; +export type IStatBackend = { + users: { + total: number; + alive: number; + }; + nodes: { + images: number; + audios: number; + videos: number; + texts: number; + total: number; + }; + comments: { + total: number; + }; + files: { + count: number; + size: number; + }; +}; + export type IBorisState = Readonly<{ stats: { git: IStatGitRow[]; + backend: IStatBackend; is_loading: boolean; }; }>; @@ -17,6 +39,7 @@ export type IBorisState = Readonly<{ const BORIS_INITIAL_STATE: IBorisState = { stats: { git: [], + backend: null, is_loading: false, }, }; diff --git a/src/redux/boris/sagas.ts b/src/redux/boris/sagas.ts index a2df8fb1..1f92b6a3 100644 --- a/src/redux/boris/sagas.ts +++ b/src/redux/boris/sagas.ts @@ -1,16 +1,21 @@ import { takeLatest, put, call } from 'redux-saga/effects'; import { BORIS_ACTIONS } from './constants'; -import { borisSet, borisSetStats } from './actions'; -import { getBorisGitStats } from './api'; +import { borisSetStats } from './actions'; +import { getBorisGitStats, getBorisBackendStats } from './api'; +import { Unwrap } from '../types'; function* loadStats() { yield put(borisSetStats({ is_loading: true })); try { - const git = yield getBorisGitStats(); - yield put(borisSetStats({ git, is_loading: false })); + const git: Unwrap> = yield call(getBorisGitStats); + const backend: Unwrap> = yield call( + getBorisBackendStats + ); + + yield put(borisSetStats({ git, backend: backend.data, is_loading: false })); } catch (e) { - yield put(borisSetStats({ git: [], is_loading: false })); + yield put(borisSetStats({ git: [], backend: null, is_loading: false })); } } diff --git a/src/redux/types.ts b/src/redux/types.ts index 4b4bfa74..786fec6f 100644 --- a/src/redux/types.ts +++ b/src/redux/types.ts @@ -49,7 +49,7 @@ export interface IApiErrorResult { export interface IResultWithStatus { status: any; - data?: Partial & IApiErrorResult; + data?: T & IApiErrorResult; error?: string; debug?: string; }