From b8fbd5ef6c60a40fa218dc9c1333a4d67eeca4d8 Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Sun, 14 Jun 2020 14:14:32 -0500 Subject: [PATCH] Use requests_cache and add a time range function. --- modules/jenkins.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/modules/jenkins.py b/modules/jenkins.py index d97d0bf..494a601 100755 --- a/modules/jenkins.py +++ b/modules/jenkins.py @@ -15,10 +15,15 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import datetime +import requests_cache +import time from jenkinsapi.custom_exceptions import NoBuildData from jenkinsapi.jenkins import Jenkins from os import getenv +requests_cache.install_cache("jenkins", backend="sqlite", expire_after=300) + class JenkinsModule: """Jenkins module for the Metrics program""" @@ -109,3 +114,20 @@ class JenkinsModule: date, nonpassing, failing, total) return command + + def sqlite_time_range(self, days): + """Get the rows which have been inserted given days + + e.g. if days is 180, it gets all of the values which have been + inserted in the past 180 days. + + Note: this just returns the command to be ran, it doesn't actually run + """ + + now = datetime.datetime.now() + timedelta = datetime.timedelta(days=days) + unix_time = int(time.mktime((now - timedelta).timetuple())) + + command = "SELECT * FROM jenkins WHERE date > %s;" % unix_time + + return command