Postgres Query to group data by day/week/month from timestamp - mysql

My goal is to get the statistics count of data through timestamp, some what like google analytics. I'm using node.js & sequelize The timestamp field in model looks
request_timestamp: {
type: DataTypes.STRING,
allowNull: false
},
For now, I want to get data grouped by day of every timestamp. I tries date_truc but it's not working for some reason.

Related

Ruby SQL to arel query

I am looking to create a query within the scope that will search for records that have a date less than or equal to the current date. I understand how to do this in SQL but I'm not sure how this would translate over to this. Currently all I have is this
scope :index_overdue, -> { where(active: true, end_date: nil).order(arel_table[:complete_by], arel_table[:latest_update_date], arel_table[:id]) }
I need the query to only retrieve records that have a complete_by date that is <= The current date.
I would use a beginless range to write that condition:
scope :index_overdue, -> {
where(active: true, end_date: nil, complete_by: ..Date.today)
.order(...)
}

MySQL datetime column shows incorrect time

I am trying to insert a date\time value in the format YYYY-MM-DD HH:mm:ss in MySQL DB table using Sequlize as ORM in a nodejs (Expressjs middleware). For some odd reason the time element gets stored with an incorrect time. For example, I try to insert 05:50 PM but the DB column post insert shows 06:12 pm. Any idea how to fix this?
My model has below definition for the datetime column
time_stamp: {
type: DataTypes.DATE,
allowNull: false,
},
My DB table has below definition for the field
time_stamp DATETIME NOT NULL,

Sequelize, update timestamp if older when upserting new data

Sequelize/ MySQL table:
id: integer,
timestamp: Date
anotherField: string
I am upserting with a new document:
{ id: alreadyInTheDatabase, timestamp: reallyOldValue, anotherField: "newValue" }
I need to update timestamp but only if it is older (smaller) then the one upserting the database, anotherField needs to be always updated.
What is the best approach for it?
Thank you! :-)

PyMongo - Delete documents based on a date condition where date is stored in string format (%m/%d/%y) in MongoDB

How to delete Mongodb documents based on certain date condition using PyMongo which stores the date object in string format in MongoDB ?
Sample code:
API takes a date parameter as payload and returns only that much data.So before inserting new documents, want to ensure previous data is not delete and only the ones which are being currently processed.
import requests
import json
from pymongo import MongoClient
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
past_4_month= datetime.today() + relativedelta(months=-4)
past_4_month_1st_day=past_4_month.strftime("%m/01/%Y")
url='xxx'
header={'Auth':'xyz', 'Content-Type': 'application/json'}
payload={'start_date': past_4_month_1st_day }
api_data = requests.request("POST", url, data=json.dumps(payload), headers=header).json()
client = MongoClient('mongodb://localhost:27017')
db = client[test_db][test_collection]
db.delete_many({'date':{'$gte':past_4_month_1st_day}})
db.insert_many(api_data)
client.close()
Sample output stored in mongodb:
{
"id": ObjectId("5f44e33ffb3be4ed19a10ed3"),
"xyz_id" : "12345",
"name" : "xyz",
"date" : "3/3/2020"
}
For example - when running in Aug-2020 - it will return data from past 4 months i.e. May-2020 onwards and always truncate/delete and load same data until end of Aug-2020.
On Sep-2020 1st of the month, the prev 4 month date would be June-2020, so it should not delete May-2020 data but only from June-2020 till current month (Sep-2020). Meaning only process past 4 months data always with every run (truncate/delete and reload).
However, since the date is stored as string in MongoDB document, tried using converting it to date from string using $dateFromString, $toDate and in Python etc. it is still not working as expected and deletes all the documents (from May-2020) from previous runs/loads.
Please suggest.

Graph for data updated every 30 minutes

I make weather station which uploads data to my MySQL database every 30-60 minutes. How to make on example temperature plot for a week on my website? I've looked for such option in Highcharts but I don't know does it is possible. Date and time is saved in database as timestamp.
They have an example specifically for time data with irregular intervals: http://www.highcharts.com/demo/spline-irregular-time
Get your data from database for last week, then preprocess in backend to fit Highcharts data format, and as result you should have something like this:
var myData = [
[1388534400000, 12],
[next_timestamp, next_value],
[another_timestamp, another_value],
...
]
Now you can use that data to generate chart:
$("#container").highcharts({
series: [{
data: myData
}]
})
Note: timestamps are in milliseconds.
Now to update chart every 30minutes, just create call some AJAX call to get new data from the server:
setInterval(function() {
$.getJSON('path/to/data', function(myData) {
$("#container").highcharts().series[0].setData(myData);
});
}, 30 * 60 * 1000); // 30minutes