Rails: how to save statistics based on some conditions - ruby-on-rails

I'm developing a Rails application and I need to save and show some statistics based on the following conditions:
For the current month, display all entries in the database
For the last 8 months, display a cumulative of each month
For the rest, show a cumulative of all values
The thing is that I don't want to save all the data in my database and than apply the condition when showing. Is there a way of keeping my database structured in a way that it also covers the conditions (for example, when the current month changes, apply all the conditions and update the database)?

Related

Combining a dynamic total by state in Tableau

I am using a US shootings database where the event is specified by 3 columns, state event occured, date, total casualties. I want to make a dashboard in Tableau that has dynamic sum and sorting where if the year column was a page that I could click through, the graphic would reflect the top ten states of sum(casualties) of that specified range. So my data ranges from 1924-2022, and if I started the page at 1980, it would graph the top 10 states with the sum of totals between 1924-1980. The next page could potentially be a different top 10 of states and would reflect the current top 10 states as the sum(casualties) from 1924-1981.
I hope this makes sense. I apologize if it does not as I am just starting out. I did attempt to sort the data in python by making a column for each year, and you could move horizontally along a state to see it's totals change as each year goes by. Would it be best to add these year columns as a group and sort by top 10 and year that way?
Edit:
Attempting to click through the year filter and dynamically sort the graph by top 10 states with total shootings from 1924-current year
Can you use SQL to query the database? If so, you can insert a date parameter in the query that replaces the use of pages to calculate the sum based on the earliest datapoint to the year you have selected and then just click show parameter. When you create the parameter make sure you set step size to one year and switch the parameter to slider. You can also create a state parameter that is based off that column of data and then use it to filter the data in SQL. However, you may need to use a calculated field or SQL case statements if you want to change the granularity from all the states to one selected state.
I hope this helps you. If you need more clarification, please comment and I will try to provide an example.

Update several Tbl Fields based on Start/End Dates

Good day to everyone! Hope all is well!
I am looking to run an update query or a group of queries that looks at my Date_Start and Date_End to determine if the Units (quantity of the respective record) fall in my defined current quarter 1/2/3/4 from another table (this table is a master table I’m using to provide the dates that I need to consider for defining the quarters).
I’ve been able to create queries that do this and then join them together to basically display the units out by quarter based on their respective start/end dates. The problem I am running into is this process takes a decent amount of time for the queries to populate that will drastically effect other processes down the line.
Thus we get to my desire. I am trying to no avail to create an update query that will update the quarter fields in my table based off of the queries I built to determine if the records start/end date fall in the respective quarter. I figure that running this update when records change will be an ok run time vs when I’m running reports or running an email script for the reports.
I have tried pulling in the table and query, joining them as equal on ID (the query pulls in the table's IDs), and selecting my field “CQ1” from the table, and setting the Update ether the respective field from the table or the query (which is the same as the field in the table).
All I get are the current values of the field in the data sheet view and an error of “Operation must use an updateable query.”
I have even tried placing a zero to see if that would do it with no luck. I have verified that all the fields are the same data type.
What am I doing wrong? Thanks!
Apologies to everyone.....I think my conscious brain was trying to overly complicate the process and while talking to a buddy about my issue distractedly created a new update query that worked. It all tied down to that I forgot to put a criteria on my quarter filed of is not null I believe. Thanks for anyone that has read this and is responding while I’m typing this or for those of you formulating a response.

Storing large amount of boolean values in Rails

I am to store quite large amount of boolean values in database used by Rails application - it needs to store 60 boolean values in single record per day. What is best way to do this in Rails?
Queries that I will need to program or execute:
* CRUD
* summing up how many true values are for each day
* possibly (but not nessesarily) other reports like how often true is recorded in each of field
UPDATE: This is to store events that may or may not occur in 5 minute intervals between 9am and 1pm. If it occurs, then I need to set it to true, if not then false. Measurements are done manually and users will be reporting these information using checkboxes on the website. There might be small updates, but most of the time it's just one time entry and then queries as listed above.
UPDATE 2: 60 values per day is per one user, there will be between 1000-2000 users. If there isn't some library that helps with that, I will go for simplest approach and deal with it later if I will get issues with performance. Every day user reports events by checking desired checkboxes on the website, so there is normally a single data entry moment per day (or few if not done on daily basis).
This is dependent on a lot of different things. Do you need callbacks to run? Do you need AR objects instantiated? What is the frequency of these updates? Is it done frequently but not many at a time or rarely but a bunch at once? Could you represent these booleans as a mask instead? We definitely need more context.
Why do these need to be in a single record? Can't you use a 'days' table to tie them all together, then use a day_id column in your 'events' table?
Specify in the Day model that it 'has_many :events' and specify in the Event model file that it 'belongs_to :day'. Then you can find all the events for a day with just the id for the day.
For the third day record, you'd do this:
this_day = Day.find 3
Then you can you use 'this_day.events' to get all the events for that day.
You'll need to decide what you wish to use to identify each day so you query for a day's events using something that you understand. The id column I used above to find it probably won't work.
You could use the timestamp first moment of each day to do that, for example. Or you could rely upon the 'created_at' column of the table to be between the start and end of a day
And you'll want to be sure to thing about what time zone you are using and how this will be stored in the database.
And if your data will be stored close to midnight, daylight savings time could also be an issue. I find it best to use GMT to avoid that issue.
Good luck.

Efficiently plot database activity as a time series

I am working on a system where 200,000+ records have been created in the past year and I need to plot their creation on a time series with various added filters. At this present, this requires performing lots of count queries (30 for each month plotted). How should these dates be stored for maximum speed?
One idea: store the most commonly-visualized data in a number of serialized fields containing counts for each day over the past month. Update each day with cron and serve up as necessary. (Where should these be stored - some new database table or a separate file accessible by Heroku cron?)

How to build rails analytics dashboard

I'm looking to build an analytics dashboard for my data in a rails application.
Let's say I have a list of request types "Fizz", "Buzz", "Bang", "Bar".
I want to display a count for each day based on type.
How should I do this?
Here is what I plan on doing:
Add get_bazz_by_day, get_fizz_by_day, etc to the appropriate models.
In each model get all records of type Fizz, then create an array that stores date and count.
format in view so a JS library can format it into a pretty graph.
Does this sound reasonable?
Depending on number of records, your dashboard can soon get performance problems.
Step 1 is misleading. Don't get the data for each day individually, try to get them all at once.
In Step 2 you can have the database do the the aggregation over days, with the group method.
See http://guides.rubyonrails.org/active_record_querying.html#group
Fizz.select("date(created_at) as fizzed_day, count(*) as day_count").
group("date(created_at)")
In Step 3 you need to take care that days without any fizzbuzz are still displayed, as they are not returned in the query.

Resources