File size: 7,971 Bytes
7a511fb
 
 
 
 
 
 
 
 
 
 
 
 
 
bfb0701
7a511fb
 
 
 
 
 
 
 
 
 
 
 
 
 
bfb0701
 
 
7a511fb
 
 
 
 
bfb0701
 
 
 
 
 
 
7a511fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfb0701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a511fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfb0701
 
 
 
 
7a511fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfb0701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a511fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfb0701
 
 
 
 
7a511fb
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from uuid import UUID
import uuid

from sqlalchemy import select
from src.repositories import BaseRepository

from src.models import (
    AnalysisType,
    Analysis,
    Proposal,
    ComparativeWeights,
    Weights,
    Evaluations,
    EvaluationType,
    AnalysisStatus,
)


class ScoreClient:
    def __init__(self):
        self.repository = BaseRepository
        self.weighted_scores = {"NONE": 0, "LOW": 1, "MEDIUM": 3, "HIGH": 7}

    async def __aenter__(self):
        return self

    async def __aexit__(self, exc_type, exc_value, traceback):
        pass

    async def update_ai_score(
        self, evaluation_id: str, analysis_score_type: str = None
    ):
        async with self.repository(model=Evaluations).get_session() as session:
            query = select(Evaluations).where(Evaluations.id == evaluation_id)
            result = await session.execute(query)
            evaluation = result.scalars().first()
            proposal_id = evaluation.proposal_id

        async with self.repository(model=Proposal).get_session() as session:
            query = select(Proposal).where(Proposal.id == proposal_id)
            result = await session.execute(query)
            proposal = result.scalars().first()
            rfp_id = proposal.rfp_id

        async with self.repository(model=Analysis).get_session() as session:
            query = select(Analysis).where(Analysis.evaluation_id == evaluation_id)
            result = await session.execute(query)
            results = result.scalars().all()
            deficiency = [
                result.insights
                for result in results
                if result.analysis_type == AnalysisType.DEFICIENCIES
            ]
            weaknesses = [
                result.insights
                for result in results
                if result.analysis_type == AnalysisType.WEAKNESSES
            ]
            strengths = [
                result.insights
                for result in results
                if result.analysis_type == AnalysisType.STRENGTHS
            ]
            if analysis_score_type != "AI":
                deficiency = [
                    result.insights
                    for result in results
                    if result.analysis_type == AnalysisType.DEFICIENCIES
                    and result.status != AnalysisStatus.REJECTED
                ]
                weaknesses = [
                    result.insights
                    for result in results
                    if result.analysis_type == AnalysisType.WEAKNESSES
                    and result.status != AnalysisStatus.REJECTED
                ]
                strengths = [
                    result.insights
                    for result in results
                    if result.analysis_type == AnalysisType.STRENGTHS
                    and result.status != AnalysisStatus.REJECTED
                ]
            start_score = 0 if deficiency else 100

        async with self.repository(model=ComparativeWeights).get_session() as session:
            query = select(ComparativeWeights).where(
                ComparativeWeights.rfp_id == rfp_id
            )
            result = await session.execute(query)
            comparative_weights = result.scalars().first()

        start_score += len(strengths) * comparative_weights.strengths_weight
        start_score -= len(weaknesses) * comparative_weights.weaknesses_weight

        async with self.repository(model=Evaluations).get_session() as session:
            query = select(Evaluations).where(Evaluations.id == evaluation_id)
            result = await session.execute(query)
            evaluation = result.scalars().first()
            if analysis_score_type == "AI":
                evaluation.ai_score = start_score
                evaluation.adjusted_score = start_score
            else:
                evaluation.adjusted_score = start_score
            await session.commit()
            await session.refresh(evaluation)
            query = select(Evaluations).where(Evaluations.proposal_id == proposal_id)
            result = await session.execute(query)
            evaluations = result.scalars().all()

        technical_ai_score = [
            evaluation.ai_score
            for evaluation in evaluations
            if evaluation.evaluation_type == EvaluationType.TECHNICAL
            and evaluation.ai_score is not None
        ]
        management_ai_score = [
            evaluation.ai_score
            for evaluation in evaluations
            if evaluation.evaluation_type == EvaluationType.MANAGEMENT
            and evaluation.ai_score is not None
        ]
        past_performance_ai_score = [
            evaluation.ai_score
            for evaluation in evaluations
            if evaluation.evaluation_type == EvaluationType.PAST_PERFORMANCE
            and evaluation.ai_score is not None
        ]
        price_ai_score = [
            evaluation.ai_score
            for evaluation in evaluations
            if evaluation.evaluation_type == EvaluationType.PRICE
            and evaluation.ai_score is not None
        ]

        if analysis_score_type != "AI":
            technical_ai_score = [
                evaluation.adjusted_score
                for evaluation in evaluations
                if evaluation.evaluation_type == EvaluationType.TECHNICAL
                and evaluation.adjusted_score is not None
            ]
            management_ai_score = [
                evaluation.adjusted_score
                for evaluation in evaluations
                if evaluation.evaluation_type == EvaluationType.MANAGEMENT
                and evaluation.adjusted_score is not None
            ]
            past_performance_ai_score = [
                evaluation.adjusted_score
                for evaluation in evaluations
                if evaluation.evaluation_type == EvaluationType.PAST_PERFORMANCE
                and evaluation.adjusted_score is not None
            ]
            price_ai_score = [
                evaluation.adjusted_score
                for evaluation in evaluations
                if evaluation.evaluation_type == EvaluationType.PRICE
                and evaluation.adjusted_score is not None
            ]

        technical_ai_score = technical_ai_score[0] if technical_ai_score else 0
        management_ai_score = management_ai_score[0] if management_ai_score else 0
        past_performance_ai_score = (
            past_performance_ai_score[0] if past_performance_ai_score else 0
        )
        price_ai_score = price_ai_score[0] if price_ai_score else 0

        technical_weight = self.weighted_scores[
            comparative_weights.technical_weight.name
        ]
        management_weight = self.weighted_scores[
            comparative_weights.management_weight.name
        ]
        past_performance_weight = self.weighted_scores[
            comparative_weights.past_performance_weight.name
        ]
        price_weight = self.weighted_scores[comparative_weights.price_weight.name]

        ai_score = (
            technical_ai_score * technical_weight
            + management_ai_score * management_weight
            + past_performance_ai_score * past_performance_weight
            + price_ai_score * price_weight
        )
        ai_score /= (
            technical_weight
            + management_weight
            + past_performance_weight
            + price_weight
        )

        async with self.repository(model=Proposal).get_session() as session:
            query = select(Proposal).where(Proposal.id == proposal_id)
            result = await session.execute(query)
            evaluation = result.scalars().first()
            if analysis_score_type == "AI":
                evaluation.ai_score = ai_score
                evaluation.final_score = ai_score
            else:
                evaluation.final_score = ai_score
            await session.commit()
            await session.refresh(evaluation)