1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use gitql_ast::object::GQLObject;
use gitql_ast::types::DataType;

use lazy_static::lazy_static;
use std::collections::HashMap;

type Aggregation = fn(&String, &Vec<GQLObject>) -> String;

pub struct AggregationPrototype {
    pub parameter: DataType,
    pub result: DataType,
}

lazy_static! {
    pub static ref AGGREGATIONS: HashMap<&'static str, Aggregation> = {
        let mut map: HashMap<&'static str, Aggregation> = HashMap::new();
        map.insert("max", aggregation_max);
        map.insert("min", aggregation_min);
        map.insert("sum", aggregation_sum);
        map.insert("avg", aggregation_average);
        map.insert("count", aggregation_count);
        map
    };
}

lazy_static! {
    pub static ref AGGREGATIONS_PROTOS: HashMap<&'static str, AggregationPrototype> = {
        let mut map: HashMap<&'static str, AggregationPrototype> = HashMap::new();
        map.insert(
            "max",
            AggregationPrototype {
                parameter: DataType::Number,
                result: DataType::Number,
            },
        );
        map.insert(
            "min",
            AggregationPrototype {
                parameter: DataType::Number,
                result: DataType::Number,
            },
        );
        map.insert(
            "sum",
            AggregationPrototype {
                parameter: DataType::Any,
                result: DataType::Number,
            },
        );
        map.insert(
            "avg",
            AggregationPrototype {
                parameter: DataType::Any,
                result: DataType::Number,
            },
        );
        map.insert(
            "count",
            AggregationPrototype {
                parameter: DataType::Any,
                result: DataType::Number,
            },
        );
        map
    };
}

fn aggregation_max(field_name: &String, objects: &Vec<GQLObject>) -> String {
    let mut max_length: i64 = 0;
    for object in objects {
        let field_value = &object.attributes.get(field_name).unwrap();
        let int_value = field_value.parse::<i64>().unwrap();
        if int_value > max_length {
            max_length = int_value;
        }
    }
    return max_length.to_string();
}

fn aggregation_min(field_name: &String, objects: &Vec<GQLObject>) -> String {
    let mut max_length: i64 = 0;
    for object in objects {
        let field_value = &object.attributes.get(field_name).unwrap();
        let int_value = field_value.parse::<i64>().unwrap();
        if int_value < max_length {
            max_length = int_value;
        }
    }
    return max_length.to_string();
}

fn aggregation_sum(field_name: &String, objects: &Vec<GQLObject>) -> String {
    let mut sum: i64 = 0;
    for object in objects {
        let field_value = &object.attributes.get(field_name).unwrap();
        sum += field_value.parse::<i64>().unwrap();
    }
    return sum.to_string();
}

fn aggregation_average(field_name: &String, objects: &Vec<GQLObject>) -> String {
    let mut sum: i64 = 0;
    let count: i64 = objects.len().try_into().unwrap();
    for object in objects {
        let field_value = &object.attributes.get(field_name).unwrap();
        sum += field_value.parse::<i64>().unwrap();
    }
    let avg = sum / count;
    return avg.to_string();
}

fn aggregation_count(_field_name: &String, objects: &Vec<GQLObject>) -> String {
    return objects.len().to_string();
}