Skip to content

Instantly share code, notes, and snippets.

@cpietsch
Created May 17, 2022 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpietsch/3173d58e4afb77d9ab840d03aee0039e to your computer and use it in GitHub Desktop.
Save cpietsch/3173d58e4afb77d9ab840d03aee0039e to your computer and use it in GitHub Desktop.
e3dc post history to firebase
jq -ncM "$(cat ./input.jq)" | ./e3dc -splitrequests | jq -cM "$(cat ./output.jq)" | jq '.result.DB_HISTORY_DATA + {"date":{".sv": "timestamp"}}' | curl -X POST -d @- https://XXXX.europe-west1.firebasedatabase.app/history.json
sources:
- https://knx-user-forum.de/forum/%C3%B6ffentlicher-bereich/knx-eib-forum/diy-do-it-yourself/1614158-neue-library-f%C3%BCr-rscp-kommunikation-mit-e3dc-speicher-inkl-kommandozeilen-utility
(
now as $now |
# apply local timezone diff to utc time
def fixTimezone: . | (
. -= (0|strflocaltime("%H") | tonumber) * 3600 |
. -= (0|strflocaltime("%M") | tonumber) * 60
);
# returns the timespan the given amount of days as timestamp (seconds)
def timespanDay(days): . | (
. = (24 * 60 * 60 * days)
);
# returns the start of the day of the given date as timestamp
def startOfDay: . | (
. = if . == null then $now else . end |
gmtime | [.[0],.[1],.[2],0,0,0,0,0] | mktime
);
# returns the start of the month of the given date as timestamp
def startOfMonth: . | (
. = if . == null then $now else . end |
gmtime | [.[0],.[1],1,0,0,0,0,0] | mktime
);
# returns the last day of the month (or number of days in the month) from the given date
def daysInMonth: . | (
. = if . == null then $now else . end |
(. | gmtime | .[2] = 28 | mktime | . += timespanDay(4)) as $next_month |
$next_month - timespanDay($next_month | gmtime | .[2]) | gmtime | .[2]
);
# returns the start of the year from the given date as timestamp
def startOfYear: . | (
. = if . == null then $now else . end |
gmtime | [.[0],0,1,0,0,0,0,0] | mktime
);
# returns the last day of the year (or number of days in the year) from the given date
def daysInYear: . | (
. = if . == null then $now else . end |
gmtime | [.[0],11,31,0,0,0,0,0] | mktime | strftime("%j") | tonumber
);
. =
[
[ # day current
"DB_REQ_HISTORY_DATA_DAY",
[
["DB_REQ_HISTORY_TIME_START", ( . = startOfDay | fixTimezone | todateiso8601 )],
["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay(1) | todateiso8601 )],
["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay(1) | todateiso8601 )]
]
],
[ # day before
"DB_REQ_HISTORY_DATA_DAY",
[
["DB_REQ_HISTORY_TIME_START", ( . = (startOfDay - timespanDay(1)) | fixTimezone | todateiso8601 )],
["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay(1) | todateiso8601 )],
["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay(1) | todateiso8601 )]
]
],
[ # month current
"DB_REQ_HISTORY_DATA_DAY",
[
["DB_REQ_HISTORY_TIME_START", ( . = startOfMonth | fixTimezone | todateiso8601 )],
["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay(daysInMonth) | todateiso8601 )],
["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay(daysInMonth) | todateiso8601 )]
]
],
[ # month before
"DB_REQ_HISTORY_DATA_DAY",
[
["DB_REQ_HISTORY_TIME_START", ( . = (startOfMonth - timespanDay(1)) | startOfMonth | fixTimezone | todateiso8601 )],
["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay((startOfMonth - timespanDay(1)) | startOfMonth | daysInMonth) | todateiso8601 )],
["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay((startOfMonth - timespanDay(1)) | startOfMonth | daysInMonth) | todateiso8601 )]
]
],
[ # year current
"DB_REQ_HISTORY_DATA_DAY",
[
["DB_REQ_HISTORY_TIME_START", ( . = startOfYear | fixTimezone | todateiso8601 )],
["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay(daysInYear) | todateiso8601 )],
["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay(daysInYear) | todateiso8601 )]
]
],
[ # year before
"DB_REQ_HISTORY_DATA_DAY",
[
["DB_REQ_HISTORY_TIME_START", ( . = (startOfYear - timespanDay(1)) | startOfYear | fixTimezone | todateiso8601 )],
["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay((startOfYear - timespanDay(1)) | startOfYear | daysInYear) | todateiso8601 )],
["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay((startOfYear - timespanDay(1)) | startOfYear | daysInYear) | todateiso8601 )]
]
]
]
)
def weekdays: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
def chargeType: ["idleCharge","idleDischarge"];
def historyPeriod: ["day","lastday", "month", "lastmonth", "year", "lastyear"];
{ "result":
( # cleanup db history data
.DB_HISTORY_DATA = (
.DB_HISTORY_DATA_DAY | to_entries | map(
{ # name keys by array index
(historyPeriod[.key]): (
# only get sum container witouth index
.value.DB_SUM_CONTAINER | del(.DB_GRAPH_INDEX)
)
}
) | add
) | del (.DB_HISTORY_DATA_DAY) # delete origin DB_HISTORY_DATA_DAY
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment