Get Favourite day from data set - cloudant

I have data like this
State City Day
Arizona Chandler Monday
Arizona Mesa Wednesday
Arizona Chandler Monday
Arizona Mesa Saturday
Washington Vancouver Friday
Washington Vancouver Friday
Washington Spokane Monday
Washington Vancouver Tuesday
Washington Spokane Monday
I want to get favourite day by state and city
eg Favourite day for Washington-Vancouver is Friday and that for Arizon-Chandler is Monday

You should look into using views. You could create your map function to emit the key as [state, city, day] with a value of 1 you could then use the _count reduce to see how many times each key is mentioned. From that you will be able to determine the favourite day for each state / city. You can read more about views in the cloudant documentation.

So in JSON a document would look like this:
{
"state": "Arizona",
"city": "Chandler",
"day": "Monday"
}
To find "favourite day by state and city" we can create an index where the key comprises of all three things:
function(doc) {
emit([doc.state,doc.city,doc.day], null);
}
And use the built-in _count reducer.
This will give keys like this:
["Arizona","Chandler","Monday"]
The counts then then be collected by interrogating the view:
/db/_design/mydesigndoc/_view/myview?group_level=3
which will give counts for each distinct combination of state/city/day.
It can't just return the "favourite"; only all the unique combinations and you would have to iterate through the result set to find the one with largest total.

Related

How to create an iCalender event that repeats very irregulary?

Background: where I park my car, during the winter it is cleaning day once a week (=parking is not allowed) so I have to move the car before that to avoid getting a ticket. I want to create an iCal reminder about that but the pattern is very complicated.
The season starts Nov 1 and ends May 15.
Only weekdays are cleaning nights.
There are two repeating patterns, yearly (Nov to May) and every 4/6 days (see the examples below for details)
Examples:
I am parked on a street where Wednesday is the cleaning day (i.e., "Wednesday street") so Tuesday evening I move the car to a street that just was cleaned (i.e., a "Tuesday street"). Every week the day I have to move the car is moved forward one day. The repeating pattern is "every 6th day".
However, if I have parked on a Monday street, the situation is different - then I want to move the car on the Friday, not the Sunday. Suddenly the repeating pattern is "every 4th day".
I have read this question Can iCal schedule an event for the first weekday after BYMONTHDAY if BYMONTHDAY is a weekend? and have figured out a couple things:
I can set BYMONTH=11,12,1,2,3,4,5 to limit it to Nov to May.
BYDAY=MO,TU,WE,TH,FR limits it to weekdays
but after that I am stuck. I have been playing around at http://recurrence-expansion-service.appspot.com/ with different combinations but so far without success.
How do I limit it to mid-May?
How do I write a rule that "skips" the weekends when I have parked on a Monday street?
How do I achieve two repeating patterns (a yearly pattern - Nov to May - and a 4/6 day pattern)?

Build complex queries with multiple fields in cloudant

Date State | City | Zip | Water | Weight
-------------------------------------------------------------------
01/01/2016 Arizona Chandler 1011 10 ltr 40 kg
01/04/2016 Arizona Mesa 1012 20 ltr 50 kg
06/05/2015 Washington Spokane 1013 30 ltrs 44 kg
06/08/2015 Washington Spokane 1013 30 ltrs 44 kg
What I want are complex queries, like I want to know average water, weight by passing a city or state or ip for a date range or month, or any field or all fields.
I am not sure how to go about this. Read about map reduce, but cant guess how will I get above output
If you have link for examples which covers above scenarios that will also help.
Thanks in advance
So first we need to model your structured data in JSON. Something like this would work:
{
"date": "2016-01-01",
"location": "Arizona Chandler",
"pressure": 1101,
"water": 10,
"weight": 40
}
Here's your data in a Cloudant database:
https://reader.cloudant.com/so37613808/_all_docs?include_docs=true
Next we'd need to create a MapReduce view to aggregate the a specific field by date. A map function to create an index whose key is the date and whose value is the water would look like this:
function(doc) {
emit(doc.date, doc.water);
}
Every key/value pair emitted from the map function is added to an index which can be queried later in its entirety or by a range of keys (keys which in this case represent a date).
And if an average is required we would use the built-in _stats reducer. The Map and Reduce portions are expressed in a Design Document like this one: https://reader.cloudant.com/so37613808/_design/aggregate
The subsequent index allows us to get an aggregate across the whole data set with:
https://reader.cloudant.com/so37613808/_design/aggregate/_view/waterbydate
Dividing the sum by the count gives us an average.
We can use the same index to provide data grouped by keys too:
https://reader.cloudant.com/so37613808/_design/aggregate/_view/waterbydate?group=true
Or we can select a portion of the data by supplying startkey and endkey parameters:
https://reader.cloudant.com/so37613808/_design/aggregate/_view/waterbydate?startkey=%222016-01-01%22&endkey=%222016-06-03%22
See https://docs.cloudant.com/creating_views.html for more details.

How should one represent repeating date/time?

I'm building a backend at the moment which has a number of objects which have associated dates and times, which repeat at a fixed interval, for instance:
Object 1: Repeat Weekly on Tuesdays at 17:00 UTC.
Object 2: Repeat on the first Wednesday of the month at 12:00 UTC.
Object 3: Repeat every other week (fortnightly) on Friday at 13:00 UTC from January 1, 1970.
I'm looking for the best way to represent this interval in a database, short of generating a calendar file with the object's repeating date/time info in it.

Need design ideas: Searchable time/datetime for C# and SQL Server

I am trying to add a feature on one of my applications. Here is a sample:
Company ABC
Business hours:
Sunday closed
Monday 7am - 5pm
Tuesday 7am - 5pm
Wednesday 7am - 5pm
Thursday 7am - 5pm
Friday 7am - 5pm
Saturday closed
Company DEF
Sunday 10am - 12pm
Monday 8am - 4pm
Tuesday 8am - 4pm
Wednesday 8am - 4pm
Thursday 8am - 4pm
Friday 8am - 4pm
Saturday closed
Now, what I want to do is to have my users be able to search/filter the business hours. For example, only return businesses who are open during Monday 2PM, or Saturday 1PM, or Monday any time.
My idea is to use datetime in SQL Server, but it appears I may need to create 7 columns for each day (Sunday to Saturday) and different columns for Starting time to open to Closing time to open.
The next idea is to make these columns searchable such that I will be able to filter them. for example, if a user searches for Monday 12PM, the search will be Monday (open time) <= (user search, 12pm) <= (close time).
So basically, it will be similar to:
SELECT *
FROM Companies
WHERE [user_search_time] IS "BETWEEN open_time AND close_time WHERE day=[user_search_day]
I'm having a hard time designing this, let alone how to implement it.
Technology used is C#/ASP.Net and SQL Server 2008.
The problem is how to design/implement this feature using the technologies mentioned above.
Any idea/tips will be appreciated. Thanks!
Might want to push this to codereview, or programmers. I think you are on the right path, but instead of adding columns, add a new table. called business_hours
BUSINESS_HOURS
business (company fk)
open_time (datetime)
close_time (datetime)
day (M,T,W,TH,F,S,Sun) // you can do this however u want 1-7 would work as well
Then your query is almost the same as what you have.
SELECT *
FROM
business_hours
WHERE
business = user_company_id
[user_search_time] IS BETWEEN open_time AND close_time and
day=[user_search_day]
Benefits to a table is it's more denormalized and you could support hours that have breaks in them. I could suport 12-2 as well as 5-8. You would just insert a new record and it would work. I am not a big fan of doing all day by doing a range from 12AM to 12PM but it will work, and keep your design simple.

Nagios - Custom Report Generation

I want to generate a custom report in "Nagios-3.2.0". I have defined the work-hours in "timeperiods.cfg" as follows:
'workhours' timeperiod definition
define timeperiod {
timeperiod_name 0800-2000
alias full time
monday 08:00-20:00
tuesday 08:00-20:00
wednesday 08:00-20:00
thursday 08:00-20:00
friday 08:00-20:00
saturday 08:00-20:00
}
Now, if I replace "saturday" with "2010-03-27" or "march 27" as shown below:
'workhours' timeperiod definition
define timeperiod {
timeperiod_name 0800-2000
alias full time
monday 08:00-20:00
tuesday 08:00-20:00
wednesday 08:00-20:00
thursday 08:00-20:00
friday 08:00-20:00
2010-03-27 08:00-20:00
}
Nagios is not generating report for the given date (2010-03-27).
How can I modify "timeperiods.cfg" so that I can generate reports for the given dates ?
have you reviewed this?
http://nagios.sourceforge.net/docs/3_0/objectdefinitions.html#timeperiod
I think you might have some luck trying other naming conventions.
Try changing the timeperiod_name to a non numneric name.
eg "eightoeight"

Resources