[mythtv] patch for filldata fixing executions crossing the midnight boundary

Kirby Vandivort kvandivo at ks.uiuc.edu
Sun Oct 5 21:51:47 EDT 2003


The attached patch fixes a problem when grabbing north american data
very close to midnight.  If you start execution before midnight, the
previous code was getting data for day X, and when day X became day Y,
CURRENT_DATE calls in the SQL were causing mysql to do bad things.  (It
deletes a day Y's data, and complains that it can't insert day X's
data twice.

So, this patch causes the "current date" to be passed among the
functions so that it stays consistent.  As well, if the day changes
while running, it tweaks the number of days of data it will attempt
to retrieve appropriately.  If you don't happen to be running from
north america, it should behave as it always has (assuming time on the
machine mythfilldatabase is running from matches the time on your db
server machine (which it should.  not meaning that it does, but that
it _should_))

So, this patch really makes it ideal to start running mythfilldatabase
at 11:59 pm.  That way, it will get 'as accurate as possible' data for
tomorrow, _and_ it will go ahead and grab an extra day's data at the
end of the ten or so days of data that tv_grab_na can get that we hadn't
been getting before.

-- 

Kirby Vandivort                      Theoretical and 
Senior Research Programmer            Computational Biophysics 
Email: kvandivo at ks.uiuc.edu          3051 Beckman Institute
http://www.ks.uiuc.edu/~kvandivo/    University of Illinois
Phone: (217) 244-5711                405 N. Mathews Ave
Fax  : (217) 244-6078                Urbana, IL  61801, USA
-------------- next part --------------
Index: programs/mythfilldatabase/filldata.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythfilldatabase/filldata.cpp,v
retrieving revision 1.64
diff -b -u -2 -r1.64 filldata.cpp
--- programs/mythfilldatabase/filldata.cpp	4 Oct 2003 17:37:54 -0000	1.64
+++ programs/mythfilldatabase/filldata.cpp	6 Oct 2003 01:42:08 -0000
@@ -981,9 +983,16 @@
 }
 
-void clearDBAtOffset(int offset, int chanid)
+void clearDBAtOffset(int offset, int chanid, QDate *qCurrentDate)
 {
     if (no_delete)
         return;
 
+    QDate newDate; 
+    if (qCurrentDate == 0)
+    {
+       newDate = QDate::currentDate();
+       qCurrentDate = &newDate;
+    }
+
     int nextoffset = offset + 1;
 
@@ -998,24 +1007,33 @@
 
     querystr.sprintf("DELETE FROM program WHERE starttime >= "
-                     "DATE_ADD(CURRENT_DATE, INTERVAL %d DAY) "
-                     "AND starttime < DATE_ADD(CURRENT_DATE, INTERVAL "
-                     "%d DAY) AND chanid = %d;", offset, nextoffset, chanid);
+                     "DATE_ADD('%s', INTERVAL %d DAY) "
+                     "AND starttime < DATE_ADD('%s', INTERVAL "
+                     "%d DAY) AND chanid = %d;", 
+                     qCurrentDate->toString("yyyyMMdd").ascii(), offset, 
+                     qCurrentDate->toString("yyyyMMdd").ascii(), nextoffset, 
+                     chanid);
     query.exec(querystr);
 
     querystr.sprintf("DELETE FROM programrating WHERE starttime >= "
-                     "DATE_ADD(CURRENT_DATE, INTERVAL %d DAY) "
-                     "AND starttime < DATE_ADD(CURRENT_DATE, INTERVAL "
-                     "%d DAY) AND chanid = %d;", offset, nextoffset, chanid);
+                     "DATE_ADD('%s', INTERVAL %d DAY) "
+                     "AND starttime < DATE_ADD('%s', INTERVAL "
+                     "%d DAY) AND chanid = %d;", 
+                     qCurrentDate->toString("yyyyMMdd").ascii(), offset, 
+                     qCurrentDate->toString("yyyyMMdd").ascii(), nextoffset, 
+                     chanid);
     query.exec(querystr);
 
     querystr.sprintf("DELETE FROM credits WHERE starttime >= "
-                     "DATE_ADD(CURRENT_DATE, INTERVAL %d DAY) "
-                     "AND starttime < DATE_ADD(CURRENT_DATE, INTERVAL "
-                     "%d DAY) AND chanid = %d;", offset, nextoffset, chanid);
+                     "DATE_ADD('%s', INTERVAL %d DAY) "
+                     "AND starttime < DATE_ADD('%s', INTERVAL "
+                     "%d DAY) AND chanid = %d;", 
+                     qCurrentDate->toString("yyyyMMdd").ascii(), offset, 
+                     qCurrentDate->toString("yyyyMMdd").ascii(), nextoffset, 
+                     chanid);
     query.exec(querystr);
 }
 
 void handlePrograms(int id, int offset, QMap<QString, 
-                    QValueList<ProgInfo> > *proglist)
+                    QValueList<ProgInfo> > *proglist, QDate *qCurrentDate)
 {
     QMap<QString, QValueList<ProgInfo> >::Iterator mapiter;
@@ -1049,5 +1067,5 @@
         }
 
-        clearDBAtOffset(offset, chanid);
+        clearDBAtOffset(offset, chanid, qCurrentDate);
 
         QValueList<ProgInfo> *sortlist = &((*proglist)[mapiter.key()]);
@@ -1243,5 +1261,6 @@
 
 
-void grabDataFromFile(int id, int offset, QString &filename)
+void grabDataFromFile(int id, int offset, QString &filename, 
+                                        QDate *qCurrentDate = 0)
 {
     QValueList<ChanInfo> chanlist;
@@ -1251,5 +1270,5 @@
 
     handleChannels(id, &chanlist);
-    handlePrograms(id, offset, &proglist);
+    handlePrograms(id, offset, &proglist, qCurrentDate);
 }
 
@@ -1270,5 +1289,5 @@
 }
 
-bool grabData(Source source, int offset)
+bool grabData(Source source, int offset, QDate *qCurrentDate = 0)
 {
     char tempfilename[] = "/tmp/mythXXXXXX";
@@ -1351,5 +1370,5 @@
          cout << "------------------ End of XMLTV output ------------------" << endl;
 
-    grabDataFromFile(source.id, offset, filename);
+    grabDataFromFile(source.id, offset, filename, qCurrentDate);
 
     QFile thefile(filename);
@@ -1444,10 +1463,23 @@
         else if (xmltv_grabber == "tv_grab_na")
         {
-            if (!grabData(*it, 1))
+            QDate qCurrentDate = QDate::currentDate();
+
+            if (!grabData(*it, 1, &qCurrentDate))
                 ++failures;
 
             for (int i = 0; i < 9; i++)
             {
-                QString date(QDate::currentDate().addDays(i).toString());
+                // we need to check and see if the current date has changed since
+                // we started in this loop.  If it has, we need to adjust
+                // the value of 'i' to compensate for this.
+                if (QDate::currentDate() != qCurrentDate)
+                {
+                   QDate newDate = QDate::currentDate();
+                   i += (newDate.daysTo(qCurrentDate));
+                   if (i < 0) i=0;
+                   qCurrentDate = newDate;
+                }
+
+                QString date(qCurrentDate.addDays(i).toString());
                 QString querystr;
                 querystr.sprintf("SELECT COUNT(*) as 'hits' "
@@ -1470,5 +1502,5 @@
                         if (!quiet)
                             cout << "Fetching data for " << date << endl;
-                        if (!grabData(*it, i))
+                        if (!grabData(*it, i, &qCurrentDate))
                             ++failures;
                     }


More information about the mythtv-dev mailing list