Commit Graph

44 Commits

Author SHA1 Message Date
Javi Martín
d18c627392 Add and apply Layout/EmptyLinesAfterModuleInclusion rule
This rule was added in rubocop 1.79. We were inconsistent about it, so
we're adding it to get more consistency.
2025-11-05 14:27:12 +01:00
Javi Martín
048bdb2e9e Add and apply Rails/OrderArguments rubocop rule
This rule was introduced in rubocop-rails 2.33. We were following it
most of the time.
2025-11-05 11:51:23 +01:00
Javi Martín
2abe9f27b5 Use ranges instead of comparisons in SQL queries
These cases aren't covered by the `Rails/WhereRange` rubocop rule, but
IMHO using ranges makes them more consistent. Besides, they generate SQL
which is more consistent with what Rails usually generates. For example,
`Poll.where("starts_at <= :time and ends_at >= :time", time:
Time.current)` generates:

```
SELECT \"polls\".\"id\", (...) WHERE \"polls\".\"hidden_at\" IS NULL AND
(starts_at <= '2024-07-(...)' and ends_at >= '2024-07-(...)')
```

And `Poll.where(starts_at: ..Time.current, ends_at: Time.current..)`
generates:

```
SELECT \"polls\".\"id\", (...) WHERE \"polls\".\"hidden_at\" IS NULL AND
\"polls\".\"starts_at\" <= '2024-07-(...)' AND \"polls\".\"ends_at\" >=
'2024-07-(...)'"
```

Note that the `not_archived` scope in proposals slightly changes, since
we were using `>` and now we use the equivalent of `>=`. However, since
the `created_at` field is a time, this will only mean that a proposal
will be archived about one microsecond later.

For consistency, we're also changing the `archived` scope, so a proposal
is never archived and not archived at the same time (not even for a
microsecond).
2024-07-05 17:24:56 +02:00
Javi Martín
a4461a1a56 Expire the stats cache once per day
When we first started caching the stats, generating them was a process
that took several minutes, so we never expired the cache.

However, there have been cases where we run into issues where the stats
shown on the screen were outdated. That's why we introduced a task to
manually expire the cache.

But now, generating the stats only takes a few seconds, so we can
automatically expire them every day, remove all the logic needed to
manually expire them, and get rid of most of the issues related to the
cache being outdated.

We're expiring them every day because it's the same day we were doing in
public stats (which we removed in commit 631b48f58), only we're using
`expires_at:` to set the expiration time, in order to simplify the code.

Note that, in the test, we're using `travel_to(time)` so the test passes
even when it starts an instant before midnight. We aren't using
`:with_frozen_time` because, in similar cases (although not in this
case, but I'm not sure whether that's intentional), `travel_to` shows
this error:

> Calling `travel_to` with a block, when we have previously already made
> a call to `travel_to`, can lead to confusing time stubbing.
2024-05-17 20:11:16 +02:00
Javi Martín
653848fc4e Extract method to get the stats key in stats
This way we remove a bit of duplication and it'll be easier to change
the `stats_cache` method.
2024-05-17 16:08:08 +02:00
Javi Martín
1d85a63e7c Calculate age stats based on the participation date
We were calculating the age stats based on the age of the users who
participated... at the moment where we were calculating the stats. That
means that, if 20 years ago, 1000 people who were 16 years old
participated, they would be shown as having 36 years in the stats.

Instead, we want to show the stats at the time when the process took
place, so we're implementing a `participation_date` method.

Note that, for polls, we could actually use the `age` column in the
`poll_voters` table. However, doing so would be harder, would only work
for polls but not for budgets, and it wouldn't be statistically very
relevant, since the stats are shown by age groups, and only a small
percentage of people would change their age group (and only to the
nearest one) between the time they participate and the time the process
ends.

We might use the `poll_voters` table in the future, though, since we
have a similar issue with geozones and genders, and using the
information in `poll_voters` would solve it as well (only for polls,
though).

Also note that we're using the `ends_at` dates because some people but
be too young to vote when a process starts but old enough to vote when
the process ends.

Finally, note that we might need to change the way we calculate the
participation date for a budget, since some budgets might not enabled
every phase. Not sure how stats work in that scenario (even before these
changes).
2024-05-13 15:42:37 +02:00
Javi Martín
f87d4b589d Add and apply Naming/BlockForwarding rubocop rule
This syntax has been added in Ruby 3.1.

Not using a variable name might not be very descriptive, but it's just
as descriptive as using "block" as a variable name. Using just `&` we
get the same amount of information than using `&block`: that we're
passing a block.

We're still using `&action` in `around_action` methods because here we
aren't using a generic name for the variable, so (at least for now) we
aren't running this cop on controllers using `around_action`.
2023-09-12 15:17:28 +02:00
Javi Martín
5b6de96241 Add and apply MultilineMethodCallIndentation rule 2023-08-18 14:56:16 +02:00
Javi Martín
e51e034468 Use budget stats model to calculate admin stats
This way we reduce duplication.
2023-02-20 14:21:22 +01:00
Javi Martín
e619ca992c Add and apply Performance/Sum rubocop rule
We're not adding it for performance reasons but because it simplifies
the code.
2021-08-10 13:30:07 +02:00
Javi Martín
eafb4018bf Apply Style/CollectionMethods rubocop rule
We were already using `map` and `reduce` almost everywhere.
2019-10-26 13:03:49 +02:00
Javi Martín
d0d681a44b Add and apply EmptyLineAfterGuardClause rule
We were inconsistent on this one. I consider it particularly useful when
a method starts with a `return` statement.

In other cases, we probably shouldn't have a guard rule in the middle of
a method in any case, but that's a different refactoring.
2019-10-24 17:56:03 +02:00
Javi Martín
f9ed186909 Add rubocop spacing rules
We were following these rules in most places; we just didn't define them
anywhere.
2019-09-10 21:04:56 +02:00
Javi Martín
aa0e813970 Use ruby cache for stats helper methods
These methods are only used while stats are being generated; once stats
are generated, they aren't used anymore. So there's no need to store
them using the Dalli cache.

Furthermore, there are polls (and even budgets) with hundreds of
thousands of participants. Calculating stats for them takes a very long
time because we can't store all those records in the Dalli cache.

However, since these records aren't used once the stats are generated,
we can store them in an instance variable while we generate the stats,
speeding up the process.
2019-05-21 13:50:19 +02:00
Javi Martín
7c0e499eee Add table to store stats versions
We need a way to manually expire the cache for a budget or poll without
expiring the cache of every budget or poll.

Using the `updated_at` column would be dangerous because most of the
times we update a budget or a poll, we don't need to regenerate their
stats.

We've considered adding a `stats_updated_at` column to each of these
tables. However, in that case we would also need to add a similar column
in the future to every process type whose stats we want to generate.
2019-05-21 13:50:19 +02:00
Javi Martín
1f4707facd Extract method to get stats participant_ids
This way we can share the `participants` method between budget and poll
stats.
2019-05-21 13:50:19 +02:00
Javi Martín
04a5e38874 Expire budget stats cache when a phase is finished
This way we recalculate all data including the participants in the phase
which has just finished.
2019-05-21 13:50:19 +02:00
Javi Martín
66ecb2835b Show only available budget stats phases
This way we can show statistics for the supports phase before the vote
phase is over.
2019-05-21 13:50:18 +02:00
Javi Martín
bf2292ba18 Rename budget stats "all phase" to "every phase"
"All phase" doesn't sound right in English, and we're going to refactor
the code related to the phases.
2019-05-21 13:50:18 +02:00
Javi Martín
a13a8a2270 Simplify code to calculate supports
The code is easier to read now, it returns the same results it used to
return, and performance-wise it's probably the same thing, but if it's
not, we'll trust Rails will do optimizations that we don't when we
manually pluck the IDs.
2019-05-21 13:50:18 +02:00
Javi Martín
7a79c36137 Select only distinct voters/balloters in stats
It is way more efficient because we're caching the result of that
method, and this way we only store each voter once in the cache. We were
storing many voters several times and then we were filtering them with
`uniq`.
2019-05-21 13:50:18 +02:00
Javi Martín
76c7827cf4 Use stats objects instead of hashes
It will make it far easier to call other methods on the stats object,
and we're already caching the methods.

We had to remove the view fragment caching because the stats object
isn't as easy to cache. The good thing about it is the view will
automatically be updated when we change logic regarding which stats to
show, and the methods taking long to execute are cached in the model.
2019-05-21 13:50:18 +02:00
Javi Martín
dc077bb03c Simplify budget stats in voting phase
So we only show the total number of participants, and not divided by web
participants and booth participants.
2019-05-21 13:50:17 +02:00
Javi Martín
d42454a1a8 Include booth users in participants in vote phase
We were expecting `balloters` to include `poll_ballot_voters` (that's
why we're substracting them to calculate web participants), but reality
has proven `poll_ballot_voters` aren't included in `balloters`.
2019-05-21 13:50:17 +02:00
Javi Martín
cf32cc940b Exclude nil balloters from vote phase participants
The `user_id` can be `nil` for ballots.
2019-05-21 13:50:17 +02:00
Javi Martín
0a578f2775 Create less records in budget stats specs
Creating only the necessary data for each test makes tests easier to
read. It also makes tests slightly faster.
2019-05-21 13:50:17 +02:00
Javi Martín
e4a032ee68 Split common and specific stats methods 2019-05-21 13:49:42 +02:00
Javi Martín
5d2f5d1d81 Move gender and age methods to a common concern
These are generic methods which only depend on the participants.
2019-05-21 13:49:42 +02:00
Javi Martín
ccaa2e1a77 Remove duplication to calculate percentage 2019-05-21 13:49:42 +02:00
Javi Martín
188278296c Simplify the way we cache stats 2019-05-21 13:48:54 +02:00
Javi Martín
d627215af4 Use symbols for method names 2019-05-21 13:27:03 +02:00
Javi Martín
313ffb589b Share method to generate stats 2019-05-21 13:27:03 +02:00
Javi Martín
62a97f9003 Add a common concern for budget and poll stats 2019-05-21 13:27:03 +02:00
María Checa
fac99cfb10 Add web and booth participants in budget stats 2019-05-16 22:01:42 +02:00
María Checa
3291b3274a Added physical votes to stats 2019-05-16 22:01:42 +02:00
Bertocq
4b2a2dd995 Improve percent round 2019-05-16 22:01:42 +02:00
Bertocq
be89289b09 Improve budget stats page with a new row showing each column totals 2019-05-16 22:01:42 +02:00
rgarcia
50c0f5f287 more efficient calculation of ballot lines 2019-05-16 22:01:42 +02:00
rgarcia
e5ec0bf6bb counts votes instead of ballots 2019-05-16 22:01:42 +02:00
Bertocq
cdece48898 Order Budget headings by id when calculating heading stats 2019-05-16 22:01:42 +02:00
Bertocq
1f3bc2c8d9 Switch from feasible to selected investments on budget stats 2019-05-16 22:01:42 +02:00
rgarcia
f9e45dc2e2 displays selected instead of feasible investments 2019-05-16 22:01:42 +02:00
rgarcia
ef958aae59 adds version to cache 2019-05-16 22:01:42 +02:00
Bertocq
132397b610 Add Budget Stats class 2019-05-16 22:01:42 +02:00