Fix uid handling and other errors

This commit is contained in:
Troels Liebe Bentsen 2014-10-06 23:59:32 +02:00
parent bad9f8bf0b
commit 090f0c9c55
3 changed files with 74 additions and 42 deletions

View File

@ -12,16 +12,11 @@ class ImportTimesheetData extends Command {
public function fire() { public function fire() {
$this->info(date('Y-m-d') . ' Running ImportTimesheetData...'); $this->info(date('Y-m-d') . ' Running ImportTimesheetData...');
/* try { // Seems we are using the console timezone
$dt = new DateTime("now"); DB::statement("SET SESSION time_zone = '+00:00'");
var_dump($dt);
echo "1:".$dt."\n"; // Get the Unix epoch
echo $dt->getTimestamp()."\n"; $unix_epoch = new DateTime('1970-01-01T00:00:01', new DateTimeZone("UTC"));
} catch (Exception $ex) {
echo $ex->getMessage();
echo $ex->getTraceAsString();
}
exit(0); */
// Create some initial sources we can test with // Create some initial sources we can test with
$user = User::first(); $user = User::first();
@ -97,7 +92,14 @@ class ImportTimesheetData extends Command {
$this->info("Find events in " . $event_source->name); $this->info("Find events in " . $event_source->name);
file_put_contents("/tmp/" . $event_source->name . ".ical", $results[$i]); file_put_contents("/tmp/" . $event_source->name . ".ical", $results[$i]);
if (preg_match_all('/BEGIN:VEVENT\r?\n(.+?)\r?\nEND:VEVENT/s', $results[$i], $icalmatches)) { if (preg_match_all('/BEGIN:VEVENT\r?\n(.+?)\r?\nEND:VEVENT/s', $results[$i], $icalmatches)) {
$this->info("Found ".(count($icalmatches[1])-1)." events\n");
// Fetch all uids so we can do a quick lookup
$uids = []; $uids = [];
$event_source->events()->get(['uid', 'org_updated_at'])->map(function($item) use(&$uids) {
$uids[$item->uid] = $item->org_updated_at;
});
// Loop over all the found events
foreach ($icalmatches[1] as $eventstr) { foreach ($icalmatches[1] as $eventstr) {
//print "---\n"; //print "---\n";
//print $eventstr."\n"; //print $eventstr."\n";
@ -116,34 +118,24 @@ class ImportTimesheetData extends Command {
# Add RECURRENCE-ID to the UID to make sure the event is unique # Add RECURRENCE-ID to the UID to make sure the event is unique
if (isset($data['recurrence-id'])) { if (isset($data['recurrence-id'])) {
$event->uid .= $data['recurrence-id']; $event->uid .= "::".$data['recurrence-id'];
} }
// Check for duplicate events in the feed //FIXME: Bail on RRULE as we don't support that
if (isset($uids[$event->uid])) {
echo "Duplicate event found:";
echo "org:\n";
var_dump($uids[$event->uid]);
echo "new:\n";
var_dump($data);
continue;
}
$uids[$event->uid] = $data;
//TODO: Bail on RRULE as we don't support that
// Convert to DateTime objects // Convert to DateTime objects
foreach (['dtstart', 'dtend', 'created', 'last-modified'] as $key) { foreach (['dtstart', 'dtend', 'created', 'last-modified'] as $key) {
// Parse and create DataTime object from ICAL format // Parse and create DataTime object from ICAL format
list($dt, $timezone) = TimesheetUtils::parseICALDate($data[$key]); list($dt, $timezone) = TimesheetUtils::parseICALDate($data[$key]);
// Handle bad dates in created and last-modified // Handle bad dates in created and last-modified
if ($dt == null) { if ($dt == null || $dt < $unix_epoch) {
if ($key == 'created' || $key == 'last-modified') { if ($key == 'created' || $key == 'last-modified') {
$dt = new DateTime('1970-01-01T00:00:00', new DateTimeZone("UTC")); // Default to UNIX epoc $dt = $unix_epoch; // Default to UNIX epoch
echo "Could not parse date for $key: '" . $data[$key] . "' so default to UNIX Epoc\n"; // TODO write to error table $event->import_error = "Could not parse date for $key: '" . $data[$key] . "' so default to UNIX Epoc\n";
} else { } else {
echo "Could not parse date for $key: '" . $data[$key] . "'\n"; // TODO write to error table echo "Could not parse date for $key: '" . $data[$key] . "'\n";
exit(255); // TODO: Bail on this event exit(255); // TODO: Bail on this event or write to error table
} }
} }
@ -157,9 +149,11 @@ class ImportTimesheetData extends Command {
$event->end_date = $dt; $event->end_date = $dt;
$event->org_end_date_timezone = $timezone; $event->org_end_date_timezone = $timezone;
break; break;
case 'created': $event->org_created_at = $dt; case 'created':
$event->org_created_at = $dt;
break; break;
case 'last-modified': $event->org_updated_at = $dt; case 'last-modified':
$event->org_updated_at = $dt;
break; break;
} }
} }
@ -174,6 +168,24 @@ class ImportTimesheetData extends Command {
} }
} }
// Check for events we allready have
if (isset($uids[$event->uid])) {
$db_event_org_updated_at = new DateTime($uids[$event->uid], new DateTimeZone('UTC'));
if($event->org_updated_at <= $db_event_org_updated_at) {
// Same or older version of new event so skip
continue;
} else {
var_dump($event->uid);
var_dump($uids[$event->uid]);
var_dump($event);
// updated version of the event
echo "update version of the event\n";
}
}
$uids[$event->uid] = $event->org_updated_at;
// Calculate number of hours // Calculate number of hours
$di = $event->end_date->diff($event->start_date); $di = $event->end_date->diff($event->start_date);
$event->hours = $di->h + $di->i / 60; $event->hours = $di->h + $di->i / 60;
@ -182,7 +194,7 @@ class ImportTimesheetData extends Command {
$event->org_data = $eventstr; $event->org_data = $eventstr;
$event->summary = $title; $event->summary = $title;
$event->description = $title; $event->description = $title;
$event->org_code = $code; $event->org_code = $codename;
$event->owner = $event_source->owner; $event->owner = $event_source->owner;
$event->timesheet_event_source_id = $event_source->id; $event->timesheet_event_source_id = $event_source->id;
if (isset($codes[$codename])) { if (isset($codes[$codename])) {
@ -194,14 +206,17 @@ class ImportTimesheetData extends Command {
} }
try { try {
// Save event //$this->info("Save event: ".$event->summary);
//if($event->uid == '2nvv4qjnlc293vq3h2833uslhc@google.com') {
//var_dump($event);
$event->save(); $event->save();
//}
} catch (Exception $ex) { } catch (Exception $ex) {
echo "'" . $event->summary . "'\n"; echo "'" . $event->summary . "'\n";
var_dump($data); var_dump($data);
echo $ex->getMessage(); echo $ex->getMessage();
echo $ex->getTraceAsString(); echo $ex->getTraceAsString();
//exit(); exit(0);
} }
} }
} }

View File

@ -5,6 +5,18 @@ class TimesheetEvent extends Eloquent
public $timestamps = true; public $timestamps = true;
protected $softDelete = true; protected $softDelete = true;
/* protected $dates = array('org_updated_at');
public function getDates() {
return array('created_at', 'updated_at', 'deleted_at');
} */
/* public function setOrgUpdatedAtAttribute($value)
{
var_dump($value);
$this->attributes['org_updated_at'] = $value->getTimestamp();
}*/
public function account() public function account()
{ {
return $this->belongsTo('Account'); return $this->belongsTo('Account');

View File

@ -15,6 +15,11 @@ class TimesheetEventSource extends Eloquent
return $this->belongsTo('User'); return $this->belongsTo('User');
} }
public function events()
{
return $this->hasMany('TimesheetEvent');
}
public static function createNew($parent = false) public static function createNew($parent = false)
{ {
$className = get_called_class(); $className = get_called_class();