fix(http): use BTreeMap instead of HashMap to preserve stats order

This commit is contained in:
Alexey Shekhirin 2021-04-13 11:58:22 +03:00
parent adfdb99abc
commit 9eaf048a06
No known key found for this signature in database
GPG Key ID: AF9A26AA133B5B98

View File

@ -1,18 +1,18 @@
use std::collections::HashMap; use std::collections::BTreeMap;
use std::iter::FromIterator;
use actix_web::get; use actix_web::get;
use actix_web::web; use actix_web::web;
use actix_web::HttpResponse; use actix_web::HttpResponse;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use milli::FieldsDistribution;
use serde::Serialize; use serde::Serialize;
use crate::data::Stats;
use crate::error::ResponseError; use crate::error::ResponseError;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::index_controller::IndexStats; use crate::index_controller::IndexStats;
use crate::routes::IndexParam; use crate::routes::IndexParam;
use crate::Data; use crate::Data;
use crate::data::Stats;
pub fn services(cfg: &mut web::ServiceConfig) { pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(get_index_stats) cfg.service(get_index_stats)
@ -25,7 +25,7 @@ pub fn services(cfg: &mut web::ServiceConfig) {
struct IndexStatsResponse { struct IndexStatsResponse {
number_of_documents: u64, number_of_documents: u64,
is_indexing: bool, is_indexing: bool,
fields_distribution: FieldsDistribution, fields_distribution: BTreeMap<String, u64>,
} }
impl From<IndexStats> for IndexStatsResponse { impl From<IndexStats> for IndexStatsResponse {
@ -33,7 +33,7 @@ impl From<IndexStats> for IndexStatsResponse {
Self { Self {
number_of_documents: stats.number_of_documents, number_of_documents: stats.number_of_documents,
is_indexing: stats.is_indexing, is_indexing: stats.is_indexing,
fields_distribution: stats.fields_distribution, fields_distribution: BTreeMap::from_iter(stats.fields_distribution.into_iter()),
} }
} }
} }
@ -53,7 +53,7 @@ async fn get_index_stats(
struct StatsResponse { struct StatsResponse {
database_size: u64, database_size: u64,
last_update: Option<DateTime<Utc>>, last_update: Option<DateTime<Utc>>,
indexes: HashMap<String, IndexStatsResponse>, indexes: BTreeMap<String, IndexStatsResponse>,
} }
impl From<Stats> for StatsResponse { impl From<Stats> for StatsResponse {
@ -61,7 +61,8 @@ impl From<Stats> for StatsResponse {
Self { Self {
database_size: stats.database_size, database_size: stats.database_size,
last_update: stats.last_update, last_update: stats.last_update,
indexes: stats.indexes indexes: stats
.indexes
.into_iter() .into_iter()
.map(|(uid, index_stats)| (uid, index_stats.into())) .map(|(uid, index_stats)| (uid, index_stats.into()))
.collect(), .collect(),